2016-12-30 10:19:58 -05:00
|
|
|
/**
|
|
|
|
* @fileoverview
|
|
|
|
* Partial implementation of a SB3 serializer and deserializer. Parses provided
|
|
|
|
* JSON and then generates all needed scratch-vm runtime structures.
|
|
|
|
*/
|
|
|
|
|
2017-04-26 16:50:53 -04:00
|
|
|
const vmPackage = require('../../package.json');
|
|
|
|
const Blocks = require('../engine/blocks');
|
|
|
|
const Sprite = require('../sprites/sprite');
|
|
|
|
const Variable = require('../engine/variable');
|
2016-12-30 10:19:58 -05:00
|
|
|
|
2017-09-11 09:42:16 -04:00
|
|
|
const {loadCostume} = require('../import/load-costume.js');
|
|
|
|
const {loadSound} = require('../import/load-sound.js');
|
2018-02-16 00:44:23 -05:00
|
|
|
const {deserializeCostume, deserializeSound} = require('./deserialize-assets.js');
|
2017-04-27 17:08:06 -04:00
|
|
|
|
2017-11-03 14:17:16 -04:00
|
|
|
/**
|
|
|
|
* @typedef {object} ImportedProject
|
|
|
|
* @property {Array.<Target>} targets - the imported Scratch 3.0 target objects.
|
|
|
|
* @property {ImportedExtensionsInfo} extensionsInfo - the ID of each extension actually used by this project.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {object} ImportedExtensionsInfo
|
|
|
|
* @property {Set.<string>} extensionIDs - the ID of each extension actually in use by blocks in this project.
|
|
|
|
* @property {Map.<string, string>} extensionURLs - map of ID => URL from project metadata. May not match extensionIDs.
|
|
|
|
*/
|
|
|
|
|
2018-03-09 15:43:11 -05:00
|
|
|
const serializeBlock = function (block) {
|
|
|
|
const obj = Object.create(null);
|
|
|
|
obj.id = block.id;
|
|
|
|
obj.opcode = block.opcode;
|
2018-03-09 16:03:28 -05:00
|
|
|
obj.next = block.next;
|
|
|
|
obj.parent = block.parent;
|
2018-03-09 15:43:11 -05:00
|
|
|
obj.inputs = block.inputs;
|
|
|
|
obj.fields = block.fields;
|
2018-03-16 11:43:27 -04:00
|
|
|
obj.topLevel = block.topLevel ? block.topLevel : false;
|
2018-03-09 15:43:11 -05:00
|
|
|
obj.shadow = block.shadow;
|
|
|
|
if (block.topLevel) {
|
|
|
|
if (block.x) {
|
|
|
|
obj.x = Math.round(block.x);
|
|
|
|
}
|
|
|
|
if (block.y) {
|
|
|
|
obj.y = Math.round(block.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (block.mutation) {
|
|
|
|
obj.mutation = block.mutation;
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
|
|
|
|
const serializeBlocks = function (blocks) {
|
|
|
|
const obj = Object.create(null);
|
|
|
|
for (const blockID in blocks) {
|
2018-03-21 17:42:01 -04:00
|
|
|
if (!blocks.hasOwnProperty(blockID)) continue;
|
2018-03-09 15:43:11 -05:00
|
|
|
obj[blockID] = serializeBlock(blocks[blockID]);
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
|
|
|
|
const serializeCostume = function (costume) {
|
|
|
|
const obj = Object.create(null);
|
|
|
|
obj.assetId = costume.assetId;
|
|
|
|
obj.name = costume.name;
|
|
|
|
obj.bitmapResolution = costume.bitmapResolution;
|
2018-03-21 16:51:40 -04:00
|
|
|
// serialize this property with the name 'md5ext' because that's
|
|
|
|
// what it's actually referring to. TODO runtime objects need to be
|
|
|
|
// updated to actually refer to this as 'md5ext' instead of 'md5'
|
|
|
|
// but that change should be made carefully since it is very
|
|
|
|
// pervasive
|
|
|
|
obj.md5ext = costume.md5;
|
2018-03-09 15:43:11 -05:00
|
|
|
obj.dataFormat = costume.dataFormat;
|
|
|
|
obj.rotationCenterX = costume.rotationCenterX;
|
|
|
|
obj.rotationCenterY = costume.rotationCenterY;
|
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
|
|
|
|
const serializeSound = function (sound) {
|
|
|
|
const obj = Object.create(null);
|
|
|
|
obj.assetId = sound.assetId;
|
|
|
|
obj.name = sound.name;
|
|
|
|
obj.dataFormat = sound.dataFormat;
|
|
|
|
obj.format = sound.format;
|
|
|
|
obj.rate = sound.rate;
|
|
|
|
obj.sampleCount = sound.sampleCount;
|
2018-03-21 16:51:40 -04:00
|
|
|
// serialize this property with the name 'md5ext' because that's
|
|
|
|
// what it's actually referring to. TODO runtime objects need to be
|
|
|
|
// updated to actually refer to this as 'md5ext' instead of 'md5'
|
|
|
|
// but that change should be made carefully since it is very
|
|
|
|
// pervasive
|
|
|
|
obj.md5ext = sound.md5;
|
2018-03-09 15:43:11 -05:00
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
|
2018-03-09 17:26:41 -05:00
|
|
|
const serializeTarget = function (target) {
|
2018-03-09 15:43:11 -05:00
|
|
|
const obj = Object.create(null);
|
2018-03-09 17:26:41 -05:00
|
|
|
obj.isStage = target.isStage;
|
2018-03-09 15:43:11 -05:00
|
|
|
obj.name = target.name;
|
|
|
|
obj.variables = target.variables; // This means that uids for variables will persist across saves/loads
|
|
|
|
obj.blocks = serializeBlocks(target.blocks);
|
|
|
|
obj.currentCostume = target.currentCostume;
|
|
|
|
obj.costumes = target.costumes.map(serializeCostume);
|
|
|
|
obj.sounds = target.sounds.map(serializeSound);
|
|
|
|
if (!obj.isStage) {
|
|
|
|
// Stage does not need the following properties
|
|
|
|
obj.visible = target.visible;
|
|
|
|
obj.x = target.x;
|
|
|
|
obj.y = target.y;
|
|
|
|
obj.size = target.size;
|
|
|
|
obj.direction = target.direction;
|
|
|
|
obj.draggable = target.draggable;
|
|
|
|
obj.rotationStyle = target.rotationStyle;
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
|
2016-12-30 10:19:58 -05:00
|
|
|
/**
|
|
|
|
* Serializes the specified VM runtime.
|
|
|
|
* @param {!Runtime} runtime VM runtime instance to be serialized.
|
2017-04-27 17:49:57 -04:00
|
|
|
* @return {object} Serialized runtime instance.
|
2016-12-30 10:19:58 -05:00
|
|
|
*/
|
2017-04-26 16:50:53 -04:00
|
|
|
const serialize = function (runtime) {
|
2016-12-30 10:19:58 -05:00
|
|
|
// Fetch targets
|
2017-04-26 16:50:53 -04:00
|
|
|
const obj = Object.create(null);
|
2018-03-09 15:43:11 -05:00
|
|
|
const flattenedOriginalTargets = JSON.parse(JSON.stringify(
|
|
|
|
runtime.targets.filter(target => target.isOriginal)));
|
|
|
|
obj.targets = flattenedOriginalTargets.map(t => serializeTarget(t, runtime));
|
|
|
|
|
|
|
|
// TODO Serialize monitors
|
2016-12-30 10:19:58 -05:00
|
|
|
|
|
|
|
// Assemble metadata
|
2017-04-26 16:50:53 -04:00
|
|
|
const meta = Object.create(null);
|
2016-12-30 10:19:58 -05:00
|
|
|
meta.semver = '3.0.0';
|
2017-04-26 16:50:53 -04:00
|
|
|
meta.vm = vmPackage.version;
|
2016-12-30 10:19:58 -05:00
|
|
|
|
|
|
|
// Attach full user agent string to metadata if available
|
|
|
|
meta.agent = null;
|
|
|
|
if (typeof navigator !== 'undefined') meta.agent = navigator.userAgent;
|
|
|
|
|
|
|
|
// Assemble payload and return
|
|
|
|
obj.meta = meta;
|
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
|
2017-01-27 20:05:54 -05:00
|
|
|
/**
|
|
|
|
* Parse a single "Scratch object" and create all its in-memory VM objects.
|
2017-04-26 16:50:53 -04:00
|
|
|
* @param {!object} object From-JSON "Scratch object:" sprite, stage, watcher.
|
2017-01-27 20:05:54 -05:00
|
|
|
* @param {!Runtime} runtime Runtime object to load all structures into.
|
2017-11-03 14:17:16 -04:00
|
|
|
* @param {ImportedExtensionsInfo} extensions - (in/out) parsed extension information will be stored here.
|
2018-02-16 00:44:23 -05:00
|
|
|
* @param {JSZip} zip Sb3 file describing this project (to load assets from)
|
2017-11-03 14:17:16 -04:00
|
|
|
* @return {!Promise.<Target>} Promise for the target created (stage or sprite), or null for unsupported objects.
|
2017-01-27 20:05:54 -05:00
|
|
|
*/
|
2018-02-16 00:44:23 -05:00
|
|
|
const parseScratchObject = function (object, runtime, extensions, zip) {
|
2017-01-27 20:05:54 -05:00
|
|
|
if (!object.hasOwnProperty('name')) {
|
|
|
|
// Watcher/monitor - skip this object until those are implemented in VM.
|
|
|
|
// @todo
|
2017-11-03 14:17:16 -04:00
|
|
|
return Promise.resolve(null);
|
2017-01-27 20:05:54 -05:00
|
|
|
}
|
|
|
|
// Blocks container for this object.
|
2017-04-26 16:50:53 -04:00
|
|
|
const blocks = new Blocks();
|
2017-01-27 20:05:54 -05:00
|
|
|
|
|
|
|
// @todo: For now, load all Scratch objects (stage/sprites) as a Sprite.
|
2017-04-26 16:50:53 -04:00
|
|
|
const sprite = new Sprite(blocks, runtime);
|
2017-01-27 20:05:54 -05:00
|
|
|
|
|
|
|
// Sprite/stage name from JSON.
|
|
|
|
if (object.hasOwnProperty('name')) {
|
|
|
|
sprite.name = object.name;
|
|
|
|
}
|
|
|
|
if (object.hasOwnProperty('blocks')) {
|
2017-08-26 13:07:47 -04:00
|
|
|
for (const blockId in object.blocks) {
|
2018-03-21 17:42:01 -04:00
|
|
|
if (!object.blocks.hasOwnProperty(blockId)) continue;
|
2017-11-03 14:42:20 -04:00
|
|
|
const blockJSON = object.blocks[blockId];
|
2017-11-03 14:17:16 -04:00
|
|
|
blocks.createBlock(blockJSON);
|
|
|
|
|
|
|
|
const dotIndex = blockJSON.opcode.indexOf('.');
|
|
|
|
if (dotIndex >= 0) {
|
|
|
|
const extensionId = blockJSON.opcode.substring(0, dotIndex);
|
|
|
|
extensions.extensionIDs.add(extensionId);
|
|
|
|
}
|
2017-01-27 20:05:54 -05:00
|
|
|
}
|
2017-04-26 16:50:53 -04:00
|
|
|
// console.log(blocks);
|
2017-01-27 20:05:54 -05:00
|
|
|
}
|
|
|
|
// Costumes from JSON.
|
2017-04-27 17:08:06 -04:00
|
|
|
const costumePromises = (object.costumes || []).map(costumeSource => {
|
|
|
|
// @todo: Make sure all the relevant metadata is being pulled out.
|
|
|
|
const costume = {
|
2018-03-21 16:51:40 -04:00
|
|
|
assetId: costumeSource.assetId,
|
2017-04-27 17:08:06 -04:00
|
|
|
skinId: null,
|
|
|
|
name: costumeSource.name,
|
|
|
|
bitmapResolution: costumeSource.bitmapResolution,
|
|
|
|
rotationCenterX: costumeSource.rotationCenterX,
|
|
|
|
rotationCenterY: costumeSource.rotationCenterY
|
|
|
|
};
|
2017-06-13 17:56:06 -04:00
|
|
|
const dataFormat =
|
|
|
|
costumeSource.dataFormat ||
|
|
|
|
(costumeSource.assetType && costumeSource.assetType.runtimeFormat) || // older format
|
|
|
|
'png'; // if all else fails, guess that it might be a PNG
|
2018-03-21 16:51:40 -04:00
|
|
|
const costumeMd5Ext = costumeSource.hasOwnProperty('md5ext') ?
|
|
|
|
costumeSource.md5ext : `${costumeSource.assetId}.${dataFormat}`;
|
|
|
|
costume.md5 = costumeMd5Ext;
|
|
|
|
costume.dataFormat = dataFormat;
|
|
|
|
// deserializeCostume should be called on the costume object we're
|
|
|
|
// creating above instead of the source costume object, because this way
|
|
|
|
// we're always loading the 'sb3' representation of the costume
|
|
|
|
// any translation that needs to happen will happen in the process
|
|
|
|
// of building up the costume object into an sb3 format
|
|
|
|
return deserializeCostume(costume, runtime, zip)
|
|
|
|
.then(() => loadCostume(costumeMd5Ext, costume, runtime));
|
2018-02-16 00:44:23 -05:00
|
|
|
// Only attempt to load the costume after the deserialization
|
|
|
|
// process has been completed
|
2017-04-27 17:08:06 -04:00
|
|
|
});
|
2017-01-27 20:05:54 -05:00
|
|
|
// Sounds from JSON
|
2017-04-27 17:08:06 -04:00
|
|
|
const soundPromises = (object.sounds || []).map(soundSource => {
|
|
|
|
const sound = {
|
2018-03-21 16:51:40 -04:00
|
|
|
assetId: soundSource.assetId,
|
2017-04-27 17:08:06 -04:00
|
|
|
format: soundSource.format,
|
|
|
|
rate: soundSource.rate,
|
|
|
|
sampleCount: soundSource.sampleCount,
|
|
|
|
name: soundSource.name,
|
2018-03-21 16:51:40 -04:00
|
|
|
// TODO we eventually want this property to be called md5ext,
|
|
|
|
// but there are many things relying on this particular name at the
|
|
|
|
// moment, so this translation is very important
|
|
|
|
md5: soundSource.md5ext,
|
|
|
|
dataFormat: soundSource.dataFormat,
|
2017-04-27 17:08:06 -04:00
|
|
|
data: null
|
|
|
|
};
|
2018-03-21 16:51:40 -04:00
|
|
|
// deserializeSound should be called on the sound object we're
|
|
|
|
// creating above instead of the source sound object, because this way
|
|
|
|
// we're always loading the 'sb3' representation of the costume
|
|
|
|
// any translation that needs to happen will happen in the process
|
|
|
|
// of building up the costume object into an sb3 format
|
|
|
|
return deserializeSound(sound, runtime, zip)
|
2018-02-16 00:44:23 -05:00
|
|
|
.then(() => loadSound(sound, runtime));
|
|
|
|
// Only attempt to load the sound after the deserialization
|
|
|
|
// process has been completed.
|
2017-04-27 17:08:06 -04:00
|
|
|
});
|
2017-01-27 20:05:54 -05:00
|
|
|
// Create the first clone, and load its run-state from JSON.
|
2017-04-26 16:50:53 -04:00
|
|
|
const target = sprite.createClone();
|
2017-01-27 20:05:54 -05:00
|
|
|
// Load target properties from JSON.
|
|
|
|
if (object.hasOwnProperty('variables')) {
|
2017-08-26 13:07:47 -04:00
|
|
|
for (const j in object.variables) {
|
2017-04-26 16:50:53 -04:00
|
|
|
const variable = object.variables[j];
|
2017-06-15 17:29:15 -04:00
|
|
|
const newVariable = new Variable(
|
|
|
|
variable.id,
|
2017-01-27 20:05:54 -05:00
|
|
|
variable.name,
|
2017-11-09 17:19:34 -05:00
|
|
|
variable.type,
|
2017-01-27 20:05:54 -05:00
|
|
|
variable.isPersistent
|
|
|
|
);
|
2017-11-09 17:19:34 -05:00
|
|
|
newVariable.value = variable.value;
|
2017-06-15 17:29:15 -04:00
|
|
|
target.variables[newVariable.id] = newVariable;
|
2017-01-27 20:05:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (object.hasOwnProperty('x')) {
|
|
|
|
target.x = object.x;
|
|
|
|
}
|
|
|
|
if (object.hasOwnProperty('y')) {
|
|
|
|
target.y = object.y;
|
|
|
|
}
|
|
|
|
if (object.hasOwnProperty('direction')) {
|
|
|
|
target.direction = object.direction;
|
|
|
|
}
|
|
|
|
if (object.hasOwnProperty('size')) {
|
|
|
|
target.size = object.size;
|
|
|
|
}
|
|
|
|
if (object.hasOwnProperty('visible')) {
|
|
|
|
target.visible = object.visible;
|
|
|
|
}
|
|
|
|
if (object.hasOwnProperty('currentCostume')) {
|
|
|
|
target.currentCostume = object.currentCostume;
|
|
|
|
}
|
|
|
|
if (object.hasOwnProperty('rotationStyle')) {
|
|
|
|
target.rotationStyle = object.rotationStyle;
|
|
|
|
}
|
2017-02-07 18:38:44 -05:00
|
|
|
if (object.hasOwnProperty('isStage')) {
|
|
|
|
target.isStage = object.isStage;
|
2017-01-27 20:05:54 -05:00
|
|
|
}
|
2017-04-27 17:08:06 -04:00
|
|
|
Promise.all(costumePromises).then(costumes => {
|
|
|
|
sprite.costumes = costumes;
|
|
|
|
});
|
|
|
|
Promise.all(soundPromises).then(sounds => {
|
|
|
|
sprite.sounds = sounds;
|
|
|
|
});
|
|
|
|
return Promise.all(costumePromises.concat(soundPromises)).then(() => target);
|
2017-01-27 20:05:54 -05:00
|
|
|
};
|
|
|
|
|
2016-12-30 10:19:58 -05:00
|
|
|
/**
|
2017-11-03 14:17:16 -04:00
|
|
|
* Deserialize the specified representation of a VM runtime and loads it into the provided runtime instance.
|
|
|
|
* TODO: parse extension info (also, design extension info storage...)
|
|
|
|
* @param {object} json - JSON representation of a VM runtime.
|
|
|
|
* @param {Runtime} runtime - Runtime instance
|
2018-02-16 00:44:23 -05:00
|
|
|
* @param {JSZip} zip - Sb3 file describing this project (to load assets from)
|
2017-11-03 14:17:16 -04:00
|
|
|
* @returns {Promise.<ImportedProject>} Promise that resolves to the list of targets after the project is deserialized
|
2016-12-30 10:19:58 -05:00
|
|
|
*/
|
2018-02-16 00:44:23 -05:00
|
|
|
const deserialize = function (json, runtime, zip) {
|
2017-11-03 14:17:16 -04:00
|
|
|
const extensions = {
|
|
|
|
extensionIDs: new Set(),
|
|
|
|
extensionURLs: new Map()
|
|
|
|
};
|
|
|
|
return Promise.all(
|
2018-02-16 00:44:23 -05:00
|
|
|
(json.targets || []).map(target => parseScratchObject(target, runtime, extensions, zip))
|
2017-11-03 14:17:16 -04:00
|
|
|
).then(targets => ({
|
|
|
|
targets,
|
|
|
|
extensions
|
|
|
|
}));
|
2016-12-30 10:19:58 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
serialize: serialize,
|
|
|
|
deserialize: deserialize
|
|
|
|
};
|