Let GQM replace broken costume data but explicitly save and serialize broken costume data before finishing loading the costume.

This commit is contained in:
Karishma Chadha 2022-03-15 13:07:50 -04:00
parent ca67f4ef4f
commit deadce3bc6
4 changed files with 31 additions and 7 deletions

View file

@ -280,11 +280,25 @@ const loadCostumeFromAsset = function (costume, runtime, optVersion) {
return loadVector_(costume, runtime, rotationCenter, optVersion) return loadVector_(costume, runtime, rotationCenter, optVersion)
.catch(error => { .catch(error => {
log.warn(`Error loading vector image: ${error.name}: ${error.message}`); log.warn(`Error loading vector image: ${error.name}: ${error.message}`);
// Keep track of the old assetId until we're done loading the default costume
const oldAssetId = costume.assetId;
const oldRotationX = costume.rotationCenterX;
const oldRotationY = costume.rotationCenterY;
// Use default asset if original fails to load // Use default asset if original fails to load
costume.assetId = runtime.storage.defaultAssetId.ImageVector; costume.assetId = runtime.storage.defaultAssetId.ImageVector;
costume.asset = runtime.storage.get(costume.assetId); costume.asset = runtime.storage.get(costume.assetId);
costume.md5 = `${costume.assetId}.${AssetType.ImageVector.runtimeFormat}`; costume.md5 = `${costume.assetId}.${AssetType.ImageVector.runtimeFormat}`;
return loadVector_(costume, runtime); return loadVector_(costume, runtime).then(loadedCostume => {
loadedCostume.broken = {};
loadedCostume.broken.assetId = oldAssetId;
loadedCostume.broken.md5 = `${oldAssetId}.${AssetType.ImageVector.runtimeFormat}`;
loadedCostume.broken.asset = runtime.storage.get(oldAssetId);
loadedCostume.broken.rotationCenterX = oldRotationX;
loadedCostume.broken.rotationCenterY = oldRotationY;
return loadedCostume;
});
}); });
} }
return loadBitmap_(costume, runtime, rotationCenter, optVersion); return loadBitmap_(costume, runtime, rotationCenter, optVersion);

View file

@ -345,7 +345,7 @@ const serializeBlocks = function (blocks) {
*/ */
const serializeCostume = function (costume) { const serializeCostume = function (costume) {
const obj = Object.create(null); const obj = Object.create(null);
obj.assetId = costume.assetId; obj.assetId = (costume.broken && costume.broken.assetId) || costume.assetId;
obj.name = costume.name; obj.name = costume.name;
obj.bitmapResolution = costume.bitmapResolution; obj.bitmapResolution = costume.bitmapResolution;
// serialize this property with the name 'md5ext' because that's // serialize this property with the name 'md5ext' because that's
@ -353,10 +353,11 @@ const serializeCostume = function (costume) {
// updated to actually refer to this as 'md5ext' instead of 'md5' // updated to actually refer to this as 'md5ext' instead of 'md5'
// but that change should be made carefully since it is very // but that change should be made carefully since it is very
// pervasive // pervasive
obj.md5ext = costume.md5; obj.md5ext = (costume.broken && costume.broken.md5) || costume.md5;
obj.dataFormat = costume.dataFormat.toLowerCase(); obj.dataFormat = costume.dataFormat.toLowerCase();
obj.rotationCenterX = costume.rotationCenterX; // TODO: WATCH OUT FOR ZEROs HERE
obj.rotationCenterY = costume.rotationCenterY; obj.rotationCenterX = (costume.broken && costume.broken.rotationCenterX) || costume.rotationCenterX;
obj.rotationCenterY = (costume.broken && costume.broken.rotationCenterY) || costume.rotationCenterY;
return obj; return obj;
}; };

View file

@ -16,10 +16,18 @@ const serializeAssets = function (runtime, assetType, optTargetId) {
const currAssets = currTarget.sprite[assetType]; const currAssets = currTarget.sprite[assetType];
for (let j = 0; j < currAssets.length; j++) { for (let j = 0; j < currAssets.length; j++) {
const currAsset = currAssets[j]; const currAsset = currAssets[j];
const asset = currAsset.asset; let asset = currAsset.asset;
if (currAsset.broken) {
if (currAsset.broken.asset) {
asset = currAsset.broken.asset;
} else {
continue;
}
}
assetDescs.push({ assetDescs.push({
fileName: `${asset.assetId}.${asset.dataFormat}`, fileName: `${asset.assetId}.${asset.dataFormat}`,
fileContent: asset.data}); fileContent: asset.data
});
} }
} }
return assetDescs; return assetDescs;

View file

@ -932,6 +932,7 @@ class VirtualMachine extends EventEmitter {
*/ */
updateSvg (costumeIndex, svg, rotationCenterX, rotationCenterY) { updateSvg (costumeIndex, svg, rotationCenterX, rotationCenterY) {
const costume = this.editingTarget.getCostumes()[costumeIndex]; const costume = this.editingTarget.getCostumes()[costumeIndex];
if (costume && costume.broken) costume.broken = null;
if (costume && this.runtime && this.runtime.renderer) { if (costume && this.runtime && this.runtime.renderer) {
costume.rotationCenterX = rotationCenterX; costume.rotationCenterX = rotationCenterX;
costume.rotationCenterY = rotationCenterY; costume.rotationCenterY = rotationCenterY;