diff --git a/src/extensions/botch/botch-storage-helper.js b/src/extensions/botch/botch-storage-helper.js index cfb8c2c343d..ec0f9ee62ae 100644 --- a/src/extensions/botch/botch-storage-helper.js +++ b/src/extensions/botch/botch-storage-helper.js @@ -133,15 +133,29 @@ class BotchStorageHelper extends Helper { * @param {Buffer} data - The data for the cached asset. * @param {(string|number)} id - The id for the cached asset. * @param {string} name - The name for the cached asset (Botch: we added it) + * @param {string} parentId - The id of the parent. If missing, use parent_0 (Botch: we added it) + * @returns {string} The calculated id of the cached asset, or the supplied id if the asset is mutable. */ - _store (assetType, dataFormat, data, id, name) { + _store (assetType, dataFormat, data, id, name, parentId) { if (!name){ throw new Error(`Missing name:${name}`); } if (!name.trim()){ throw new Error('Provided name is all blank !'); } + if (parentId !== 'parent_0'){ + if (!parentId){ + throw new Error(`Missing parentId:${parentId}`); + } + if (!parentId.trim()){ + throw new Error('Provided parentId is all blank !'); + } + if (!(parentId in this.assets)){ + throw new Error('Provided parentId is not in assets !'); + } + } + if (!dataFormat) throw new Error('Data cached without specifying its format'); if (id !== '' && id !== null && typeof id !== 'undefined') { if (this.assets.hasOwnProperty(id) && assetType.immutable) { @@ -158,7 +172,8 @@ class BotchStorageHelper extends Helper { format: dataFormat, id: id, data: data, - name: name + name: name, + parentId: parentId }; return id; } diff --git a/src/extensions/botch/index.js b/src/extensions/botch/index.js index 4020c384db7..bdb76c77c3b 100644 --- a/src/extensions/botch/index.js +++ b/src/extensions/botch/index.js @@ -717,15 +717,34 @@ class Scratch3Botch { const p = this.exportSprite(id, 'uint8array', newName); const newId = p.md5; + const retp = p.then(data => { - + const target = this.runtime.getTargetById(id); log.log('Botch: using newId from md5:', newId); + + let parentId = 'parent_0'; + + if (target.variables && + target.variables.botch_parent && + target.variables.botch_parent.value){ + const candidate = target.variables.botch_parent.value; + if (candidate !== 'parent_0'){ + if (candidate in this.storageHelper.assets){ + parentId = target.variables.botch_parent.value; + } else { + log.warn('Trying to store sprite with parentId not in store, defaulting to parent_0'); + } + } + } else { + log.warn('Trying to store sprite with no valid parentId, defaulting to parent_0'); + } this.storageHelper._store( this.storage.AssetType.Sprite, this.storage.DataFormat.SB3, data, newId, - newName ? newName : this.runtime.getTargetById(id).sprite.name + newName ? newName : target.sprite.name, + parentId ); log.log('Botch: emitting ', Scratch3Botch.BOTCH_STORAGE_HELPER_UPDATE); this.runtime.emit(Scratch3Botch.BOTCH_STORAGE_HELPER_UPDATE); @@ -793,6 +812,8 @@ class Scratch3Botch { 1 ]; + // Botch: we added this + asset.parentId = storedSprite.parentId; // TO DO what about the id? createAsset setss assetId and assetName asset.name = sprite.name;