mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-24 23:12:24 -05:00
Try to make loadCostume more readable, and revert deserializeCostume to changing the costume object directly
This commit is contained in:
parent
692b71a737
commit
86149325b1
4 changed files with 33 additions and 23 deletions
|
@ -152,21 +152,31 @@ const loadCostumeFromAsset = function (costume, costumeAsset, runtime, optVersio
|
||||||
* @returns {?Promise} - a promise which will resolve after skinId is set, or null on error.
|
* @returns {?Promise} - a promise which will resolve after skinId is set, or null on error.
|
||||||
*/
|
*/
|
||||||
const loadCostume = function (md5ext, costume, runtime, optVersion) {
|
const loadCostume = function (md5ext, costume, runtime, optVersion) {
|
||||||
if (!runtime.storage) {
|
let costumePromise;
|
||||||
log.error('No storage module present; cannot load costume asset: ', md5ext);
|
if (costume.asset) {
|
||||||
return Promise.resolve(costume);
|
// Costume comes with asset. It could be coming from camera, image upload, drag and drop, or sb file
|
||||||
|
costumePromise = Promise.resolve(costume.asset);
|
||||||
|
} else {
|
||||||
|
// Need to load the costume from storage. The server should have a reference to this md5.
|
||||||
|
if (!runtime.storage) {
|
||||||
|
log.error('No storage module present; cannot load costume asset: ', md5ext);
|
||||||
|
return Promise.resolve(costume);
|
||||||
|
}
|
||||||
|
|
||||||
|
const AssetType = runtime.storage.AssetType;
|
||||||
|
const idParts = StringUtil.splitFirst(md5ext, '.');
|
||||||
|
const md5 = idParts[0];
|
||||||
|
const ext = idParts[1].toLowerCase();
|
||||||
|
const assetType = (ext === 'svg') ? AssetType.ImageVector : AssetType.ImageBitmap;
|
||||||
|
costume.dataFormat = ext;
|
||||||
|
costumePromise = runtime.storage.load(assetType, md5, ext);
|
||||||
|
if (!costumePromise) {
|
||||||
|
log.error(`Couldn't fetch costume asset: ${md5ext}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const AssetType = runtime.storage.AssetType;
|
return costumePromise.then(costumeAsset => {
|
||||||
const idParts = StringUtil.splitFirst(md5ext, '.');
|
|
||||||
const md5 = idParts[0];
|
|
||||||
const ext = idParts[1].toLowerCase();
|
|
||||||
const assetType = (ext === 'svg') ? AssetType.ImageVector : AssetType.ImageBitmap;
|
|
||||||
costume.dataFormat = ext;
|
|
||||||
return (
|
|
||||||
(costume.asset && Promise.resolve(costume.asset)) ||
|
|
||||||
runtime.storage.load(assetType, md5, ext)
|
|
||||||
).then(costumeAsset => {
|
|
||||||
costume.asset = costumeAsset;
|
costume.asset = costumeAsset;
|
||||||
return loadCostumeFromAsset(costume, costumeAsset, runtime, optVersion);
|
return loadCostumeFromAsset(costume, costumeAsset, runtime, optVersion);
|
||||||
})
|
})
|
||||||
|
|
|
@ -79,7 +79,10 @@ const deserializeCostume = function (costume, runtime, zip, assetFileName) {
|
||||||
costume.asset.dataFormat,
|
costume.asset.dataFormat,
|
||||||
new Uint8Array(Object.keys(costume.asset.data).map(key => costume.asset.data[key])),
|
new Uint8Array(Object.keys(costume.asset.data).map(key => costume.asset.data[key])),
|
||||||
costume.asset.assetId
|
costume.asset.assetId
|
||||||
));
|
))
|
||||||
|
.then(asset => {
|
||||||
|
costume.asset = asset;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!zip) {
|
if (!zip) {
|
||||||
|
@ -112,7 +115,10 @@ const deserializeCostume = function (costume, runtime, zip, assetFileName) {
|
||||||
costumeFormat,
|
costumeFormat,
|
||||||
data,
|
data,
|
||||||
assetId
|
assetId
|
||||||
));
|
))
|
||||||
|
.then(asset => {
|
||||||
|
costume.asset = asset;
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
|
@ -413,10 +413,7 @@ const parseScratchObject = function (object, runtime, extensions, topLevel, zip)
|
||||||
// the file name of the costume should be the baseLayerID followed by the file ext
|
// the file name of the costume should be the baseLayerID followed by the file ext
|
||||||
const assetFileName = `${costumeSource.baseLayerID}.${ext}`;
|
const assetFileName = `${costumeSource.baseLayerID}.${ext}`;
|
||||||
costumePromises.push(deserializeCostume(costume, runtime, zip, assetFileName)
|
costumePromises.push(deserializeCostume(costume, runtime, zip, assetFileName)
|
||||||
.then(asset => {
|
.then(() => loadCostume(costume.md5, costume, runtime, 2 /* optVersion */))
|
||||||
costume.asset = asset;
|
|
||||||
return loadCostume(costume.md5, costume, runtime, 2 /* optVersion */);
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -847,10 +847,7 @@ const parseScratchObject = function (object, runtime, extensions, zip) {
|
||||||
// any translation that needs to happen will happen in the process
|
// any translation that needs to happen will happen in the process
|
||||||
// of building up the costume object into an sb3 format
|
// of building up the costume object into an sb3 format
|
||||||
return deserializeCostume(costume, runtime, zip)
|
return deserializeCostume(costume, runtime, zip)
|
||||||
.then(asset => {
|
.then(() => loadCostume(costumeMd5Ext, costume, runtime));
|
||||||
costume.asset = asset;
|
|
||||||
return loadCostume(costumeMd5Ext, costume, runtime);
|
|
||||||
});
|
|
||||||
// Only attempt to load the costume after the deserialization
|
// Only attempt to load the costume after the deserialization
|
||||||
// process has been completed
|
// process has been completed
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue