From 5941b9173e40aabb183e85576d31e01da1869bf9 Mon Sep 17 00:00:00 2001 From: Katie Broida Date: Mon, 10 Dec 2018 14:39:17 -0500 Subject: [PATCH] Use default builtin SVG asset for SVGs that can't be loaded for some reason --- src/import/load-costume.js | 65 +++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 26 deletions(-) diff --git a/src/import/load-costume.js b/src/import/load-costume.js index 2fe994151..b8a1ff8c0 100644 --- a/src/import/load-costume.js +++ b/src/import/load-costume.js @@ -2,32 +2,34 @@ const StringUtil = require('../util/string-util'); const log = require('../util/log'); const loadVector_ = function (costume, runtime, rotationCenter, optVersion) { - let svgString = costume.asset.decodeText(); - // SVG Renderer load fixes "quirks" associated with Scratch 2 projects - if (optVersion && optVersion === 2 && !runtime.v2SvgAdapter) { - log.error('No V2 SVG adapter present; SVGs may not render correctly.'); - } else if (optVersion && optVersion === 2 && runtime.v2SvgAdapter) { - runtime.v2SvgAdapter.loadString(svgString, true /* fromVersion2 */); - svgString = runtime.v2SvgAdapter.toString(); - // Put back into storage - const storage = runtime.storage; - costume.asset.encodeTextData(svgString, storage.DataFormat.SVG, true); - costume.assetId = costume.asset.assetId; - costume.md5 = `${costume.assetId}.${costume.dataFormat}`; - } - // createSVGSkin does the right thing if rotationCenter isn't provided, so it's okay if it's - // undefined here - costume.skinId = runtime.renderer.createSVGSkin(svgString, rotationCenter); - 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; - } + return new Promise(resolve => { + let svgString = costume.asset.decodeText(); + // SVG Renderer load fixes "quirks" associated with Scratch 2 projects + if (optVersion && optVersion === 2 && !runtime.v2SvgAdapter) { + log.error('No V2 SVG adapter present; SVGs may not render correctly.'); + } else if (optVersion && optVersion === 2 && runtime.v2SvgAdapter) { + runtime.v2SvgAdapter.loadString(svgString, true /* fromVersion2 */); + svgString = runtime.v2SvgAdapter.toString(); + // Put back into storage + const storage = runtime.storage; + costume.asset.encodeTextData(svgString, storage.DataFormat.SVG, true); + costume.assetId = costume.asset.assetId; + costume.md5 = `${costume.assetId}.${costume.dataFormat}`; + } + // createSVGSkin does the right thing if rotationCenter isn't provided, so it's okay if it's + // undefined here + costume.skinId = runtime.renderer.createSVGSkin(svgString, rotationCenter); + 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; + } - return Promise.resolve(costume); + resolve(costume); + }); }; /** @@ -212,7 +214,13 @@ const loadCostumeFromAsset = function (costume, runtime, optVersion) { rotationCenter = [costume.rotationCenterX, costume.rotationCenterY]; } if (costume.asset.assetType.runtimeFormat === AssetType.ImageVector.runtimeFormat) { - return loadVector_(costume, runtime, rotationCenter, optVersion); + return loadVector_(costume, runtime, rotationCenter, optVersion) + .catch(() => { + // Use default asset if original fails to load + costume.assetId = runtime.storage.defaultAssetId.ImageVector; + costume.asset = runtime.storage.get(costume.assetId); + return loadVector_(costume, runtime); + }); } return loadBitmap_(costume, runtime, rotationCenter, optVersion); }; @@ -248,6 +256,11 @@ const loadCostume = function (md5ext, costume, runtime, optVersion) { return Promise.resolve(costume); } + if (!runtime.storage.defaultAssetId) { + log.error(`No default assets found`); + return Promise.resolve(costume); + } + const AssetType = runtime.storage.AssetType; const assetType = (ext === 'svg') ? AssetType.ImageVector : AssetType.ImageBitmap;