Merge pull request #1840 from ktbee/default-asset-for-broken-svg

Use default builtin SVG asset when an SVG can't be loaded
This commit is contained in:
Katie Broida 2018-12-19 14:54:15 -05:00 committed by GitHub
commit 3dcee7b1ad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,32 +2,34 @@ const StringUtil = require('../util/string-util');
const log = require('../util/log'); const log = require('../util/log');
const loadVector_ = function (costume, runtime, rotationCenter, optVersion) { const loadVector_ = function (costume, runtime, rotationCenter, optVersion) {
let svgString = costume.asset.decodeText(); return new Promise(resolve => {
// SVG Renderer load fixes "quirks" associated with Scratch 2 projects let svgString = costume.asset.decodeText();
if (optVersion && optVersion === 2 && !runtime.v2SvgAdapter) { // SVG Renderer load fixes "quirks" associated with Scratch 2 projects
log.error('No V2 SVG adapter present; SVGs may not render correctly.'); if (optVersion && optVersion === 2 && !runtime.v2SvgAdapter) {
} else if (optVersion && optVersion === 2 && runtime.v2SvgAdapter) { log.error('No V2 SVG adapter present; SVGs may not render correctly.');
runtime.v2SvgAdapter.loadString(svgString, true /* fromVersion2 */); } else if (optVersion && optVersion === 2 && runtime.v2SvgAdapter) {
svgString = runtime.v2SvgAdapter.toString(); runtime.v2SvgAdapter.loadString(svgString, true /* fromVersion2 */);
// Put back into storage svgString = runtime.v2SvgAdapter.toString();
const storage = runtime.storage; // Put back into storage
costume.asset.encodeTextData(svgString, storage.DataFormat.SVG, true); const storage = runtime.storage;
costume.assetId = costume.asset.assetId; costume.asset.encodeTextData(svgString, storage.DataFormat.SVG, true);
costume.md5 = `${costume.assetId}.${costume.dataFormat}`; 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 // createSVGSkin does the right thing if rotationCenter isn't provided, so it's okay if it's
costume.skinId = runtime.renderer.createSVGSkin(svgString, rotationCenter); // undefined here
costume.size = runtime.renderer.getSkinSize(costume.skinId); costume.skinId = runtime.renderer.createSVGSkin(svgString, rotationCenter);
// Now we should have a rotationCenter even if we didn't before costume.size = runtime.renderer.getSkinSize(costume.skinId);
if (!rotationCenter) { // Now we should have a rotationCenter even if we didn't before
rotationCenter = runtime.renderer.getSkinRotationCenter(costume.skinId); if (!rotationCenter) {
costume.rotationCenterX = rotationCenter[0]; rotationCenter = runtime.renderer.getSkinRotationCenter(costume.skinId);
costume.rotationCenterY = rotationCenter[1]; costume.rotationCenterX = rotationCenter[0];
costume.bitmapResolution = 1; 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]; rotationCenter = [costume.rotationCenterX, costume.rotationCenterY];
} }
if (costume.asset.assetType.runtimeFormat === AssetType.ImageVector.runtimeFormat) { 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); return loadBitmap_(costume, runtime, rotationCenter, optVersion);
}; };
@ -248,6 +256,11 @@ const loadCostume = function (md5ext, costume, runtime, optVersion) {
return Promise.resolve(costume); 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 = runtime.storage.AssetType;
const assetType = (ext === 'svg') ? AssetType.ImageVector : AssetType.ImageBitmap; const assetType = (ext === 'svg') ? AssetType.ImageVector : AssetType.ImageBitmap;