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,6 +2,7 @@ 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) {
return new Promise(resolve => {
let svgString = costume.asset.decodeText(); let svgString = costume.asset.decodeText();
// SVG Renderer load fixes "quirks" associated with Scratch 2 projects // SVG Renderer load fixes "quirks" associated with Scratch 2 projects
if (optVersion && optVersion === 2 && !runtime.v2SvgAdapter) { if (optVersion && optVersion === 2 && !runtime.v2SvgAdapter) {
@ -27,7 +28,8 @@ const loadVector_ = function (costume, runtime, rotationCenter, optVersion) {
costume.bitmapResolution = 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;