Get text assets from zip
This commit is contained in:
1 parent
86149325b1
commit
88b6bdd06c
2 files changed
+46
-12
No files matched your search
@@ -56,11 +56,13 @@ const deserializeSound = function (sound, runtime, zip, assetFileName) {
|
||||
* @param {string} assetFileName Optional file name for the given asset
|
||||
* (sb2 files have filenames of the form [int].[ext],
|
||||
* sb3 files have filenames of the form [md5].[ext])
|
||||
* @param {string} textLayerFileName Optional file name for the given asset's text layer
|
||||
* (sb2 only; files have filenames of the form [int].png)
|
||||
* @return {Promise} Promise that resolves after the described costume has been stored
|
||||
* into the runtime storage cache, the costume was already stored, or an error has
|
||||
* occurred.
|
||||
*/
|
||||
const deserializeCostume = function (costume, runtime, zip, assetFileName) {
|
||||
const deserializeCostume = function (costume, runtime, zip, assetFileName, textLayerFileName) {
|
||||
const storage = runtime.storage;
|
||||
const assetId = costume.assetId;
|
||||
const fileName = assetFileName ? assetFileName :
|
||||
@@ -109,16 +111,44 @@ const deserializeCostume = function (costume, runtime, zip, assetFileName) {
|
||||
return Promise.resolve(null);
|
||||
}
|
||||
|
||||
return costumeFile.async('uint8array').then(data => storage.createAsset(
|
||||
assetType,
|
||||
// TODO eventually we want to map non-png's to their actual file types?
|
||||
costumeFormat,
|
||||
data,
|
||||
assetId
|
||||
))
|
||||
.then(asset => {
|
||||
costume.asset = asset;
|
||||
});
|
||||
// textLayerMD5 exists if there is a text layer, which is a png of text from Scratch 1.4
|
||||
// that was opened in Scratch 2.0. In this case, set costume,textLayerAsset.
|
||||
let textLayerFilePromise;
|
||||
if (costume.textLayerMD5) {
|
||||
textLayerFileName = textLayerFileName ? textLayerFileName :
|
||||
`${costume.textLayerMD5}.png`;
|
||||
const textLayerFile = zip.file(textLayerFileName);
|
||||
if (!textLayerFile) {
|
||||
log.error(`Could not find text layer file associated with the ${costume.name} costume.`);
|
||||
return Promise.resolve(null);
|
||||
}
|
||||
textLayerFilePromise = textLayerFile.async('uint8array')
|
||||
.then(data => storage.createAsset(
|
||||
storage.AssetType.ImageBitmap,
|
||||
'png',
|
||||
data,
|
||||
costume.textLayerMD5
|
||||
))
|
||||
.then(asset => {
|
||||
costume.textLayerAsset = asset;
|
||||
});
|
||||
} else {
|
||||
textLayerFilePromise = Promise.resolve(null);
|
||||
}
|
||||
|
||||
return Promise.all([textLayerFilePromise,
|
||||
costumeFile.async('uint8array')
|
||||
.then(data => storage.createAsset(
|
||||
assetType,
|
||||
// TODO eventually we want to map non-png's to their actual file types?
|
||||
costumeFormat,
|
||||
data,
|
||||
assetId
|
||||
))
|
||||
.then(asset => {
|
||||
costume.asset = asset;
|
||||
})
|
||||
]);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
|
||||
@@ -408,11 +408,15 @@ const parseScratchObject = function (object, runtime, extensions, topLevel, zip)
|
||||
const ext = idParts[1].toLowerCase();
|
||||
costume.dataFormat = ext;
|
||||
costume.assetId = md5;
|
||||
if (costumeSource.textLayerMD5) {
|
||||
costume.textLayerMD5 = StringUtil.splitFirst(costumeSource.textLayerMD5, '.')[0];
|
||||
}
|
||||
// If there is no internet connection, or if the asset is not in storage
|
||||
// for some reason, and we are doing a local .sb2 import, (e.g. zip is provided)
|
||||
// the file name of the costume should be the baseLayerID followed by the file ext
|
||||
const assetFileName = `${costumeSource.baseLayerID}.${ext}`;
|
||||
costumePromises.push(deserializeCostume(costume, runtime, zip, assetFileName)
|
||||
const textLayerFileName = costumeSource.textLayerID ? `${costumeSource.textLayerID}.png` : null;
|
||||
costumePromises.push(deserializeCostume(costume, runtime, zip, assetFileName, textLayerFileName)
|
||||
.then(() => loadCostume(costume.md5, costume, runtime, 2 /* optVersion */))
|
||||
);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user