2016-12-30 10:19:58 -05:00
|
|
|
/**
|
|
|
|
* @fileoverview
|
2018-04-06 10:37:23 -04:00
|
|
|
* An SB3 serializer and deserializer. Parses provided
|
2016-12-30 10:19:58 -05:00
|
|
|
* 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');
|
2018-06-14 14:11:20 -04:00
|
|
|
const Comment = require('../engine/comment');
|
2018-10-21 00:22:39 -04:00
|
|
|
const MonitorRecord = require('../engine/monitor-record');
|
2018-05-18 10:11:59 -04:00
|
|
|
const StageLayering = require('../engine/stage-layering');
|
2018-03-27 13:00:58 -04:00
|
|
|
const log = require('../util/log');
|
2018-04-04 17:26:30 -04:00
|
|
|
const uid = require('../util/uid');
|
2018-07-24 11:00:48 -04:00
|
|
|
const MathUtil = require('../util/math-util');
|
2019-02-04 15:32:01 -05:00
|
|
|
const StringUtil = require('../util/string-util');
|
|
|
|
const VariableUtil = require('../util/variable-util');
|
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
|
|
|
|
2018-08-20 13:25:24 -04:00
|
|
|
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
|
|
|
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-04-06 10:37:23 -04:00
|
|
|
// Constants used during serialization and deserialization
|
2018-04-04 17:26:30 -04:00
|
|
|
const INPUT_SAME_BLOCK_SHADOW = 1; // unobscured shadow
|
|
|
|
const INPUT_BLOCK_NO_SHADOW = 2; // no shadow
|
|
|
|
const INPUT_DIFF_BLOCK_SHADOW = 3; // obscured shadow
|
2018-04-06 10:37:23 -04:00
|
|
|
// There shouldn't be a case where block is null, but shadow is present...
|
2018-03-27 13:00:58 -04:00
|
|
|
|
2018-04-20 07:54:02 -04:00
|
|
|
// Constants used during deserialization of an SB3 file
|
|
|
|
const CORE_EXTENSIONS = [
|
|
|
|
'argument',
|
2018-04-20 10:49:06 -04:00
|
|
|
'colour',
|
2018-04-20 07:54:02 -04:00
|
|
|
'control',
|
|
|
|
'data',
|
|
|
|
'event',
|
|
|
|
'looks',
|
|
|
|
'math',
|
|
|
|
'motion',
|
|
|
|
'operator',
|
|
|
|
'procedures',
|
|
|
|
'sensing',
|
|
|
|
'sound'
|
|
|
|
];
|
|
|
|
|
2018-04-04 17:26:30 -04:00
|
|
|
// Constants referring to 'primitive' blocks that are usually shadows,
|
|
|
|
// or in the case of variables and lists, appear quite often in projects
|
2018-03-28 09:47:46 -04:00
|
|
|
// math_number
|
2018-04-04 17:26:30 -04:00
|
|
|
const MATH_NUM_PRIMITIVE = 4; // there's no reason these constants can't collide
|
|
|
|
// math_positive_number
|
|
|
|
const POSITIVE_NUM_PRIMITIVE = 5; // with the above, but removing duplication for clarity
|
|
|
|
// math_whole_number
|
|
|
|
const WHOLE_NUM_PRIMITIVE = 6;
|
|
|
|
// math_integer
|
|
|
|
const INTEGER_NUM_PRIMITIVE = 7;
|
|
|
|
// math_angle
|
|
|
|
const ANGLE_NUM_PRIMITIVE = 8;
|
|
|
|
// colour_picker
|
|
|
|
const COLOR_PICKER_PRIMITIVE = 9;
|
2018-03-28 09:47:46 -04:00
|
|
|
// text
|
2018-04-04 17:26:30 -04:00
|
|
|
const TEXT_PRIMITIVE = 10;
|
2018-03-28 09:47:46 -04:00
|
|
|
// event_broadcast_menu
|
2018-04-04 17:26:30 -04:00
|
|
|
const BROADCAST_PRIMITIVE = 11;
|
2018-03-28 09:47:46 -04:00
|
|
|
// data_variable
|
2018-04-04 17:26:30 -04:00
|
|
|
const VAR_PRIMITIVE = 12;
|
2018-03-28 09:47:46 -04:00
|
|
|
// data_listcontents
|
2018-04-04 17:26:30 -04:00
|
|
|
const LIST_PRIMITIVE = 13;
|
|
|
|
|
2018-04-06 10:37:23 -04:00
|
|
|
// Map block opcodes to the above primitives and the name of the field we can use
|
|
|
|
// to find the value of the field
|
2018-04-04 17:26:30 -04:00
|
|
|
const primitiveOpcodeInfoMap = {
|
|
|
|
math_number: [MATH_NUM_PRIMITIVE, 'NUM'],
|
|
|
|
math_positive_number: [POSITIVE_NUM_PRIMITIVE, 'NUM'],
|
|
|
|
math_whole_number: [WHOLE_NUM_PRIMITIVE, 'NUM'],
|
|
|
|
math_integer: [INTEGER_NUM_PRIMITIVE, 'NUM'],
|
|
|
|
math_angle: [ANGLE_NUM_PRIMITIVE, 'NUM'],
|
|
|
|
colour_picker: [COLOR_PICKER_PRIMITIVE, 'COLOUR'],
|
|
|
|
text: [TEXT_PRIMITIVE, 'TEXT'],
|
|
|
|
event_broadcast_menu: [BROADCAST_PRIMITIVE, 'BROADCAST_OPTION'],
|
|
|
|
data_variable: [VAR_PRIMITIVE, 'VARIABLE'],
|
|
|
|
data_listcontents: [LIST_PRIMITIVE, 'LIST']
|
|
|
|
};
|
2018-03-28 09:47:46 -04:00
|
|
|
|
2018-04-06 10:37:23 -04:00
|
|
|
/**
|
|
|
|
* Serializes primitives described above into a more compact format
|
|
|
|
* @param {object} block the block to serialize
|
|
|
|
* @return {array} An array representing the information in the block,
|
|
|
|
* or null if the given block is not one of the primitives described above.
|
|
|
|
*/
|
2018-03-28 09:47:46 -04:00
|
|
|
const serializePrimitiveBlock = function (block) {
|
|
|
|
// Returns an array represeting a primitive block or null if not one of
|
|
|
|
// the primitive types above
|
2018-08-20 13:25:24 -04:00
|
|
|
if (hasOwnProperty.call(primitiveOpcodeInfoMap, block.opcode)) {
|
2018-04-04 17:26:30 -04:00
|
|
|
const primitiveInfo = primitiveOpcodeInfoMap[block.opcode];
|
|
|
|
const primitiveConstant = primitiveInfo[0];
|
|
|
|
const fieldName = primitiveInfo[1];
|
|
|
|
const field = block.fields[fieldName];
|
|
|
|
const primitiveDesc = [primitiveConstant, field.value];
|
|
|
|
if (block.opcode === 'event_broadcast_menu') {
|
|
|
|
primitiveDesc.push(field.id);
|
|
|
|
} else if (block.opcode === 'data_variable' || block.opcode === 'data_listcontents') {
|
|
|
|
primitiveDesc.push(field.id);
|
|
|
|
if (block.topLevel) {
|
|
|
|
primitiveDesc.push(block.x ? Math.round(block.x) : 0);
|
|
|
|
primitiveDesc.push(block.y ? Math.round(block.y) : 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return primitiveDesc;
|
2018-03-28 09:47:46 -04:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2018-04-06 10:37:23 -04:00
|
|
|
/**
|
|
|
|
* Serializes the inputs field of a block in a compact form using
|
|
|
|
* constants described above to represent the relationship between the
|
|
|
|
* inputs of this block (e.g. if there is an unobscured shadow, an obscured shadow
|
|
|
|
* -- a block plugged into a droppable input -- or, if there is just a block).
|
|
|
|
* Based on this relationship, serializes the ids of the block and shadow (if present)
|
|
|
|
*
|
|
|
|
* @param {object} inputs The inputs to serialize
|
|
|
|
* @return {object} An object representing the serialized inputs
|
|
|
|
*/
|
2018-03-27 13:00:58 -04:00
|
|
|
const serializeInputs = function (inputs) {
|
|
|
|
const obj = Object.create(null);
|
|
|
|
for (const inputName in inputs) {
|
2018-08-20 13:25:24 -04:00
|
|
|
if (!hasOwnProperty.call(inputs, inputName)) continue;
|
2018-03-27 13:00:58 -04:00
|
|
|
// if block and shadow refer to the same block, only serialize one
|
|
|
|
if (inputs[inputName].block === inputs[inputName].shadow) {
|
|
|
|
// has block and shadow, and they are the same
|
|
|
|
obj[inputName] = [
|
|
|
|
INPUT_SAME_BLOCK_SHADOW,
|
|
|
|
inputs[inputName].block
|
|
|
|
];
|
|
|
|
} else if (inputs[inputName].shadow === null) {
|
|
|
|
// does not have shadow
|
|
|
|
obj[inputName] = [
|
|
|
|
INPUT_BLOCK_NO_SHADOW,
|
|
|
|
inputs[inputName].block
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
// block and shadow are both present and are different
|
|
|
|
obj[inputName] = [
|
|
|
|
INPUT_DIFF_BLOCK_SHADOW,
|
|
|
|
inputs[inputName].block,
|
|
|
|
inputs[inputName].shadow
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
|
2018-04-06 10:37:23 -04:00
|
|
|
/**
|
|
|
|
* Serialize the fields of a block in a more compact form.
|
|
|
|
* @param {object} fields The fields object to serialize
|
|
|
|
* @return {object} An object representing the serialized fields
|
|
|
|
*/
|
2018-03-27 13:00:58 -04:00
|
|
|
const serializeFields = function (fields) {
|
|
|
|
const obj = Object.create(null);
|
|
|
|
for (const fieldName in fields) {
|
2018-08-20 13:25:24 -04:00
|
|
|
if (!hasOwnProperty.call(fields, fieldName)) continue;
|
2018-03-27 13:00:58 -04:00
|
|
|
obj[fieldName] = [fields[fieldName].value];
|
|
|
|
if (fields[fieldName].hasOwnProperty('id')) {
|
|
|
|
obj[fieldName].push(fields[fieldName].id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
|
2018-04-06 10:37:23 -04:00
|
|
|
/**
|
|
|
|
* Serialize the given block in the SB3 format with some compression of inputs,
|
|
|
|
* fields, and primitives.
|
|
|
|
* @param {object} block The block to serialize
|
|
|
|
* @return {object | array} A serialized representation of the block. This is an
|
|
|
|
* array if the block is one of the primitive types described above or an object,
|
|
|
|
* if not.
|
|
|
|
*/
|
2018-03-09 15:43:11 -05:00
|
|
|
const serializeBlock = function (block) {
|
2018-03-28 09:47:46 -04:00
|
|
|
const serializedPrimitive = serializePrimitiveBlock(block);
|
|
|
|
if (serializedPrimitive) return serializedPrimitive;
|
|
|
|
// If serializedPrimitive is null, proceed with serializing a non-primitive block
|
2018-03-09 15:43:11 -05:00
|
|
|
const obj = Object.create(null);
|
|
|
|
obj.opcode = block.opcode;
|
2018-04-04 17:26:30 -04:00
|
|
|
// NOTE: this is extremely important to serialize even if null;
|
2018-04-10 16:53:07 -04:00
|
|
|
// not serializing `next: null` results in strange behavior with block
|
|
|
|
// execution
|
2018-04-04 17:26:30 -04:00
|
|
|
obj.next = block.next;
|
2018-03-09 16:03:28 -05:00
|
|
|
obj.parent = block.parent;
|
2018-03-27 13:00:58 -04:00
|
|
|
obj.inputs = serializeInputs(block.inputs);
|
|
|
|
obj.fields = serializeFields(block.fields);
|
2018-04-10 15:44:32 -04:00
|
|
|
obj.shadow = block.shadow;
|
2018-03-09 15:43:11 -05:00
|
|
|
if (block.topLevel) {
|
2018-12-12 18:51:36 -05:00
|
|
|
obj.topLevel = true;
|
|
|
|
obj.x = block.x ? Math.round(block.x) : 0;
|
|
|
|
obj.y = block.y ? Math.round(block.y) : 0;
|
|
|
|
} else {
|
|
|
|
obj.topLevel = false;
|
2018-03-09 15:43:11 -05:00
|
|
|
}
|
|
|
|
if (block.mutation) {
|
|
|
|
obj.mutation = block.mutation;
|
|
|
|
}
|
2018-06-14 14:11:20 -04:00
|
|
|
if (block.comment) {
|
|
|
|
obj.comment = block.comment;
|
|
|
|
}
|
2018-03-09 15:43:11 -05:00
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
|
2018-04-06 10:37:23 -04:00
|
|
|
/**
|
|
|
|
* Compresses the serialized inputs replacing block/shadow ids that refer to
|
|
|
|
* one of the primitives with the primitive itself. E.g.
|
|
|
|
*
|
|
|
|
* blocks: {
|
|
|
|
* aUidForMyBlock: {
|
|
|
|
* inputs: {
|
|
|
|
* MYINPUT: [1, 'aUidForAnUnobscuredShadowPrimitive']
|
|
|
|
* }
|
|
|
|
* },
|
|
|
|
* aUidForAnUnobscuredShadowPrimitive: [4, 10]
|
|
|
|
* // the above is a primitive representing a 'math_number' with value 10
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* becomes:
|
|
|
|
*
|
|
|
|
* blocks: {
|
|
|
|
* aUidForMyBlock: {
|
|
|
|
* inputs: {
|
|
|
|
* MYINPUT: [1, [4, 10]]
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* Note: this function modifies the given blocks object in place
|
|
|
|
* @param {object} block The block with inputs to compress
|
|
|
|
* @param {objec} blocks The object containing all the blocks currently getting serialized
|
|
|
|
* @return {object} The serialized block with compressed inputs
|
|
|
|
*/
|
2018-03-28 09:47:46 -04:00
|
|
|
const compressInputTree = function (block, blocks) {
|
2018-04-06 10:37:23 -04:00
|
|
|
// This is the second pass on the block
|
2018-03-28 09:47:46 -04:00
|
|
|
// so the inputs field should be an object of key - array pairs
|
|
|
|
const serializedInputs = block.inputs;
|
|
|
|
for (const inputName in serializedInputs) {
|
|
|
|
// don't need to check for hasOwnProperty because of how we constructed
|
|
|
|
// inputs
|
|
|
|
const currInput = serializedInputs[inputName];
|
|
|
|
// traverse currInput skipping the first element, which describes whether the block
|
|
|
|
// and shadow are the same
|
|
|
|
for (let i = 1; i < currInput.length; i++) {
|
|
|
|
if (!currInput[i]) continue; // need this check b/c block/shadow can be null
|
|
|
|
const blockOrShadowID = currInput[i];
|
|
|
|
// replace element of currInput directly
|
|
|
|
// (modifying input block directly)
|
|
|
|
const blockOrShadow = blocks[blockOrShadowID];
|
|
|
|
if (Array.isArray(blockOrShadow)) {
|
|
|
|
currInput[i] = blockOrShadow;
|
2018-04-06 10:37:23 -04:00
|
|
|
// Modifying blocks in place!
|
2018-03-28 09:47:46 -04:00
|
|
|
delete blocks[blockOrShadowID];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return block;
|
|
|
|
};
|
|
|
|
|
2018-11-05 12:57:09 -05:00
|
|
|
/**
|
2020-06-15 20:41:35 -04:00
|
|
|
* Get sanitized non-core extension ID for a given sb3 opcode.
|
|
|
|
* Note that this should never return a URL. If in the future the SB3 loader supports loading extensions by URL, this
|
|
|
|
* ID should be used to (for example) look up the extension's full URL from a table in the SB3's JSON.
|
2018-11-05 12:57:09 -05:00
|
|
|
* @param {!string} opcode The opcode to examine for extension.
|
2018-11-07 10:11:16 -05:00
|
|
|
* @return {?string} The extension ID, if it exists and is not a core extension.
|
2018-11-05 12:57:09 -05:00
|
|
|
*/
|
|
|
|
const getExtensionIdForOpcode = function (opcode) {
|
2020-06-15 20:41:35 -04:00
|
|
|
// Allowed ID characters are those matching the regular expression [\w-]: A-Z, a-z, 0-9, and hyphen ("-").
|
2018-11-05 12:57:09 -05:00
|
|
|
const index = opcode.indexOf('_');
|
2020-06-15 20:41:35 -04:00
|
|
|
const forbiddenSymbols = /[^\w-]/g;
|
|
|
|
const prefix = opcode.substring(0, index).replace(forbiddenSymbols, '-');
|
2018-11-05 12:57:09 -05:00
|
|
|
if (CORE_EXTENSIONS.indexOf(prefix) === -1) {
|
|
|
|
if (prefix !== '') return prefix;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-04-06 10:37:23 -04:00
|
|
|
/**
|
|
|
|
* Serialize the given blocks object (representing all the blocks for the target
|
|
|
|
* currently being serialized.)
|
|
|
|
* @param {object} blocks The blocks to be serialized
|
2018-06-20 15:42:56 -04:00
|
|
|
* @return {Array} An array of the serialized blocks with compressed inputs and
|
|
|
|
* compressed primitives and the list of all extension IDs present
|
|
|
|
* in the serialized blocks.
|
2018-04-06 10:37:23 -04:00
|
|
|
*/
|
2018-06-20 15:42:56 -04:00
|
|
|
const serializeBlocks = function (blocks) {
|
2018-03-09 15:43:11 -05:00
|
|
|
const obj = Object.create(null);
|
2018-06-20 15:42:56 -04:00
|
|
|
const extensionIDs = new Set();
|
2018-03-09 15:43:11 -05:00
|
|
|
for (const blockID in blocks) {
|
2018-03-21 17:42:01 -04:00
|
|
|
if (!blocks.hasOwnProperty(blockID)) continue;
|
2018-03-28 09:47:46 -04:00
|
|
|
obj[blockID] = serializeBlock(blocks[blockID], blocks);
|
2018-11-05 12:57:09 -05:00
|
|
|
const extensionID = getExtensionIdForOpcode(blocks[blockID].opcode);
|
|
|
|
if (extensionID) {
|
|
|
|
extensionIDs.add(extensionID);
|
2018-06-18 15:00:33 -04:00
|
|
|
}
|
2018-03-28 09:47:46 -04:00
|
|
|
}
|
|
|
|
// once we have completed a first pass, do a second pass on block inputs
|
2018-04-10 16:55:27 -04:00
|
|
|
for (const blockID in obj) {
|
2018-03-28 09:47:46 -04:00
|
|
|
// don't need to do the hasOwnProperty check here since we
|
|
|
|
// created an object that doesn't get extra properties/functions
|
2018-04-10 16:55:27 -04:00
|
|
|
const serializedBlock = obj[blockID];
|
2018-03-28 09:47:46 -04:00
|
|
|
// caution, this function deletes parts of this object in place as
|
2018-04-10 16:55:27 -04:00
|
|
|
// it's traversing it
|
|
|
|
obj[blockID] = compressInputTree(serializedBlock, obj);
|
2018-03-28 09:47:46 -04:00
|
|
|
// second pass on connecting primitives to serialized inputs directly
|
2018-03-09 15:43:11 -05:00
|
|
|
}
|
2018-04-10 16:55:27 -04:00
|
|
|
// Do one last pass and remove any top level shadows (these are caused by
|
|
|
|
// a bug: LLK/scratch-vm#1011, and this pass should be removed once that is
|
|
|
|
// completely fixed)
|
|
|
|
for (const blockID in obj) {
|
|
|
|
const serializedBlock = obj[blockID];
|
|
|
|
// If the current block is serialized as a primitive (e.g. it's an array
|
|
|
|
// instead of an object), AND it is not one of the top level primitives
|
|
|
|
// e.g. variable getter or list getter, then it should be deleted as it's
|
|
|
|
// a shadow block, and there are no blocks that reference it, otherwise
|
|
|
|
// they would have been compressed in the last pass)
|
|
|
|
if (Array.isArray(serializedBlock) &&
|
2018-12-05 11:35:22 -05:00
|
|
|
[VAR_PRIMITIVE, LIST_PRIMITIVE].indexOf(serializedBlock[0]) < 0) {
|
2018-04-10 16:55:27 -04:00
|
|
|
log.warn(`Found an unexpected top level primitive with block ID: ${
|
|
|
|
blockID}; deleting it from serialized blocks.`);
|
|
|
|
delete obj[blockID];
|
|
|
|
}
|
|
|
|
}
|
2018-06-20 15:42:56 -04:00
|
|
|
return [obj, Array.from(extensionIDs)];
|
2018-03-09 15:43:11 -05:00
|
|
|
};
|
|
|
|
|
2018-04-06 10:37:23 -04:00
|
|
|
/**
|
|
|
|
* Serialize the given costume.
|
|
|
|
* @param {object} costume The costume to be serialized.
|
|
|
|
* @return {object} A serialized representation of the costume.
|
|
|
|
*/
|
2018-03-09 15:43:11 -05:00
|
|
|
const serializeCostume = function (costume) {
|
|
|
|
const obj = Object.create(null);
|
|
|
|
obj.name = costume.name;
|
|
|
|
obj.bitmapResolution = costume.bitmapResolution;
|
2018-04-10 17:01:31 -04:00
|
|
|
obj.dataFormat = costume.dataFormat.toLowerCase();
|
2022-05-13 19:14:25 -04:00
|
|
|
if (costume.broken) {
|
2022-05-16 16:23:41 -04:00
|
|
|
obj.assetId = costume.broken.assetId;
|
|
|
|
|
|
|
|
// 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.broken.md5);
|
|
|
|
|
2022-05-13 19:14:25 -04:00
|
|
|
obj.rotationCenterX = costume.broken.rotationCenterX;
|
|
|
|
obj.rotationCenterY = costume.broken.rotationCenterY;
|
|
|
|
} else {
|
2022-05-16 16:23:41 -04:00
|
|
|
obj.assetId = costume.assetId;
|
|
|
|
|
|
|
|
// See related comment above
|
|
|
|
obj.md5ext = costume.md5;
|
|
|
|
|
2022-05-13 19:14:25 -04:00
|
|
|
obj.rotationCenterX = costume.rotationCenterX;
|
|
|
|
obj.rotationCenterY = costume.rotationCenterY;
|
|
|
|
}
|
2018-03-09 15:43:11 -05:00
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
|
2018-04-06 10:37:23 -04:00
|
|
|
/**
|
|
|
|
* Serialize the given sound.
|
|
|
|
* @param {object} sound The sound to be serialized.
|
|
|
|
* @return {object} A serialized representation of the sound.
|
|
|
|
*/
|
2018-03-09 15:43:11 -05:00
|
|
|
const serializeSound = function (sound) {
|
|
|
|
const obj = Object.create(null);
|
|
|
|
obj.assetId = sound.assetId;
|
|
|
|
obj.name = sound.name;
|
2018-04-10 17:01:31 -04:00
|
|
|
obj.dataFormat = sound.dataFormat.toLowerCase();
|
2018-03-09 15:43:11 -05:00
|
|
|
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-04-06 10:37:23 -04:00
|
|
|
/**
|
|
|
|
* Serialize the given variables object.
|
|
|
|
* @param {object} variables The variables to be serialized.
|
|
|
|
* @return {object} A serialized representation of the variables. They get
|
|
|
|
* separated by type to compress the representation of each given variable and
|
|
|
|
* reduce duplicate information.
|
|
|
|
*/
|
2018-03-27 13:00:58 -04:00
|
|
|
const serializeVariables = function (variables) {
|
|
|
|
const obj = Object.create(null);
|
|
|
|
// separate out variables into types at the top level so we don't have
|
|
|
|
// keep track of a type for each
|
|
|
|
obj.variables = Object.create(null);
|
|
|
|
obj.lists = Object.create(null);
|
|
|
|
obj.broadcasts = Object.create(null);
|
|
|
|
for (const varId in variables) {
|
|
|
|
const v = variables[varId];
|
|
|
|
if (v.type === Variable.BROADCAST_MESSAGE_TYPE) {
|
2018-03-28 09:47:46 -04:00
|
|
|
obj.broadcasts[varId] = v.value; // name and value is the same for broadcast msgs
|
2018-03-27 13:00:58 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (v.type === Variable.LIST_TYPE) {
|
|
|
|
obj.lists[varId] = [v.name, v.value];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-04-04 17:26:30 -04:00
|
|
|
// otherwise should be a scalar type
|
|
|
|
obj.variables[varId] = [v.name, v.value];
|
|
|
|
// only scalar vars have the potential to be cloud vars
|
2018-10-30 18:53:57 -04:00
|
|
|
if (v.isCloud) obj.variables[varId].push(true);
|
2018-03-27 13:00:58 -04:00
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
|
2018-06-14 14:11:20 -04:00
|
|
|
const serializeComments = function (comments) {
|
|
|
|
const obj = Object.create(null);
|
|
|
|
for (const commentId in comments) {
|
|
|
|
if (!comments.hasOwnProperty(commentId)) continue;
|
|
|
|
const comment = comments[commentId];
|
|
|
|
|
|
|
|
const serializedComment = Object.create(null);
|
|
|
|
serializedComment.blockId = comment.blockId;
|
|
|
|
serializedComment.x = comment.x;
|
|
|
|
serializedComment.y = comment.y;
|
|
|
|
serializedComment.width = comment.width;
|
|
|
|
serializedComment.height = comment.height;
|
|
|
|
serializedComment.minimized = comment.minimized;
|
|
|
|
serializedComment.text = comment.text;
|
|
|
|
|
|
|
|
obj[commentId] = serializedComment;
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
|
2018-04-06 10:37:23 -04:00
|
|
|
/**
|
|
|
|
* Serialize the given target. Only serialize properties that are necessary
|
|
|
|
* for saving and loading this target.
|
|
|
|
* @param {object} target The target to be serialized.
|
2018-06-21 15:00:17 -04:00
|
|
|
* @param {Set} extensions A set of extensions to add extension IDs to
|
2018-04-06 10:37:23 -04:00
|
|
|
* @return {object} A serialized representation of the given target.
|
|
|
|
*/
|
2018-06-21 15:00:17 -04:00
|
|
|
const serializeTarget = function (target, extensions) {
|
2018-03-09 15:43:11 -05:00
|
|
|
const obj = Object.create(null);
|
2018-06-21 15:00:17 -04:00
|
|
|
let targetExtensions = [];
|
2018-03-09 17:26:41 -05:00
|
|
|
obj.isStage = target.isStage;
|
2018-04-06 10:37:23 -04:00
|
|
|
obj.name = obj.isStage ? 'Stage' : target.name;
|
2018-03-27 13:00:58 -04:00
|
|
|
const vars = serializeVariables(target.variables);
|
|
|
|
obj.variables = vars.variables;
|
|
|
|
obj.lists = vars.lists;
|
|
|
|
obj.broadcasts = vars.broadcasts;
|
2018-06-21 15:00:17 -04:00
|
|
|
[obj.blocks, targetExtensions] = serializeBlocks(target.blocks);
|
2018-06-14 14:11:20 -04:00
|
|
|
obj.comments = serializeComments(target.comments);
|
2019-01-08 13:26:06 -05:00
|
|
|
|
|
|
|
// TODO remove this check/patch when (#1901) is fixed
|
|
|
|
if (target.currentCostume < 0 || target.currentCostume >= target.costumes.length) {
|
|
|
|
log.warn(`currentCostume property for target ${target.name} is out of range`);
|
|
|
|
target.currentCostume = MathUtil.clamp(target.currentCostume, 0, target.costumes.length - 1);
|
|
|
|
}
|
|
|
|
|
2018-03-09 15:43:11 -05:00
|
|
|
obj.currentCostume = target.currentCostume;
|
|
|
|
obj.costumes = target.costumes.map(serializeCostume);
|
|
|
|
obj.sounds = target.sounds.map(serializeSound);
|
2018-04-04 18:54:05 -04:00
|
|
|
if (target.hasOwnProperty('volume')) obj.volume = target.volume;
|
2018-07-24 11:00:48 -04:00
|
|
|
if (target.hasOwnProperty('layerOrder')) obj.layerOrder = target.layerOrder;
|
2018-04-04 18:54:05 -04:00
|
|
|
if (obj.isStage) { // Only the stage should have these properties
|
|
|
|
if (target.hasOwnProperty('tempo')) obj.tempo = target.tempo;
|
|
|
|
if (target.hasOwnProperty('videoTransparency')) obj.videoTransparency = target.videoTransparency;
|
|
|
|
if (target.hasOwnProperty('videoState')) obj.videoState = target.videoState;
|
2018-10-17 17:34:12 -04:00
|
|
|
if (target.hasOwnProperty('textToSpeechLanguage')) obj.textToSpeechLanguage = target.textToSpeechLanguage;
|
2018-04-04 18:54:05 -04:00
|
|
|
} else { // The stage does not need the following properties, but sprites should
|
2018-03-09 15:43:11 -05:00
|
|
|
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;
|
|
|
|
}
|
2018-06-21 15:00:17 -04:00
|
|
|
|
|
|
|
// Add found extensions to the extensions object
|
|
|
|
targetExtensions.forEach(extensionId => {
|
|
|
|
extensions.add(extensionId);
|
|
|
|
});
|
2018-03-09 15:43:11 -05:00
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
|
2018-07-24 11:00:48 -04:00
|
|
|
const getSimplifiedLayerOrdering = function (targets) {
|
|
|
|
const layerOrders = targets.map(t => t.getLayerOrder());
|
|
|
|
return MathUtil.reducedSortOrdering(layerOrders);
|
|
|
|
};
|
|
|
|
|
2018-10-21 00:22:39 -04:00
|
|
|
const serializeMonitors = function (monitors) {
|
|
|
|
return monitors.valueSeq().map(monitorData => {
|
|
|
|
const serializedMonitor = {
|
|
|
|
id: monitorData.id,
|
|
|
|
mode: monitorData.mode,
|
|
|
|
opcode: monitorData.opcode,
|
|
|
|
params: monitorData.params,
|
|
|
|
spriteName: monitorData.spriteName,
|
|
|
|
value: monitorData.value,
|
|
|
|
width: monitorData.width,
|
|
|
|
height: monitorData.height,
|
|
|
|
x: monitorData.x,
|
|
|
|
y: monitorData.y,
|
|
|
|
visible: monitorData.visible
|
|
|
|
};
|
|
|
|
if (monitorData.mode !== 'list') {
|
2019-01-25 14:14:46 -05:00
|
|
|
serializedMonitor.sliderMin = monitorData.sliderMin;
|
|
|
|
serializedMonitor.sliderMax = monitorData.sliderMax;
|
2019-02-11 11:09:01 -05:00
|
|
|
serializedMonitor.isDiscrete = monitorData.isDiscrete;
|
2018-10-21 00:22:39 -04:00
|
|
|
}
|
|
|
|
return serializedMonitor;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-12-30 10:19:58 -05:00
|
|
|
/**
|
|
|
|
* Serializes the specified VM runtime.
|
2018-06-19 08:51:16 -04:00
|
|
|
* @param {!Runtime} runtime VM runtime instance to be serialized.
|
|
|
|
* @param {string=} targetId Optional target id if serializing only a single target
|
2018-04-06 10:37:23 -04:00
|
|
|
* @return {object} Serialized runtime instance.
|
2016-12-30 10:19:58 -05:00
|
|
|
*/
|
2018-06-19 08:51:16 -04:00
|
|
|
const serialize = function (runtime, targetId) {
|
2016-12-30 10:19:58 -05:00
|
|
|
// Fetch targets
|
2017-04-26 16:50:53 -04:00
|
|
|
const obj = Object.create(null);
|
2018-06-21 15:01:00 -04:00
|
|
|
// Create extension set to hold extension ids found while serializing targets
|
|
|
|
const extensions = new Set();
|
2018-07-24 11:00:48 -04:00
|
|
|
|
|
|
|
const originalTargetsToSerialize = targetId ?
|
2018-06-19 08:51:16 -04:00
|
|
|
[runtime.getTargetById(targetId)] :
|
2018-07-24 11:00:48 -04:00
|
|
|
runtime.targets.filter(target => target.isOriginal);
|
|
|
|
|
|
|
|
const layerOrdering = getSimplifiedLayerOrdering(originalTargetsToSerialize);
|
|
|
|
|
2019-01-07 13:02:30 -05:00
|
|
|
const flattenedOriginalTargets = originalTargetsToSerialize.map(t => t.toJSON());
|
2018-07-24 11:00:48 -04:00
|
|
|
|
2018-07-24 16:11:18 -04:00
|
|
|
// If the renderer is attached, and we're serializing a whole project (not a sprite)
|
|
|
|
// add a temporary layerOrder property to each target.
|
|
|
|
if (runtime.renderer && !targetId) {
|
2018-07-24 11:00:48 -04:00
|
|
|
flattenedOriginalTargets.forEach((t, index) => {
|
|
|
|
t.layerOrder = layerOrdering[index];
|
|
|
|
});
|
|
|
|
}
|
2018-06-19 08:51:16 -04:00
|
|
|
|
2018-06-26 08:56:42 -04:00
|
|
|
const serializedTargets = flattenedOriginalTargets.map(t => serializeTarget(t, extensions));
|
2018-06-19 08:51:16 -04:00
|
|
|
|
|
|
|
if (targetId) {
|
|
|
|
return serializedTargets[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
obj.targets = serializedTargets;
|
|
|
|
|
2018-10-21 00:22:39 -04:00
|
|
|
obj.monitors = serializeMonitors(runtime.getMonitorState());
|
2016-12-30 10:19:58 -05:00
|
|
|
|
2018-06-21 15:01:00 -04:00
|
|
|
// Assemble extension list
|
|
|
|
obj.extensions = Array.from(extensions);
|
|
|
|
|
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;
|
2021-04-29 11:39:05 -04:00
|
|
|
if (runtime.origin) {
|
2021-04-29 11:32:13 -04:00
|
|
|
meta.origin = runtime.origin;
|
|
|
|
}
|
2016-12-30 10:19:58 -05:00
|
|
|
|
|
|
|
// Attach full user agent string to metadata if available
|
2019-01-25 14:14:46 -05:00
|
|
|
meta.agent = 'none';
|
2016-12-30 10:19:58 -05:00
|
|
|
if (typeof navigator !== 'undefined') meta.agent = navigator.userAgent;
|
|
|
|
|
|
|
|
// Assemble payload and return
|
|
|
|
obj.meta = meta;
|
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
|
2018-04-06 10:37:23 -04:00
|
|
|
/**
|
|
|
|
* Deserialize a block input descriptors. This is either a
|
|
|
|
* block id or a serialized primitive, e.g. an array
|
|
|
|
* (see serializePrimitiveBlock function).
|
|
|
|
* @param {string | array} inputDescOrId The block input descriptor to be serialized.
|
|
|
|
* @param {string} parentId The id of the parent block for this input block.
|
|
|
|
* @param {boolean} isShadow Whether or not this input block is a shadow.
|
|
|
|
* @param {object} blocks The entire blocks object currently in the process of getting serialized.
|
|
|
|
* @return {object} The deserialized input descriptor.
|
|
|
|
*/
|
2018-04-04 17:26:30 -04:00
|
|
|
const deserializeInputDesc = function (inputDescOrId, parentId, isShadow, blocks) {
|
|
|
|
if (!Array.isArray(inputDescOrId)) return inputDescOrId;
|
|
|
|
const primitiveObj = Object.create(null);
|
|
|
|
const newId = uid();
|
|
|
|
primitiveObj.id = newId;
|
|
|
|
primitiveObj.next = null;
|
|
|
|
primitiveObj.parent = parentId;
|
|
|
|
primitiveObj.shadow = isShadow;
|
|
|
|
primitiveObj.inputs = Object.create(null);
|
|
|
|
// need a reference to parent id
|
|
|
|
switch (inputDescOrId[0]) {
|
|
|
|
case MATH_NUM_PRIMITIVE: {
|
|
|
|
primitiveObj.opcode = 'math_number';
|
|
|
|
primitiveObj.fields = {
|
|
|
|
NUM: {
|
|
|
|
name: 'NUM',
|
|
|
|
value: inputDescOrId[1]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
primitiveObj.topLevel = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case POSITIVE_NUM_PRIMITIVE: {
|
|
|
|
primitiveObj.opcode = 'math_positive_number';
|
|
|
|
primitiveObj.fields = {
|
|
|
|
NUM: {
|
|
|
|
name: 'NUM',
|
|
|
|
value: inputDescOrId[1]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
primitiveObj.topLevel = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case WHOLE_NUM_PRIMITIVE: {
|
|
|
|
primitiveObj.opcode = 'math_whole_number';
|
|
|
|
primitiveObj.fields = {
|
|
|
|
NUM: {
|
|
|
|
name: 'NUM',
|
|
|
|
value: inputDescOrId[1]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
primitiveObj.topLevel = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case INTEGER_NUM_PRIMITIVE: {
|
|
|
|
primitiveObj.opcode = 'math_integer';
|
|
|
|
primitiveObj.fields = {
|
|
|
|
NUM: {
|
|
|
|
name: 'NUM',
|
|
|
|
value: inputDescOrId[1]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
primitiveObj.topLevel = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ANGLE_NUM_PRIMITIVE: {
|
|
|
|
primitiveObj.opcode = 'math_angle';
|
|
|
|
primitiveObj.fields = {
|
|
|
|
NUM: {
|
|
|
|
name: 'NUM',
|
|
|
|
value: inputDescOrId[1]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
primitiveObj.topLevel = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case COLOR_PICKER_PRIMITIVE: {
|
|
|
|
primitiveObj.opcode = 'colour_picker';
|
|
|
|
primitiveObj.fields = {
|
|
|
|
COLOUR: {
|
|
|
|
name: 'COLOUR',
|
|
|
|
value: inputDescOrId[1]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
primitiveObj.topLevel = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TEXT_PRIMITIVE: {
|
|
|
|
primitiveObj.opcode = 'text';
|
|
|
|
primitiveObj.fields = {
|
|
|
|
TEXT: {
|
|
|
|
name: 'TEXT',
|
|
|
|
value: inputDescOrId[1]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
primitiveObj.topLevel = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BROADCAST_PRIMITIVE: {
|
|
|
|
primitiveObj.opcode = 'event_broadcast_menu';
|
|
|
|
primitiveObj.fields = {
|
|
|
|
BROADCAST_OPTION: {
|
|
|
|
name: 'BROADCAST_OPTION',
|
|
|
|
value: inputDescOrId[1],
|
|
|
|
id: inputDescOrId[2],
|
|
|
|
variableType: Variable.BROADCAST_MESSAGE_TYPE
|
|
|
|
}
|
|
|
|
};
|
|
|
|
primitiveObj.topLevel = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case VAR_PRIMITIVE: {
|
|
|
|
primitiveObj.opcode = 'data_variable';
|
|
|
|
primitiveObj.fields = {
|
|
|
|
VARIABLE: {
|
|
|
|
name: 'VARIABLE',
|
|
|
|
value: inputDescOrId[1],
|
|
|
|
id: inputDescOrId[2],
|
|
|
|
variableType: Variable.SCALAR_TYPE
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (inputDescOrId.length > 3) {
|
|
|
|
primitiveObj.topLevel = true;
|
|
|
|
primitiveObj.x = inputDescOrId[3];
|
|
|
|
primitiveObj.y = inputDescOrId[4];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LIST_PRIMITIVE: {
|
|
|
|
primitiveObj.opcode = 'data_listcontents';
|
|
|
|
primitiveObj.fields = {
|
|
|
|
LIST: {
|
|
|
|
name: 'LIST',
|
|
|
|
value: inputDescOrId[1],
|
|
|
|
id: inputDescOrId[2],
|
|
|
|
variableType: Variable.LIST_TYPE
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (inputDescOrId.length > 3) {
|
|
|
|
primitiveObj.topLevel = true;
|
|
|
|
primitiveObj.x = inputDescOrId[3];
|
|
|
|
primitiveObj.y = inputDescOrId[4];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
log.error(`Found unknown primitive type during deserialization: ${JSON.stringify(inputDescOrId)}`);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
blocks[newId] = primitiveObj;
|
|
|
|
return newId;
|
|
|
|
};
|
|
|
|
|
2018-04-06 10:37:23 -04:00
|
|
|
/**
|
|
|
|
* Deserialize the given block inputs.
|
|
|
|
* @param {object} inputs The inputs to deserialize.
|
|
|
|
* @param {string} parentId The block id of the parent block
|
|
|
|
* @param {object} blocks The object representing the entire set of blocks currently
|
|
|
|
* in the process of getting deserialized.
|
|
|
|
* @return {object} The deserialized and uncompressed inputs.
|
|
|
|
*/
|
2018-04-04 17:26:30 -04:00
|
|
|
const deserializeInputs = function (inputs, parentId, blocks) {
|
2018-03-27 13:00:58 -04:00
|
|
|
// Explicitly not using Object.create(null) here
|
|
|
|
// because we call prototype functions later in the vm
|
|
|
|
const obj = {};
|
|
|
|
for (const inputName in inputs) {
|
2018-08-20 13:25:24 -04:00
|
|
|
if (!hasOwnProperty.call(inputs, inputName)) continue;
|
2018-03-27 13:00:58 -04:00
|
|
|
const inputDescArr = inputs[inputName];
|
2018-08-20 13:25:24 -04:00
|
|
|
// If this block has already been deserialized (it's not an array) skip it
|
2018-08-15 17:04:49 -04:00
|
|
|
if (!Array.isArray(inputDescArr)) continue;
|
2018-03-27 13:00:58 -04:00
|
|
|
let block = null;
|
|
|
|
let shadow = null;
|
|
|
|
const blockShadowInfo = inputDescArr[0];
|
|
|
|
if (blockShadowInfo === INPUT_SAME_BLOCK_SHADOW) {
|
|
|
|
// block and shadow are the same id, and only one is provided
|
2018-04-04 17:26:30 -04:00
|
|
|
block = shadow = deserializeInputDesc(inputDescArr[1], parentId, true, blocks);
|
2018-03-27 13:00:58 -04:00
|
|
|
} else if (blockShadowInfo === INPUT_BLOCK_NO_SHADOW) {
|
2018-04-04 17:26:30 -04:00
|
|
|
block = deserializeInputDesc(inputDescArr[1], parentId, false, blocks);
|
2018-03-27 13:00:58 -04:00
|
|
|
} else { // assume INPUT_DIFF_BLOCK_SHADOW
|
2018-04-04 17:26:30 -04:00
|
|
|
block = deserializeInputDesc(inputDescArr[1], parentId, false, blocks);
|
|
|
|
shadow = deserializeInputDesc(inputDescArr[2], parentId, true, blocks);
|
2018-03-27 13:00:58 -04:00
|
|
|
}
|
|
|
|
obj[inputName] = {
|
|
|
|
name: inputName,
|
|
|
|
block: block,
|
|
|
|
shadow: shadow
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
|
2018-04-06 10:37:23 -04:00
|
|
|
/**
|
|
|
|
* Deserialize the given block fields.
|
|
|
|
* @param {object} fields The fields to be deserialized
|
|
|
|
* @return {object} The deserialized and uncompressed block fields.
|
|
|
|
*/
|
2018-03-27 13:00:58 -04:00
|
|
|
const deserializeFields = function (fields) {
|
|
|
|
// Explicitly not using Object.create(null) here
|
|
|
|
// because we call prototype functions later in the vm
|
|
|
|
const obj = {};
|
|
|
|
for (const fieldName in fields) {
|
2018-08-20 13:25:24 -04:00
|
|
|
if (!hasOwnProperty.call(fields, fieldName)) continue;
|
2018-03-27 13:00:58 -04:00
|
|
|
const fieldDescArr = fields[fieldName];
|
2018-08-20 13:25:24 -04:00
|
|
|
// If this block has already been deserialized (it's not an array) skip it
|
2018-08-15 17:04:49 -04:00
|
|
|
if (!Array.isArray(fieldDescArr)) continue;
|
2018-03-27 13:00:58 -04:00
|
|
|
obj[fieldName] = {
|
|
|
|
name: fieldName,
|
|
|
|
value: fieldDescArr[0]
|
|
|
|
};
|
|
|
|
if (fieldDescArr.length > 1) {
|
|
|
|
obj[fieldName].id = fieldDescArr[1];
|
|
|
|
}
|
|
|
|
if (fieldName === 'BROADCAST_OPTION') {
|
|
|
|
obj[fieldName].variableType = Variable.BROADCAST_MESSAGE_TYPE;
|
|
|
|
} else if (fieldName === 'VARIABLE') {
|
|
|
|
obj[fieldName].variableType = Variable.SCALAR_TYPE;
|
|
|
|
} else if (fieldName === 'LIST') {
|
|
|
|
obj[fieldName].variableType = Variable.LIST_TYPE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
};
|
|
|
|
|
2018-08-14 11:17:47 -04:00
|
|
|
/**
|
|
|
|
* Covnert serialized INPUT and FIELD primitives back to hydrated block templates.
|
2018-08-20 13:25:24 -04:00
|
|
|
* Should be able to deserialize a format that has already been deserialized. The only
|
|
|
|
* "east" path to adding new targets/code requires going through deserialize, so it should
|
|
|
|
* work with pre-parsed deserialized blocks.
|
2018-08-14 11:17:47 -04:00
|
|
|
*
|
|
|
|
* @param {object} blocks Serialized SB3 "blocks" property of a target. Will be mutated.
|
|
|
|
* @return {object} input is modified and returned
|
|
|
|
*/
|
|
|
|
const deserializeBlocks = function (blocks) {
|
|
|
|
for (const blockId in blocks) {
|
2018-08-20 13:25:24 -04:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(blocks, blockId)) {
|
2018-08-14 11:17:47 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const block = blocks[blockId];
|
|
|
|
if (Array.isArray(block)) {
|
|
|
|
// this is one of the primitives
|
|
|
|
// delete the old entry in object.blocks and replace it w/the
|
|
|
|
// deserialized object
|
2018-12-05 16:59:47 -05:00
|
|
|
delete blocks[blockId];
|
2018-08-14 11:17:47 -04:00
|
|
|
deserializeInputDesc(block, null, false, blocks);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
block.id = blockId; // add id back to block since it wasn't serialized
|
|
|
|
block.inputs = deserializeInputs(block.inputs, blockId, blocks);
|
|
|
|
block.fields = deserializeFields(block.fields);
|
|
|
|
}
|
|
|
|
return blocks;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-01-27 20:05:54 -05:00
|
|
|
/**
|
2019-02-20 14:51:48 -05:00
|
|
|
* Parse the assets of a single "Scratch object" and load them. This
|
|
|
|
* preprocesses objects to support loading the data for those assets over a
|
|
|
|
* network while the objects are further processed into Blocks, Sprites, and a
|
|
|
|
* list of needed Extensions.
|
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.
|
2018-02-16 00:44:23 -05:00
|
|
|
* @param {JSZip} zip Sb3 file describing this project (to load assets from)
|
2019-02-20 14:51:48 -05:00
|
|
|
* @return {?{costumePromises:Array.<Promise>,soundPromises:Array.<Promise>,soundBank:SoundBank}}
|
|
|
|
* Object of arrays of promises for asset objects used in Sprites. As well as a
|
|
|
|
* SoundBank for the sound assets. null for unsupported objects.
|
2017-01-27 20:05:54 -05:00
|
|
|
*/
|
2019-02-20 14:51:48 -05:00
|
|
|
const parseScratchAssets = function (object, runtime, 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
|
|
|
}
|
|
|
|
|
2019-02-20 14:51:48 -05:00
|
|
|
const assets = {
|
|
|
|
costumePromises: null,
|
|
|
|
soundPromises: null,
|
|
|
|
soundBank: runtime.audioEngine && runtime.audioEngine.createBank()
|
|
|
|
};
|
2017-11-03 14:17:16 -04:00
|
|
|
|
2017-01-27 20:05:54 -05:00
|
|
|
// Costumes from JSON.
|
2019-02-20 14:51:48 -05:00
|
|
|
assets.costumePromises = (object.costumes || []).map(costumeSource => {
|
2017-04-27 17:08:06 -04:00
|
|
|
// @todo: Make sure all the relevant metadata is being pulled out.
|
|
|
|
const costume = {
|
2018-11-01 14:28:54 -04:00
|
|
|
// costumeSource only has an asset if an image is being uploaded as
|
|
|
|
// a sprite
|
|
|
|
asset: costumeSource.asset,
|
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)
|
2018-11-05 15:50:28 -05:00
|
|
|
.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
|
2019-02-20 14:51:48 -05:00
|
|
|
assets.soundPromises = (object.sounds || []).map(soundSource => {
|
2017-04-27 17:08:06 -04:00
|
|
|
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)
|
2019-02-20 14:51:48 -05:00
|
|
|
.then(() => loadSound(sound, runtime, assets.soundBank));
|
2018-02-16 00:44:23 -05:00
|
|
|
// Only attempt to load the sound after the deserialization
|
|
|
|
// process has been completed.
|
2017-04-27 17:08:06 -04:00
|
|
|
});
|
2019-02-20 14:51:48 -05:00
|
|
|
|
|
|
|
return assets;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a single "Scratch object" and create all its in-memory VM objects.
|
|
|
|
* @param {!object} object From-JSON "Scratch object:" sprite, stage, watcher.
|
|
|
|
* @param {!Runtime} runtime Runtime object to load all structures into.
|
|
|
|
* @param {ImportedExtensionsInfo} extensions - (in/out) parsed extension information will be stored here.
|
|
|
|
* @param {JSZip} zip Sb3 file describing this project (to load assets from)
|
|
|
|
* @param {object} assets - Promises for assets of this scratch object grouped
|
|
|
|
* into costumes and sounds
|
|
|
|
* @return {!Promise.<Target>} Promise for the target created (stage or sprite), or null for unsupported objects.
|
|
|
|
*/
|
|
|
|
const parseScratchObject = function (object, runtime, extensions, zip, assets) {
|
|
|
|
if (!object.hasOwnProperty('name')) {
|
|
|
|
// Watcher/monitor - skip this object until those are implemented in VM.
|
|
|
|
// @todo
|
|
|
|
return Promise.resolve(null);
|
|
|
|
}
|
|
|
|
// Blocks container for this object.
|
|
|
|
const blocks = new Blocks(runtime);
|
|
|
|
|
|
|
|
// @todo: For now, load all Scratch objects (stage/sprites) as a Sprite.
|
|
|
|
const sprite = new Sprite(blocks, runtime);
|
|
|
|
|
|
|
|
// Sprite/stage name from JSON.
|
|
|
|
if (object.hasOwnProperty('name')) {
|
|
|
|
sprite.name = object.name;
|
|
|
|
}
|
|
|
|
if (object.hasOwnProperty('blocks')) {
|
|
|
|
deserializeBlocks(object.blocks);
|
|
|
|
// Take a second pass to create objects and add extensions
|
|
|
|
for (const blockId in object.blocks) {
|
|
|
|
if (!object.blocks.hasOwnProperty(blockId)) continue;
|
|
|
|
const blockJSON = object.blocks[blockId];
|
|
|
|
blocks.createBlock(blockJSON);
|
|
|
|
|
|
|
|
// If the block is from an extension, record it.
|
|
|
|
const extensionID = getExtensionIdForOpcode(blockJSON.opcode);
|
|
|
|
if (extensionID) {
|
|
|
|
extensions.extensionIDs.add(extensionID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Costumes from JSON.
|
|
|
|
const {costumePromises} = assets;
|
|
|
|
// Sounds from JSON
|
|
|
|
const {soundBank, soundPromises} = assets;
|
2017-01-27 20:05:54 -05:00
|
|
|
// Create the first clone, and load its run-state from JSON.
|
2018-05-18 10:11:59 -04:00
|
|
|
const target = sprite.createClone(object.isStage ? StageLayering.BACKGROUND_LAYER : StageLayering.SPRITE_LAYER);
|
2017-01-27 20:05:54 -05:00
|
|
|
// Load target properties from JSON.
|
2018-04-04 18:54:05 -04:00
|
|
|
if (object.hasOwnProperty('tempo')) {
|
|
|
|
target.tempo = object.tempo;
|
|
|
|
}
|
|
|
|
if (object.hasOwnProperty('volume')) {
|
|
|
|
target.volume = object.volume;
|
|
|
|
}
|
|
|
|
if (object.hasOwnProperty('videoTransparency')) {
|
|
|
|
target.videoTransparency = object.videoTransparency;
|
|
|
|
}
|
|
|
|
if (object.hasOwnProperty('videoState')) {
|
|
|
|
target.videoState = object.videoState;
|
|
|
|
}
|
2018-10-17 17:34:12 -04:00
|
|
|
if (object.hasOwnProperty('textToSpeechLanguage')) {
|
|
|
|
target.textToSpeechLanguage = object.textToSpeechLanguage;
|
|
|
|
}
|
2017-01-27 20:05:54 -05:00
|
|
|
if (object.hasOwnProperty('variables')) {
|
2018-03-27 13:00:58 -04:00
|
|
|
for (const varId in object.variables) {
|
|
|
|
const variable = object.variables[varId];
|
2018-10-30 18:53:57 -04:00
|
|
|
// A variable is a cloud variable if:
|
|
|
|
// - the project says it's a cloud variable, and
|
|
|
|
// - it's a stage variable, and
|
|
|
|
// - the runtime can support another cloud variable
|
2018-10-31 13:40:05 -04:00
|
|
|
const isCloud = (variable.length === 3) && variable[2] &&
|
|
|
|
object.isStage && runtime.canAddCloudVariable();
|
2018-04-06 11:31:52 -04:00
|
|
|
const newVariable = new Variable(
|
|
|
|
varId, // var id is the index of the variable desc array in the variables obj
|
|
|
|
variable[0], // name of the variable
|
|
|
|
Variable.SCALAR_TYPE, // type of the variable
|
2018-10-30 18:53:57 -04:00
|
|
|
isCloud
|
2018-04-06 11:31:52 -04:00
|
|
|
);
|
2018-10-31 13:40:05 -04:00
|
|
|
if (isCloud) runtime.addCloudVariable();
|
2018-04-06 11:31:52 -04:00
|
|
|
newVariable.value = variable[1];
|
2017-06-15 17:29:15 -04:00
|
|
|
target.variables[newVariable.id] = newVariable;
|
2017-01-27 20:05:54 -05:00
|
|
|
}
|
|
|
|
}
|
2018-03-27 13:00:58 -04:00
|
|
|
if (object.hasOwnProperty('lists')) {
|
|
|
|
for (const listId in object.lists) {
|
|
|
|
const list = object.lists[listId];
|
|
|
|
const newList = new Variable(
|
|
|
|
listId,
|
|
|
|
list[0],
|
|
|
|
Variable.LIST_TYPE,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
newList.value = list[1];
|
|
|
|
target.variables[newList.id] = newList;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (object.hasOwnProperty('broadcasts')) {
|
|
|
|
for (const broadcastId in object.broadcasts) {
|
|
|
|
const broadcast = object.broadcasts[broadcastId];
|
|
|
|
const newBroadcast = new Variable(
|
|
|
|
broadcastId,
|
2018-03-28 09:47:46 -04:00
|
|
|
broadcast,
|
2018-03-27 13:00:58 -04:00
|
|
|
Variable.BROADCAST_MESSAGE_TYPE,
|
|
|
|
false
|
|
|
|
);
|
2018-03-28 09:47:46 -04:00
|
|
|
// no need to explicitly set the value, variable constructor
|
|
|
|
// sets the value to the same as the name for broadcast msgs
|
2018-03-27 13:00:58 -04:00
|
|
|
target.variables[newBroadcast.id] = newBroadcast;
|
|
|
|
}
|
|
|
|
}
|
2018-06-14 14:11:20 -04:00
|
|
|
if (object.hasOwnProperty('comments')) {
|
|
|
|
for (const commentId in object.comments) {
|
|
|
|
const comment = object.comments[commentId];
|
|
|
|
const newComment = new Comment(
|
2018-06-15 14:57:49 -04:00
|
|
|
commentId,
|
2018-06-14 14:11:20 -04:00
|
|
|
comment.text,
|
|
|
|
comment.x,
|
|
|
|
comment.y,
|
|
|
|
comment.width,
|
|
|
|
comment.height,
|
|
|
|
comment.minimized
|
|
|
|
);
|
|
|
|
if (comment.blockId) {
|
|
|
|
newComment.blockId = comment.blockId;
|
|
|
|
}
|
|
|
|
target.comments[newComment.id] = newComment;
|
|
|
|
}
|
|
|
|
}
|
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')) {
|
2019-01-08 13:07:46 -05:00
|
|
|
target.currentCostume = MathUtil.clamp(object.currentCostume, 0, object.costumes.length - 1);
|
2017-01-27 20:05:54 -05:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2018-07-24 11:00:48 -04:00
|
|
|
if (object.hasOwnProperty('targetPaneOrder')) {
|
|
|
|
// Temporarily store the 'targetPaneOrder' property
|
|
|
|
// so that we can correctly order sprites in the target pane.
|
|
|
|
// This will be deleted after we are done parsing and ordering the targets list.
|
|
|
|
target.targetPaneOrder = object.targetPaneOrder;
|
|
|
|
}
|
2018-12-14 19:17:36 -05:00
|
|
|
if (object.hasOwnProperty('draggable')) {
|
|
|
|
target.draggable = object.draggable;
|
|
|
|
}
|
2017-04-27 17:08:06 -04:00
|
|
|
Promise.all(costumePromises).then(costumes => {
|
|
|
|
sprite.costumes = costumes;
|
|
|
|
});
|
|
|
|
Promise.all(soundPromises).then(sounds => {
|
|
|
|
sprite.sounds = sounds;
|
2019-02-20 14:51:48 -05:00
|
|
|
// Make sure if soundBank is undefined, sprite.soundBank is then null.
|
|
|
|
sprite.soundBank = soundBank || null;
|
2017-04-27 17:08:06 -04:00
|
|
|
});
|
|
|
|
return Promise.all(costumePromises.concat(soundPromises)).then(() => target);
|
2017-01-27 20:05:54 -05:00
|
|
|
};
|
|
|
|
|
2018-11-09 23:34:06 -05:00
|
|
|
const deserializeMonitor = function (monitorData, runtime, targets, extensions) {
|
2018-11-09 23:32:50 -05:00
|
|
|
// If the serialized monitor has spriteName defined, look up the sprite
|
|
|
|
// by name in the given list of targets and update the monitor's targetId
|
|
|
|
// to match the sprite's id.
|
2018-10-21 00:22:39 -04:00
|
|
|
if (monitorData.spriteName) {
|
|
|
|
const filteredTargets = targets.filter(t => t.sprite.name === monitorData.spriteName);
|
2018-11-09 23:32:50 -05:00
|
|
|
if (filteredTargets && filteredTargets.length > 0) {
|
|
|
|
monitorData.targetId = filteredTargets[0].id;
|
|
|
|
} else {
|
|
|
|
log.warn(`Tried to deserialize sprite specific monitor ${
|
|
|
|
monitorData.opcode} but could not find sprite ${monitorData.spriteName}.`);
|
2018-10-21 00:22:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-09 23:32:50 -05:00
|
|
|
// Get information about this monitor, if it exists, given the monitor's opcode.
|
|
|
|
// This will be undefined for extension blocks
|
|
|
|
const monitorBlockInfo = runtime.monitorBlockInfo[monitorData.opcode];
|
|
|
|
|
2020-02-17 01:34:36 -05:00
|
|
|
// Due to a bug (see https://github.com/LLK/scratch-vm/pull/2322), renamed list monitors may have been serialized
|
|
|
|
// with an outdated/incorrect LIST parameter. Fix it up to use the current name of the actual corresponding list.
|
|
|
|
if (monitorData.opcode === 'data_listcontents') {
|
|
|
|
const listTarget = monitorData.targetId ?
|
|
|
|
targets.find(t => t.id === monitorData.targetId) :
|
|
|
|
targets.find(t => t.isStage);
|
|
|
|
if (
|
|
|
|
listTarget &&
|
|
|
|
Object.prototype.hasOwnProperty.call(listTarget.variables, monitorData.id)
|
|
|
|
) {
|
|
|
|
monitorData.params.LIST = listTarget.variables[monitorData.id].name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-09 23:32:50 -05:00
|
|
|
// Convert the serialized monitorData params into the block fields structure
|
|
|
|
const fields = {};
|
|
|
|
for (const paramKey in monitorData.params) {
|
|
|
|
const field = {
|
|
|
|
name: paramKey,
|
|
|
|
value: monitorData.params[paramKey]
|
|
|
|
};
|
|
|
|
fields[paramKey] = field;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Variables, lists, and non-sprite-specific monitors, including any extension
|
|
|
|
// monitors should already have the correct monitor ID serialized in the monitorData,
|
|
|
|
// find the correct id for all other monitors.
|
|
|
|
if (monitorData.opcode !== 'data_variable' && monitorData.opcode !== 'data_listcontents' &&
|
|
|
|
monitorBlockInfo && monitorBlockInfo.isSpriteSpecific) {
|
2018-10-21 00:22:39 -04:00
|
|
|
monitorData.id = monitorBlockInfo.getId(
|
2018-11-09 23:32:50 -05:00
|
|
|
monitorData.targetId, fields);
|
2019-02-04 15:32:01 -05:00
|
|
|
} else {
|
|
|
|
// Replace unsafe characters in monitor ID, if there are any.
|
|
|
|
// These would have come from projects that were originally 2.0 projects
|
|
|
|
// that had unsafe characters in the variable name (and then the name was
|
|
|
|
// used as part of the variable ID when importing the project).
|
|
|
|
monitorData.id = StringUtil.replaceUnsafeChars(monitorData.id);
|
2018-10-21 00:22:39 -04:00
|
|
|
}
|
|
|
|
|
2018-11-09 23:32:50 -05:00
|
|
|
// If the runtime already has a monitor block for this monitor's id,
|
|
|
|
// update the existing block with the relevant monitor information.
|
2018-10-21 00:22:39 -04:00
|
|
|
const existingMonitorBlock = runtime.monitorBlocks._blocks[monitorData.id];
|
|
|
|
if (existingMonitorBlock) {
|
|
|
|
// A monitor block already exists if the toolbox has been loaded and
|
|
|
|
// the monitor block is not target specific (because the block gets recycled).
|
|
|
|
existingMonitorBlock.isMonitored = monitorData.visible;
|
|
|
|
existingMonitorBlock.targetId = monitorData.targetId;
|
2018-11-09 23:32:50 -05:00
|
|
|
} else {
|
2018-11-14 13:15:36 -05:00
|
|
|
// If a monitor block doesn't already exist for this monitor,
|
2018-11-09 23:32:50 -05:00
|
|
|
// construct a monitor block to add to the monitor blocks container
|
|
|
|
const monitorBlock = {
|
|
|
|
id: monitorData.id,
|
|
|
|
opcode: monitorData.opcode,
|
|
|
|
inputs: {}, // Assuming that monitor blocks don't have droppable fields
|
|
|
|
fields: fields,
|
|
|
|
topLevel: true,
|
|
|
|
next: null,
|
|
|
|
parent: null,
|
|
|
|
shadow: false,
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
isMonitored: monitorData.visible,
|
|
|
|
targetId: monitorData.targetId
|
|
|
|
};
|
|
|
|
|
|
|
|
// Variables and lists have additional properties
|
|
|
|
// stored in their fields, update this info in the
|
|
|
|
// monitor block fields
|
|
|
|
if (monitorData.opcode === 'data_variable') {
|
|
|
|
const field = monitorBlock.fields.VARIABLE;
|
|
|
|
field.id = monitorData.id;
|
|
|
|
field.variableType = Variable.SCALAR_TYPE;
|
|
|
|
} else if (monitorData.opcode === 'data_listcontents') {
|
|
|
|
const field = monitorBlock.fields.LIST;
|
|
|
|
field.id = monitorData.id;
|
|
|
|
field.variableType = Variable.LIST_TYPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.monitorBlocks.createBlock(monitorBlock);
|
2018-11-09 23:34:06 -05:00
|
|
|
|
|
|
|
// If the block is from an extension, record it.
|
|
|
|
const extensionID = getExtensionIdForOpcode(monitorBlock.opcode);
|
|
|
|
if (extensionID) {
|
|
|
|
extensions.extensionIDs.add(extensionID);
|
|
|
|
}
|
2018-10-21 00:22:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
runtime.requestAddMonitor(MonitorRecord(monitorData));
|
|
|
|
};
|
|
|
|
|
2019-02-04 15:32:01 -05:00
|
|
|
// Replace variable IDs throughout the project with
|
|
|
|
// xml-safe versions.
|
|
|
|
// This is to fix up projects imported from 2.0 where xml-unsafe names
|
|
|
|
// were getting added to the variable ids.
|
|
|
|
const replaceUnsafeCharsInVariableIds = function (targets) {
|
2019-02-06 16:58:46 -05:00
|
|
|
const allVarRefs = VariableUtil.getAllVarRefsForTargets(targets, true);
|
2019-02-04 15:32:01 -05:00
|
|
|
// Re-id the variables in the actual targets
|
|
|
|
targets.forEach(t => {
|
|
|
|
Object.keys(t.variables).forEach(id => {
|
|
|
|
const newId = StringUtil.replaceUnsafeChars(id);
|
|
|
|
if (newId === id) return;
|
|
|
|
t.variables[id].id = newId;
|
|
|
|
t.variables[newId] = t.variables[id];
|
|
|
|
delete t.variables[id];
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Replace the IDs in the blocks refrencing variables or lists
|
|
|
|
for (const id in allVarRefs) {
|
|
|
|
const newId = StringUtil.replaceUnsafeChars(id);
|
|
|
|
if (id === newId) continue; // ID was already safe, skip
|
|
|
|
// We're calling this on the stage target because we need a
|
|
|
|
// target to call on but this shouldn't matter because we're passing
|
|
|
|
// in all the varRefs we want to operate on
|
2019-02-04 19:05:28 -05:00
|
|
|
VariableUtil.updateVariableIdentifiers(allVarRefs[id], newId);
|
2019-02-04 15:32:01 -05:00
|
|
|
}
|
|
|
|
return targets;
|
|
|
|
};
|
|
|
|
|
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.
|
|
|
|
* @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)
|
2018-05-03 16:19:29 -04:00
|
|
|
* @param {boolean} isSingleSprite - If true treat as single sprite, else treat as whole project
|
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-05-03 16:19:29 -04:00
|
|
|
const deserialize = function (json, runtime, zip, isSingleSprite) {
|
2017-11-03 14:17:16 -04:00
|
|
|
const extensions = {
|
|
|
|
extensionIDs: new Set(),
|
|
|
|
extensionURLs: new Map()
|
|
|
|
};
|
2018-07-24 11:00:48 -04:00
|
|
|
|
2021-04-29 11:32:13 -04:00
|
|
|
// Store the origin field (e.g. project originated at CSFirst) so that we can save it again.
|
|
|
|
if (json.meta && json.meta.origin) {
|
|
|
|
runtime.origin = json.meta.origin;
|
|
|
|
} else {
|
|
|
|
runtime.origin = null;
|
|
|
|
}
|
|
|
|
|
2018-07-24 11:00:48 -04:00
|
|
|
// First keep track of the current target order in the json,
|
|
|
|
// then sort by the layer order property before parsing the targets
|
|
|
|
// so that their corresponding render drawables can be created in
|
|
|
|
// their layer order (e.g. back to front)
|
|
|
|
const targetObjects = ((isSingleSprite ? [json] : json.targets) || [])
|
|
|
|
.map((t, i) => Object.assign(t, {targetPaneOrder: i}))
|
|
|
|
.sort((a, b) => a.layerOrder - b.layerOrder);
|
|
|
|
|
2018-10-21 00:22:39 -04:00
|
|
|
const monitorObjects = json.monitors || [];
|
|
|
|
|
2019-02-20 14:51:48 -05:00
|
|
|
return Promise.resolve(
|
2018-07-24 11:00:48 -04:00
|
|
|
targetObjects.map(target =>
|
2019-02-20 14:51:48 -05:00
|
|
|
parseScratchAssets(target, runtime, zip))
|
2018-06-19 14:02:54 -04:00
|
|
|
)
|
2019-02-20 14:51:48 -05:00
|
|
|
// Force this promise to wait for the next loop in the js tick. Let
|
|
|
|
// storage have some time to send off asset requests.
|
|
|
|
.then(assets => Promise.resolve(assets))
|
|
|
|
.then(assets => Promise.all(targetObjects
|
|
|
|
.map((target, index) =>
|
|
|
|
parseScratchObject(target, runtime, extensions, zip, assets[index]))))
|
2018-07-24 11:00:48 -04:00
|
|
|
.then(targets => targets // Re-sort targets back into original sprite-pane ordering
|
2019-01-11 14:52:34 -05:00
|
|
|
.map((t, i) => {
|
|
|
|
// Add layer order property to deserialized targets.
|
|
|
|
// This property is used to initialize executable targets in
|
|
|
|
// the correct order and is deleted in VM's installTargets function
|
|
|
|
t.layerOrder = i;
|
|
|
|
return t;
|
|
|
|
})
|
2018-07-24 11:00:48 -04:00
|
|
|
.sort((a, b) => a.targetPaneOrder - b.targetPaneOrder)
|
|
|
|
.map(t => {
|
|
|
|
// Delete the temporary properties used for
|
|
|
|
// sprite pane ordering and stage layer ordering
|
|
|
|
delete t.targetPaneOrder;
|
|
|
|
return t;
|
|
|
|
}))
|
2019-02-04 15:32:01 -05:00
|
|
|
.then(targets => replaceUnsafeCharsInVariableIds(targets))
|
2018-10-21 00:22:39 -04:00
|
|
|
.then(targets => {
|
2018-11-09 23:34:06 -05:00
|
|
|
monitorObjects.map(monitorDesc => deserializeMonitor(monitorDesc, runtime, targets, extensions));
|
2018-10-21 00:22:39 -04:00
|
|
|
return targets;
|
|
|
|
})
|
2018-06-19 14:02:54 -04:00
|
|
|
.then(targets => ({
|
|
|
|
targets,
|
|
|
|
extensions
|
|
|
|
}));
|
2016-12-30 10:19:58 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
serialize: serialize,
|
2018-08-14 11:17:47 -04:00
|
|
|
deserialize: deserialize,
|
2018-08-15 17:06:30 -04:00
|
|
|
deserializeBlocks: deserializeBlocks,
|
2018-11-05 12:57:09 -05:00
|
|
|
serializeBlocks: serializeBlocks,
|
|
|
|
getExtensionIdForOpcode: getExtensionIdForOpcode
|
2016-12-30 10:19:58 -05:00
|
|
|
};
|