2017-05-24 18:30:29 -04:00
|
|
|
const StringUtil = require('../util/string-util');
|
2017-04-17 15:10:04 -04:00
|
|
|
const log = require('../util/log');
|
2021-03-04 15:32:14 -05:00
|
|
|
const {loadSvgString, serializeSvgToString} = require('scratch-svg-renderer');
|
2017-03-27 15:04:44 -04:00
|
|
|
|
2018-11-06 16:44:17 -05:00
|
|
|
const loadVector_ = function (costume, runtime, rotationCenter, optVersion) {
|
2018-12-10 14:39:17 -05:00
|
|
|
return new Promise(resolve => {
|
2021-04-10 22:28:54 -04:00
|
|
|
let svgString = costume.asset.decodeText();
|
|
|
|
// SVG Renderer load fixes "quirks" associated with Scratch 2 projects
|
|
|
|
if (optVersion && optVersion === 2) {
|
|
|
|
// scratch-svg-renderer fixes syntax that causes loading issues,
|
|
|
|
// and if optVersion is 2, fixes "quirks" associated with Scratch 2 SVGs,
|
|
|
|
const fixedSvgString = serializeSvgToString(loadSvgString(svgString, true /* fromVersion2 */));
|
2021-03-04 15:32:14 -05:00
|
|
|
|
2021-04-10 22:28:54 -04:00
|
|
|
// If the string changed, put back into storage
|
|
|
|
if (svgString !== fixedSvgString) {
|
|
|
|
svgString = fixedSvgString;
|
|
|
|
const storage = runtime.storage;
|
|
|
|
costume.asset.encodeTextData(fixedSvgString, storage.DataFormat.SVG, true);
|
|
|
|
costume.assetId = costume.asset.assetId;
|
|
|
|
costume.md5 = `${costume.assetId}.${costume.dataFormat}`;
|
|
|
|
}
|
2018-12-10 14:39:17 -05:00
|
|
|
}
|
2021-03-04 15:32:14 -05:00
|
|
|
|
2018-12-10 14:39:17 -05:00
|
|
|
// createSVGSkin does the right thing if rotationCenter isn't provided, so it's okay if it's
|
|
|
|
// undefined here
|
2021-04-10 22:28:54 -04:00
|
|
|
costume.skinId = runtime.renderer.createSVGSkin(svgString, rotationCenter);
|
2018-12-10 14:39:17 -05:00
|
|
|
costume.size = runtime.renderer.getSkinSize(costume.skinId);
|
|
|
|
// Now we should have a rotationCenter even if we didn't before
|
|
|
|
if (!rotationCenter) {
|
|
|
|
rotationCenter = runtime.renderer.getSkinRotationCenter(costume.skinId);
|
|
|
|
costume.rotationCenterX = rotationCenter[0];
|
|
|
|
costume.rotationCenterY = rotationCenter[1];
|
|
|
|
costume.bitmapResolution = 1;
|
|
|
|
}
|
2018-04-20 00:04:08 -04:00
|
|
|
|
2018-12-10 14:39:17 -05:00
|
|
|
resolve(costume);
|
|
|
|
});
|
2018-07-10 10:21:21 -04:00
|
|
|
};
|
2017-03-27 15:04:44 -04:00
|
|
|
|
2019-02-04 13:38:46 -05:00
|
|
|
const canvasPool = (function () {
|
|
|
|
/**
|
|
|
|
* A pool of canvas objects that can be reused to reduce memory
|
|
|
|
* allocations. And time spent in those allocations and the later garbage
|
|
|
|
* collection.
|
|
|
|
*/
|
|
|
|
class CanvasPool {
|
|
|
|
constructor () {
|
|
|
|
this.pool = [];
|
|
|
|
this.clearSoon = null;
|
|
|
|
}
|
2019-02-04 12:43:31 -05:00
|
|
|
|
2019-02-04 13:38:46 -05:00
|
|
|
/**
|
|
|
|
* After a short wait period clear the pool to let the VM collect
|
|
|
|
* garbage.
|
|
|
|
*/
|
|
|
|
clear () {
|
|
|
|
if (!this.clearSoon) {
|
|
|
|
this.clearSoon = new Promise(resolve => setTimeout(resolve, 1000))
|
2019-02-04 12:43:31 -05:00
|
|
|
.then(() => {
|
2019-02-04 13:38:46 -05:00
|
|
|
this.pool.length = 0;
|
|
|
|
this.clearSoon = null;
|
2019-02-04 12:43:31 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2019-02-04 13:38:46 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a canvas. Create the canvas if the pool is empty.
|
|
|
|
* @returns {HTMLCanvasElement} A canvas element.
|
|
|
|
*/
|
|
|
|
create () {
|
|
|
|
return this.pool.pop() || document.createElement('canvas');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Release the canvas to be reused.
|
|
|
|
* @param {HTMLCanvasElement} canvas A canvas element.
|
|
|
|
*/
|
|
|
|
release (canvas) {
|
|
|
|
this.clear();
|
|
|
|
this.pool.push(canvas);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new CanvasPool();
|
2019-02-04 12:43:31 -05:00
|
|
|
}());
|
|
|
|
|
2018-11-06 16:44:17 -05:00
|
|
|
/**
|
2018-11-06 17:51:39 -05:00
|
|
|
* Return a promise to fetch a bitmap from storage and return it as a canvas
|
2018-11-06 16:44:17 -05:00
|
|
|
* If the costume has bitmapResolution 1, it will be converted to bitmapResolution 2 here (the standard for Scratch 3)
|
|
|
|
* If the costume has a text layer asset, which is a text part from Scratch 1.4, then this function
|
|
|
|
* will merge the two image assets. See the issue LLK/scratch-vm#672 for more information.
|
|
|
|
* @param {!object} costume - the Scratch costume object.
|
2018-11-06 17:22:25 -05:00
|
|
|
* @param {!Runtime} runtime - Scratch runtime, used to access the v2BitmapAdapter
|
2018-11-06 16:44:17 -05:00
|
|
|
* @param {?object} rotationCenter - optionally passed in coordinates for the center of rotation for the image. If
|
|
|
|
* none is given, the rotation center of the costume will be set to the middle of the costume later on.
|
|
|
|
* @property {number} costume.bitmapResolution - the resolution scale for a bitmap costume.
|
|
|
|
* @returns {?Promise} - a promise which will resolve to an object {canvas, rotationCenter, assetMatchesBase},
|
|
|
|
* or reject on error.
|
|
|
|
* assetMatchesBase is true if the asset matches the base layer; false if it required adjustment
|
|
|
|
*/
|
2018-11-06 17:22:25 -05:00
|
|
|
const fetchBitmapCanvas_ = function (costume, runtime, rotationCenter) {
|
2022-05-23 17:00:19 -04:00
|
|
|
if (!costume || !costume.asset) { // TODO: We can probably remove this check...
|
2018-11-06 16:44:17 -05:00
|
|
|
return Promise.reject('Costume load failed. Assets were missing.');
|
|
|
|
}
|
2018-11-20 15:08:25 -05:00
|
|
|
if (!runtime.v2BitmapAdapter) {
|
|
|
|
return Promise.reject('No V2 Bitmap adapter present.');
|
|
|
|
}
|
|
|
|
|
2019-02-04 12:45:07 -05:00
|
|
|
return Promise.all([costume.asset, costume.textLayerAsset].map(asset => {
|
|
|
|
if (!asset) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof createImageBitmap !== 'undefined') {
|
|
|
|
return createImageBitmap(
|
|
|
|
new Blob([asset.data], {type: asset.assetType.contentType})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const image = new Image();
|
|
|
|
image.onload = function () {
|
|
|
|
resolve(image);
|
|
|
|
image.onload = null;
|
|
|
|
image.onerror = null;
|
|
|
|
};
|
|
|
|
image.onerror = function () {
|
|
|
|
reject('Costume load failed. Asset could not be read.');
|
|
|
|
image.onload = null;
|
|
|
|
image.onerror = null;
|
|
|
|
};
|
|
|
|
image.src = asset.encodeDataURI();
|
|
|
|
});
|
|
|
|
}))
|
|
|
|
.then(([baseImageElement, textImageElement]) => {
|
2019-02-04 13:38:46 -05:00
|
|
|
const mergeCanvas = canvasPool.create();
|
2018-11-06 16:44:17 -05:00
|
|
|
|
2019-02-04 12:45:07 -05:00
|
|
|
const scale = costume.bitmapResolution === 1 ? 2 : 1;
|
|
|
|
mergeCanvas.width = baseImageElement.width;
|
|
|
|
mergeCanvas.height = baseImageElement.height;
|
|
|
|
|
|
|
|
const ctx = mergeCanvas.getContext('2d');
|
|
|
|
ctx.drawImage(baseImageElement, 0, 0);
|
2018-11-13 15:31:00 -05:00
|
|
|
if (textImageElement) {
|
2019-02-04 12:45:07 -05:00
|
|
|
ctx.drawImage(textImageElement, 0, 0);
|
|
|
|
}
|
2019-02-04 13:38:46 -05:00
|
|
|
// Track the canvas we merged the bitmaps onto separately from the
|
|
|
|
// canvas that we receive from resize if scale is not 1. We know
|
|
|
|
// resize treats mergeCanvas as read only data. We don't know when
|
|
|
|
// resize may use or modify the canvas. So we'll only release the
|
|
|
|
// mergeCanvas back into the canvas pool. Reusing the canvas from
|
|
|
|
// resize may cause errors.
|
2019-02-04 12:45:07 -05:00
|
|
|
let canvas = mergeCanvas;
|
|
|
|
if (scale !== 1) {
|
|
|
|
canvas = runtime.v2BitmapAdapter.resize(mergeCanvas, canvas.width * scale, canvas.height * scale);
|
2018-11-13 15:31:00 -05:00
|
|
|
}
|
2018-11-06 16:44:17 -05:00
|
|
|
|
2019-02-04 12:45:07 -05:00
|
|
|
// By scaling, we've converted it to bitmap resolution 2
|
|
|
|
if (rotationCenter) {
|
|
|
|
rotationCenter[0] = rotationCenter[0] * scale;
|
|
|
|
rotationCenter[1] = rotationCenter[1] * scale;
|
|
|
|
costume.rotationCenterX = rotationCenter[0];
|
|
|
|
costume.rotationCenterY = rotationCenter[1];
|
|
|
|
}
|
|
|
|
costume.bitmapResolution = 2;
|
|
|
|
|
|
|
|
// Clean up the costume object
|
|
|
|
delete costume.textLayerMD5;
|
|
|
|
delete costume.textLayerAsset;
|
|
|
|
|
|
|
|
return {
|
|
|
|
canvas,
|
|
|
|
mergeCanvas,
|
|
|
|
rotationCenter,
|
|
|
|
// True if the asset matches the base layer; false if it required adjustment
|
|
|
|
assetMatchesBase: scale === 1 && !textImageElement
|
|
|
|
};
|
|
|
|
})
|
2022-05-23 17:00:19 -04:00
|
|
|
.finally(() => {
|
2018-12-03 16:47:46 -05:00
|
|
|
// Clean up the text layer properties if it fails to load
|
2018-11-06 16:54:41 -05:00
|
|
|
delete costume.textLayerMD5;
|
|
|
|
delete costume.textLayerAsset;
|
|
|
|
});
|
2018-11-06 16:44:17 -05:00
|
|
|
};
|
|
|
|
|
2019-02-04 12:45:07 -05:00
|
|
|
const loadBitmap_ = function (costume, runtime, _rotationCenter) {
|
|
|
|
return fetchBitmapCanvas_(costume, runtime, _rotationCenter)
|
|
|
|
.then(fetched => {
|
|
|
|
const updateCostumeAsset = function (dataURI) {
|
|
|
|
if (!runtime.v2BitmapAdapter) {
|
|
|
|
// TODO: This might be a bad practice since the returned
|
|
|
|
// promise isn't acted on. If this is something we should be
|
|
|
|
// creating a rejected promise for we should also catch it
|
|
|
|
// somewhere and act on that error (like logging).
|
|
|
|
//
|
|
|
|
// Return a rejection to stop executing updateCostumeAsset.
|
|
|
|
return Promise.reject('No V2 Bitmap adapter present.');
|
|
|
|
}
|
|
|
|
|
|
|
|
const storage = runtime.storage;
|
|
|
|
costume.asset = storage.createAsset(
|
|
|
|
storage.AssetType.ImageBitmap,
|
|
|
|
storage.DataFormat.PNG,
|
|
|
|
runtime.v2BitmapAdapter.convertDataURIToBinary(dataURI),
|
|
|
|
null,
|
|
|
|
true // generate md5
|
|
|
|
);
|
|
|
|
costume.dataFormat = storage.DataFormat.PNG;
|
|
|
|
costume.assetId = costume.asset.assetId;
|
|
|
|
costume.md5 = `${costume.assetId}.${costume.dataFormat}`;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!fetched.assetMatchesBase) {
|
|
|
|
updateCostumeAsset(fetched.canvas.toDataURL());
|
2018-11-06 17:22:25 -05:00
|
|
|
}
|
|
|
|
|
2019-02-04 12:45:07 -05:00
|
|
|
return fetched;
|
|
|
|
})
|
|
|
|
.then(({canvas, mergeCanvas, rotationCenter}) => {
|
2020-03-28 01:07:14 -04:00
|
|
|
// createBitmapSkin does the right thing if costume.rotationCenter is undefined.
|
2020-03-26 23:12:10 -04:00
|
|
|
// That will be the case if you upload a bitmap asset or create one by taking a photo.
|
2020-01-30 00:03:42 -05:00
|
|
|
let center;
|
|
|
|
if (rotationCenter) {
|
2020-03-26 23:12:10 -04:00
|
|
|
// fetchBitmapCanvas will ensure that the costume's bitmap resolution is 2 and its rotation center is
|
|
|
|
// scaled to match, so it's okay to always divide by 2.
|
2020-01-30 00:03:42 -05:00
|
|
|
center = [
|
|
|
|
rotationCenter[0] / 2,
|
|
|
|
rotationCenter[1] / 2
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-03-26 23:12:10 -04:00
|
|
|
// TODO: costume.bitmapResolution will always be 2 at this point because of fetchBitmapCanvas_, so we don't
|
|
|
|
// need to pass it in here.
|
2020-01-30 00:03:42 -05:00
|
|
|
costume.skinId = runtime.renderer.createBitmapSkin(canvas, costume.bitmapResolution, center);
|
2019-02-04 13:38:46 -05:00
|
|
|
canvasPool.release(mergeCanvas);
|
2018-11-06 16:54:41 -05:00
|
|
|
const renderSize = runtime.renderer.getSkinSize(costume.skinId);
|
|
|
|
costume.size = [renderSize[0] * 2, renderSize[1] * 2]; // Actual size, since all bitmaps are resolution 2
|
|
|
|
|
|
|
|
if (!rotationCenter) {
|
|
|
|
rotationCenter = runtime.renderer.getSkinRotationCenter(costume.skinId);
|
|
|
|
// Actual rotation center, since all bitmaps are resolution 2
|
|
|
|
costume.rotationCenterX = rotationCenter[0] * 2;
|
|
|
|
costume.rotationCenterY = rotationCenter[1] * 2;
|
|
|
|
costume.bitmapResolution = 2;
|
|
|
|
}
|
|
|
|
return costume;
|
|
|
|
});
|
2017-03-27 15:04:44 -04:00
|
|
|
};
|
|
|
|
|
2022-05-02 16:38:56 -04:00
|
|
|
// Handle all manner of costume errors with a Gray Question Mark (default costume)
|
|
|
|
// and preserve as much of the original costume data as possible
|
|
|
|
// Returns a promise of a costume
|
2022-05-13 18:34:34 -04:00
|
|
|
const handleCostumeLoadError = function (costume, runtime) {
|
|
|
|
// Keep track of the old asset information until we're done loading the default costume
|
|
|
|
const oldAsset = costume.asset; // could be null
|
2022-05-02 16:38:56 -04:00
|
|
|
const oldAssetId = costume.assetId;
|
|
|
|
const oldRotationX = costume.rotationCenterX;
|
|
|
|
const oldRotationY = costume.rotationCenterY;
|
2022-05-18 21:26:36 -04:00
|
|
|
const oldBitmapResolution = costume.bitmapResolution;
|
|
|
|
const oldDataFormat = costume.dataFormat;
|
2022-05-16 16:24:10 -04:00
|
|
|
|
|
|
|
const AssetType = runtime.storage.AssetType;
|
|
|
|
const isVector = costume.dataFormat === AssetType.ImageVector.runtimeFormat;
|
2022-05-02 16:38:56 -04:00
|
|
|
|
|
|
|
// Use default asset if original fails to load
|
2022-05-16 16:24:10 -04:00
|
|
|
costume.assetId = isVector ?
|
|
|
|
runtime.storage.defaultAssetId.ImageVector :
|
|
|
|
runtime.storage.defaultAssetId.ImageBitmap;
|
2022-05-02 16:38:56 -04:00
|
|
|
costume.asset = runtime.storage.get(costume.assetId);
|
2022-05-18 21:26:36 -04:00
|
|
|
costume.md5 = `${costume.assetId}.${costume.asset.dataFormat}`;
|
2022-05-16 16:24:10 -04:00
|
|
|
|
|
|
|
const defaultCostumePromise = (isVector) ?
|
2022-05-02 16:38:56 -04:00
|
|
|
loadVector_(costume, runtime) : loadBitmap_(costume, runtime);
|
|
|
|
|
|
|
|
return defaultCostumePromise.then(loadedCostume => {
|
|
|
|
loadedCostume.broken = {};
|
|
|
|
loadedCostume.broken.assetId = oldAssetId;
|
2022-05-18 21:26:36 -04:00
|
|
|
loadedCostume.broken.md5 = `${oldAssetId}.${oldDataFormat}`;
|
2022-05-13 18:34:34 -04:00
|
|
|
|
2022-05-02 16:38:56 -04:00
|
|
|
// Should be null if we got here because the costume was missing
|
2022-05-13 18:34:34 -04:00
|
|
|
loadedCostume.broken.asset = oldAsset;
|
2022-05-18 21:26:36 -04:00
|
|
|
loadedCostume.broken.dataFormat = oldDataFormat;
|
2022-05-13 18:34:34 -04:00
|
|
|
|
2022-05-02 16:38:56 -04:00
|
|
|
loadedCostume.broken.rotationCenterX = oldRotationX;
|
|
|
|
loadedCostume.broken.rotationCenterY = oldRotationY;
|
2022-05-18 21:26:36 -04:00
|
|
|
loadedCostume.broken.bitmapResolution = oldBitmapResolution;
|
2022-05-02 16:38:56 -04:00
|
|
|
return loadedCostume;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-07-10 10:21:21 -04:00
|
|
|
/**
|
|
|
|
* Initialize a costume from an asset asynchronously.
|
|
|
|
* Do not call this unless there is a renderer attached.
|
|
|
|
* @param {!object} costume - the Scratch costume object.
|
|
|
|
* @property {int} skinId - the ID of the costume's render skin, once installed.
|
|
|
|
* @property {number} rotationCenterX - the X component of the costume's origin.
|
|
|
|
* @property {number} rotationCenterY - the Y component of the costume's origin.
|
|
|
|
* @property {number} [bitmapResolution] - the resolution scale for a bitmap costume.
|
2018-11-13 15:31:00 -05:00
|
|
|
* @property {!Asset} costume.asset - the asset of the costume loaded from storage.
|
2018-07-10 10:21:21 -04:00
|
|
|
* @param {!Runtime} runtime - Scratch runtime, used to access the storage module.
|
|
|
|
* @param {?int} optVersion - Version of Scratch that the costume comes from. If this is set
|
|
|
|
* to 2, scratch 3 will perform an upgrade step to handle quirks in SVGs from Scratch 2.0.
|
|
|
|
* @returns {?Promise} - a promise which will resolve after skinId is set, or null on error.
|
|
|
|
*/
|
2018-11-06 16:44:17 -05:00
|
|
|
const loadCostumeFromAsset = function (costume, runtime, optVersion) {
|
|
|
|
costume.assetId = costume.asset.assetId;
|
2018-07-10 10:21:21 -04:00
|
|
|
const renderer = runtime.renderer;
|
|
|
|
if (!renderer) {
|
2022-05-18 16:55:46 -04:00
|
|
|
log.warn('No rendering module present; cannot load costume: ', costume.name);
|
2018-10-03 14:58:36 -04:00
|
|
|
return Promise.resolve(costume);
|
2018-07-10 10:21:21 -04:00
|
|
|
}
|
|
|
|
const AssetType = runtime.storage.AssetType;
|
|
|
|
let rotationCenter;
|
|
|
|
// Use provided rotation center and resolution if they are defined. Bitmap resolution
|
|
|
|
// should only ever be 1 or 2.
|
|
|
|
if (typeof costume.rotationCenterX === 'number' && !isNaN(costume.rotationCenterX) &&
|
|
|
|
typeof costume.rotationCenterY === 'number' && !isNaN(costume.rotationCenterY)) {
|
|
|
|
rotationCenter = [costume.rotationCenterX, costume.rotationCenterY];
|
|
|
|
}
|
2018-12-07 21:46:28 -05:00
|
|
|
if (costume.asset.assetType.runtimeFormat === AssetType.ImageVector.runtimeFormat) {
|
2018-12-10 14:39:17 -05:00
|
|
|
return loadVector_(costume, runtime, rotationCenter, optVersion)
|
2019-09-13 16:13:21 -04:00
|
|
|
.catch(error => {
|
2022-05-13 18:34:34 -04:00
|
|
|
log.warn(`Error loading vector image: ${error}`);
|
2022-05-02 16:38:56 -04:00
|
|
|
return handleCostumeLoadError(costume, runtime);
|
2022-03-15 13:07:50 -04:00
|
|
|
|
2018-12-10 14:39:17 -05:00
|
|
|
});
|
2018-07-10 10:21:21 -04:00
|
|
|
}
|
2022-05-23 17:00:19 -04:00
|
|
|
return loadBitmap_(costume, runtime, rotationCenter, optVersion)
|
|
|
|
.catch(error => {
|
|
|
|
log.warn(`Error loading bitmap image: ${error}`);
|
|
|
|
return handleCostumeLoadError(costume, runtime);
|
|
|
|
});
|
2018-07-10 10:21:21 -04:00
|
|
|
};
|
|
|
|
|
2022-05-02 16:38:56 -04:00
|
|
|
|
2017-09-11 09:42:16 -04:00
|
|
|
/**
|
|
|
|
* Load a costume's asset into memory asynchronously.
|
|
|
|
* Do not call this unless there is a renderer attached.
|
2018-11-06 17:39:26 -05:00
|
|
|
* @param {!string} md5ext - the MD5 and extension of the costume to be loaded.
|
2017-09-11 09:42:16 -04:00
|
|
|
* @param {!object} costume - the Scratch costume object.
|
|
|
|
* @property {int} skinId - the ID of the costume's render skin, once installed.
|
|
|
|
* @property {number} rotationCenterX - the X component of the costume's origin.
|
|
|
|
* @property {number} rotationCenterY - the Y component of the costume's origin.
|
|
|
|
* @property {number} [bitmapResolution] - the resolution scale for a bitmap costume.
|
|
|
|
* @param {!Runtime} runtime - Scratch runtime, used to access the storage module.
|
2018-05-09 15:48:49 -04:00
|
|
|
* @param {?int} optVersion - Version of Scratch that the costume comes from. If this is set
|
|
|
|
* to 2, scratch 3 will perform an upgrade step to handle quirks in SVGs from Scratch 2.0.
|
2017-09-11 09:42:16 -04:00
|
|
|
* @returns {?Promise} - a promise which will resolve after skinId is set, or null on error.
|
|
|
|
*/
|
2018-05-09 15:48:49 -04:00
|
|
|
const loadCostume = function (md5ext, costume, runtime, optVersion) {
|
2018-11-06 17:39:26 -05:00
|
|
|
const idParts = StringUtil.splitFirst(md5ext, '.');
|
|
|
|
const md5 = idParts[0];
|
|
|
|
const ext = idParts[1].toLowerCase();
|
|
|
|
costume.dataFormat = ext;
|
|
|
|
|
2018-11-05 15:50:28 -05:00
|
|
|
if (costume.asset) {
|
2021-06-01 13:42:13 -04:00
|
|
|
// Costume comes with asset. It could be coming from image upload, drag and drop, or file
|
2018-11-06 17:39:26 -05:00
|
|
|
return loadCostumeFromAsset(costume, runtime, optVersion);
|
|
|
|
}
|
2018-11-05 15:50:28 -05:00
|
|
|
|
2018-11-06 17:39:26 -05:00
|
|
|
// Need to load the costume from storage. The server should have a reference to this md5.
|
|
|
|
if (!runtime.storage) {
|
2022-05-18 16:55:46 -04:00
|
|
|
log.warn('No storage module present; cannot load costume asset: ', md5ext);
|
2018-11-06 17:39:26 -05:00
|
|
|
return Promise.resolve(costume);
|
|
|
|
}
|
2018-11-06 10:36:52 -05:00
|
|
|
|
2018-12-10 14:39:17 -05:00
|
|
|
if (!runtime.storage.defaultAssetId) {
|
2022-05-18 16:55:46 -04:00
|
|
|
log.warn(`No default assets found`);
|
2018-12-10 14:39:17 -05:00
|
|
|
return Promise.resolve(costume);
|
|
|
|
}
|
|
|
|
|
2018-11-06 17:39:26 -05:00
|
|
|
const AssetType = runtime.storage.AssetType;
|
|
|
|
const assetType = (ext === 'svg') ? AssetType.ImageVector : AssetType.ImageBitmap;
|
|
|
|
|
|
|
|
const costumePromise = runtime.storage.load(assetType, md5, ext);
|
|
|
|
|
|
|
|
let textLayerPromise;
|
|
|
|
if (costume.textLayerMD5) {
|
|
|
|
textLayerPromise = runtime.storage.load(AssetType.ImageBitmap, costume.textLayerMD5, 'png');
|
|
|
|
} else {
|
|
|
|
textLayerPromise = Promise.resolve(null);
|
2017-09-11 09:42:16 -04:00
|
|
|
}
|
|
|
|
|
2022-05-23 17:00:19 -04:00
|
|
|
return Promise.all([costumePromise, textLayerPromise])
|
|
|
|
.then(assetArray => {
|
|
|
|
if (assetArray[0]) {
|
|
|
|
costume.asset = assetArray[0];
|
|
|
|
} else {
|
|
|
|
return handleCostumeLoadError(costume, runtime);
|
|
|
|
}
|
2022-05-02 16:38:56 -04:00
|
|
|
|
2022-05-23 17:00:19 -04:00
|
|
|
if (assetArray[1]) {
|
|
|
|
costume.textLayerAsset = assetArray[1];
|
|
|
|
}
|
|
|
|
return loadCostumeFromAsset(costume, runtime, optVersion);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
// Handle case where storage.load rejects with errors
|
|
|
|
// instead of resolving null
|
|
|
|
log.warn('Error loading costume: ', error);
|
|
|
|
return handleCostumeLoadError(costume, runtime);
|
|
|
|
});
|
2017-09-11 09:42:16 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
loadCostume,
|
2018-11-06 16:44:17 -05:00
|
|
|
loadCostumeFromAsset
|
2017-09-11 09:42:16 -04:00
|
|
|
};
|