Skip to content

Commit

Permalink
en #9: some parentId stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLeoni committed Aug 25, 2020
1 parent f924d71 commit 08d777f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
19 changes: 17 additions & 2 deletions src/extensions/botch/botch-storage-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -158,7 +172,8 @@ class BotchStorageHelper extends Helper {
format: dataFormat,
id: id,
data: data,
name: name
name: name,
parentId: parentId
};
return id;
}
Expand Down
25 changes: 23 additions & 2 deletions src/extensions/botch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 08d777f

Please sign in to comment.