2016-08-31 13:56:05 -04:00
|
|
|
/**
|
|
|
|
* @fileoverview
|
|
|
|
* Partial implementation of an SB2 JSON importer.
|
|
|
|
* Parses provided JSON and then generates all needed
|
|
|
|
* scratch-vm runtime structures.
|
|
|
|
*/
|
|
|
|
|
2017-04-17 15:10:04 -04:00
|
|
|
const Blocks = require('../engine/blocks');
|
|
|
|
const RenderedTarget = require('../sprites/rendered-target');
|
|
|
|
const Sprite = require('../sprites/sprite');
|
|
|
|
const Color = require('../util/color');
|
|
|
|
const log = require('../util/log');
|
|
|
|
const uid = require('../util/uid');
|
2018-03-25 18:14:30 -04:00
|
|
|
const StringUtil = require('../util/string-util');
|
2019-01-08 13:07:46 -05:00
|
|
|
const MathUtil = require('../util/math-util');
|
2017-04-26 11:44:53 -04:00
|
|
|
const specMap = require('./sb2_specmap');
|
2018-05-21 11:30:22 -04:00
|
|
|
const Comment = require('../engine/comment');
|
2017-04-17 15:10:04 -04:00
|
|
|
const Variable = require('../engine/variable');
|
2018-05-08 14:09:18 -04:00
|
|
|
const MonitorRecord = require('../engine/monitor-record');
|
2018-05-18 10:11:59 -04:00
|
|
|
const StageLayering = require('../engine/stage-layering');
|
2016-08-31 13:56:05 -04:00
|
|
|
|
2017-09-11 09:42:16 -04:00
|
|
|
const {loadCostume} = require('../import/load-costume.js');
|
|
|
|
const {loadSound} = require('../import/load-sound.js');
|
2018-03-25 18:14:30 -04:00
|
|
|
const {deserializeCostume, deserializeSound} = require('./deserialize-assets.js');
|
2017-03-27 15:04:44 -04:00
|
|
|
|
2018-04-20 10:34:17 -04:00
|
|
|
// Constants used during deserialization of an SB2 file
|
2018-04-20 07:54:02 -04:00
|
|
|
const CORE_EXTENSIONS = [
|
2018-04-20 10:58:03 -04:00
|
|
|
'argument',
|
2018-04-20 07:54:02 -04:00
|
|
|
'control',
|
|
|
|
'data',
|
|
|
|
'event',
|
|
|
|
'looks',
|
2018-04-20 10:58:03 -04:00
|
|
|
'math',
|
2018-04-20 07:54:02 -04:00
|
|
|
'motion',
|
|
|
|
'operator',
|
|
|
|
'procedures',
|
|
|
|
'sensing',
|
|
|
|
'sound'
|
|
|
|
];
|
|
|
|
|
2018-05-31 16:33:42 -04:00
|
|
|
// Adjust script coordinates to account for
|
|
|
|
// larger block size in scratch-blocks.
|
|
|
|
// @todo: Determine more precisely the right formulas here.
|
|
|
|
const WORKSPACE_X_SCALE = 1.5;
|
|
|
|
const WORKSPACE_Y_SCALE = 2.2;
|
|
|
|
|
2017-04-17 17:15:19 -04:00
|
|
|
/**
|
|
|
|
* Convert a Scratch 2.0 procedure string (e.g., "my_procedure %s %b %n")
|
|
|
|
* into an argument map. This allows us to provide the expected inputs
|
|
|
|
* to a mutated procedure call.
|
|
|
|
* @param {string} procCode Scratch 2.0 procedure string.
|
|
|
|
* @return {object} Argument map compatible with those in sb2specmap.
|
|
|
|
*/
|
|
|
|
const parseProcedureArgMap = function (procCode) {
|
|
|
|
const argMap = [
|
|
|
|
{} // First item in list is op string.
|
|
|
|
];
|
|
|
|
const INPUT_PREFIX = 'input';
|
|
|
|
let inputCount = 0;
|
|
|
|
// Split by %n, %b, %s.
|
|
|
|
const parts = procCode.split(/(?=[^\\]%[nbs])/);
|
|
|
|
for (let i = 0; i < parts.length; i++) {
|
|
|
|
const part = parts[i].trim();
|
|
|
|
if (part.substring(0, 1) === '%') {
|
|
|
|
const argType = part.substring(1, 2);
|
|
|
|
const arg = {
|
|
|
|
type: 'input',
|
|
|
|
inputName: INPUT_PREFIX + (inputCount++)
|
|
|
|
};
|
|
|
|
if (argType === 'n') {
|
|
|
|
arg.inputOp = 'math_number';
|
|
|
|
} else if (argType === 's') {
|
|
|
|
arg.inputOp = 'text';
|
2018-09-13 12:42:54 -04:00
|
|
|
} else if (argType === 'b') {
|
|
|
|
arg.inputOp = 'boolean';
|
2017-04-17 17:15:19 -04:00
|
|
|
}
|
|
|
|
argMap.push(arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return argMap;
|
|
|
|
};
|
|
|
|
|
2017-11-02 17:03:12 -04:00
|
|
|
/**
|
|
|
|
* Generate a list of "argument IDs" for procdefs and caller mutations.
|
|
|
|
* IDs just end up being `input0`, `input1`, ... which is good enough.
|
|
|
|
* @param {string} procCode Scratch 2.0 procedure string.
|
|
|
|
* @return {Array.<string>} Array of argument id strings.
|
|
|
|
*/
|
|
|
|
const parseProcedureArgIds = function (procCode) {
|
|
|
|
return parseProcedureArgMap(procCode)
|
|
|
|
.map(arg => arg.inputName)
|
|
|
|
.filter(name => name); // Filter out unnamed inputs which are labels
|
2017-11-02 17:14:00 -04:00
|
|
|
};
|
2017-11-02 17:03:12 -04:00
|
|
|
|
2017-04-17 17:15:19 -04:00
|
|
|
/**
|
|
|
|
* Flatten a block tree into a block list.
|
|
|
|
* Children are temporarily stored on the `block.children` property.
|
|
|
|
* @param {Array.<object>} blocks list generated by `parseBlockList`.
|
|
|
|
* @return {Array.<object>} Flattened list to be passed to `blocks.createBlock`.
|
|
|
|
*/
|
|
|
|
const flatten = function (blocks) {
|
|
|
|
let finalBlocks = [];
|
|
|
|
for (let i = 0; i < blocks.length; i++) {
|
|
|
|
const block = blocks[i];
|
|
|
|
finalBlocks.push(block);
|
|
|
|
if (block.children) {
|
|
|
|
finalBlocks = finalBlocks.concat(flatten(block.children));
|
|
|
|
}
|
|
|
|
delete block.children;
|
|
|
|
}
|
|
|
|
return finalBlocks;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse any list of blocks from SB2 JSON into a list of VM-format blocks.
|
|
|
|
* Could be used to parse a top-level script,
|
|
|
|
* a list of blocks in a branch (e.g., in forever),
|
|
|
|
* or a list of blocks in an argument (e.g., move [pick random...]).
|
|
|
|
* @param {Array.<object>} blockList SB2 JSON-format block list.
|
2017-12-01 10:29:32 -05:00
|
|
|
* @param {Function} addBroadcastMsg function to update broadcast message name map
|
2017-07-17 12:02:48 -04:00
|
|
|
* @param {Function} getVariableId function to retreive a variable's ID based on name
|
2017-11-03 14:17:16 -04:00
|
|
|
* @param {ImportedExtensionsInfo} extensions - (in/out) parsed extension information will be stored here.
|
2018-09-06 17:22:11 -04:00
|
|
|
* @param {ParseState} parseState - info on the state of parsing beyond the current block.
|
2018-05-21 11:30:22 -04:00
|
|
|
* @param {object<int, Comment>} comments - Comments from sb2 project that need to be attached to blocks.
|
|
|
|
* They are indexed in this object by the sb2 flattened block list index indicating
|
|
|
|
* which block they should attach to.
|
|
|
|
* @param {int} commentIndex The current index of the top block in this list if it were in a flattened
|
|
|
|
* list of all blocks for the target
|
2018-07-31 09:01:37 -04:00
|
|
|
* @return {Array<Array.<object>|int>} Tuple where first item is the Scratch VM-format block list, and
|
2018-05-21 11:30:22 -04:00
|
|
|
* second item is the updated comment index
|
2017-04-17 17:15:19 -04:00
|
|
|
*/
|
2018-09-06 17:22:11 -04:00
|
|
|
const parseBlockList = function (blockList, addBroadcastMsg, getVariableId, extensions, parseState, comments,
|
|
|
|
commentIndex) {
|
2017-04-17 17:15:19 -04:00
|
|
|
const resultingList = [];
|
|
|
|
let previousBlock = null; // For setting next.
|
|
|
|
for (let i = 0; i < blockList.length; i++) {
|
|
|
|
const block = blockList[i];
|
|
|
|
// eslint-disable-next-line no-use-before-define
|
2018-05-21 11:30:22 -04:00
|
|
|
const parsedBlockAndComments = parseBlock(block, addBroadcastMsg, getVariableId,
|
2018-09-06 17:22:11 -04:00
|
|
|
extensions, parseState, comments, commentIndex);
|
2018-06-26 16:41:10 -04:00
|
|
|
const parsedBlock = parsedBlockAndComments[0];
|
|
|
|
// Update commentIndex
|
|
|
|
commentIndex = parsedBlockAndComments[1];
|
|
|
|
|
|
|
|
if (!parsedBlock) continue;
|
|
|
|
if (previousBlock) {
|
|
|
|
parsedBlock.parent = previousBlock.id;
|
|
|
|
previousBlock.next = parsedBlock.id;
|
2017-04-17 17:15:19 -04:00
|
|
|
}
|
2018-06-26 16:41:10 -04:00
|
|
|
previousBlock = parsedBlock;
|
|
|
|
resultingList.push(parsedBlock);
|
2017-04-17 17:15:19 -04:00
|
|
|
}
|
2018-05-21 11:30:22 -04:00
|
|
|
return [resultingList, commentIndex];
|
2017-04-17 17:15:19 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a Scratch object's scripts into VM blocks.
|
|
|
|
* This should only handle top-level scripts that include X, Y coordinates.
|
|
|
|
* @param {!object} scripts Scripts object from SB2 JSON.
|
|
|
|
* @param {!Blocks} blocks Blocks object to load parsed blocks into.
|
2017-12-01 10:29:32 -05:00
|
|
|
* @param {Function} addBroadcastMsg function to update broadcast message name map
|
2017-07-17 12:02:48 -04:00
|
|
|
* @param {Function} getVariableId function to retreive a variable's ID based on name
|
2017-11-03 14:17:16 -04:00
|
|
|
* @param {ImportedExtensionsInfo} extensions - (in/out) parsed extension information will be stored here.
|
2018-05-21 11:30:22 -04:00
|
|
|
* @param {object} comments Comments that need to be attached to the blocks that need to be parsed
|
2017-04-17 17:15:19 -04:00
|
|
|
*/
|
2018-05-21 11:30:22 -04:00
|
|
|
const parseScripts = function (scripts, blocks, addBroadcastMsg, getVariableId, extensions, comments) {
|
|
|
|
// Keep track of the index of the current script being
|
|
|
|
// parsed in order to attach block comments correctly
|
|
|
|
let scriptIndexForComment = 0;
|
|
|
|
|
2017-04-17 17:15:19 -04:00
|
|
|
for (let i = 0; i < scripts.length; i++) {
|
|
|
|
const script = scripts[i];
|
|
|
|
const scriptX = script[0];
|
|
|
|
const scriptY = script[1];
|
|
|
|
const blockList = script[2];
|
2018-09-06 17:22:11 -04:00
|
|
|
const parseState = {};
|
2018-05-21 11:30:22 -04:00
|
|
|
const [parsedBlockList, newCommentIndex] = parseBlockList(blockList, addBroadcastMsg, getVariableId, extensions,
|
2018-09-06 17:22:11 -04:00
|
|
|
parseState, comments, scriptIndexForComment);
|
2018-05-21 11:30:22 -04:00
|
|
|
scriptIndexForComment = newCommentIndex;
|
2017-04-17 17:15:19 -04:00
|
|
|
if (parsedBlockList[0]) {
|
2018-05-31 16:33:42 -04:00
|
|
|
parsedBlockList[0].x = scriptX * WORKSPACE_X_SCALE;
|
|
|
|
parsedBlockList[0].y = scriptY * WORKSPACE_Y_SCALE;
|
2017-04-17 17:15:19 -04:00
|
|
|
parsedBlockList[0].topLevel = true;
|
|
|
|
parsedBlockList[0].parent = null;
|
|
|
|
}
|
|
|
|
// Flatten children and create add the blocks.
|
|
|
|
const convertedBlocks = flatten(parsedBlockList);
|
|
|
|
for (let j = 0; j < convertedBlocks.length; j++) {
|
|
|
|
blocks.createBlock(convertedBlocks[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-17 12:02:48 -04:00
|
|
|
/**
|
|
|
|
* Create a callback for assigning fixed IDs to imported variables
|
|
|
|
* Generator stores the global variable mapping in a closure
|
|
|
|
* @param {!string} targetId the id of the target to scope the variable to
|
|
|
|
* @return {string} variable ID
|
|
|
|
*/
|
|
|
|
const generateVariableIdGetter = (function () {
|
|
|
|
let globalVariableNameMap = {};
|
2019-02-04 10:29:24 -05:00
|
|
|
const namer = (targetId, name, type) => `${targetId}-${StringUtil.replaceUnsafeChars(name)}-${type}`;
|
2017-07-17 12:02:48 -04:00
|
|
|
return function (targetId, topLevel) {
|
|
|
|
// Reset the global variable map if topLevel
|
|
|
|
if (topLevel) globalVariableNameMap = {};
|
2018-09-07 19:08:44 -04:00
|
|
|
return function (name, type) {
|
2017-07-17 12:02:48 -04:00
|
|
|
if (topLevel) { // Store the name/id pair in the globalVariableNameMap
|
2018-09-07 19:08:44 -04:00
|
|
|
globalVariableNameMap[`${name}-${type}`] = namer(targetId, name, type);
|
|
|
|
return globalVariableNameMap[`${name}-${type}`];
|
2017-07-17 12:02:48 -04:00
|
|
|
}
|
|
|
|
// Not top-level, so first check the global name map
|
2018-09-07 19:08:44 -04:00
|
|
|
if (globalVariableNameMap[`${name}-${type}`]) return globalVariableNameMap[`${name}-${type}`];
|
|
|
|
return namer(targetId, name, type);
|
2017-07-17 12:02:48 -04:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}());
|
|
|
|
|
2017-12-01 10:29:32 -05:00
|
|
|
const globalBroadcastMsgStateGenerator = (function () {
|
|
|
|
let broadcastMsgNameMap = {};
|
2017-12-15 14:00:53 -05:00
|
|
|
const allBroadcastFields = [];
|
2017-12-13 17:15:18 -05:00
|
|
|
const emptyStringName = uid();
|
2017-12-01 10:29:32 -05:00
|
|
|
return function (topLevel) {
|
|
|
|
if (topLevel) broadcastMsgNameMap = {};
|
|
|
|
return {
|
2017-12-15 14:00:53 -05:00
|
|
|
broadcastMsgMapUpdater: function (name, field) {
|
2017-12-13 17:15:18 -05:00
|
|
|
name = name.toLowerCase();
|
|
|
|
if (name === '') {
|
|
|
|
name = emptyStringName;
|
|
|
|
}
|
2019-02-06 16:48:39 -05:00
|
|
|
broadcastMsgNameMap[name] = `broadcastMsgId-${StringUtil.replaceUnsafeChars(name)}`;
|
2017-12-15 14:00:53 -05:00
|
|
|
allBroadcastFields.push(field);
|
|
|
|
return broadcastMsgNameMap[name];
|
2017-12-01 10:29:32 -05:00
|
|
|
},
|
2017-12-15 11:02:26 -05:00
|
|
|
globalBroadcastMsgs: broadcastMsgNameMap,
|
2017-12-15 14:00:53 -05:00
|
|
|
allBroadcastFields: allBroadcastFields,
|
2017-12-15 11:02:26 -05:00
|
|
|
emptyMsgName: emptyStringName
|
2017-12-01 10:29:32 -05:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}());
|
|
|
|
|
2018-05-08 14:09:18 -04:00
|
|
|
/**
|
|
|
|
* Parse a single monitor object and create all its in-memory VM objects.
|
2018-11-14 10:42:52 -05:00
|
|
|
*
|
|
|
|
* It is important that monitors are parsed last,
|
|
|
|
* - after all sprite targets have finished parsing, and
|
|
|
|
* - after the rest of the stage has finished parsing.
|
|
|
|
*
|
|
|
|
* It is specifically important that all the scripts in the project
|
|
|
|
* have been parsed and all the relevant targets exist, have uids,
|
|
|
|
* and have their variables initialized.
|
|
|
|
* Calling this function before these things are true, will result in
|
|
|
|
* undefined behavior.
|
2018-05-09 11:08:49 -04:00
|
|
|
* @param {!object} object - From-JSON "Monitor object"
|
2018-05-08 14:09:18 -04:00
|
|
|
* @param {!Runtime} runtime - (in/out) Runtime object to load monitor info into.
|
|
|
|
* @param {!Array.<Target>} targets - Targets have already been parsed.
|
|
|
|
* @param {ImportedExtensionsInfo} extensions - (in/out) parsed extension information will be stored here.
|
|
|
|
*/
|
2018-11-14 10:42:52 -05:00
|
|
|
|
2018-05-08 14:09:18 -04:00
|
|
|
const parseMonitorObject = (object, runtime, targets, extensions) => {
|
2019-01-08 15:00:00 -05:00
|
|
|
// If we can't find the block in the spec map, ignore it.
|
|
|
|
// This happens for things like Lego Wedo 1.0 monitors.
|
|
|
|
const mapped = specMap[object.cmd];
|
|
|
|
if (!mapped) {
|
|
|
|
log.warn(`Could not find monitor block with opcode: ${object.cmd}`);
|
|
|
|
return;
|
|
|
|
}
|
2018-11-09 00:09:54 -05:00
|
|
|
// In scratch 2.0, there are two monitors that now correspond to extension
|
|
|
|
// blocks (tempo and video motion/direction). In the case of the
|
|
|
|
// video motion/direction block, this reporter is not monitorable in Scratch 3.0.
|
|
|
|
// In the case of the tempo block, we should import it and load the music extension
|
|
|
|
// only when the monitor is actually visible.
|
|
|
|
|
2018-10-24 16:43:01 -04:00
|
|
|
const opcode = specMap[object.cmd].opcode;
|
|
|
|
const extIndex = opcode.indexOf('_');
|
|
|
|
const extID = opcode.substring(0, extIndex);
|
|
|
|
|
2018-11-09 00:09:54 -05:00
|
|
|
if (extID === 'videoSensing') {
|
|
|
|
return;
|
|
|
|
} else if (CORE_EXTENSIONS.indexOf(extID) === -1 && extID !== '' &&
|
|
|
|
!extensions.extensionIDs.has(extID) && !object.visible) {
|
|
|
|
// Don't import this monitor if it refers to a non-core extension that
|
|
|
|
// doesn't exist anywhere else in the project and it isn't visible.
|
|
|
|
// This should only apply to the tempo block at this point since
|
|
|
|
// there are no other sb2 blocks that are now extension monitors.
|
|
|
|
return;
|
2018-10-24 16:43:01 -04:00
|
|
|
}
|
|
|
|
|
2018-11-09 00:09:54 -05:00
|
|
|
let target = null;
|
2018-05-08 14:09:18 -04:00
|
|
|
// List blocks don't come in with their target name set.
|
|
|
|
// Find the target by searching for a target with matching variable name/type.
|
|
|
|
if (!object.hasOwnProperty('target')) {
|
|
|
|
for (let i = 0; i < targets.length; i++) {
|
|
|
|
const currTarget = targets[i];
|
|
|
|
const listVariables = Object.keys(currTarget.variables).filter(key => {
|
|
|
|
const variable = currTarget.variables[key];
|
|
|
|
return variable.type === Variable.LIST_TYPE && variable.name === object.listName;
|
|
|
|
});
|
|
|
|
if (listVariables.length > 0) {
|
|
|
|
target = currTarget; // Keep this target for later use
|
|
|
|
object.target = currTarget.getName(); // Set target name to normalize with other monitors
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-09 11:08:49 -04:00
|
|
|
// Get the target for this monitor, if not gotten above.
|
2018-05-08 14:09:18 -04:00
|
|
|
target = target || targets.filter(t => t.getName() === object.target)[0];
|
|
|
|
if (!target) throw new Error('Cannot create monitor for target that cannot be found by name');
|
|
|
|
|
|
|
|
// Create var id getter to make block naming/parsing easier, variables already created.
|
|
|
|
const getVariableId = generateVariableIdGetter(target.id, false);
|
|
|
|
// eslint-disable-next-line no-use-before-define
|
2018-05-21 11:30:22 -04:00
|
|
|
const [block, _] = parseBlock(
|
2018-05-08 14:09:18 -04:00
|
|
|
[object.cmd, object.param], // Scratch 2 monitor blocks only have one param.
|
|
|
|
null, // `addBroadcastMsg`, not needed for monitor blocks.
|
|
|
|
getVariableId,
|
2018-05-21 11:30:22 -04:00
|
|
|
extensions,
|
2018-09-06 17:22:11 -04:00
|
|
|
{},
|
2018-05-21 11:30:22 -04:00
|
|
|
null, // `comments`, not needed for monitor blocks
|
|
|
|
null // `commentIndex`, not needed for monitor blocks
|
2018-05-08 14:09:18 -04:00
|
|
|
);
|
|
|
|
|
2018-05-09 11:08:49 -04:00
|
|
|
// Monitor blocks have special IDs to match the toolbox obtained from the getId
|
|
|
|
// function in the runtime.monitorBlocksInfo. Variable monitors, however,
|
|
|
|
// get their IDs from the variable id they reference.
|
2018-09-07 19:08:44 -04:00
|
|
|
if (object.cmd === 'getVar:') {
|
|
|
|
block.id = getVariableId(object.param, Variable.SCALAR_TYPE);
|
|
|
|
} else if (object.cmd === 'contentsOfList:') {
|
|
|
|
block.id = getVariableId(object.param, Variable.LIST_TYPE);
|
2018-05-09 11:08:49 -04:00
|
|
|
} else if (runtime.monitorBlockInfo.hasOwnProperty(block.opcode)) {
|
2018-11-08 01:33:26 -05:00
|
|
|
block.id = runtime.monitorBlockInfo[block.opcode].getId(target.id, block.fields);
|
2018-11-08 00:37:28 -05:00
|
|
|
} else {
|
|
|
|
// If the opcode can't be found in the runtime monitorBlockInfo,
|
|
|
|
// then default to using the block opcode as the id instead.
|
|
|
|
// This is for extension monitors, and assumes that extension monitors
|
|
|
|
// cannot be sprite specific.
|
|
|
|
block.id = block.opcode;
|
2018-05-08 14:09:18 -04:00
|
|
|
}
|
|
|
|
|
2018-05-09 11:08:49 -04:00
|
|
|
// Block needs a targetId if it is targetting something other than the stage
|
|
|
|
block.targetId = target.isStage ? null : target.id;
|
2018-05-08 14:09:18 -04:00
|
|
|
|
|
|
|
// Property required for running monitored blocks.
|
|
|
|
block.isMonitored = object.visible;
|
|
|
|
|
2018-11-08 00:37:28 -05:00
|
|
|
const existingMonitorBlock = runtime.monitorBlocks._blocks[block.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).
|
|
|
|
// Update the existing block with the relevant monitor information.
|
|
|
|
existingMonitorBlock.isMonitored = object.visible;
|
|
|
|
existingMonitorBlock.targetId = block.targetId;
|
|
|
|
} else {
|
|
|
|
// Blocks can be created with children, flatten and add to monitorBlocks.
|
|
|
|
const newBlocks = flatten([block]);
|
|
|
|
for (let i = 0; i < newBlocks.length; i++) {
|
|
|
|
runtime.monitorBlocks.createBlock(newBlocks[i]);
|
|
|
|
}
|
2018-05-08 14:09:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert numbered mode into strings for better understandability.
|
|
|
|
switch (object.mode) {
|
|
|
|
case 1:
|
|
|
|
object.mode = 'default';
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
object.mode = 'large';
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
object.mode = 'slider';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a monitor record for the runtime's monitorState
|
|
|
|
runtime.requestAddMonitor(MonitorRecord({
|
|
|
|
id: block.id,
|
|
|
|
targetId: block.targetId,
|
|
|
|
spriteName: block.targetId ? object.target : null,
|
|
|
|
opcode: block.opcode,
|
|
|
|
params: runtime.monitorBlocks._getBlockParams(block),
|
|
|
|
value: '',
|
|
|
|
mode: object.mode,
|
|
|
|
sliderMin: object.sliderMin,
|
|
|
|
sliderMax: object.sliderMax,
|
2019-02-11 11:09:01 -05:00
|
|
|
isDiscrete: object.isDiscrete,
|
2018-05-08 14:09:18 -04:00
|
|
|
x: object.x,
|
|
|
|
y: object.y,
|
|
|
|
width: object.width,
|
|
|
|
height: object.height,
|
|
|
|
visible: object.visible
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
2016-08-31 13:56:05 -04:00
|
|
|
/**
|
|
|
|
* Parse a single "Scratch object" and create all its in-memory VM objects.
|
2017-11-03 14:17:16 -04:00
|
|
|
* TODO: parse the "info" section, especially "savedExtensions"
|
|
|
|
* @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 {boolean} topLevel - Whether this is the top-level object (stage).
|
2018-03-25 18:14:30 -04:00
|
|
|
* @param {?object} zip - Optional zipped assets for local file import
|
2017-11-03 14:17:16 -04:00
|
|
|
* @return {!Promise.<Array.<Target>>} Promise for the loaded targets when ready, or null for unsupported objects.
|
2016-08-31 13:56:05 -04:00
|
|
|
*/
|
2018-03-25 18:14:30 -04:00
|
|
|
const parseScratchObject = function (object, runtime, extensions, topLevel, zip) {
|
2016-08-31 13:56:05 -04:00
|
|
|
if (!object.hasOwnProperty('objName')) {
|
2018-05-08 14:09:18 -04:00
|
|
|
if (object.hasOwnProperty('listName')) {
|
|
|
|
// Shim these objects so they can be processed as monitors
|
|
|
|
object.cmd = 'contentsOfList:';
|
|
|
|
object.param = object.listName;
|
|
|
|
object.mode = 'list';
|
|
|
|
}
|
|
|
|
// Defer parsing monitors until targets are all parsed
|
|
|
|
object.deferredMonitor = true;
|
|
|
|
return Promise.resolve(object);
|
2016-08-31 13:56:05 -04:00
|
|
|
}
|
2018-05-08 14:09:18 -04:00
|
|
|
|
2016-08-31 13:56:05 -04:00
|
|
|
// Blocks container for this object.
|
2019-01-23 18:18:38 -05:00
|
|
|
const blocks = new Blocks(runtime);
|
2016-08-31 13:56:05 -04:00
|
|
|
// @todo: For now, load all Scratch objects (stage/sprites) as a Sprite.
|
2017-04-17 15:10:04 -04:00
|
|
|
const sprite = new Sprite(blocks, runtime);
|
2016-08-31 13:56:05 -04:00
|
|
|
// Sprite/stage name from JSON.
|
|
|
|
if (object.hasOwnProperty('objName')) {
|
2018-09-07 14:36:44 -04:00
|
|
|
if (topLevel && object.objName !== 'Stage') {
|
|
|
|
for (const child of object.children) {
|
|
|
|
if (!child.hasOwnProperty('objName') && child.target === object.objName) {
|
|
|
|
child.target = 'Stage';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
object.objName = 'Stage';
|
|
|
|
}
|
|
|
|
|
|
|
|
sprite.name = object.objName;
|
2016-08-31 13:56:05 -04:00
|
|
|
}
|
|
|
|
// Costumes from JSON.
|
2017-04-17 15:10:04 -04:00
|
|
|
const costumePromises = [];
|
2016-08-31 13:56:05 -04:00
|
|
|
if (object.hasOwnProperty('costumes')) {
|
2017-04-17 15:10:04 -04:00
|
|
|
for (let i = 0; i < object.costumes.length; i++) {
|
|
|
|
const costumeSource = object.costumes[i];
|
2018-05-23 14:18:22 -04:00
|
|
|
const bitmapResolution = costumeSource.bitmapResolution || 1;
|
2017-04-17 15:10:04 -04:00
|
|
|
const costume = {
|
2017-01-27 13:44:48 -05:00
|
|
|
name: costumeSource.costumeName,
|
2018-05-23 14:18:22 -04:00
|
|
|
bitmapResolution: bitmapResolution,
|
|
|
|
rotationCenterX: topLevel ? 240 * bitmapResolution : costumeSource.rotationCenterX,
|
|
|
|
rotationCenterY: topLevel ? 180 * bitmapResolution : costumeSource.rotationCenterY,
|
2018-03-21 16:51:40 -04:00
|
|
|
// TODO we eventually want this next property to be called
|
|
|
|
// md5ext to reflect what it actually contains, however this
|
|
|
|
// will be a very extensive change across many repositories
|
|
|
|
// and should be done carefully and altogether
|
|
|
|
md5: costumeSource.baseLayerMD5,
|
2017-01-27 13:44:48 -05:00
|
|
|
skinId: null
|
|
|
|
};
|
2018-03-25 18:14:30 -04:00
|
|
|
const md5ext = costumeSource.baseLayerMD5;
|
|
|
|
const idParts = StringUtil.splitFirst(md5ext, '.');
|
|
|
|
const md5 = idParts[0];
|
2018-12-10 18:17:24 -05:00
|
|
|
let ext;
|
|
|
|
if (idParts.length === 2 && idParts[1]) {
|
|
|
|
ext = idParts[1];
|
|
|
|
} else {
|
|
|
|
// Default to 'png' if baseLayerMD5 is not formatted correctly
|
|
|
|
ext = 'png';
|
|
|
|
// Fix costume md5 for later
|
|
|
|
costume.md5 = `${costume.md5}.${ext}`;
|
|
|
|
}
|
2018-03-25 18:14:30 -04:00
|
|
|
costume.dataFormat = ext;
|
|
|
|
costume.assetId = md5;
|
2018-11-05 18:07:56 -05:00
|
|
|
if (costumeSource.textLayerMD5) {
|
|
|
|
costume.textLayerMD5 = StringUtil.splitFirst(costumeSource.textLayerMD5, '.')[0];
|
|
|
|
}
|
2018-03-25 18:14:30 -04:00
|
|
|
// If there is no internet connection, or if the asset is not in storage
|
|
|
|
// for some reason, and we are doing a local .sb2 import, (e.g. zip is provided)
|
|
|
|
// the file name of the costume should be the baseLayerID followed by the file ext
|
|
|
|
const assetFileName = `${costumeSource.baseLayerID}.${ext}`;
|
2018-11-05 18:07:56 -05:00
|
|
|
const textLayerFileName = costumeSource.textLayerID ? `${costumeSource.textLayerID}.png` : null;
|
|
|
|
costumePromises.push(deserializeCostume(costume, runtime, zip, assetFileName, textLayerFileName)
|
2018-11-05 15:50:28 -05:00
|
|
|
.then(() => loadCostume(costume.md5, costume, runtime, 2 /* optVersion */))
|
2018-10-18 07:39:28 -04:00
|
|
|
);
|
2016-08-31 13:56:05 -04:00
|
|
|
}
|
|
|
|
}
|
2016-09-28 16:42:25 -04:00
|
|
|
// Sounds from JSON
|
2017-04-19 17:54:52 -04:00
|
|
|
const soundPromises = [];
|
2016-10-13 11:35:52 -04:00
|
|
|
if (object.hasOwnProperty('sounds')) {
|
2017-04-17 15:10:04 -04:00
|
|
|
for (let s = 0; s < object.sounds.length; s++) {
|
|
|
|
const soundSource = object.sounds[s];
|
|
|
|
const sound = {
|
2017-01-27 13:44:48 -05:00
|
|
|
name: soundSource.soundName,
|
|
|
|
format: soundSource.format,
|
|
|
|
rate: soundSource.rate,
|
|
|
|
sampleCount: soundSource.sampleCount,
|
2018-03-21 16:51:40 -04:00
|
|
|
// TODO we eventually want this next property to be called
|
|
|
|
// md5ext to reflect what it actually contains, however this
|
|
|
|
// will be a very extensive change across many repositories
|
|
|
|
// and should be done carefully and altogether
|
|
|
|
// (for example, the audio engine currently relies on this
|
|
|
|
// property to be named 'md5')
|
2017-01-27 13:44:48 -05:00
|
|
|
md5: soundSource.md5,
|
|
|
|
data: null
|
|
|
|
};
|
2018-03-25 18:14:30 -04:00
|
|
|
const md5ext = soundSource.md5;
|
|
|
|
const idParts = StringUtil.splitFirst(md5ext, '.');
|
|
|
|
const md5 = idParts[0];
|
|
|
|
const ext = idParts[1].toLowerCase();
|
|
|
|
sound.dataFormat = ext;
|
|
|
|
sound.assetId = md5;
|
|
|
|
// If there is no internet connection, or if the asset is not in storage
|
|
|
|
// for some reason, and we are doing a local .sb2 import, (e.g. zip is provided)
|
|
|
|
// the file name of the sound should be the soundID (provided from the project.json)
|
|
|
|
// followed by the file ext
|
|
|
|
const assetFileName = `${soundSource.soundID}.${ext}`;
|
|
|
|
soundPromises.push(deserializeSound(sound, runtime, zip, assetFileName)
|
2019-01-02 11:41:13 -05:00
|
|
|
.then(() => loadSound(sound, runtime, sprite))
|
|
|
|
);
|
2016-09-28 16:42:25 -04:00
|
|
|
}
|
|
|
|
}
|
2017-07-17 12:02:48 -04:00
|
|
|
|
2016-08-31 13:56:05 -04:00
|
|
|
// Create the first clone, and load its run-state from JSON.
|
2018-05-18 10:11:59 -04:00
|
|
|
const target = sprite.createClone(topLevel ? StageLayering.BACKGROUND_LAYER : StageLayering.SPRITE_LAYER);
|
2017-04-18 09:27:58 -04:00
|
|
|
|
2017-07-17 12:02:48 -04:00
|
|
|
const getVariableId = generateVariableIdGetter(target.id, topLevel);
|
|
|
|
|
2017-12-01 10:29:32 -05:00
|
|
|
const globalBroadcastMsgObj = globalBroadcastMsgStateGenerator(topLevel);
|
|
|
|
const addBroadcastMsg = globalBroadcastMsgObj.broadcastMsgMapUpdater;
|
|
|
|
|
2016-09-21 16:38:33 -04:00
|
|
|
// Load target properties from JSON.
|
|
|
|
if (object.hasOwnProperty('variables')) {
|
2017-04-17 15:10:04 -04:00
|
|
|
for (let j = 0; j < object.variables.length; j++) {
|
|
|
|
const variable = object.variables[j];
|
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.isPersistent && topLevel && runtime.canAddCloudVariable();
|
2017-06-15 17:29:15 -04:00
|
|
|
const newVariable = new Variable(
|
2018-09-07 19:08:44 -04:00
|
|
|
getVariableId(variable.name, Variable.SCALAR_TYPE),
|
2016-09-21 16:38:33 -04:00
|
|
|
variable.name,
|
2017-11-13 16:55:57 -05:00
|
|
|
Variable.SCALAR_TYPE,
|
2018-10-30 18:53:57 -04:00
|
|
|
isCloud
|
2016-09-21 16:38:33 -04:00
|
|
|
);
|
2018-10-31 13:40:05 -04:00
|
|
|
if (isCloud) runtime.addCloudVariable();
|
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;
|
2016-09-21 16:38:33 -04:00
|
|
|
}
|
|
|
|
}
|
2017-07-17 12:02:48 -04:00
|
|
|
|
2018-05-21 11:30:22 -04:00
|
|
|
// If included, parse any and all comments on the object (this includes top-level
|
|
|
|
// workspace comments as well as comments attached to specific blocks)
|
|
|
|
const blockComments = {};
|
|
|
|
if (object.hasOwnProperty('scriptComments')) {
|
|
|
|
const comments = object.scriptComments.map(commentDesc => {
|
2018-05-31 16:33:42 -04:00
|
|
|
const [
|
|
|
|
commentX,
|
|
|
|
commentY,
|
|
|
|
commentWidth,
|
|
|
|
commentHeight,
|
|
|
|
commentFullSize,
|
|
|
|
flattenedBlockIndex,
|
|
|
|
commentText
|
|
|
|
] = commentDesc;
|
2018-05-28 21:24:47 -04:00
|
|
|
const isBlockComment = commentDesc[5] >= 0;
|
2018-05-21 11:30:22 -04:00
|
|
|
const newComment = new Comment(
|
|
|
|
null, // generate a new id for this comment
|
2018-05-31 16:33:42 -04:00
|
|
|
commentText, // text content of sb2 comment
|
2018-05-28 21:24:47 -04:00
|
|
|
// Only serialize x & y position of comment if it's a workspace comment
|
|
|
|
// If it's a block comment, we'll let scratch-blocks handle positioning
|
2018-05-31 16:33:42 -04:00
|
|
|
isBlockComment ? null : commentX * WORKSPACE_X_SCALE,
|
|
|
|
isBlockComment ? null : commentY * WORKSPACE_Y_SCALE,
|
|
|
|
commentWidth * WORKSPACE_X_SCALE,
|
|
|
|
commentHeight * WORKSPACE_Y_SCALE,
|
|
|
|
!commentFullSize
|
2018-05-21 11:30:22 -04:00
|
|
|
);
|
2018-05-28 21:24:47 -04:00
|
|
|
if (isBlockComment) {
|
2018-05-21 11:30:22 -04:00
|
|
|
// commentDesc[5] refers to the index of the block that this
|
|
|
|
// comment is attached to -- in a flattened version of the
|
|
|
|
// scripts array.
|
|
|
|
// If commentDesc[5] is -1, this is a workspace comment (we don't need to do anything
|
|
|
|
// extra at this point), otherwise temporarily save the flattened script array
|
|
|
|
// index as the blockId property of the new comment. We will
|
|
|
|
// change this to refer to the actual block id of the corresponding
|
|
|
|
// block when that block gets created
|
2018-05-31 16:33:42 -04:00
|
|
|
newComment.blockId = flattenedBlockIndex;
|
2018-05-21 11:30:22 -04:00
|
|
|
// Add this comment to the block comments object with its script index
|
|
|
|
// as the key
|
2018-05-31 16:33:42 -04:00
|
|
|
if (blockComments.hasOwnProperty(flattenedBlockIndex)) {
|
|
|
|
blockComments[flattenedBlockIndex].push(newComment);
|
2018-05-21 11:30:22 -04:00
|
|
|
} else {
|
2018-05-31 16:33:42 -04:00
|
|
|
blockComments[flattenedBlockIndex] = [newComment];
|
2018-05-21 11:30:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return newComment;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Add all the comments that were just created to the target.comments,
|
|
|
|
// referenced by id
|
|
|
|
comments.forEach(comment => {
|
|
|
|
target.comments[comment.id] = comment;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-17 12:02:48 -04:00
|
|
|
// If included, parse any and all scripts/blocks on the object.
|
|
|
|
if (object.hasOwnProperty('scripts')) {
|
2018-05-21 11:30:22 -04:00
|
|
|
parseScripts(object.scripts, blocks, addBroadcastMsg, getVariableId, extensions, blockComments);
|
2017-07-17 12:02:48 -04:00
|
|
|
}
|
|
|
|
|
2018-08-24 14:29:10 -04:00
|
|
|
// If there are any comments referring to a numerical block ID, make them
|
|
|
|
// workspace comments. These are comments that were originally created as
|
|
|
|
// block comments, detached from the block, and then had the associated
|
|
|
|
// block deleted.
|
|
|
|
// These comments should be imported as workspace comments
|
|
|
|
// by making their blockIDs (which currently refer to non-existing blocks)
|
|
|
|
// null (See #1452).
|
|
|
|
for (const commentIndex in blockComments) {
|
|
|
|
const currBlockComments = blockComments[commentIndex];
|
|
|
|
currBlockComments.forEach(c => {
|
|
|
|
if (typeof c.blockId === 'number') {
|
|
|
|
c.blockId = null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-20 09:50:57 -04:00
|
|
|
// Update stage specific blocks (e.g. sprite clicked <=> stage clicked)
|
|
|
|
blocks.updateTargetSpecificBlocks(topLevel); // topLevel = isStage
|
|
|
|
|
2016-09-21 16:38:33 -04:00
|
|
|
if (object.hasOwnProperty('lists')) {
|
2017-04-17 15:10:04 -04:00
|
|
|
for (let k = 0; k < object.lists.length; k++) {
|
|
|
|
const list = object.lists[k];
|
2017-11-09 17:19:34 -05:00
|
|
|
const newVariable = new Variable(
|
2018-09-07 19:08:44 -04:00
|
|
|
getVariableId(list.listName, Variable.LIST_TYPE),
|
2016-09-21 16:38:33 -04:00
|
|
|
list.listName,
|
2017-11-13 16:55:57 -05:00
|
|
|
Variable.LIST_TYPE,
|
2017-11-15 09:17:20 -05:00
|
|
|
false
|
2016-09-21 16:38:33 -04:00
|
|
|
);
|
2017-11-09 17:19:34 -05:00
|
|
|
newVariable.value = list.contents;
|
|
|
|
target.variables[newVariable.id] = newVariable;
|
2016-09-21 16:38:33 -04:00
|
|
|
}
|
|
|
|
}
|
2016-09-19 14:40:01 -04:00
|
|
|
if (object.hasOwnProperty('scratchX')) {
|
2016-08-31 13:56:05 -04:00
|
|
|
target.x = object.scratchX;
|
|
|
|
}
|
2016-09-19 14:40:01 -04:00
|
|
|
if (object.hasOwnProperty('scratchY')) {
|
2016-08-31 13:56:05 -04:00
|
|
|
target.y = object.scratchY;
|
|
|
|
}
|
2016-09-19 14:40:01 -04:00
|
|
|
if (object.hasOwnProperty('direction')) {
|
2016-08-31 13:56:05 -04:00
|
|
|
target.direction = object.direction;
|
|
|
|
}
|
2017-03-01 18:49:17 -05:00
|
|
|
if (object.hasOwnProperty('isDraggable')) {
|
|
|
|
target.draggable = object.isDraggable;
|
|
|
|
}
|
2016-09-19 14:40:01 -04:00
|
|
|
if (object.hasOwnProperty('scale')) {
|
2016-08-31 13:56:05 -04:00
|
|
|
// SB2 stores as 1.0 = 100%; we use % in the VM.
|
|
|
|
target.size = object.scale * 100;
|
|
|
|
}
|
2016-09-19 14:40:01 -04:00
|
|
|
if (object.hasOwnProperty('visible')) {
|
2016-08-31 13:56:05 -04:00
|
|
|
target.visible = object.visible;
|
|
|
|
}
|
2016-09-19 14:40:01 -04:00
|
|
|
if (object.hasOwnProperty('currentCostumeIndex')) {
|
2019-01-08 13:07:46 -05:00
|
|
|
// Current costume index can sometimes be a floating
|
|
|
|
// point number, use Math.floor to come up with an appropriate index
|
|
|
|
// and clamp it to the actual number of costumes the object has for good measure.
|
|
|
|
target.currentCostume = MathUtil.clamp(Math.floor(object.currentCostumeIndex), 0, object.costumes.length - 1);
|
2016-08-31 13:56:05 -04:00
|
|
|
}
|
2016-09-28 17:09:04 -04:00
|
|
|
if (object.hasOwnProperty('rotationStyle')) {
|
2016-10-23 17:55:31 -04:00
|
|
|
if (object.rotationStyle === 'none') {
|
2016-10-26 11:19:43 -04:00
|
|
|
target.rotationStyle = RenderedTarget.ROTATION_STYLE_NONE;
|
2016-10-23 17:55:31 -04:00
|
|
|
} else if (object.rotationStyle === 'leftRight') {
|
2016-10-26 11:19:43 -04:00
|
|
|
target.rotationStyle = RenderedTarget.ROTATION_STYLE_LEFT_RIGHT;
|
2016-10-23 17:55:31 -04:00
|
|
|
} else if (object.rotationStyle === 'normal') {
|
2016-10-26 11:19:43 -04:00
|
|
|
target.rotationStyle = RenderedTarget.ROTATION_STYLE_ALL_AROUND;
|
2016-09-28 17:09:04 -04:00
|
|
|
}
|
2016-08-31 13:56:05 -04:00
|
|
|
}
|
2018-02-27 11:48:23 -05:00
|
|
|
if (object.hasOwnProperty('tempoBPM')) {
|
|
|
|
target.tempo = object.tempoBPM;
|
|
|
|
}
|
2018-04-03 16:36:58 -04:00
|
|
|
if (object.hasOwnProperty('videoAlpha')) {
|
|
|
|
// SB2 stores alpha as opacity, where 1.0 is opaque.
|
|
|
|
// We convert to a percentage, and invert it so 100% is full transparency.
|
|
|
|
target.videoTransparency = 100 - (100 * object.videoAlpha);
|
|
|
|
}
|
|
|
|
if (object.hasOwnProperty('info')) {
|
|
|
|
if (object.info.hasOwnProperty('videoOn')) {
|
|
|
|
if (object.info.videoOn) {
|
|
|
|
target.videoState = RenderedTarget.VIDEO_STATE.ON;
|
2018-11-02 15:25:34 -04:00
|
|
|
} else {
|
|
|
|
target.videoState = RenderedTarget.VIDEO_STATE.OFF;
|
2018-04-03 16:36:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-23 15:25:05 -04:00
|
|
|
if (object.hasOwnProperty('indexInLibrary')) {
|
|
|
|
// Temporarily store the 'indexInLibrary' property from the sb2 file
|
|
|
|
// 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.indexInLibrary;
|
|
|
|
}
|
2017-04-18 09:27:58 -04:00
|
|
|
|
2016-09-08 09:40:27 -04:00
|
|
|
target.isStage = topLevel;
|
2017-04-18 09:27:58 -04:00
|
|
|
|
2017-04-19 17:54:52 -04:00
|
|
|
Promise.all(costumePromises).then(costumes => {
|
2017-04-03 15:58:13 -04:00
|
|
|
sprite.costumes = costumes;
|
2017-01-27 13:44:48 -05:00
|
|
|
});
|
2017-04-18 09:27:58 -04:00
|
|
|
|
2017-04-19 17:54:52 -04:00
|
|
|
Promise.all(soundPromises).then(sounds => {
|
2017-04-18 09:27:58 -04:00
|
|
|
sprite.sounds = sounds;
|
|
|
|
});
|
|
|
|
|
2016-08-31 13:56:05 -04:00
|
|
|
// The stage will have child objects; recursively process them.
|
2017-04-19 17:54:52 -04:00
|
|
|
const childrenPromises = [];
|
2016-08-31 13:56:05 -04:00
|
|
|
if (object.children) {
|
2017-04-19 17:54:52 -04:00
|
|
|
for (let m = 0; m < object.children.length; m++) {
|
2018-03-25 18:14:30 -04:00
|
|
|
childrenPromises.push(parseScratchObject(object.children[m], runtime, extensions, false, zip));
|
2016-08-31 13:56:05 -04:00
|
|
|
}
|
|
|
|
}
|
2017-04-18 09:27:58 -04:00
|
|
|
|
2017-04-19 17:54:52 -04:00
|
|
|
return Promise.all(
|
|
|
|
costumePromises.concat(soundPromises)
|
|
|
|
).then(() =>
|
|
|
|
Promise.all(
|
|
|
|
childrenPromises
|
|
|
|
).then(children => {
|
2017-12-01 10:29:32 -05:00
|
|
|
// Need create broadcast msgs as variables after
|
|
|
|
// all other targets have finished processing.
|
|
|
|
if (target.isStage) {
|
|
|
|
const allBroadcastMsgs = globalBroadcastMsgObj.globalBroadcastMsgs;
|
2017-12-15 14:00:53 -05:00
|
|
|
const allBroadcastMsgFields = globalBroadcastMsgObj.allBroadcastFields;
|
|
|
|
const oldEmptyMsgName = globalBroadcastMsgObj.emptyMsgName;
|
|
|
|
if (allBroadcastMsgs[oldEmptyMsgName]) {
|
|
|
|
// Find a fresh 'messageN'
|
2017-12-15 11:02:26 -05:00
|
|
|
let currIndex = 1;
|
|
|
|
while (allBroadcastMsgs[`message${currIndex}`]) {
|
|
|
|
currIndex += 1;
|
|
|
|
}
|
|
|
|
const newEmptyMsgName = `message${currIndex}`;
|
2017-12-15 14:00:53 -05:00
|
|
|
// Add the new empty message name to the broadcast message
|
|
|
|
// name map, and assign it the old id.
|
|
|
|
// Then, delete the old entry in map.
|
|
|
|
allBroadcastMsgs[newEmptyMsgName] = allBroadcastMsgs[oldEmptyMsgName];
|
|
|
|
delete allBroadcastMsgs[oldEmptyMsgName];
|
|
|
|
// Now update all the broadcast message fields with
|
|
|
|
// the new empty message name.
|
|
|
|
for (let i = 0; i < allBroadcastMsgFields.length; i++) {
|
|
|
|
if (allBroadcastMsgFields[i].value === '') {
|
|
|
|
allBroadcastMsgFields[i].value = newEmptyMsgName;
|
2017-12-15 11:02:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-15 14:00:53 -05:00
|
|
|
// Traverse the broadcast message name map and create
|
|
|
|
// broadcast messages as variables on the stage (which is this
|
|
|
|
// target).
|
2017-12-01 10:29:32 -05:00
|
|
|
for (const msgName in allBroadcastMsgs) {
|
|
|
|
const msgId = allBroadcastMsgs[msgName];
|
|
|
|
const newMsg = new Variable(
|
|
|
|
msgId,
|
|
|
|
msgName,
|
|
|
|
Variable.BROADCAST_MESSAGE_TYPE,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
target.variables[newMsg.id] = newMsg;
|
|
|
|
}
|
|
|
|
}
|
2017-04-19 17:54:52 -04:00
|
|
|
let targets = [target];
|
2018-05-08 14:09:18 -04:00
|
|
|
const deferredMonitors = [];
|
2017-04-19 17:54:52 -04:00
|
|
|
for (let n = 0; n < children.length; n++) {
|
2018-05-08 14:09:18 -04:00
|
|
|
if (children[n]) {
|
|
|
|
if (children[n].deferredMonitor) {
|
|
|
|
deferredMonitors.push(children[n]);
|
|
|
|
} else {
|
|
|
|
targets = targets.concat(children[n]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-14 10:42:52 -05:00
|
|
|
// It is important that monitors are parsed last
|
|
|
|
// - after all sprite targets have finished parsing
|
|
|
|
// - and this is the last thing that happens in the stage parsing
|
|
|
|
// It is specifically important that all the scripts in the project
|
|
|
|
// have been parsed and all the relevant targets exist, have uids,
|
|
|
|
// and have their variables initialized.
|
2018-05-08 14:09:18 -04:00
|
|
|
for (let n = 0; n < deferredMonitors.length; n++) {
|
|
|
|
parseMonitorObject(deferredMonitors[n], runtime, targets, extensions);
|
2017-04-18 09:27:58 -04:00
|
|
|
}
|
|
|
|
return targets;
|
2017-04-19 17:54:52 -04:00
|
|
|
})
|
|
|
|
);
|
2016-10-23 17:55:31 -04:00
|
|
|
};
|
|
|
|
|
2018-07-23 15:25:05 -04:00
|
|
|
const reorderParsedTargets = function (targets) {
|
|
|
|
// Reorder parsed targets based on the temporary targetPaneOrder property
|
2018-07-23 15:48:27 -04:00
|
|
|
// and then delete it.
|
2018-07-23 15:25:05 -04:00
|
|
|
|
2019-01-11 14:52:34 -05:00
|
|
|
const reordered = targets.map((t, index) => {
|
|
|
|
t.layerOrder = index;
|
|
|
|
return t;
|
|
|
|
}).sort((a, b) => a.targetPaneOrder - b.targetPaneOrder);
|
2018-07-23 15:25:05 -04:00
|
|
|
|
|
|
|
// Delete the temporary target pane ordering since we shouldn't need it anymore.
|
2019-01-11 14:52:34 -05:00
|
|
|
reordered.forEach(t => {
|
2018-07-23 15:25:05 -04:00
|
|
|
delete t.targetPaneOrder;
|
|
|
|
});
|
|
|
|
|
2019-01-11 14:52:34 -05:00
|
|
|
return reordered;
|
2018-07-23 15:25:05 -04:00
|
|
|
};
|
|
|
|
|
2018-10-18 11:06:13 -04:00
|
|
|
|
2016-10-23 17:55:31 -04:00
|
|
|
/**
|
|
|
|
* Top-level handler. Parse provided JSON,
|
|
|
|
* and process the top-level object (the stage object).
|
2017-04-27 17:49:57 -04:00
|
|
|
* @param {!object} json SB2-format JSON to load.
|
2016-10-23 17:55:31 -04:00
|
|
|
* @param {!Runtime} runtime Runtime object to load all structures into.
|
2017-02-01 15:59:50 -05:00
|
|
|
* @param {boolean=} optForceSprite If set, treat as sprite (Sprite2).
|
2018-03-25 18:14:30 -04:00
|
|
|
* @param {?object} zip Optional zipped assets for local file import
|
2017-11-03 14:17:16 -04:00
|
|
|
* @return {Promise.<ImportedProject>} Promise that resolves to the loaded targets when ready.
|
2016-10-23 17:55:31 -04:00
|
|
|
*/
|
2018-03-25 18:14:30 -04:00
|
|
|
const sb2import = function (json, runtime, optForceSprite, zip) {
|
2017-11-03 14:17:16 -04:00
|
|
|
const extensions = {
|
|
|
|
extensionIDs: new Set(),
|
|
|
|
extensionURLs: new Map()
|
|
|
|
};
|
2018-03-25 18:14:30 -04:00
|
|
|
return parseScratchObject(json, runtime, extensions, !optForceSprite, zip)
|
2018-07-23 15:25:05 -04:00
|
|
|
.then(reorderParsedTargets)
|
2017-11-03 14:17:16 -04:00
|
|
|
.then(targets => ({
|
|
|
|
targets,
|
|
|
|
extensions
|
|
|
|
}));
|
2016-10-23 17:55:31 -04:00
|
|
|
};
|
2016-08-31 13:56:05 -04:00
|
|
|
|
2018-04-05 15:39:16 -04:00
|
|
|
/**
|
|
|
|
* Given the sb2 block, inspect the specmap for a translation method or object.
|
|
|
|
* @param {!object} block a sb2 formatted block
|
|
|
|
* @return {object} specmap block to parse this opcode
|
|
|
|
*/
|
|
|
|
const specMapBlock = function (block) {
|
|
|
|
const opcode = block[0];
|
|
|
|
const mapped = opcode && specMap[opcode];
|
|
|
|
if (!mapped) {
|
|
|
|
log.warn(`Couldn't find SB2 block: ${opcode}`);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (typeof mapped === 'function') {
|
|
|
|
return mapped(block);
|
|
|
|
}
|
|
|
|
return mapped;
|
|
|
|
};
|
|
|
|
|
2016-08-31 13:56:05 -04:00
|
|
|
/**
|
|
|
|
* Parse a single SB2 JSON-formatted block and its children.
|
2017-02-01 15:59:50 -05:00
|
|
|
* @param {!object} sb2block SB2 JSON-formatted block.
|
2017-12-01 10:29:32 -05:00
|
|
|
* @param {Function} addBroadcastMsg function to update broadcast message name map
|
2017-11-03 14:17:16 -04:00
|
|
|
* @param {Function} getVariableId function to retrieve a variable's ID based on name
|
|
|
|
* @param {ImportedExtensionsInfo} extensions - (in/out) parsed extension information will be stored here.
|
2018-09-06 17:22:11 -04:00
|
|
|
* @param {ParseState} parseState - info on the state of parsing beyond the current block.
|
2018-05-21 11:30:22 -04:00
|
|
|
* @param {object<int, Comment>} comments - Comments from sb2 project that need to be attached to blocks.
|
|
|
|
* They are indexed in this object by the sb2 flattened block list index indicating
|
|
|
|
* which block they should attach to.
|
|
|
|
* @param {int} commentIndex The comment index for the block to be parsed if it were in a flattened
|
|
|
|
* list of all blocks for the target
|
2018-07-31 09:01:37 -04:00
|
|
|
* @return {Array.<object|int>} Tuple where first item is the Scratch VM-format block (or null if unsupported object),
|
2018-05-21 11:30:22 -04:00
|
|
|
* and second item is the updated comment index (after this block and its children are parsed)
|
2016-08-31 13:56:05 -04:00
|
|
|
*/
|
2018-09-06 17:22:11 -04:00
|
|
|
const parseBlock = function (sb2block, addBroadcastMsg, getVariableId, extensions, parseState, comments, commentIndex) {
|
2018-06-26 16:41:10 -04:00
|
|
|
const commentsForParsedBlock = (comments && typeof commentIndex === 'number' && !isNaN(commentIndex)) ?
|
|
|
|
comments[commentIndex] : null;
|
2018-04-05 15:39:16 -04:00
|
|
|
const blockMetadata = specMapBlock(sb2block);
|
|
|
|
if (!blockMetadata) {
|
2018-06-27 14:17:33 -04:00
|
|
|
// No block opcode found, exclude this block, increment the commentIndex,
|
2018-06-27 10:44:28 -04:00
|
|
|
// make all block comments into workspace comments and send them to zero/zero
|
|
|
|
// to prevent serialization issues.
|
2018-06-26 16:41:10 -04:00
|
|
|
if (commentsForParsedBlock) {
|
|
|
|
commentsForParsedBlock.forEach(comment => {
|
|
|
|
comment.blockId = null;
|
|
|
|
comment.x = comment.y = 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return [null, commentIndex + 1];
|
2016-08-31 13:56:05 -04:00
|
|
|
}
|
2018-04-05 15:39:16 -04:00
|
|
|
const oldOpcode = sb2block[0];
|
2018-04-20 07:54:02 -04:00
|
|
|
|
2017-11-03 14:17:16 -04:00
|
|
|
// If the block is from an extension, record it.
|
2018-04-20 07:54:02 -04:00
|
|
|
const index = blockMetadata.opcode.indexOf('_');
|
|
|
|
const prefix = blockMetadata.opcode.substring(0, index);
|
|
|
|
if (CORE_EXTENSIONS.indexOf(prefix) === -1) {
|
2018-04-20 11:36:16 -04:00
|
|
|
if (prefix !== '') extensions.extensionIDs.add(prefix);
|
2017-11-03 14:17:16 -04:00
|
|
|
}
|
2018-04-20 07:54:02 -04:00
|
|
|
|
2016-08-31 13:56:05 -04:00
|
|
|
// Block skeleton.
|
2017-04-17 15:10:04 -04:00
|
|
|
const activeBlock = {
|
2016-08-31 13:56:05 -04:00
|
|
|
id: uid(), // Generate a new block unique ID.
|
|
|
|
opcode: blockMetadata.opcode, // Converted, e.g. "motion_movesteps".
|
|
|
|
inputs: {}, // Inputs to this block and the blocks they point to.
|
|
|
|
fields: {}, // Fields on this block and their values.
|
2016-09-06 10:55:52 -04:00
|
|
|
next: null, // Next block.
|
2016-08-31 13:56:05 -04:00
|
|
|
shadow: false, // No shadow blocks in an SB2 by default.
|
|
|
|
children: [] // Store any generated children, flattened in `flatten`.
|
|
|
|
};
|
2018-05-21 11:30:22 -04:00
|
|
|
|
|
|
|
// Attach any comments to this block..
|
|
|
|
if (commentsForParsedBlock) {
|
2018-06-26 16:41:10 -04:00
|
|
|
// Attach only the last comment to the block, make all others workspace comments
|
2018-05-21 11:30:22 -04:00
|
|
|
activeBlock.comment = commentsForParsedBlock[commentsForParsedBlock.length - 1].id;
|
|
|
|
commentsForParsedBlock.forEach(comment => {
|
2018-06-26 16:41:10 -04:00
|
|
|
if (comment.id === activeBlock.comment) {
|
|
|
|
comment.blockId = activeBlock.id;
|
|
|
|
} else {
|
|
|
|
// All other comments don't get a block ID and are sent back to zero.
|
|
|
|
// This is important, because if they have `null` x/y, serialization breaks.
|
|
|
|
comment.blockId = null;
|
|
|
|
comment.x = comment.y = 0;
|
|
|
|
}
|
2018-05-21 11:30:22 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
commentIndex++;
|
|
|
|
|
2018-09-06 18:35:18 -04:00
|
|
|
const parentExpectedArg = parseState.expectedArg;
|
|
|
|
|
2016-10-13 13:11:26 -04:00
|
|
|
// For a procedure call, generate argument map from proc string.
|
2016-10-23 17:55:31 -04:00
|
|
|
if (oldOpcode === 'call') {
|
2016-10-13 13:11:26 -04:00
|
|
|
blockMetadata.argMap = parseProcedureArgMap(sb2block[1]);
|
|
|
|
}
|
2016-08-31 13:56:05 -04:00
|
|
|
// Look at the expected arguments in `blockMetadata.argMap.`
|
|
|
|
// The basic problem here is to turn positional SB2 arguments into
|
|
|
|
// non-positional named Scratch VM arguments.
|
2017-04-17 15:10:04 -04:00
|
|
|
for (let i = 0; i < blockMetadata.argMap.length; i++) {
|
|
|
|
const expectedArg = blockMetadata.argMap[i];
|
|
|
|
const providedArg = sb2block[i + 1]; // (i = 0 is opcode)
|
2016-09-06 10:55:52 -04:00
|
|
|
// Whether the input is obscuring a shadow.
|
2017-04-17 15:10:04 -04:00
|
|
|
let shadowObscured = false;
|
2016-08-31 13:56:05 -04:00
|
|
|
// Positional argument is an input.
|
2016-10-23 17:55:31 -04:00
|
|
|
if (expectedArg.type === 'input') {
|
2016-08-31 13:56:05 -04:00
|
|
|
// Create a new block and input metadata.
|
2017-04-17 15:10:04 -04:00
|
|
|
const inputUid = uid();
|
2016-08-31 13:56:05 -04:00
|
|
|
activeBlock.inputs[expectedArg.inputName] = {
|
|
|
|
name: expectedArg.inputName,
|
2016-09-06 10:55:52 -04:00
|
|
|
block: null,
|
|
|
|
shadow: null
|
2016-08-31 13:56:05 -04:00
|
|
|
};
|
2016-10-23 17:55:31 -04:00
|
|
|
if (typeof providedArg === 'object' && providedArg) {
|
2016-08-31 13:56:05 -04:00
|
|
|
// Block or block list occupies the input.
|
2017-04-17 17:15:19 -04:00
|
|
|
let innerBlocks;
|
2018-09-06 18:35:18 -04:00
|
|
|
parseState.expectedArg = expectedArg;
|
2016-10-23 17:55:31 -04:00
|
|
|
if (typeof providedArg[0] === 'object' && providedArg[0]) {
|
2016-08-31 13:56:05 -04:00
|
|
|
// Block list occupies the input.
|
2018-05-21 11:30:22 -04:00
|
|
|
[innerBlocks, commentIndex] = parseBlockList(providedArg, addBroadcastMsg, getVariableId,
|
2018-09-06 17:22:11 -04:00
|
|
|
extensions, parseState, comments, commentIndex);
|
2016-08-31 13:56:05 -04:00
|
|
|
} else {
|
|
|
|
// Single block occupies the input.
|
2018-05-21 11:30:22 -04:00
|
|
|
const parsedBlockDesc = parseBlock(providedArg, addBroadcastMsg, getVariableId, extensions,
|
2018-09-06 17:22:11 -04:00
|
|
|
parseState, comments, commentIndex);
|
2018-10-31 17:37:51 -04:00
|
|
|
innerBlocks = parsedBlockDesc[0] ? [parsedBlockDesc[0]] : [];
|
2018-05-21 11:30:22 -04:00
|
|
|
// Update commentIndex
|
|
|
|
commentIndex = parsedBlockDesc[1];
|
2016-08-31 13:56:05 -04:00
|
|
|
}
|
2018-09-06 18:35:18 -04:00
|
|
|
parseState.expectedArg = parentExpectedArg;
|
2018-10-31 17:37:51 -04:00
|
|
|
|
|
|
|
// Check if innerBlocks is not an empty list.
|
|
|
|
// An empty list indicates that all the inner blocks from the sb2 have
|
2018-08-23 18:20:45 -04:00
|
|
|
// unknown opcodes and have been skipped.
|
2018-10-31 17:37:51 -04:00
|
|
|
if (innerBlocks.length > 0) {
|
|
|
|
let previousBlock = null;
|
|
|
|
for (let j = 0; j < innerBlocks.length; j++) {
|
|
|
|
if (j === 0) {
|
|
|
|
innerBlocks[j].parent = activeBlock.id;
|
|
|
|
} else {
|
|
|
|
innerBlocks[j].parent = previousBlock;
|
|
|
|
}
|
|
|
|
previousBlock = innerBlocks[j].id;
|
2016-10-17 16:44:33 -04:00
|
|
|
}
|
2018-10-31 17:37:51 -04:00
|
|
|
activeBlock.inputs[expectedArg.inputName].block = (
|
|
|
|
innerBlocks[0].id
|
|
|
|
);
|
|
|
|
activeBlock.children = (
|
|
|
|
activeBlock.children.concat(innerBlocks)
|
|
|
|
);
|
2016-09-08 09:40:53 -04:00
|
|
|
}
|
2018-11-14 14:24:09 -05:00
|
|
|
|
2016-09-06 10:55:52 -04:00
|
|
|
// Obscures any shadow.
|
|
|
|
shadowObscured = true;
|
|
|
|
}
|
|
|
|
// Generate a shadow block to occupy the input.
|
|
|
|
if (!expectedArg.inputOp) {
|
2018-09-13 12:42:54 -04:00
|
|
|
// Undefined inputOp. inputOp should always be defined for inputs.
|
|
|
|
log.warn(`Unknown input operation for input ${expectedArg.inputName} of opcode ${activeBlock.opcode}.`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (expectedArg.inputOp === 'boolean' || expectedArg.inputOp === 'substack') {
|
2016-09-06 10:55:52 -04:00
|
|
|
// No editable shadow input; e.g., for a boolean.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Each shadow has a field generated for it automatically.
|
|
|
|
// Value to be filled in the field.
|
2017-04-17 15:10:04 -04:00
|
|
|
let fieldValue = providedArg;
|
2016-09-06 10:55:52 -04:00
|
|
|
// Shadows' field names match the input name, except for these:
|
2017-04-17 15:10:04 -04:00
|
|
|
let fieldName = expectedArg.inputName;
|
2016-10-23 17:55:31 -04:00
|
|
|
if (expectedArg.inputOp === 'math_number' ||
|
|
|
|
expectedArg.inputOp === 'math_whole_number' ||
|
|
|
|
expectedArg.inputOp === 'math_positive_number' ||
|
|
|
|
expectedArg.inputOp === 'math_integer' ||
|
|
|
|
expectedArg.inputOp === 'math_angle') {
|
2016-09-06 10:55:52 -04:00
|
|
|
fieldName = 'NUM';
|
|
|
|
// Fields are given Scratch 2.0 default values if obscured.
|
|
|
|
if (shadowObscured) {
|
|
|
|
fieldValue = 10;
|
|
|
|
}
|
2016-10-23 17:55:31 -04:00
|
|
|
} else if (expectedArg.inputOp === 'text') {
|
2016-09-06 10:55:52 -04:00
|
|
|
fieldName = 'TEXT';
|
|
|
|
if (shadowObscured) {
|
|
|
|
fieldValue = '';
|
2016-08-31 13:56:05 -04:00
|
|
|
}
|
2016-10-23 17:55:31 -04:00
|
|
|
} else if (expectedArg.inputOp === 'colour_picker') {
|
2016-09-06 11:46:10 -04:00
|
|
|
// Convert SB2 color to hex.
|
2016-09-12 10:58:50 -04:00
|
|
|
fieldValue = Color.decimalToHex(providedArg);
|
2016-09-06 11:46:10 -04:00
|
|
|
fieldName = 'COLOUR';
|
2016-09-06 10:55:52 -04:00
|
|
|
if (shadowObscured) {
|
|
|
|
fieldValue = '#990000';
|
|
|
|
}
|
2017-12-13 17:15:18 -05:00
|
|
|
} else if (expectedArg.inputOp === 'event_broadcast_menu') {
|
|
|
|
fieldName = 'BROADCAST_OPTION';
|
|
|
|
if (shadowObscured) {
|
|
|
|
fieldValue = '';
|
|
|
|
}
|
2018-10-09 11:27:54 -04:00
|
|
|
} else if (expectedArg.inputOp === 'sensing_of_object_menu') {
|
|
|
|
if (shadowObscured) {
|
|
|
|
fieldValue = '_stage_';
|
|
|
|
} else if (fieldValue === 'Stage') {
|
|
|
|
fieldValue = '_stage_';
|
|
|
|
}
|
2018-11-14 14:55:12 -05:00
|
|
|
} else if (expectedArg.inputOp === 'note') {
|
|
|
|
if (shadowObscured) {
|
|
|
|
fieldValue = 60;
|
|
|
|
}
|
2018-02-26 17:28:20 -05:00
|
|
|
} else if (expectedArg.inputOp === 'music.menu.DRUM') {
|
|
|
|
if (shadowObscured) {
|
|
|
|
fieldValue = 1;
|
|
|
|
}
|
|
|
|
} else if (expectedArg.inputOp === 'music.menu.INSTRUMENT') {
|
|
|
|
if (shadowObscured) {
|
|
|
|
fieldValue = 1;
|
|
|
|
}
|
2018-04-06 14:21:20 -04:00
|
|
|
} else if (expectedArg.inputOp === 'videoSensing.menu.ATTRIBUTE') {
|
2018-03-26 15:32:05 -04:00
|
|
|
if (shadowObscured) {
|
2018-04-06 14:21:20 -04:00
|
|
|
fieldValue = 'motion';
|
2018-03-26 15:32:05 -04:00
|
|
|
}
|
2018-04-06 14:21:20 -04:00
|
|
|
} else if (expectedArg.inputOp === 'videoSensing.menu.SUBJECT') {
|
2018-03-26 15:32:05 -04:00
|
|
|
if (shadowObscured) {
|
2018-04-06 14:21:20 -04:00
|
|
|
fieldValue = 'this sprite';
|
2018-03-26 15:32:05 -04:00
|
|
|
}
|
2018-04-05 15:10:16 -04:00
|
|
|
} else if (expectedArg.inputOp === 'videoSensing.menu.VIDEO_STATE') {
|
|
|
|
if (shadowObscured) {
|
2018-04-06 13:11:10 -04:00
|
|
|
fieldValue = 'on';
|
2018-04-05 15:10:16 -04:00
|
|
|
}
|
2016-10-26 10:26:56 -04:00
|
|
|
} else if (shadowObscured) {
|
|
|
|
// Filled drop-down menu.
|
|
|
|
fieldValue = '';
|
2016-09-06 10:55:52 -04:00
|
|
|
}
|
2017-04-17 15:10:04 -04:00
|
|
|
const fields = {};
|
2016-09-06 10:55:52 -04:00
|
|
|
fields[fieldName] = {
|
2017-12-15 14:00:53 -05:00
|
|
|
name: fieldName,
|
|
|
|
value: fieldValue
|
2016-09-06 10:55:52 -04:00
|
|
|
};
|
2017-12-13 17:15:18 -05:00
|
|
|
// event_broadcast_menus have some extra properties to add to the
|
|
|
|
// field and a different value than the rest
|
|
|
|
if (expectedArg.inputOp === 'event_broadcast_menu') {
|
2018-04-10 15:16:18 -04:00
|
|
|
// Need to update the broadcast message name map with
|
|
|
|
// the value of this field.
|
|
|
|
// Also need to provide the fields[fieldName] object,
|
|
|
|
// so that we can later update its value property, e.g.
|
|
|
|
// if sb2 message name is empty string, we will later
|
|
|
|
// replace this field's value with messageN
|
|
|
|
// once we can traverse through all the existing message names
|
|
|
|
// and come up with a fresh messageN.
|
|
|
|
const broadcastId = addBroadcastMsg(fieldValue, fields[fieldName]);
|
|
|
|
fields[fieldName].id = broadcastId;
|
2017-12-13 17:15:18 -05:00
|
|
|
fields[fieldName].variableType = expectedArg.variableType;
|
|
|
|
}
|
2016-09-06 10:55:52 -04:00
|
|
|
activeBlock.children.push({
|
|
|
|
id: inputUid,
|
|
|
|
opcode: expectedArg.inputOp,
|
|
|
|
inputs: {},
|
|
|
|
fields: fields,
|
|
|
|
next: null,
|
|
|
|
topLevel: false,
|
2016-09-08 09:40:53 -04:00
|
|
|
parent: activeBlock.id,
|
2016-09-06 10:55:52 -04:00
|
|
|
shadow: true
|
|
|
|
});
|
|
|
|
activeBlock.inputs[expectedArg.inputName].shadow = inputUid;
|
2016-09-06 11:46:10 -04:00
|
|
|
// If no block occupying the input, alias to the shadow.
|
2016-09-06 10:55:52 -04:00
|
|
|
if (!activeBlock.inputs[expectedArg.inputName].block) {
|
|
|
|
activeBlock.inputs[expectedArg.inputName].block = inputUid;
|
2016-08-31 13:56:05 -04:00
|
|
|
}
|
2016-10-23 17:55:31 -04:00
|
|
|
} else if (expectedArg.type === 'field') {
|
2016-08-31 13:56:05 -04:00
|
|
|
// Add as a field on this block.
|
|
|
|
activeBlock.fields[expectedArg.fieldName] = {
|
|
|
|
name: expectedArg.fieldName,
|
|
|
|
value: providedArg
|
|
|
|
};
|
2017-07-17 12:02:48 -04:00
|
|
|
|
2018-11-08 14:11:30 -05:00
|
|
|
if (expectedArg.fieldName === 'CURRENTMENU') {
|
|
|
|
// In 3.0, the field value of the `sensing_current` block
|
|
|
|
// is in all caps.
|
|
|
|
activeBlock.fields[expectedArg.fieldName].value = providedArg.toUpperCase();
|
|
|
|
if (providedArg === 'day of week') {
|
|
|
|
activeBlock.fields[expectedArg.fieldName].value = 'DAYOFWEEK';
|
|
|
|
}
|
2018-11-08 00:37:49 -05:00
|
|
|
}
|
|
|
|
|
2018-09-07 19:08:44 -04:00
|
|
|
if (expectedArg.fieldName === 'VARIABLE') {
|
2017-07-17 12:02:48 -04:00
|
|
|
// Add `id` property to variable fields
|
2018-09-07 19:08:44 -04:00
|
|
|
activeBlock.fields[expectedArg.fieldName].id = getVariableId(providedArg, Variable.SCALAR_TYPE);
|
|
|
|
} else if (expectedArg.fieldName === 'LIST') {
|
|
|
|
// Add `id` property to variable fields
|
|
|
|
activeBlock.fields[expectedArg.fieldName].id = getVariableId(providedArg, Variable.LIST_TYPE);
|
2017-12-01 10:29:32 -05:00
|
|
|
} else if (expectedArg.fieldName === 'BROADCAST_OPTION') {
|
2017-12-15 14:00:53 -05:00
|
|
|
// Add the name in this field to the broadcast msg name map.
|
|
|
|
// Also need to provide the fields[fieldName] object,
|
|
|
|
// so that we can later update its value property, e.g.
|
|
|
|
// if sb2 message name is empty string, we will later
|
|
|
|
// replace this field's value with messageN
|
|
|
|
// once we can traverse through all the existing message names
|
|
|
|
// and come up with a fresh messageN.
|
|
|
|
const broadcastId = addBroadcastMsg(providedArg, activeBlock.fields[expectedArg.fieldName]);
|
|
|
|
activeBlock.fields[expectedArg.fieldName].id = broadcastId;
|
2017-07-17 12:02:48 -04:00
|
|
|
}
|
2017-11-13 14:24:30 -05:00
|
|
|
const varType = expectedArg.variableType;
|
|
|
|
if (typeof varType === 'string') {
|
|
|
|
activeBlock.fields[expectedArg.fieldName].variableType = varType;
|
|
|
|
}
|
2016-08-31 13:56:05 -04:00
|
|
|
}
|
|
|
|
}
|
2017-12-28 14:52:06 -05:00
|
|
|
|
2018-01-04 10:24:13 -05:00
|
|
|
// Updates for blocks that have new menus (e.g. in Looks)
|
|
|
|
switch (oldOpcode) {
|
|
|
|
case 'comeToFront':
|
2017-12-28 14:52:06 -05:00
|
|
|
activeBlock.fields.FRONT_BACK = {
|
|
|
|
name: 'FRONT_BACK',
|
|
|
|
value: 'front'
|
|
|
|
};
|
2018-01-04 10:24:13 -05:00
|
|
|
break;
|
|
|
|
case 'goBackByLayers:':
|
2017-12-28 14:52:06 -05:00
|
|
|
activeBlock.fields.FORWARD_BACKWARD = {
|
|
|
|
name: 'FORWARD_BACKWARD',
|
|
|
|
value: 'backward'
|
|
|
|
};
|
2018-01-04 10:24:13 -05:00
|
|
|
break;
|
|
|
|
case 'backgroundIndex':
|
|
|
|
activeBlock.fields.NUMBER_NAME = {
|
|
|
|
name: 'NUMBER_NAME',
|
|
|
|
value: 'number'
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case 'sceneName':
|
|
|
|
activeBlock.fields.NUMBER_NAME = {
|
|
|
|
name: 'NUMBER_NAME',
|
|
|
|
value: 'name'
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case 'costumeIndex':
|
|
|
|
activeBlock.fields.NUMBER_NAME = {
|
|
|
|
name: 'NUMBER_NAME',
|
|
|
|
value: 'number'
|
|
|
|
};
|
|
|
|
break;
|
2018-06-12 18:27:35 -04:00
|
|
|
case 'costumeName':
|
|
|
|
activeBlock.fields.NUMBER_NAME = {
|
|
|
|
name: 'NUMBER_NAME',
|
|
|
|
value: 'name'
|
|
|
|
};
|
|
|
|
break;
|
2017-12-28 14:52:06 -05:00
|
|
|
}
|
|
|
|
|
2016-10-03 17:43:24 -04:00
|
|
|
// Special cases to generate mutations.
|
2016-10-23 17:55:31 -04:00
|
|
|
if (oldOpcode === 'stopScripts') {
|
2016-10-13 13:11:26 -04:00
|
|
|
// Mutation for stop block: if the argument is 'other scripts',
|
|
|
|
// the block needs a next connection.
|
2016-10-23 17:55:31 -04:00
|
|
|
if (sb2block[1] === 'other scripts in sprite' ||
|
|
|
|
sb2block[1] === 'other scripts in stage') {
|
2016-10-13 13:11:26 -04:00
|
|
|
activeBlock.mutation = {
|
|
|
|
tagName: 'mutation',
|
|
|
|
hasnext: 'true',
|
|
|
|
children: []
|
|
|
|
};
|
|
|
|
}
|
2016-10-23 17:55:31 -04:00
|
|
|
} else if (oldOpcode === 'procDef') {
|
2016-10-13 13:11:26 -04:00
|
|
|
// Mutation for procedure definition:
|
|
|
|
// store all 2.0 proc data.
|
2017-04-17 15:10:04 -04:00
|
|
|
const procData = sb2block.slice(1);
|
2017-10-06 15:34:32 -04:00
|
|
|
// Create a new block and input metadata.
|
|
|
|
const inputUid = uid();
|
|
|
|
const inputName = 'custom_block';
|
|
|
|
activeBlock.inputs[inputName] = {
|
|
|
|
name: inputName,
|
|
|
|
block: inputUid,
|
|
|
|
shadow: inputUid
|
2016-10-13 13:11:26 -04:00
|
|
|
};
|
2017-10-06 15:34:32 -04:00
|
|
|
activeBlock.children = [{
|
|
|
|
id: inputUid,
|
2017-11-16 14:17:08 -05:00
|
|
|
opcode: 'procedures_prototype',
|
2017-10-06 15:34:32 -04:00
|
|
|
inputs: {},
|
|
|
|
fields: {},
|
|
|
|
next: null,
|
|
|
|
shadow: true,
|
|
|
|
children: [],
|
|
|
|
mutation: {
|
|
|
|
tagName: 'mutation',
|
|
|
|
proccode: procData[0], // e.g., "abc %n %b %s"
|
|
|
|
argumentnames: JSON.stringify(procData[1]), // e.g. ['arg1', 'arg2']
|
2017-11-02 17:03:12 -04:00
|
|
|
argumentids: JSON.stringify(parseProcedureArgIds(procData[0])),
|
2017-10-06 15:34:32 -04:00
|
|
|
argumentdefaults: JSON.stringify(procData[2]), // e.g., [1, 'abc']
|
|
|
|
warp: procData[3], // Warp mode, e.g., true/false.
|
|
|
|
children: []
|
|
|
|
}
|
|
|
|
}];
|
2016-10-23 17:55:31 -04:00
|
|
|
} else if (oldOpcode === 'call') {
|
2016-10-13 13:11:26 -04:00
|
|
|
// Mutation for procedure call:
|
|
|
|
// string for proc code (e.g., "abc %n %b %s").
|
|
|
|
activeBlock.mutation = {
|
|
|
|
tagName: 'mutation',
|
|
|
|
children: [],
|
2017-11-02 17:03:12 -04:00
|
|
|
proccode: sb2block[1],
|
|
|
|
argumentids: JSON.stringify(parseProcedureArgIds(sb2block[1]))
|
2016-10-13 13:11:26 -04:00
|
|
|
};
|
2016-10-23 17:55:31 -04:00
|
|
|
} else if (oldOpcode === 'getParam') {
|
2018-09-06 18:35:18 -04:00
|
|
|
let returnCode = sb2block[2];
|
|
|
|
|
|
|
|
// Ensure the returnCode is "b" if used in a boolean input.
|
2018-09-13 12:42:54 -04:00
|
|
|
if (parentExpectedArg && parentExpectedArg.inputOp === 'boolean' && returnCode !== 'b') {
|
2018-09-06 18:35:18 -04:00
|
|
|
returnCode = 'b';
|
|
|
|
}
|
|
|
|
|
2017-11-16 14:17:08 -05:00
|
|
|
// Assign correct opcode based on the block shape.
|
2018-09-06 18:35:18 -04:00
|
|
|
switch (returnCode) {
|
2017-11-16 14:17:08 -05:00
|
|
|
case 'r':
|
|
|
|
activeBlock.opcode = 'argument_reporter_string_number';
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
activeBlock.opcode = 'argument_reporter_boolean';
|
|
|
|
break;
|
|
|
|
}
|
2016-10-03 17:43:24 -04:00
|
|
|
}
|
2018-05-21 11:30:22 -04:00
|
|
|
return [activeBlock, commentIndex];
|
2016-10-23 17:55:31 -04:00
|
|
|
};
|
2016-08-31 13:56:05 -04:00
|
|
|
|
2016-12-30 10:19:58 -05:00
|
|
|
module.exports = {
|
|
|
|
deserialize: sb2import
|
|
|
|
};
|