scratch-vm/src/serialization/sb3.js

166 lines
5.3 KiB
JavaScript
Raw Normal View History

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');
const List = require('../engine/list');
2016-12-30 10:19:58 -05:00
/**
* Serializes the specified VM runtime.
* @param {!Runtime} runtime VM runtime instance to be serialized.
* @return {string} Serialized runtime instance.
*/
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);
2016-12-30 10:19:58 -05:00
obj.targets = runtime.targets;
// 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;
};
/**
* 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.
* @param {!Runtime} runtime Runtime object to load all structures into.
* @return {?Target} Target created (stage or sprite).
*/
2017-04-26 16:50:53 -04:00
const parseScratchObject = function (object, runtime) {
if (!object.hasOwnProperty('name')) {
// Watcher/monitor - skip this object until those are implemented in VM.
// @todo
return;
}
// Blocks container for this object.
2017-04-26 16:50:53 -04:00
const blocks = new Blocks();
// @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);
// Sprite/stage name from JSON.
if (object.hasOwnProperty('name')) {
sprite.name = object.name;
}
if (object.hasOwnProperty('blocks')) {
2017-04-26 16:50:53 -04:00
for (let blockId in object.blocks) {
blocks.createBlock(object.blocks[blockId]);
}
2017-04-26 16:50:53 -04:00
// console.log(blocks);
}
// Costumes from JSON.
if (object.hasOwnProperty('costumes') || object.hasOwnProperty('costume')) {
2017-04-26 16:50:53 -04:00
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
});
}
}
// Sounds from JSON
if (object.hasOwnProperty('sounds')) {
2017-04-26 16:50:53 -04:00
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,
2017-02-09 10:44:28 -05:00
name: sound.name,
md5: sound.md5
});
}
}
// Create the first clone, and load its run-state from JSON.
2017-04-26 16:50:53 -04:00
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')) {
2017-04-26 16:50:53 -04:00
for (let j = 0; j < object.variables.length; j++) {
const variable = object.variables[j];
target.variables[variable.name] = new Variable(
variable.name,
variable.value,
variable.isPersistent
);
}
}
if (object.hasOwnProperty('lists')) {
2017-04-26 16:50:53 -04:00
for (let k = 0; k < object.lists.length; k++) {
const list = object.lists[k];
// @todo: monitor properties.
target.lists[list.listName] = new List(
list.listName,
list.contents
);
}
}
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;
}
if (object.hasOwnProperty('isStage')) {
target.isStage = object.isStage;
}
target.updateAllDrawableProperties();
2017-04-26 16:50:53 -04:00
// console.log('returning target:');
// console.log(target);
return target;
};
2016-12-30 10:19:58 -05:00
/**
* Deserializes the specified representation of a VM runtime and loads it into
* the provided runtime instance.
* @param {string} json Stringified JSON representation of a VM runtime.
* @param {Runtime} runtime Runtime instance
*/
2017-04-26 16:50:53 -04:00
const deserialize = function (json, runtime) {
for (let i = 0; i < json.targets.length; i++) {
parseScratchObject(json.targets[i], runtime);
}
2016-12-30 10:19:58 -05:00
};
module.exports = {
serialize: serialize,
deserialize: deserialize
};