|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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'); |
|
const StringUtil = require('../util/string-util'); |
|
const MathUtil = require('../util/math-util'); |
|
const specMap = require('./sb2_specmap'); |
|
const Comment = require('../engine/comment'); |
|
const Variable = require('../engine/variable'); |
|
const MonitorRecord = require('../engine/monitor-record'); |
|
const StageLayering = require('../engine/stage-layering'); |
|
const ScratchXUtilities = require('../extension-support/tw-scratchx-utilities'); |
|
|
|
const {loadCostume} = require('../import/load-costume.js'); |
|
const {loadSound} = require('../import/load-sound.js'); |
|
const {deserializeCostume, deserializeSound} = require('./deserialize-assets.js'); |
|
|
|
|
|
const CORE_EXTENSIONS = [ |
|
'argument', |
|
'control', |
|
'data', |
|
'event', |
|
'looks', |
|
'math', |
|
'motion', |
|
'operator', |
|
'procedures', |
|
'sensing', |
|
'sound' |
|
]; |
|
|
|
|
|
|
|
|
|
const WORKSPACE_X_SCALE = 1.5; |
|
const WORKSPACE_Y_SCALE = 2.2; |
|
|
|
|
|
|
|
|
|
const SCRATCHX_OPCODE_SEPARATOR = /\u001f|\./; |
|
|
|
|
|
|
|
|
|
|
|
const isPossiblyScratchXBlock = opcode => SCRATCHX_OPCODE_SEPARATOR.test(opcode); |
|
|
|
|
|
|
|
|
|
|
|
const mapScratchXOpcode = opcode => { |
|
const [extensionName, extensionMethod] = opcode.split(SCRATCHX_OPCODE_SEPARATOR); |
|
const newOpcodeBase = ScratchXUtilities.generateExtensionId(extensionName); |
|
return `${newOpcodeBase}_${extensionMethod}`; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const mapScratchXBlock = block => { |
|
const opcode = block[0]; |
|
const argumentCount = block.length - 1; |
|
const args = []; |
|
for (let i = 0; i < argumentCount; i++) { |
|
args.push({ |
|
type: 'input', |
|
inputOp: 'text', |
|
inputName: ScratchXUtilities.argumentIndexToId(i) |
|
}); |
|
} |
|
return { |
|
opcode: mapScratchXOpcode(opcode), |
|
argMap: args |
|
}; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const parseProcedureArgMap = function (procCode) { |
|
const argMap = [ |
|
{} |
|
]; |
|
const INPUT_PREFIX = 'input'; |
|
let inputCount = 0; |
|
|
|
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'; |
|
} else if (argType === 'b') { |
|
arg.inputOp = 'boolean'; |
|
} |
|
argMap.push(arg); |
|
} |
|
} |
|
return argMap; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const parseProcedureArgIds = function (procCode) { |
|
return parseProcedureArgMap(procCode) |
|
.map(arg => arg.inputName) |
|
.filter(name => name); |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const parseBlockList = function (blockList, addBroadcastMsg, getVariableId, extensions, parseState, comments, |
|
commentIndex) { |
|
const resultingList = []; |
|
let previousBlock = null; |
|
for (let i = 0; i < blockList.length; i++) { |
|
const block = blockList[i]; |
|
|
|
const parsedBlockAndComments = parseBlock(block, addBroadcastMsg, getVariableId, |
|
extensions, parseState, comments, commentIndex); |
|
const parsedBlock = parsedBlockAndComments[0]; |
|
|
|
commentIndex = parsedBlockAndComments[1]; |
|
|
|
if (!parsedBlock) continue; |
|
if (previousBlock) { |
|
parsedBlock.parent = previousBlock.id; |
|
previousBlock.next = parsedBlock.id; |
|
} |
|
previousBlock = parsedBlock; |
|
resultingList.push(parsedBlock); |
|
} |
|
return [resultingList, commentIndex]; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const parseScripts = function (scripts, blocks, addBroadcastMsg, getVariableId, extensions, comments) { |
|
|
|
|
|
let scriptIndexForComment = 0; |
|
|
|
for (let i = 0; i < scripts.length; i++) { |
|
const script = scripts[i]; |
|
const scriptX = script[0]; |
|
const scriptY = script[1]; |
|
const blockList = script[2]; |
|
const parseState = {}; |
|
const [parsedBlockList, newCommentIndex] = parseBlockList(blockList, addBroadcastMsg, getVariableId, extensions, |
|
parseState, comments, scriptIndexForComment); |
|
scriptIndexForComment = newCommentIndex; |
|
if (parsedBlockList[0]) { |
|
parsedBlockList[0].x = scriptX * WORKSPACE_X_SCALE; |
|
parsedBlockList[0].y = scriptY * WORKSPACE_Y_SCALE; |
|
parsedBlockList[0].topLevel = true; |
|
parsedBlockList[0].parent = null; |
|
} |
|
|
|
const convertedBlocks = flatten(parsedBlockList); |
|
for (let j = 0; j < convertedBlocks.length; j++) { |
|
blocks.createBlock(convertedBlocks[j]); |
|
} |
|
} |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const generateVariableIdGetter = (function () { |
|
let globalVariableNameMap = {}; |
|
const namer = (targetId, name, type) => `${targetId}-${StringUtil.replaceUnsafeChars(name)}-${type}`; |
|
return function (targetId, topLevel) { |
|
|
|
if (topLevel) globalVariableNameMap = {}; |
|
return function (name, type) { |
|
if (topLevel) { |
|
globalVariableNameMap[`${name}-${type}`] = namer(targetId, name, type); |
|
return globalVariableNameMap[`${name}-${type}`]; |
|
} |
|
|
|
if (globalVariableNameMap[`${name}-${type}`]) return globalVariableNameMap[`${name}-${type}`]; |
|
return namer(targetId, name, type); |
|
}; |
|
}; |
|
}()); |
|
|
|
const globalBroadcastMsgStateGenerator = (function () { |
|
let broadcastMsgNameMap = {}; |
|
const allBroadcastFields = []; |
|
const emptyStringName = uid(); |
|
return function (topLevel) { |
|
if (topLevel) broadcastMsgNameMap = {}; |
|
return { |
|
broadcastMsgMapUpdater: function (name, field) { |
|
name = name.toLowerCase(); |
|
if (name === '') { |
|
name = emptyStringName; |
|
} |
|
broadcastMsgNameMap[name] = `broadcastMsgId-${StringUtil.replaceUnsafeChars(name)}`; |
|
allBroadcastFields.push(field); |
|
return broadcastMsgNameMap[name]; |
|
}, |
|
globalBroadcastMsgs: broadcastMsgNameMap, |
|
allBroadcastFields: allBroadcastFields, |
|
emptyMsgName: emptyStringName |
|
}; |
|
}; |
|
}()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const parseMonitorObject = (object, runtime, targets, extensions) => { |
|
|
|
|
|
const mapped = specMap[object.cmd]; |
|
if (!mapped) { |
|
log.warn(`Could not find monitor block with opcode: ${object.cmd}`); |
|
return; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
const opcode = specMap[object.cmd].opcode; |
|
const extIndex = opcode.indexOf('_'); |
|
const extID = opcode.substring(0, extIndex); |
|
|
|
if (extID === 'videoSensing') { |
|
return; |
|
} else if (CORE_EXTENSIONS.indexOf(extID) === -1 && extID !== '' && |
|
!extensions.extensionIDs.has(extID) && !object.visible) { |
|
|
|
|
|
|
|
|
|
return; |
|
} |
|
|
|
let target = null; |
|
|
|
|
|
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; |
|
object.target = currTarget.getName(); |
|
} |
|
} |
|
} |
|
|
|
|
|
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'); |
|
|
|
|
|
const getVariableId = generateVariableIdGetter(target.id, false); |
|
|
|
const [block, _] = parseBlock( |
|
[object.cmd, object.param], |
|
null, |
|
getVariableId, |
|
extensions, |
|
{}, |
|
null, |
|
null |
|
); |
|
|
|
|
|
|
|
|
|
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); |
|
} else if (runtime.monitorBlockInfo.hasOwnProperty(block.opcode)) { |
|
block.id = runtime.monitorBlockInfo[block.opcode].getId(target.id, block.fields); |
|
} else { |
|
|
|
|
|
|
|
|
|
block.id = block.opcode; |
|
} |
|
|
|
|
|
block.targetId = target.isStage ? null : target.id; |
|
|
|
|
|
block.isMonitored = object.visible; |
|
|
|
const existingMonitorBlock = runtime.monitorBlocks._blocks[block.id]; |
|
if (existingMonitorBlock) { |
|
|
|
|
|
|
|
existingMonitorBlock.isMonitored = object.visible; |
|
existingMonitorBlock.targetId = block.targetId; |
|
} else { |
|
|
|
const newBlocks = flatten([block]); |
|
for (let i = 0; i < newBlocks.length; i++) { |
|
runtime.monitorBlocks.createBlock(newBlocks[i]); |
|
} |
|
} |
|
|
|
|
|
switch (object.mode) { |
|
case 1: |
|
object.mode = 'default'; |
|
break; |
|
case 2: |
|
object.mode = 'large'; |
|
break; |
|
case 3: |
|
object.mode = 'slider'; |
|
break; |
|
} |
|
|
|
|
|
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, |
|
isDiscrete: object.isDiscrete, |
|
x: object.x, |
|
y: object.y, |
|
width: object.width, |
|
height: object.height, |
|
visible: object.visible |
|
})); |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const parseScratchAssets = function (object, runtime, topLevel, zip) { |
|
if (!object.hasOwnProperty('objName')) { |
|
|
|
return null; |
|
} |
|
|
|
const assets = { |
|
costumePromises: [], |
|
soundPromises: [], |
|
soundBank: runtime.audioEngine && runtime.audioEngine.createBank(), |
|
children: [] |
|
}; |
|
|
|
|
|
const costumePromises = assets.costumePromises; |
|
if (object.hasOwnProperty('costumes')) { |
|
for (let i = 0; i < object.costumes.length; i++) { |
|
const costumeSource = object.costumes[i]; |
|
const bitmapResolution = costumeSource.bitmapResolution || 1; |
|
const costume = { |
|
name: costumeSource.costumeName, |
|
bitmapResolution: bitmapResolution, |
|
rotationCenterX: topLevel ? 240 * bitmapResolution : costumeSource.rotationCenterX, |
|
rotationCenterY: topLevel ? 180 * bitmapResolution : costumeSource.rotationCenterY, |
|
|
|
|
|
|
|
|
|
md5: costumeSource.baseLayerMD5, |
|
skinId: null |
|
}; |
|
const md5ext = costumeSource.baseLayerMD5; |
|
const idParts = StringUtil.splitFirst(md5ext, '.'); |
|
const md5 = idParts[0]; |
|
let ext; |
|
if (idParts.length === 2 && idParts[1]) { |
|
ext = idParts[1]; |
|
} else { |
|
|
|
ext = 'png'; |
|
|
|
costume.md5 = `${costume.md5}.${ext}`; |
|
} |
|
costume.dataFormat = ext; |
|
costume.assetId = md5; |
|
if (costumeSource.textLayerMD5) { |
|
costume.textLayerMD5 = StringUtil.splitFirst(costumeSource.textLayerMD5, '.')[0]; |
|
} |
|
|
|
|
|
|
|
const assetFileName = `${costumeSource.baseLayerID}.${ext}`; |
|
const textLayerFileName = costumeSource.textLayerID ? `${costumeSource.textLayerID}.png` : null; |
|
costumePromises.push(deserializeCostume(costume, runtime, zip, assetFileName, textLayerFileName) |
|
.then(() => loadCostume(costume.md5, costume, runtime, 2 )) |
|
); |
|
} |
|
} |
|
|
|
const {soundBank, soundPromises} = assets; |
|
if (object.hasOwnProperty('sounds')) { |
|
for (let s = 0; s < object.sounds.length; s++) { |
|
const soundSource = object.sounds[s]; |
|
const sound = { |
|
name: soundSource.soundName, |
|
format: soundSource.format, |
|
rate: soundSource.rate, |
|
sampleCount: soundSource.sampleCount, |
|
|
|
|
|
|
|
|
|
|
|
|
|
md5: soundSource.md5, |
|
data: null |
|
}; |
|
const md5ext = soundSource.md5; |
|
const idParts = StringUtil.splitFirst(md5ext, '.'); |
|
const md5 = idParts[0]; |
|
const ext = idParts[1].toLowerCase(); |
|
sound.dataFormat = ext; |
|
sound.assetId = md5; |
|
|
|
|
|
|
|
|
|
const assetFileName = `${soundSource.soundID}.${ext}`; |
|
soundPromises.push( |
|
deserializeSound(sound, runtime, zip, assetFileName) |
|
.then(() => loadSound(sound, runtime, soundBank)) |
|
); |
|
} |
|
} |
|
|
|
|
|
const childrenAssets = assets.children; |
|
if (object.children) { |
|
for (let m = 0; m < object.children.length; m++) { |
|
childrenAssets.push(parseScratchAssets(object.children[m], runtime, false, zip)); |
|
} |
|
} |
|
|
|
return assets; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const parseScratchObject = function (object, runtime, extensions, topLevel, zip, assets) { |
|
if (!object.hasOwnProperty('objName')) { |
|
if (object.hasOwnProperty('listName')) { |
|
|
|
object.cmd = 'contentsOfList:'; |
|
object.param = object.listName; |
|
object.mode = 'list'; |
|
} |
|
|
|
object.deferredMonitor = true; |
|
return Promise.resolve(object); |
|
} |
|
|
|
|
|
const blocks = new Blocks(runtime); |
|
|
|
const sprite = new Sprite(blocks, runtime); |
|
|
|
if (object.hasOwnProperty('objName')) { |
|
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; |
|
} |
|
|
|
const costumePromises = assets.costumePromises; |
|
|
|
const {soundBank, soundPromises} = assets; |
|
|
|
|
|
const target = sprite.createClone(topLevel ? StageLayering.BACKGROUND_LAYER : StageLayering.SPRITE_LAYER); |
|
|
|
const getVariableId = generateVariableIdGetter(target.id, topLevel); |
|
|
|
const globalBroadcastMsgObj = globalBroadcastMsgStateGenerator(topLevel); |
|
const addBroadcastMsg = globalBroadcastMsgObj.broadcastMsgMapUpdater; |
|
|
|
|
|
if (object.hasOwnProperty('variables')) { |
|
for (let j = 0; j < object.variables.length; j++) { |
|
const variable = object.variables[j]; |
|
|
|
|
|
|
|
|
|
const isCloud = variable.isPersistent && topLevel && runtime.canAddCloudVariable(); |
|
const newVariable = new Variable( |
|
getVariableId(variable.name, Variable.SCALAR_TYPE), |
|
variable.name, |
|
Variable.SCALAR_TYPE, |
|
isCloud |
|
); |
|
if (isCloud) runtime.addCloudVariable(); |
|
newVariable.value = variable.value; |
|
target.variables[newVariable.id] = newVariable; |
|
} |
|
} |
|
|
|
|
|
|
|
const blockComments = {}; |
|
if (object.hasOwnProperty('scriptComments')) { |
|
const comments = object.scriptComments.map(commentDesc => { |
|
const [ |
|
commentX, |
|
commentY, |
|
commentWidth, |
|
commentHeight, |
|
commentFullSize, |
|
flattenedBlockIndex, |
|
commentText |
|
] = commentDesc; |
|
const isBlockComment = commentDesc[5] >= 0; |
|
const newComment = new Comment( |
|
null, |
|
commentText, |
|
|
|
|
|
isBlockComment ? null : commentX * WORKSPACE_X_SCALE, |
|
isBlockComment ? null : commentY * WORKSPACE_Y_SCALE, |
|
commentWidth * WORKSPACE_X_SCALE, |
|
commentHeight * WORKSPACE_Y_SCALE, |
|
!commentFullSize |
|
); |
|
if (isBlockComment) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
newComment.blockId = flattenedBlockIndex; |
|
|
|
|
|
if (blockComments.hasOwnProperty(flattenedBlockIndex)) { |
|
blockComments[flattenedBlockIndex].push(newComment); |
|
} else { |
|
blockComments[flattenedBlockIndex] = [newComment]; |
|
} |
|
} |
|
return newComment; |
|
}); |
|
|
|
|
|
|
|
comments.forEach(comment => { |
|
target.comments[comment.id] = comment; |
|
}); |
|
} |
|
|
|
|
|
if (object.hasOwnProperty('scripts')) { |
|
parseScripts(object.scripts, blocks, addBroadcastMsg, getVariableId, extensions, blockComments); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (const commentIndex in blockComments) { |
|
const currBlockComments = blockComments[commentIndex]; |
|
currBlockComments.forEach(c => { |
|
if (typeof c.blockId === 'number') { |
|
c.blockId = null; |
|
} |
|
}); |
|
} |
|
|
|
|
|
blocks.updateTargetSpecificBlocks(topLevel); |
|
|
|
if (object.hasOwnProperty('lists')) { |
|
for (let k = 0; k < object.lists.length; k++) { |
|
const list = object.lists[k]; |
|
const newVariable = new Variable( |
|
getVariableId(list.listName, Variable.LIST_TYPE), |
|
list.listName, |
|
Variable.LIST_TYPE, |
|
false |
|
); |
|
newVariable.value = list.contents; |
|
target.variables[newVariable.id] = newVariable; |
|
} |
|
} |
|
if (object.hasOwnProperty('scratchX')) { |
|
target.x = object.scratchX; |
|
} |
|
if (object.hasOwnProperty('scratchY')) { |
|
target.y = object.scratchY; |
|
} |
|
if (object.hasOwnProperty('direction')) { |
|
target.direction = object.direction; |
|
} |
|
if (object.hasOwnProperty('isDraggable')) { |
|
target.draggable = object.isDraggable; |
|
} |
|
if (object.hasOwnProperty('scale')) { |
|
|
|
target.size = object.scale * 100; |
|
} |
|
if (object.hasOwnProperty('visible')) { |
|
target.visible = object.visible; |
|
} |
|
if (object.hasOwnProperty('currentCostumeIndex')) { |
|
|
|
|
|
|
|
target.currentCostume = MathUtil.clamp(Math.floor(object.currentCostumeIndex), 0, object.costumes.length - 1); |
|
} |
|
if (object.hasOwnProperty('rotationStyle')) { |
|
if (object.rotationStyle === 'none') { |
|
target.rotationStyle = RenderedTarget.ROTATION_STYLE_NONE; |
|
} else if (object.rotationStyle === 'leftRight') { |
|
target.rotationStyle = RenderedTarget.ROTATION_STYLE_LEFT_RIGHT; |
|
} else if (object.rotationStyle === 'upDown') { |
|
target.rotationStyle = RenderedTarget.ROTATION_STYLE_UP_DOWN; |
|
} else if (object.rotationStyle === 'lookAt') { |
|
target.rotationStyle = RenderedTarget.ROTATION_STYLE_LOOK_AT; |
|
} else if (object.rotationStyle === 'normal') { |
|
target.rotationStyle = RenderedTarget.ROTATION_STYLE_ALL_AROUND; |
|
} |
|
} |
|
if (object.hasOwnProperty('tempoBPM')) { |
|
target.tempo = object.tempoBPM; |
|
} |
|
if (object.hasOwnProperty('videoAlpha')) { |
|
|
|
|
|
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; |
|
} else { |
|
target.videoState = RenderedTarget.VIDEO_STATE.OFF; |
|
} |
|
} |
|
} |
|
if (object.hasOwnProperty('indexInLibrary')) { |
|
|
|
|
|
|
|
target.targetPaneOrder = object.indexInLibrary; |
|
} |
|
|
|
target.isStage = topLevel; |
|
|
|
Promise.all(costumePromises).then(costumes => { |
|
sprite.costumes = costumes; |
|
}); |
|
|
|
Promise.all(soundPromises).then(sounds => { |
|
sprite.sounds = sounds; |
|
|
|
sprite.soundBank = soundBank || null; |
|
}); |
|
|
|
|
|
const childrenPromises = []; |
|
if (object.children) { |
|
for (let m = 0; m < object.children.length; m++) { |
|
childrenPromises.push( |
|
parseScratchObject(object.children[m], runtime, extensions, false, zip, assets.children[m]) |
|
); |
|
} |
|
} |
|
|
|
|
|
if (topLevel) { |
|
const savedExtensions = object.info && object.info.savedExtensions; |
|
if (Array.isArray(savedExtensions)) { |
|
for (const extension of savedExtensions) { |
|
const id = ScratchXUtilities.generateExtensionId(extension.extensionName); |
|
const url = extension.javascriptURL; |
|
extensions.extensionURLs.set(id, url); |
|
} |
|
} |
|
} |
|
|
|
return Promise.all( |
|
costumePromises.concat(soundPromises) |
|
).then(() => |
|
Promise.all( |
|
childrenPromises |
|
).then(children => { |
|
|
|
|
|
if (target.isStage) { |
|
const allBroadcastMsgs = globalBroadcastMsgObj.globalBroadcastMsgs; |
|
const allBroadcastMsgFields = globalBroadcastMsgObj.allBroadcastFields; |
|
const oldEmptyMsgName = globalBroadcastMsgObj.emptyMsgName; |
|
if (allBroadcastMsgs[oldEmptyMsgName]) { |
|
|
|
let currIndex = 1; |
|
while (allBroadcastMsgs[`message${currIndex}`]) { |
|
currIndex += 1; |
|
} |
|
const newEmptyMsgName = `message${currIndex}`; |
|
|
|
|
|
|
|
allBroadcastMsgs[newEmptyMsgName] = allBroadcastMsgs[oldEmptyMsgName]; |
|
delete allBroadcastMsgs[oldEmptyMsgName]; |
|
|
|
|
|
for (let i = 0; i < allBroadcastMsgFields.length; i++) { |
|
if (allBroadcastMsgFields[i].value === '') { |
|
allBroadcastMsgFields[i].value = newEmptyMsgName; |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
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; |
|
} |
|
} |
|
let targets = [target]; |
|
const deferredMonitors = []; |
|
for (let n = 0; n < children.length; n++) { |
|
if (children[n]) { |
|
if (children[n].deferredMonitor) { |
|
deferredMonitors.push(children[n]); |
|
} else { |
|
targets = targets.concat(children[n]); |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
for (let n = 0; n < deferredMonitors.length; n++) { |
|
parseMonitorObject(deferredMonitors[n], runtime, targets, extensions); |
|
} |
|
return targets; |
|
}) |
|
); |
|
}; |
|
|
|
const reorderParsedTargets = function (targets) { |
|
|
|
|
|
|
|
const reordered = targets.map((t, index) => { |
|
t.layerOrder = index; |
|
return t; |
|
}).sort((a, b) => a.targetPaneOrder - b.targetPaneOrder); |
|
|
|
|
|
reordered.forEach(t => { |
|
delete t.targetPaneOrder; |
|
}); |
|
|
|
return reordered; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const sb2import = function (json, runtime, optForceSprite, zip) { |
|
const extensions = { |
|
extensionIDs: new Set(), |
|
extensionURLs: new Map() |
|
}; |
|
return Promise.resolve(parseScratchAssets(json, runtime, !optForceSprite, zip)) |
|
|
|
|
|
.then(assets => Promise.resolve(assets)) |
|
.then(assets => ( |
|
parseScratchObject(json, runtime, extensions, !optForceSprite, zip, assets) |
|
)) |
|
.then(reorderParsedTargets) |
|
.then(targets => ({ |
|
targets, |
|
extensions |
|
})); |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
const specMapBlock = function (block) { |
|
const opcode = block[0]; |
|
const mapped = opcode && specMap[opcode]; |
|
if (!mapped) { |
|
if (opcode && isPossiblyScratchXBlock(opcode)) { |
|
return mapScratchXBlock(block); |
|
} |
|
log.warn(`Couldn't find SB2 block: ${opcode}`); |
|
return null; |
|
} |
|
if (typeof mapped === 'function') { |
|
return mapped(block); |
|
} |
|
return mapped; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const parseBlock = function (sb2block, addBroadcastMsg, getVariableId, extensions, parseState, comments, commentIndex) { |
|
const commentsForParsedBlock = (comments && typeof commentIndex === 'number' && !isNaN(commentIndex)) ? |
|
comments[commentIndex] : null; |
|
const blockMetadata = specMapBlock(sb2block); |
|
if (!blockMetadata) { |
|
|
|
|
|
|
|
if (commentsForParsedBlock) { |
|
commentsForParsedBlock.forEach(comment => { |
|
comment.blockId = null; |
|
comment.x = comment.y = 0; |
|
}); |
|
} |
|
return [null, commentIndex + 1]; |
|
} |
|
const oldOpcode = sb2block[0]; |
|
|
|
|
|
const index = blockMetadata.opcode.indexOf('_'); |
|
const prefix = blockMetadata.opcode.substring(0, index); |
|
if (CORE_EXTENSIONS.indexOf(prefix) === -1) { |
|
if (prefix !== '') extensions.extensionIDs.add(prefix); |
|
} |
|
|
|
|
|
const activeBlock = { |
|
id: uid(), |
|
opcode: blockMetadata.opcode, |
|
inputs: {}, |
|
fields: {}, |
|
next: null, |
|
shadow: false, |
|
children: [] |
|
}; |
|
|
|
|
|
if (commentsForParsedBlock) { |
|
|
|
activeBlock.comment = commentsForParsedBlock[commentsForParsedBlock.length - 1].id; |
|
commentsForParsedBlock.forEach(comment => { |
|
if (comment.id === activeBlock.comment) { |
|
comment.blockId = activeBlock.id; |
|
} else { |
|
|
|
|
|
comment.blockId = null; |
|
comment.x = comment.y = 0; |
|
} |
|
}); |
|
} |
|
commentIndex++; |
|
|
|
const parentExpectedArg = parseState.expectedArg; |
|
|
|
|
|
if (oldOpcode === 'call') { |
|
blockMetadata.argMap = parseProcedureArgMap(sb2block[1]); |
|
} |
|
|
|
|
|
|
|
for (let i = 0; i < blockMetadata.argMap.length; i++) { |
|
const expectedArg = blockMetadata.argMap[i]; |
|
const providedArg = sb2block[i + 1]; |
|
|
|
let shadowObscured = false; |
|
|
|
if (expectedArg.type === 'input') { |
|
|
|
const inputUid = uid(); |
|
activeBlock.inputs[expectedArg.inputName] = { |
|
name: expectedArg.inputName, |
|
block: null, |
|
shadow: null |
|
}; |
|
if (typeof providedArg === 'object' && providedArg) { |
|
|
|
let innerBlocks; |
|
parseState.expectedArg = expectedArg; |
|
if (typeof providedArg[0] === 'object' && providedArg[0]) { |
|
|
|
[innerBlocks, commentIndex] = parseBlockList(providedArg, addBroadcastMsg, getVariableId, |
|
extensions, parseState, comments, commentIndex); |
|
} else { |
|
|
|
const parsedBlockDesc = parseBlock(providedArg, addBroadcastMsg, getVariableId, extensions, |
|
parseState, comments, commentIndex); |
|
innerBlocks = parsedBlockDesc[0] ? [parsedBlockDesc[0]] : []; |
|
|
|
commentIndex = parsedBlockDesc[1]; |
|
} |
|
parseState.expectedArg = parentExpectedArg; |
|
|
|
|
|
|
|
|
|
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; |
|
} |
|
activeBlock.inputs[expectedArg.inputName].block = ( |
|
innerBlocks[0].id |
|
); |
|
activeBlock.children = ( |
|
activeBlock.children.concat(innerBlocks) |
|
); |
|
} |
|
|
|
|
|
shadowObscured = true; |
|
} |
|
|
|
if (!expectedArg.inputOp) { |
|
|
|
log.warn(`Unknown input operation for input ${expectedArg.inputName} of opcode ${activeBlock.opcode}.`); |
|
continue; |
|
} |
|
if (expectedArg.inputOp === 'boolean' || expectedArg.inputOp === 'substack') { |
|
|
|
continue; |
|
} |
|
|
|
|
|
let fieldValue = providedArg; |
|
|
|
let fieldName = expectedArg.inputName; |
|
if (expectedArg.inputOp === 'math_number' || |
|
expectedArg.inputOp === 'math_whole_number' || |
|
expectedArg.inputOp === 'math_positive_number' || |
|
expectedArg.inputOp === 'math_integer' || |
|
expectedArg.inputOp === 'math_angle') { |
|
fieldName = 'NUM'; |
|
|
|
if (shadowObscured) { |
|
fieldValue = 10; |
|
} |
|
} else if (expectedArg.inputOp === 'text') { |
|
fieldName = 'TEXT'; |
|
if (shadowObscured) { |
|
fieldValue = ''; |
|
} |
|
} else if (expectedArg.inputOp === 'colour_picker') { |
|
|
|
fieldValue = Color.decimalToHex(providedArg); |
|
fieldName = 'COLOUR'; |
|
if (shadowObscured) { |
|
fieldValue = '#990000'; |
|
} |
|
} else if (expectedArg.inputOp === 'event_broadcast_menu') { |
|
fieldName = 'BROADCAST_OPTION'; |
|
if (shadowObscured) { |
|
fieldValue = ''; |
|
} |
|
} else if (expectedArg.inputOp === 'sensing_of_object_menu') { |
|
if (shadowObscured) { |
|
fieldValue = '_stage_'; |
|
} else if (fieldValue === 'Stage') { |
|
fieldValue = '_stage_'; |
|
} |
|
} else if (expectedArg.inputOp === 'note') { |
|
if (shadowObscured) { |
|
fieldValue = 60; |
|
} |
|
} else if (expectedArg.inputOp === 'music.menu.DRUM') { |
|
if (shadowObscured) { |
|
fieldValue = 1; |
|
} |
|
} else if (expectedArg.inputOp === 'music.menu.INSTRUMENT') { |
|
if (shadowObscured) { |
|
fieldValue = 1; |
|
} |
|
} else if (expectedArg.inputOp === 'videoSensing.menu.ATTRIBUTE') { |
|
if (shadowObscured) { |
|
fieldValue = 'motion'; |
|
} |
|
} else if (expectedArg.inputOp === 'videoSensing.menu.SUBJECT') { |
|
if (shadowObscured) { |
|
fieldValue = 'this sprite'; |
|
} |
|
} else if (expectedArg.inputOp === 'videoSensing.menu.VIDEO_STATE') { |
|
if (shadowObscured) { |
|
fieldValue = 'on'; |
|
} |
|
} else if (shadowObscured) { |
|
|
|
fieldValue = ''; |
|
} |
|
const fields = {}; |
|
fields[fieldName] = { |
|
name: fieldName, |
|
value: fieldValue |
|
}; |
|
|
|
|
|
if (expectedArg.inputOp === 'event_broadcast_menu') { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const broadcastId = addBroadcastMsg(fieldValue, fields[fieldName]); |
|
fields[fieldName].id = broadcastId; |
|
fields[fieldName].variableType = expectedArg.variableType; |
|
} |
|
activeBlock.children.push({ |
|
id: inputUid, |
|
opcode: expectedArg.inputOp, |
|
inputs: {}, |
|
fields: fields, |
|
next: null, |
|
topLevel: false, |
|
parent: activeBlock.id, |
|
shadow: true |
|
}); |
|
activeBlock.inputs[expectedArg.inputName].shadow = inputUid; |
|
|
|
if (!activeBlock.inputs[expectedArg.inputName].block) { |
|
activeBlock.inputs[expectedArg.inputName].block = inputUid; |
|
} |
|
} else if (expectedArg.type === 'field') { |
|
|
|
activeBlock.fields[expectedArg.fieldName] = { |
|
name: expectedArg.fieldName, |
|
value: providedArg |
|
}; |
|
|
|
if (expectedArg.fieldName === 'CURRENTMENU') { |
|
|
|
|
|
activeBlock.fields[expectedArg.fieldName].value = providedArg.toUpperCase(); |
|
if (providedArg === 'day of week') { |
|
activeBlock.fields[expectedArg.fieldName].value = 'DAYOFWEEK'; |
|
} |
|
} |
|
|
|
if (expectedArg.fieldName === 'VARIABLE') { |
|
|
|
activeBlock.fields[expectedArg.fieldName].id = getVariableId(providedArg, Variable.SCALAR_TYPE); |
|
} else if (expectedArg.fieldName === 'LIST') { |
|
|
|
activeBlock.fields[expectedArg.fieldName].id = getVariableId(providedArg, Variable.LIST_TYPE); |
|
} else if (expectedArg.fieldName === 'BROADCAST_OPTION') { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const broadcastId = addBroadcastMsg(providedArg, activeBlock.fields[expectedArg.fieldName]); |
|
activeBlock.fields[expectedArg.fieldName].id = broadcastId; |
|
} |
|
const varType = expectedArg.variableType; |
|
if (typeof varType === 'string') { |
|
activeBlock.fields[expectedArg.fieldName].variableType = varType; |
|
} |
|
} |
|
} |
|
|
|
|
|
switch (oldOpcode) { |
|
case 'comeToFront': |
|
activeBlock.fields.FRONT_BACK = { |
|
name: 'FRONT_BACK', |
|
value: 'front' |
|
}; |
|
break; |
|
case 'goBackByLayers:': |
|
activeBlock.fields.FORWARD_BACKWARD = { |
|
name: 'FORWARD_BACKWARD', |
|
value: 'backward' |
|
}; |
|
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; |
|
case 'costumeName': |
|
activeBlock.fields.NUMBER_NAME = { |
|
name: 'NUMBER_NAME', |
|
value: 'name' |
|
}; |
|
break; |
|
} |
|
|
|
|
|
if (oldOpcode === 'stopScripts') { |
|
|
|
|
|
if (sb2block[1] === 'other scripts in sprite' || |
|
sb2block[1] === 'other scripts in stage') { |
|
activeBlock.mutation = { |
|
tagName: 'mutation', |
|
hasnext: 'true', |
|
children: [] |
|
}; |
|
} |
|
} else if (oldOpcode === 'procDef') { |
|
|
|
|
|
const procData = sb2block.slice(1); |
|
|
|
const inputUid = uid(); |
|
const inputName = 'custom_block'; |
|
activeBlock.inputs[inputName] = { |
|
name: inputName, |
|
block: inputUid, |
|
shadow: inputUid |
|
}; |
|
activeBlock.children = [{ |
|
id: inputUid, |
|
opcode: 'procedures_prototype', |
|
inputs: {}, |
|
fields: {}, |
|
next: null, |
|
shadow: true, |
|
children: [], |
|
mutation: { |
|
tagName: 'mutation', |
|
proccode: procData[0], |
|
argumentnames: JSON.stringify(procData[1]), |
|
argumentids: JSON.stringify(parseProcedureArgIds(procData[0])), |
|
argumentdefaults: JSON.stringify(procData[2]), |
|
warp: procData[3], |
|
children: [] |
|
} |
|
}]; |
|
} else if (oldOpcode === 'call') { |
|
|
|
|
|
activeBlock.mutation = { |
|
tagName: 'mutation', |
|
children: [], |
|
proccode: sb2block[1], |
|
argumentids: JSON.stringify(parseProcedureArgIds(sb2block[1])) |
|
}; |
|
} else if (oldOpcode === 'getParam') { |
|
let returnCode = sb2block[2]; |
|
|
|
|
|
if (parentExpectedArg && parentExpectedArg.inputOp === 'boolean' && returnCode !== 'b') { |
|
returnCode = 'b'; |
|
} |
|
|
|
|
|
switch (returnCode) { |
|
case 'r': |
|
activeBlock.opcode = 'argument_reporter_string_number'; |
|
break; |
|
case 'b': |
|
activeBlock.opcode = 'argument_reporter_boolean'; |
|
break; |
|
} |
|
} |
|
return [activeBlock, commentIndex]; |
|
}; |
|
|
|
module.exports = { |
|
deserialize: sb2import |
|
}; |
|
|