From c9ce6776aa9f084dee1fa5bb71da7f6c73f83b47 Mon Sep 17 00:00:00 2001 From: Ray Schamp Date: Thu, 27 Apr 2017 17:08:06 -0400 Subject: [PATCH] Get SB3 imports working For now, store the md5 + extension on costumes in the VM. This way we can load them the same way as we load SB2 assets. --- src/import/load-costume.js | 1 + src/serialization/sb3.js | 72 +++++++++++++++++----------------- src/sprites/rendered-target.js | 1 + 3 files changed, 37 insertions(+), 37 deletions(-) diff --git a/src/import/load-costume.js b/src/import/load-costume.js index 5d9fdc1d..83ef7800 100644 --- a/src/import/load-costume.js +++ b/src/import/load-costume.js @@ -13,6 +13,7 @@ const log = require('../util/log'); * @returns {?Promise} - a promise which will resolve after skinId is set, or null on error. */ const loadCostume = function (md5ext, costume, runtime) { + costume.md5ext = md5ext; if (!runtime.storage) { log.error('No storage module present; cannot load costume asset: ', md5ext); return Promise.resolve(costume); diff --git a/src/serialization/sb3.js b/src/serialization/sb3.js index 4eb06dd5..1f46ad94 100644 --- a/src/serialization/sb3.js +++ b/src/serialization/sb3.js @@ -10,6 +10,9 @@ const Sprite = require('../sprites/sprite'); const Variable = require('../engine/variable'); const List = require('../engine/list'); +const loadCostume = require('../import/load-costume.js'); +const loadSound = require('../import/load-sound.js'); + /** * Serializes the specified VM runtime. * @param {!Runtime} runtime VM runtime instance to be serialized. @@ -63,38 +66,33 @@ const parseScratchObject = function (object, runtime) { // console.log(blocks); } // Costumes from JSON. - if (object.hasOwnProperty('costumes') || object.hasOwnProperty('costume')) { - for (let i = 0; i < object.costumeCount; i++) { - const costume = object.costumes[i]; - // @todo: Make sure all the relevant metadata is being pulled out. - sprite.costumes.push({ - skin: costume.skin, - name: costume.name, - bitmapResolution: costume.bitmapResolution, - rotationCenterX: costume.rotationCenterX, - rotationCenterY: costume.rotationCenterY - }); - } - } + const costumePromises = (object.costumes || []).map(costumeSource => { + // @todo: Make sure all the relevant metadata is being pulled out. + const costume = { + skinId: null, + name: costumeSource.name, + bitmapResolution: costumeSource.bitmapResolution, + rotationCenterX: costumeSource.rotationCenterX, + rotationCenterY: costumeSource.rotationCenterY + }; + return loadCostume(costumeSource.md5ext, costume, runtime); + }); // Sounds from JSON - if (object.hasOwnProperty('sounds')) { - for (let s = 0; s < object.sounds.length; s++) { - const sound = object.sounds[s]; - sprite.sounds.push({ - format: sound.format, - fileUrl: sound.fileUrl, - rate: sound.rate, - sampleCount: sound.sampleCount, - soundID: sound.soundID, - name: sound.name, - md5: sound.md5 - }); - } - } + const soundPromises = (object.sounds || []).map(soundSource => { + const sound = { + format: soundSource.format, + fileUrl: soundSource.fileUrl, + rate: soundSource.rate, + sampleCount: soundSource.sampleCount, + soundID: soundSource.soundID, + name: soundSource.name, + md5: soundSource.md5, + data: null + }; + return loadSound(sound, runtime); + }); // Create the first clone, and load its run-state from JSON. const target = sprite.createClone(); - // Add it to the runtime's list of targets. - runtime.targets.push(target); // Load target properties from JSON. if (object.hasOwnProperty('variables')) { for (let j = 0; j < object.variables.length; j++) { @@ -140,11 +138,13 @@ const parseScratchObject = function (object, runtime) { if (object.hasOwnProperty('isStage')) { target.isStage = object.isStage; } - target.updateAllDrawableProperties(); - - // console.log('returning target:'); - // console.log(target); - return target; + Promise.all(costumePromises).then(costumes => { + sprite.costumes = costumes; + }); + Promise.all(soundPromises).then(sounds => { + sprite.sounds = sounds; + }); + return Promise.all(costumePromises.concat(soundPromises)).then(() => target); }; /** @@ -154,9 +154,7 @@ const parseScratchObject = function (object, runtime) { * @param {Runtime} runtime Runtime instance */ const deserialize = function (json, runtime) { - for (let i = 0; i < json.targets.length; i++) { - parseScratchObject(json.targets[i], runtime); - } + return Promise.all(json.targets.map(target => parseScratchObject(target, runtime))); }; module.exports = { diff --git a/src/sprites/rendered-target.js b/src/sprites/rendered-target.js index d48bbd24..bc863dfe 100644 --- a/src/sprites/rendered-target.js +++ b/src/sprites/rendered-target.js @@ -789,6 +789,7 @@ class RenderedTarget extends Target { size: this.size, direction: this.direction, draggable: this.draggable, + currentCostume: this.currentCostume, costume: costumes[this.currentCostume], costumeCount: costumes.length, visible: this.visible,