Add tests for load costume error handling. Fix issue where asset data was not getting saved out properly.

This commit is contained in:
Karishma Chadha 2022-05-13 18:34:34 -04:00
parent a8618b378f
commit 70a78cf7db
15 changed files with 418 additions and 13 deletions

View file

@ -250,9 +250,9 @@ const loadBitmap_ = function (costume, runtime, _rotationCenter) {
// 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
const handleCostumeLoadError = function (costume, runtime) {
// Keep track of the old assetId until we're done loading the default costume
// const oldAsset = costume.asset; // could be null
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
const oldAssetId = costume.assetId;
const oldRotationX = costume.rotationCenterX;
const oldRotationY = costume.rotationCenterY;
@ -270,8 +270,10 @@ const handleCostumeLoadError = function (costume, runtime) {
loadedCostume.broken = {};
loadedCostume.broken.assetId = oldAssetId;
loadedCostume.broken.md5 = `${oldAssetId}.${costume.dataFormat}`;
// Should be null if we got here because the costume was missing
loadedCostume.broken.asset = runtime.storage.get(oldAssetId);
loadedCostume.broken.asset = oldAsset;
loadedCostume.broken.rotationCenterX = oldRotationX;
loadedCostume.broken.rotationCenterY = oldRotationY;
return loadedCostume;
@ -310,7 +312,7 @@ const loadCostumeFromAsset = function (costume, runtime, optVersion) {
if (costume.asset.assetType.runtimeFormat === AssetType.ImageVector.runtimeFormat) {
return loadVector_(costume, runtime, rotationCenter, optVersion)
.catch(error => {
log.warn(`Error loading vector image: ${error.name}: ${error.message}`);
log.warn(`Error loading vector image: ${error}`);
return handleCostumeLoadError(costume, runtime);
});

View file

@ -427,11 +427,9 @@ class VirtualMachine extends EventEmitter {
* specified by optZipType or blob by default.
*/
exportSprite (targetId, optZipType) {
const sb3 = require('./serialization/sb3');
const soundDescs = serializeSounds(this.runtime, targetId);
const costumeDescs = serializeCostumes(this.runtime, targetId);
const spriteJson = StringUtil.stringify(sb3.serialize(this.runtime, targetId));
const spriteJson = this.toJSON(targetId);
const zip = new JSZip();
zip.file('sprite.json', spriteJson);
@ -448,12 +446,13 @@ class VirtualMachine extends EventEmitter {
}
/**
* Export project as a Scratch 3.0 JSON representation.
* Export project or sprite as a Scratch 3.0 JSON representation.
* @param {string=} optTargetId - Optional id of a sprite to serialize
* @return {string} Serialized state of the runtime.
*/
toJSON () {
toJSON (optTargetId) {
const sb3 = require('./serialization/sb3');
return StringUtil.stringify(sb3.serialize(this.runtime));
return StringUtil.stringify(sb3.serialize(this.runtime, optTargetId));
}
// TODO do we still need this function? Keeping it here so as not to introduce