|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const Blocks = require('../engine/blocks'); |
|
const Sprite = require('../sprites/sprite'); |
|
const Variable = require('../engine/variable'); |
|
const Comment = require('../engine/comment'); |
|
const MonitorRecord = require('../engine/monitor-record'); |
|
const StageLayering = require('../engine/stage-layering'); |
|
const log = require('../util/log'); |
|
const uid = require('../util/uid'); |
|
const MathUtil = require('../util/math-util'); |
|
const StringUtil = require('../util/string-util'); |
|
const VariableUtil = require('../util/variable-util'); |
|
const Clone = require('../util/clone'); |
|
const compress = require('./tw-compress-sb3'); |
|
const OldExtensions = require('./extension patcher'); |
|
|
|
const {loadCostume} = require('../import/load-costume.js'); |
|
const {loadSound} = require('../import/load-sound.js'); |
|
const {deserializeCostume, deserializeSound} = require('./deserialize-assets.js'); |
|
const replacersPatch = require('./replacers patch.json'); |
|
|
|
const hasOwnProperty = Object.prototype.hasOwnProperty; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const INPUT_SAME_BLOCK_SHADOW = 1; |
|
const INPUT_BLOCK_NO_SHADOW = 2; |
|
const INPUT_DIFF_BLOCK_SHADOW = 3; |
|
|
|
|
|
|
|
const CORE_EXTENSIONS = [ |
|
'argument', |
|
'colour', |
|
'control', |
|
'data', |
|
'event', |
|
'looks', |
|
'math', |
|
'motion', |
|
'operator', |
|
'procedures', |
|
'sensing', |
|
'sound' |
|
]; |
|
|
|
|
|
|
|
|
|
const MATH_NUM_PRIMITIVE = 4; |
|
|
|
const POSITIVE_NUM_PRIMITIVE = 5; |
|
|
|
const WHOLE_NUM_PRIMITIVE = 6; |
|
|
|
const INTEGER_NUM_PRIMITIVE = 7; |
|
|
|
const ANGLE_NUM_PRIMITIVE = 8; |
|
|
|
const COLOR_PICKER_PRIMITIVE = 9; |
|
|
|
const TEXT_PRIMITIVE = 10; |
|
|
|
const BROADCAST_PRIMITIVE = 11; |
|
|
|
const VAR_PRIMITIVE = 12; |
|
|
|
const LIST_PRIMITIVE = 13; |
|
|
|
const LONE_FIELD = 14; |
|
|
|
|
|
|
|
const primitiveOpcodeInfoMap = { |
|
math_number: [MATH_NUM_PRIMITIVE, 'NUM'], |
|
math_positive_number: [POSITIVE_NUM_PRIMITIVE, 'NUM'], |
|
math_whole_number: [WHOLE_NUM_PRIMITIVE, 'NUM'], |
|
math_integer: [INTEGER_NUM_PRIMITIVE, 'NUM'], |
|
math_angle: [ANGLE_NUM_PRIMITIVE, 'NUM'], |
|
colour_picker: [COLOR_PICKER_PRIMITIVE, 'COLOUR'], |
|
text: [TEXT_PRIMITIVE, 'TEXT'], |
|
event_broadcast_menu: [BROADCAST_PRIMITIVE, 'BROADCAST_OPTION'], |
|
data_variable: [VAR_PRIMITIVE, 'VARIABLE'], |
|
data_listcontents: [LIST_PRIMITIVE, 'LIST'] |
|
}; |
|
|
|
|
|
const uniteReplacments = { |
|
'jwUnite_always': 'event_always', |
|
'jwUnite_whenanything': 'event_whenanything', |
|
'jwUnite_getspritewithattrib': 'sensing_getspritewithattrib', |
|
'jwUnite_backToGreenFlag': 'control_backToGreenFlag', |
|
'jwUnite_trueBoolean': 'operator_trueBoolean', |
|
'jwUnite_falseBoolean': 'operator_falseBoolean', |
|
'jwUnite_randomBoolean': 'operator_randomBoolean', |
|
'jwUnite_mobile': 'sensing_mobile', |
|
'jwUnite_thing_is_text': 'sensing_thing_is_text', |
|
'jwUnite_thing_is_number': 'sensing_thing_is_number', |
|
'jwUnite_if_return_else_return': 'control_if_return_else_return', |
|
'jwUnite_indexOfTextInText': 'operator_indexOfTextInText', |
|
'jwUnite_regextest': 'sensing_regextest', |
|
'jwUnite_regexmatch': 'operator_regexmatch', |
|
'jwUnite_replaceAll': 'operator_replaceAll', |
|
'jwUnite_getLettersFromIndexToIndexInText': 'operator_getLettersFromIndexToIndexInText', |
|
'jwUnite_readLineInMultilineText': 'operator_readLineInMultilineText', |
|
'jwUnite_newLine': 'operator_newLine', |
|
'jwUnite_stringify': 'operator_stringify', |
|
'jwUnite_lerpFunc': 'operator_lerpFunc', |
|
'jwUnite_advMath': 'operator_advMath', |
|
'jwUnite_constrainnumber': 'operator_constrainnumber' |
|
}; |
|
|
|
|
|
const ExtensionPatches = { |
|
"griffpatch": {id: 'griffpatch', url: 'https://extensions.turbowarp.org/box2d.js'}, |
|
|
|
"jwUnite": (extensions, object, runtime) => { |
|
extensions.extensionIDs.delete("jwUnite"); |
|
let blocks = object.blocks; |
|
const blockIDs = Object.keys(blocks); |
|
const patcher = extensions.patcher; |
|
|
|
for (let block, idx = 0; idx < blockIDs.length; idx++) { |
|
block = blocks[blockIDs[idx]]; |
|
if (typeof block !== 'object' || Array.isArray(block)) continue; |
|
|
|
if (uniteReplacments[block.opcode]) { |
|
block.opcode = uniteReplacments[block.opcode]; |
|
if (block.opcode === 'sensing_regextest' || block.opcode === 'operator_regexmatch') { |
|
block.inputs.regrule = [ |
|
INPUT_SAME_BLOCK_SHADOW, |
|
[TEXT_PRIMITIVE, "g"] |
|
]; |
|
} |
|
} |
|
|
|
if (block.opcode === 'jwUnite_setReplacer' || block.opcode === 'jwUnite_replaceWithReplacers') { |
|
if (!patcher.loaded.includes('jgJSON')) { |
|
runtime.extensionManager.loadExtensionURL('jgJSON'); |
|
patcher.loaded.push('jgJSON'); |
|
} |
|
blocks = Object.assign(blocks, Clone.simple(replacersPatch.blocks)); |
|
object.variables = Object.assign(object.variables, Clone.simple(replacersPatch.variables)); |
|
const repBlock = block.opcode === 'jwUnite_setReplacer' |
|
? "setReplacerToDisplay" |
|
: "replaceWithReplacersDisplay"; |
|
const replacment = Clone.simple(replacersPatch.blocks[repBlock]); |
|
block.opcode = 'procedures_call'; |
|
block.mutation = replacment.mutation; |
|
} |
|
blocks[blockIDs[idx]] = block; |
|
} |
|
object.blocks = blocks; |
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const serializePrimitiveBlock = function (block) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (hasOwnProperty.call(primitiveOpcodeInfoMap, block.opcode)) { |
|
const primitiveInfo = primitiveOpcodeInfoMap[block.opcode]; |
|
const primitiveConstant = primitiveInfo[0]; |
|
const fieldName = primitiveInfo[1]; |
|
const field = block.fields[fieldName]; |
|
const primitiveDesc = [primitiveConstant, field.value]; |
|
if (block.opcode === 'event_broadcast_menu') { |
|
primitiveDesc.push(field.id); |
|
} else if (block.opcode === 'data_variable' || block.opcode === 'data_listcontents') { |
|
primitiveDesc.push(field.id); |
|
if (block.topLevel) { |
|
primitiveDesc.push(block.x ? Math.round(block.x) : 0); |
|
primitiveDesc.push(block.y ? Math.round(block.y) : 0); |
|
} |
|
} |
|
return primitiveDesc; |
|
} |
|
return null; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const serializeInputs = function (inputs) { |
|
const obj = Object.create(null); |
|
for (const inputName in inputs) { |
|
if (!hasOwnProperty.call(inputs, inputName)) continue; |
|
|
|
if (inputs[inputName].block === inputs[inputName].shadow) { |
|
|
|
obj[inputName] = [ |
|
INPUT_SAME_BLOCK_SHADOW, |
|
inputs[inputName].block |
|
]; |
|
} else if (inputs[inputName].shadow === null) { |
|
|
|
obj[inputName] = [ |
|
INPUT_BLOCK_NO_SHADOW, |
|
inputs[inputName].block |
|
]; |
|
} else { |
|
|
|
obj[inputName] = [ |
|
INPUT_DIFF_BLOCK_SHADOW, |
|
inputs[inputName].block, |
|
inputs[inputName].shadow |
|
]; |
|
} |
|
} |
|
return obj; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
const serializeFields = function (fields) { |
|
const obj = Object.create(null); |
|
for (const fieldName in fields) { |
|
if (!hasOwnProperty.call(fields, fieldName)) continue; |
|
obj[fieldName] = [fields[fieldName].value]; |
|
if (fields[fieldName].hasOwnProperty('id')) { |
|
obj[fieldName].push(fields[fieldName].id); |
|
} |
|
if (fields[fieldName].hasOwnProperty('variableType')) { |
|
obj[fieldName].push(fields[fieldName].variableType); |
|
} |
|
} |
|
return obj; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const serializeBlock = function (block) { |
|
const serializedPrimitive = serializePrimitiveBlock(block); |
|
if (serializedPrimitive) return serializedPrimitive; |
|
|
|
const obj = Object.create(null); |
|
obj.opcode = block.opcode; |
|
|
|
|
|
|
|
obj.next = block.next; |
|
obj.parent = block.parent; |
|
obj.inputs = serializeInputs(block.inputs); |
|
obj.fields = serializeFields(block.fields); |
|
obj.shadow = block.shadow; |
|
if (block.topLevel) { |
|
obj.topLevel = true; |
|
obj.x = block.x ? Math.round(block.x) : 0; |
|
obj.y = block.y ? Math.round(block.y) : 0; |
|
} else { |
|
obj.topLevel = false; |
|
} |
|
if (block.mutation) { |
|
obj.mutation = block.mutation; |
|
} |
|
if (block.comment) { |
|
obj.comment = block.comment; |
|
} |
|
return obj; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const compressInputTree = function (block, blocks) { |
|
|
|
|
|
const serializedInputs = block.inputs; |
|
for (const inputName in serializedInputs) { |
|
|
|
|
|
const currInput = serializedInputs[inputName]; |
|
|
|
|
|
for (let i = 1; i < currInput.length; i++) { |
|
if (!currInput[i]) continue; |
|
const blockOrShadowID = currInput[i]; |
|
|
|
|
|
const blockOrShadow = blocks[blockOrShadowID]; |
|
if (Array.isArray(blockOrShadow)) { |
|
currInput[i] = blockOrShadow; |
|
|
|
delete blocks[blockOrShadowID]; |
|
} |
|
} |
|
} |
|
return block; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getExtensionIdForOpcode = function (opcode) { |
|
|
|
if (!(typeof opcode === 'string')) { |
|
console.error('invalid opcode ' + opcode); |
|
return ''; |
|
} |
|
const index = opcode.indexOf('_'); |
|
const forbiddenSymbols = /[^\w-]/g; |
|
const prefix = opcode.substring(0, index).replace(forbiddenSymbols, '-'); |
|
if (CORE_EXTENSIONS.indexOf(prefix) === -1) { |
|
if (prefix !== '') return prefix; |
|
} |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const getExtensionIDs = runtime => runtime._blockInfo |
|
.map(ext => ext.id) |
|
.filter(ext => runtime.extensionManager.isExtensionLoaded(ext)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
const getExtensionURLsToSave = (extensionIDs, runtime) => { |
|
|
|
if (!runtime.extensionManager) { |
|
return null; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const extensionURLs = runtime.extensionManager.getExtensionURLs(); |
|
const toSave = {}; |
|
for (const extension of extensionIDs) { |
|
const url = extensionURLs[extension]; |
|
if (typeof url === 'string') { |
|
toSave[extension] = url; |
|
} |
|
} |
|
if (Object.keys(toSave).length === 0) { |
|
return null; |
|
} |
|
return toSave; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const serializeBlocks = function (blocks) { |
|
const obj = Object.create(null); |
|
for (const blockID in blocks) { |
|
if (!blocks.hasOwnProperty(blockID)) continue; |
|
obj[blockID] = serializeBlock(blocks[blockID], blocks); |
|
} |
|
|
|
for (const blockID in obj) { |
|
|
|
|
|
const serializedBlock = obj[blockID]; |
|
|
|
|
|
obj[blockID] = compressInputTree(serializedBlock, obj); |
|
|
|
} |
|
|
|
|
|
|
|
for (const blockID in obj) { |
|
const serializedBlock = obj[blockID]; |
|
|
|
|
|
|
|
|
|
|
|
if (Array.isArray(serializedBlock) && |
|
[VAR_PRIMITIVE, LIST_PRIMITIVE].indexOf(serializedBlock[0]) < 0) { |
|
log.warn(`Found an unexpected top level primitive with block ID: ${ |
|
blockID}; deleting it from serialized blocks.`); |
|
delete obj[blockID]; |
|
} |
|
} |
|
return obj; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const deserializeStandaloneBlocks = blocks => { |
|
|
|
blocks = JSON.parse(JSON.stringify(blocks)); |
|
|
|
if (blocks.extensionURLs) { |
|
const extensionURLs = new Map(); |
|
for (const [id, url] of Object.entries(blocks.extensionURLs)) { |
|
extensionURLs.set(id, url); |
|
} |
|
return { |
|
blocks: blocks.blocks, |
|
extensionURLs |
|
}; |
|
} |
|
|
|
|
|
return { |
|
blocks, |
|
extensionURLs: new Map() |
|
}; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
const serializeStandaloneBlocks = (blocks, runtime) => { |
|
const extensionIDs = new Set(getExtensionIDs(runtime)); |
|
const extensionURLs = getExtensionURLsToSave(extensionIDs, runtime); |
|
if (extensionURLs) { |
|
return { |
|
blocks, |
|
|
|
extensionURLs: extensionURLs |
|
}; |
|
} |
|
|
|
|
|
return blocks; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
const serializeCostume = function (costume) { |
|
const obj = Object.create(null); |
|
obj.name = costume.name; |
|
|
|
const costumeToSerialize = costume.broken || costume; |
|
|
|
obj.bitmapResolution = costumeToSerialize.bitmapResolution; |
|
obj.dataFormat = costumeToSerialize.dataFormat.toLowerCase(); |
|
|
|
obj.assetId = costumeToSerialize.assetId; |
|
|
|
|
|
|
|
|
|
|
|
|
|
obj.md5ext = costumeToSerialize.md5; |
|
|
|
obj.rotationCenterX = costumeToSerialize.rotationCenterX; |
|
obj.rotationCenterY = costumeToSerialize.rotationCenterY; |
|
|
|
return obj; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
const serializeSound = function (sound) { |
|
const obj = Object.create(null); |
|
obj.name = sound.name; |
|
|
|
const soundToSerialize = sound.broken || sound; |
|
|
|
obj.assetId = soundToSerialize.assetId; |
|
obj.dataFormat = soundToSerialize.dataFormat.toLowerCase(); |
|
obj.format = soundToSerialize.format; |
|
obj.rate = soundToSerialize.rate; |
|
obj.sampleCount = soundToSerialize.sampleCount; |
|
|
|
|
|
|
|
|
|
|
|
obj.md5ext = soundToSerialize.md5; |
|
return obj; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const isVariableValueSafeForJSON = value => ( |
|
typeof value === 'number' || |
|
typeof value === 'string' || |
|
typeof value === 'boolean' |
|
); |
|
const makeSafeForJSON = (runtime, value) => { |
|
if (Array.isArray(value)) { |
|
let copy = null; |
|
for (let i = 0; i < value.length; i++) { |
|
if (value[i].customId) { |
|
const {serialize} = runtime.serializers[value[i].customId]; |
|
value[i] = serialize(value[i]); |
|
} |
|
if (!isVariableValueSafeForJSON(value[i])) { |
|
if (!copy) { |
|
|
|
copy = value.slice(); |
|
} |
|
copy[i] = `${copy[i]}`; |
|
} |
|
} |
|
if (copy) { |
|
return copy; |
|
} |
|
return value; |
|
} |
|
if (value.customId) { |
|
const {serialize} = runtime.serializers[value.customId]; |
|
return { |
|
customType: true, |
|
typeId: value.customId, |
|
serialized: serialize(value) |
|
}; |
|
} |
|
if (isVariableValueSafeForJSON(value)) { |
|
return value; |
|
} |
|
return `${value}`; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const serializeVariables = function (obj, runtime, variables) { |
|
|
|
|
|
obj.variables = Object.create(null); |
|
obj.lists = Object.create(null); |
|
obj.broadcasts = Object.create(null); |
|
obj.customVars = []; |
|
for (const varId in variables) { |
|
const v = variables[varId]; |
|
|
|
switch (v.type) { |
|
case Variable.BROADCAST_MESSAGE_TYPE: |
|
obj.broadcasts[varId] = v.value; |
|
break; |
|
case Variable.LIST_TYPE: |
|
obj.lists[varId] = [v.name, makeSafeForJSON(runtime, v.value)]; |
|
break; |
|
case Variable.SCALAR_TYPE: |
|
obj.variables[varId] = [v.name, makeSafeForJSON(runtime, v.value)]; |
|
if (v.isCloud) obj.variables[varId].push(true); |
|
break; |
|
default: |
|
const info = v.serialize(); |
|
const variable_object = { |
|
type: v.type, |
|
id: varId, |
|
info, |
|
}; |
|
obj.customVars.push(variable_object); |
|
} |
|
} |
|
}; |
|
|
|
const serializeComments = function (comments) { |
|
const obj = Object.create(null); |
|
for (const commentId in comments) { |
|
if (!comments.hasOwnProperty(commentId)) continue; |
|
const comment = comments[commentId]; |
|
|
|
const serializedComment = Object.create(null); |
|
serializedComment.blockId = comment.blockId; |
|
serializedComment.x = comment.x; |
|
serializedComment.y = comment.y; |
|
serializedComment.width = comment.width; |
|
serializedComment.height = comment.height; |
|
serializedComment.minimized = comment.minimized; |
|
serializedComment.text = comment.text; |
|
|
|
obj[commentId] = serializedComment; |
|
} |
|
return obj; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const serializeTarget = function (runtime, target) { |
|
const obj = Object.create(null); |
|
obj.isStage = target.isStage; |
|
obj.name = obj.isStage ? 'Stage' : target.name; |
|
serializeVariables(obj, runtime, target.variables); |
|
obj.blocks = serializeBlocks(target.blocks); |
|
obj.comments = serializeComments(target.comments); |
|
|
|
|
|
if (target.currentCostume < 0 || target.currentCostume >= target.costumes.length) { |
|
log.warn(`currentCostume property for target ${target.name} is out of range`); |
|
target.currentCostume = MathUtil.clamp(target.currentCostume, 0, target.costumes.length - 1); |
|
} |
|
|
|
obj.currentCostume = target.currentCostume; |
|
obj.costumes = target.costumes.map(serializeCostume); |
|
obj.sounds = target.sounds.map(serializeSound); |
|
obj.id = target.id; |
|
if (target.hasOwnProperty('volume')) obj.volume = target.volume; |
|
if (target.hasOwnProperty('layerOrder')) obj.layerOrder = target.layerOrder; |
|
if (obj.isStage) { |
|
if (target.hasOwnProperty('tempo')) obj.tempo = target.tempo; |
|
if (target.hasOwnProperty('videoTransparency')) obj.videoTransparency = target.videoTransparency; |
|
if (target.hasOwnProperty('videoState')) obj.videoState = target.videoState; |
|
if (target.hasOwnProperty('textToSpeechLanguage')) obj.textToSpeechLanguage = target.textToSpeechLanguage; |
|
} else { |
|
obj.visible = target.visible; |
|
obj.x = target.x; |
|
obj.y = target.y; |
|
obj.size = target.size; |
|
obj.direction = target.direction; |
|
obj.draggable = target.draggable; |
|
obj.rotationStyle = target.rotationStyle; |
|
} |
|
|
|
const extensions = getExtensionIDs(runtime); |
|
const extensionData = {}; |
|
for (let extension of extensions) { |
|
if ( |
|
`ext_${extension}` in runtime && |
|
(typeof runtime[`ext_${extension}`].serializeForTarget === 'function') |
|
) { |
|
extensionData[extension] = runtime[`ext_${extension}`].serializeForTarget(target); |
|
continue; |
|
} |
|
if (extension in (target.extensionStorage ?? {})) { |
|
extensionData[extension] = target.extensionStorage[extension]; |
|
continue; |
|
} |
|
|
|
} |
|
if (extensionData) { |
|
obj.extensionData = extensionData; |
|
} |
|
|
|
return obj; |
|
}; |
|
|
|
const getSimplifiedLayerOrdering = function (targets) { |
|
const layerOrders = targets.map(t => t.getLayerOrder()); |
|
return MathUtil.reducedSortOrdering(layerOrders); |
|
}; |
|
|
|
const serializeMonitors = function (monitors, runtime) { |
|
|
|
const xOffset = (runtime.stageWidth - 480) / 2; |
|
const yOffset = (runtime.stageHeight - 360) / 2; |
|
return monitors.valueSeq() |
|
|
|
|
|
.filter(monitorData => { |
|
const extensionID = getExtensionIdForOpcode(monitorData.opcode); |
|
return !extensionID || monitorData.visible; |
|
}) |
|
.map(monitorData => { |
|
const serializedMonitor = { |
|
id: monitorData.id, |
|
mode: monitorData.mode, |
|
opcode: monitorData.opcode, |
|
params: monitorData.params, |
|
spriteName: monitorData.spriteName, |
|
value: Array.isArray(monitorData.value) ? [] : 0, |
|
width: monitorData.width, |
|
height: monitorData.height, |
|
x: monitorData.x - xOffset, |
|
y: monitorData.y - yOffset, |
|
visible: monitorData.visible, |
|
variableType: monitorData.variableType, |
|
variableId: monitorData.variableId |
|
}; |
|
if (monitorData.mode !== 'list') { |
|
serializedMonitor.sliderMin = monitorData.sliderMin; |
|
serializedMonitor.sliderMax = monitorData.sliderMax; |
|
serializedMonitor.isDiscrete = monitorData.isDiscrete; |
|
} |
|
return serializedMonitor; |
|
}); |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const serialize = function (runtime, targetId, {allowOptimization = true} = {}) { |
|
|
|
const obj = Object.create(null); |
|
|
|
const extensions = getExtensionIDs(runtime); |
|
|
|
const originalTargetsToSerialize = ( |
|
targetId |
|
? [runtime.getTargetById(targetId)] |
|
: runtime.targets.filter(target => target.isOriginal) |
|
); |
|
|
|
const layerOrdering = getSimplifiedLayerOrdering(originalTargetsToSerialize); |
|
|
|
const flattenedOriginalTargets = originalTargetsToSerialize.map(t => t.toJSON()); |
|
|
|
|
|
|
|
if (runtime.renderer && !targetId) { |
|
flattenedOriginalTargets.forEach((t, index) => { |
|
t.layerOrder = layerOrdering[index]; |
|
}); |
|
} |
|
|
|
const serializedTargets = flattenedOriginalTargets.map(t => serializeTarget(runtime, t, extensions)); |
|
const fonts = runtime.fontManager.serializeJSON(); |
|
|
|
if (targetId) { |
|
const target = serializedTargets[0]; |
|
const extensionURLs = getExtensionURLsToSave(extensions, runtime); |
|
target.extensions = extensions; |
|
if (extensionURLs) { |
|
target.extensionURLs = extensionURLs; |
|
} |
|
|
|
|
|
target.extensionData = {}; |
|
for (const extension of extensions) { |
|
if (`ext_${extension}` in runtime && typeof runtime[`ext_${extension}`].serialize === 'function') { |
|
target.extensionData[extension] = runtime[`ext_${extension}`].serialize(); |
|
continue; |
|
} |
|
if (extension in runtime.extensionStorage) { |
|
target.extensionData[extension] = runtime.extensionStorage[extension] |
|
continue; |
|
} |
|
} |
|
|
|
if (fonts) { |
|
target.customFonts = fonts; |
|
} |
|
return target; |
|
} |
|
|
|
obj.targets = serializedTargets; |
|
|
|
obj.monitors = serializeMonitors(runtime.getMonitorState(), runtime); |
|
|
|
|
|
obj.extensionData = {}; |
|
for (const extension of extensions) { |
|
if (`ext_${extension}` in runtime && typeof runtime[`ext_${extension}`].serialize === 'function') { |
|
obj.extensionData[extension] = runtime[`ext_${extension}`].serialize(); |
|
continue; |
|
} |
|
if (extension in runtime.extensionStorage) { |
|
obj.extensionData[extension] = runtime.extensionStorage[extension] |
|
continue; |
|
} |
|
} |
|
|
|
|
|
obj.extensions = extensions; |
|
const extensionURLs = getExtensionURLsToSave(extensions, runtime); |
|
if (extensionURLs) { |
|
obj.extensionURLs = extensionURLs; |
|
} |
|
|
|
if (fonts) { |
|
obj.customFonts = fonts; |
|
} |
|
|
|
|
|
const meta = Object.create(null); |
|
meta.semver = '3.0.0'; |
|
|
|
meta.vm = '0.2.0'; |
|
if (runtime.origin) { |
|
meta.origin = runtime.origin; |
|
} |
|
|
|
|
|
meta.agent = ''; |
|
|
|
|
|
|
|
|
|
const platform = Object.create(null); |
|
platform.name = "PenguinMod"; |
|
platform.url = "https://penguinmod.com/"; |
|
platform.version = "stable"; |
|
meta.platform = platform; |
|
|
|
|
|
obj.meta = meta; |
|
|
|
if (allowOptimization) { |
|
compress(obj); |
|
} |
|
|
|
return obj; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const deserializeInputDesc = function (inputDescOrId, parentId, isShadow, blocks) { |
|
if (!Array.isArray(inputDescOrId)) return inputDescOrId; |
|
const primitiveObj = Object.create(null); |
|
const newId = uid(); |
|
primitiveObj.id = newId; |
|
primitiveObj.next = null; |
|
primitiveObj.parent = parentId; |
|
primitiveObj.shadow = isShadow; |
|
primitiveObj.inputs = Object.create(null); |
|
|
|
switch (inputDescOrId[0]) { |
|
case MATH_NUM_PRIMITIVE: { |
|
primitiveObj.opcode = 'math_number'; |
|
primitiveObj.fields = { |
|
NUM: { |
|
name: 'NUM', |
|
value: inputDescOrId[1] |
|
} |
|
}; |
|
primitiveObj.topLevel = false; |
|
break; |
|
} |
|
case POSITIVE_NUM_PRIMITIVE: { |
|
primitiveObj.opcode = 'math_positive_number'; |
|
primitiveObj.fields = { |
|
NUM: { |
|
name: 'NUM', |
|
value: inputDescOrId[1] |
|
} |
|
}; |
|
primitiveObj.topLevel = false; |
|
break; |
|
} |
|
case WHOLE_NUM_PRIMITIVE: { |
|
primitiveObj.opcode = 'math_whole_number'; |
|
primitiveObj.fields = { |
|
NUM: { |
|
name: 'NUM', |
|
value: inputDescOrId[1] |
|
} |
|
}; |
|
primitiveObj.topLevel = false; |
|
break; |
|
} |
|
case INTEGER_NUM_PRIMITIVE: { |
|
primitiveObj.opcode = 'math_integer'; |
|
primitiveObj.fields = { |
|
NUM: { |
|
name: 'NUM', |
|
value: inputDescOrId[1] |
|
} |
|
}; |
|
primitiveObj.topLevel = false; |
|
break; |
|
} |
|
case ANGLE_NUM_PRIMITIVE: { |
|
primitiveObj.opcode = 'math_angle'; |
|
primitiveObj.fields = { |
|
NUM: { |
|
name: 'NUM', |
|
value: inputDescOrId[1] |
|
} |
|
}; |
|
primitiveObj.topLevel = false; |
|
break; |
|
} |
|
case COLOR_PICKER_PRIMITIVE: { |
|
primitiveObj.opcode = 'colour_picker'; |
|
primitiveObj.fields = { |
|
COLOUR: { |
|
name: 'COLOUR', |
|
value: inputDescOrId[1] |
|
} |
|
}; |
|
primitiveObj.topLevel = false; |
|
break; |
|
} |
|
case TEXT_PRIMITIVE: { |
|
primitiveObj.opcode = 'text'; |
|
primitiveObj.fields = { |
|
TEXT: { |
|
name: 'TEXT', |
|
value: inputDescOrId[1] |
|
} |
|
}; |
|
primitiveObj.topLevel = false; |
|
break; |
|
} |
|
case BROADCAST_PRIMITIVE: { |
|
primitiveObj.opcode = 'event_broadcast_menu'; |
|
primitiveObj.fields = { |
|
BROADCAST_OPTION: { |
|
name: 'BROADCAST_OPTION', |
|
value: inputDescOrId[1], |
|
id: inputDescOrId[2], |
|
variableType: Variable.BROADCAST_MESSAGE_TYPE |
|
} |
|
}; |
|
primitiveObj.topLevel = false; |
|
break; |
|
} |
|
case VAR_PRIMITIVE: { |
|
primitiveObj.opcode = 'data_variable'; |
|
primitiveObj.fields = { |
|
VARIABLE: { |
|
name: 'VARIABLE', |
|
value: inputDescOrId[1], |
|
id: inputDescOrId[2], |
|
variableType: Variable.SCALAR_TYPE |
|
} |
|
}; |
|
if (inputDescOrId.length > 3) { |
|
primitiveObj.topLevel = true; |
|
primitiveObj.x = inputDescOrId[3]; |
|
primitiveObj.y = inputDescOrId[4]; |
|
} |
|
break; |
|
} |
|
case LIST_PRIMITIVE: { |
|
primitiveObj.opcode = 'data_listcontents'; |
|
primitiveObj.fields = { |
|
LIST: { |
|
name: 'LIST', |
|
value: inputDescOrId[1], |
|
id: inputDescOrId[2], |
|
variableType: Variable.LIST_TYPE |
|
} |
|
}; |
|
if (inputDescOrId.length > 3) { |
|
primitiveObj.topLevel = true; |
|
primitiveObj.x = inputDescOrId[3]; |
|
primitiveObj.y = inputDescOrId[4]; |
|
} |
|
break; |
|
} |
|
case LONE_FIELD: { |
|
primitiveObj.opcode = inputDescOrId[1]; |
|
primitiveObj.fields = { |
|
[inputDescOrId[2]]: inputDescOrId[3] |
|
}; |
|
if (inputDescOrId.length > 4) { |
|
primitiveObj.topLevel = true; |
|
primitiveObj.x = inputDescOrId[4]; |
|
primitiveObj.y = inputDescOrId[5]; |
|
} |
|
break; |
|
} |
|
default: { |
|
log.error(`Found unknown primitive type during deserialization: ${JSON.stringify(inputDescOrId)}`); |
|
return null; |
|
} |
|
} |
|
blocks[newId] = primitiveObj; |
|
return newId; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const deserializeInputs = function (inputs, parentId, blocks) { |
|
|
|
|
|
const obj = {}; |
|
for (const inputName in inputs) { |
|
if (!hasOwnProperty.call(inputs, inputName)) continue; |
|
const inputDescArr = inputs[inputName]; |
|
|
|
if (!Array.isArray(inputDescArr)) continue; |
|
let block = null; |
|
let shadow = null; |
|
const blockShadowInfo = inputDescArr[0]; |
|
if (blockShadowInfo === INPUT_SAME_BLOCK_SHADOW) { |
|
|
|
block = shadow = deserializeInputDesc(inputDescArr[1], parentId, true, blocks); |
|
} else if (blockShadowInfo === INPUT_BLOCK_NO_SHADOW) { |
|
block = deserializeInputDesc(inputDescArr[1], parentId, false, blocks); |
|
} else { |
|
block = deserializeInputDesc(inputDescArr[1], parentId, false, blocks); |
|
shadow = deserializeInputDesc(inputDescArr[2], parentId, true, blocks); |
|
} |
|
obj[inputName] = { |
|
name: inputName, |
|
block: block, |
|
shadow: shadow |
|
}; |
|
} |
|
return obj; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
const deserializeFields = function (fields) { |
|
|
|
|
|
const obj = {}; |
|
for (const fieldName in fields) { |
|
if (!hasOwnProperty.call(fields, fieldName)) continue; |
|
const fieldDescArr = fields[fieldName]; |
|
|
|
if (!Array.isArray(fieldDescArr)) continue; |
|
obj[fieldName] = { |
|
name: fieldName, |
|
value: fieldDescArr[0] |
|
}; |
|
if (fieldDescArr.length > 1) { |
|
obj[fieldName].id = fieldDescArr[1]; |
|
} |
|
if (fieldDescArr.length > 2) { |
|
obj[fieldName].variableType = fieldDescArr[2]; |
|
} |
|
|
|
if (fieldName === 'BROADCAST_OPTION') { |
|
obj[fieldName].variableType = Variable.BROADCAST_MESSAGE_TYPE; |
|
} else if (fieldName === 'VARIABLE') { |
|
obj[fieldName].variableType = Variable.SCALAR_TYPE; |
|
} else if (fieldName === 'LIST') { |
|
obj[fieldName].variableType = Variable.LIST_TYPE; |
|
} |
|
} |
|
return obj; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const deserializeBlocks = function (blocks) { |
|
for (const blockId in blocks) { |
|
if (!Object.prototype.hasOwnProperty.call(blocks, blockId)) { |
|
continue; |
|
} |
|
const block = blocks[blockId]; |
|
if (Array.isArray(block)) { |
|
|
|
|
|
|
|
delete blocks[blockId]; |
|
deserializeInputDesc(block, null, false, blocks); |
|
continue; |
|
} |
|
block.id = blockId; |
|
block.inputs = deserializeInputs(block.inputs, blockId, blocks); |
|
block.fields = deserializeFields(block.fields); |
|
} |
|
return blocks; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const parseScratchAssets = function (object, runtime, zip) { |
|
if (!object.hasOwnProperty('name')) { |
|
|
|
|
|
return Promise.resolve(null); |
|
} |
|
|
|
const assets = { |
|
costumePromises: null, |
|
soundPromises: null, |
|
soundBank: runtime.audioEngine && runtime.audioEngine.createBank() |
|
}; |
|
|
|
|
|
assets.costumePromises = (object.costumes || []).map(costumeSource => { |
|
|
|
const costume = { |
|
|
|
|
|
asset: costumeSource.asset, |
|
assetId: costumeSource.assetId, |
|
skinId: null, |
|
name: costumeSource.name, |
|
bitmapResolution: costumeSource.bitmapResolution, |
|
rotationCenterX: costumeSource.rotationCenterX, |
|
rotationCenterY: costumeSource.rotationCenterY |
|
}; |
|
const dataFormat = |
|
costumeSource.dataFormat || |
|
(costumeSource.assetType && costumeSource.assetType.runtimeFormat) || |
|
'png'; |
|
const costumeMd5Ext = costumeSource.hasOwnProperty('md5ext') ? |
|
costumeSource.md5ext : `${costumeSource.assetId}.${dataFormat}`; |
|
costume.md5 = costumeMd5Ext; |
|
costume.dataFormat = dataFormat; |
|
|
|
|
|
|
|
|
|
|
|
return deserializeCostume(costume, runtime, zip) |
|
.then(() => loadCostume(costumeMd5Ext, costume, runtime)); |
|
|
|
|
|
}); |
|
|
|
assets.soundPromises = (object.sounds || []).map(soundSource => { |
|
const sound = { |
|
assetId: soundSource.assetId, |
|
format: soundSource.format, |
|
rate: soundSource.rate, |
|
sampleCount: soundSource.sampleCount, |
|
name: soundSource.name, |
|
|
|
|
|
|
|
md5: soundSource.md5ext, |
|
dataFormat: soundSource.dataFormat, |
|
data: null |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
return deserializeSound(sound, runtime, zip) |
|
.then(() => loadSound(sound, runtime, assets.soundBank)); |
|
|
|
|
|
}); |
|
|
|
return assets; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
const convertProcedureCompat = function (blockJSON, blocks) { |
|
if (blockJSON.opcode === 'procedures_return') { |
|
blockJSON.inputs.return = blockJSON.inputs.VALUE; |
|
blockJSON.inputs.return.name = "return"; |
|
delete blockJSON.inputs.VALUE; |
|
|
|
|
|
let thisBlock = blockJSON; |
|
let parent = thisBlock.parent; |
|
while (parent !== null) { |
|
if (parent) { |
|
thisBlock = blocks._blocks[parent]; |
|
parent = thisBlock?.parent ?? null; |
|
} |
|
} |
|
if (thisBlock && thisBlock.opcode === 'procedures_definition') { |
|
thisBlock.opcode = 'procedures_definition_return'; |
|
const proto = blocks._blocks[thisBlock.inputs.custom_block.block]; |
|
proto.mutation.returns = 'true'; |
|
} |
|
} else if (blockJSON.opcode === 'procedures_call') { |
|
const defineId = blocks.getProcedureDefinition(blockJSON.mutation.proccode); |
|
if (defineId) { |
|
const protoId = blocks._blocks[defineId].inputs.custom_block.block; |
|
if (blocks._blocks[protoId].mutation.returns === 'true') { |
|
blockJSON.mutation.returns = 'true'; |
|
} |
|
} |
|
|
|
|
|
const parent = blocks._blocks[blockJSON.parent]; |
|
if (parent) { |
|
if (parent.next === blockJSON.id) blockJSON.mutation.returns = 'false'; |
|
else { |
|
|
|
for (const input of Object.values(parent.inputs)) { |
|
if (input.block === blockJSON.id && input.name.startsWith('SUBSTACK')) { |
|
blockJSON.mutation.returns = 'false'; |
|
break; |
|
} |
|
} |
|
} |
|
} |
|
} |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const parseScratchObject = function (object, runtime, extensions, zip, assets) { |
|
if (!object.hasOwnProperty('name')) { |
|
|
|
|
|
return Promise.resolve(null); |
|
} |
|
|
|
const blocks = new Blocks(runtime); |
|
|
|
|
|
const sprite = new Sprite(blocks, runtime); |
|
|
|
|
|
if (object.hasOwnProperty('name')) { |
|
sprite.name = object.name; |
|
} |
|
if (object.hasOwnProperty('blocks')) { |
|
|
|
for (const blockId in object.blocks) { |
|
if (!object.blocks.hasOwnProperty(blockId)) continue; |
|
const blockJSON = object.blocks[blockId]; |
|
|
|
if (typeof blockJSON !== 'object' || Array.isArray(blockJSON)) continue; |
|
const extensionID = getExtensionIdForOpcode(blockJSON.opcode); |
|
const isPatched = extensions.patcher.patchExists(extensionID); |
|
if (isPatched) { |
|
extensions.patcher.runExtensionPatch(extensionID, extensions, object); |
|
} |
|
} |
|
|
|
deserializeBlocks(object.blocks); |
|
|
|
const _converterCache = []; |
|
for (const blockId in object.blocks) { |
|
if (!object.blocks.hasOwnProperty(blockId)) continue; |
|
const blockJSON = object.blocks[blockId]; |
|
if (runtime.origin === 'TurboWarp') { |
|
if (blockJSON.opcode === 'procedures_call' || blockJSON.opcode === 'procedures_return') { |
|
_converterCache.push(blockJSON); |
|
} |
|
} |
|
blocks.createBlock(blockJSON); |
|
} |
|
|
|
|
|
if (runtime.origin === 'TurboWarp') for (const block of _converterCache) convertProcedureCompat(block, blocks); |
|
} |
|
|
|
const {costumePromises} = assets; |
|
|
|
const {soundBank, soundPromises} = assets; |
|
|
|
const target = sprite.createClone(object.isStage ? StageLayering.BACKGROUND_LAYER : StageLayering.SPRITE_LAYER); |
|
|
|
if (object.hasOwnProperty('tempo')) { |
|
target.tempo = object.tempo; |
|
} |
|
if (object.hasOwnProperty('volume')) { |
|
target.volume = object.volume; |
|
} |
|
if (object.hasOwnProperty('videoTransparency')) { |
|
target.videoTransparency = object.videoTransparency; |
|
} |
|
if (object.hasOwnProperty('videoState')) { |
|
target.videoState = object.videoState; |
|
} |
|
if (object.hasOwnProperty('textToSpeechLanguage')) { |
|
target.textToSpeechLanguage = object.textToSpeechLanguage; |
|
} |
|
if (object.hasOwnProperty('variables')) { |
|
for (const varId in object.variables) { |
|
const variable = object.variables[varId]; |
|
|
|
|
|
|
|
|
|
const isCloud = (variable.length === 3) && variable[2] && |
|
object.isStage && runtime.canAddCloudVariable(); |
|
const newVariable = new Variable( |
|
varId, |
|
variable[0], |
|
Variable.SCALAR_TYPE, |
|
isCloud |
|
); |
|
if (isCloud) runtime.addCloudVariable(); |
|
newVariable.value = variable[1]; |
|
target.variables[newVariable.id] = newVariable; |
|
} |
|
} |
|
if (object.hasOwnProperty('lists')) { |
|
for (const listId in object.lists) { |
|
const list = object.lists[listId]; |
|
const newList = new Variable( |
|
listId, |
|
list[0], |
|
Variable.LIST_TYPE, |
|
false |
|
); |
|
newList.value = list[1]; |
|
target.variables[newList.id] = newList; |
|
} |
|
} |
|
if (object.hasOwnProperty('broadcasts')) { |
|
for (const broadcastId in object.broadcasts) { |
|
const broadcast = object.broadcasts[broadcastId]; |
|
const newBroadcast = new Variable( |
|
broadcastId, |
|
broadcast, |
|
Variable.BROADCAST_MESSAGE_TYPE, |
|
false |
|
); |
|
|
|
|
|
target.variables[newBroadcast.id] = newBroadcast; |
|
} |
|
} |
|
if (object.hasOwnProperty('customVars')) { |
|
for (const variable of object.customVars) { |
|
if (Array.isArray(variable)) { |
|
|
|
const newVar = runtime.newVariableInstance(...variable); |
|
target.variables[variable[1]] = newVar; |
|
continue; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const newVar = runtime.newVariableInstance(variable.type, ...variable.info); |
|
target.variables[variable.id] = newVar; |
|
} |
|
} |
|
if (object.hasOwnProperty('comments')) { |
|
for (const commentId in object.comments) { |
|
const comment = object.comments[commentId]; |
|
const newComment = new Comment( |
|
commentId, |
|
comment.text, |
|
comment.x, |
|
comment.y, |
|
comment.width, |
|
comment.height, |
|
comment.minimized |
|
); |
|
if (comment.blockId) { |
|
newComment.blockId = comment.blockId; |
|
} |
|
target.comments[newComment.id] = newComment; |
|
} |
|
} |
|
if (object.hasOwnProperty('x')) { |
|
target.x = object.x; |
|
} |
|
if (object.hasOwnProperty('y')) { |
|
target.y = object.y; |
|
} |
|
if (object.hasOwnProperty('direction')) { |
|
target.direction = object.direction; |
|
} |
|
if (object.hasOwnProperty('size')) { |
|
target.size = object.size; |
|
} |
|
if (object.hasOwnProperty('visible')) { |
|
target.visible = object.visible; |
|
} |
|
if (object.hasOwnProperty('currentCostume')) { |
|
target.currentCostume = MathUtil.clamp(object.currentCostume, 0, object.costumes.length - 1); |
|
} |
|
if (object.hasOwnProperty('rotationStyle')) { |
|
target.rotationStyle = object.rotationStyle; |
|
} |
|
if (object.hasOwnProperty('isStage')) { |
|
target.isStage = object.isStage; |
|
} |
|
if (object.hasOwnProperty('targetPaneOrder')) { |
|
|
|
|
|
|
|
target.targetPaneOrder = object.targetPaneOrder; |
|
} |
|
if (object.hasOwnProperty('draggable')) { |
|
target.draggable = object.draggable; |
|
} |
|
const existingTargetIds = runtime.targets.map(target => target.id); |
|
if (object.hasOwnProperty('id') && !existingTargetIds.includes(object.id)) { |
|
target.id = object.id; |
|
} |
|
|
|
if (object.hasOwnProperty('extensionData') || object.hasOwnProperty('extensionStorage')) { |
|
target.extensionData = Object.assign(object.extensionStorage ?? {}, object.extensionData); |
|
|
|
} |
|
|
|
Promise.all(costumePromises).then(costumes => { |
|
sprite.costumes = costumes; |
|
}); |
|
Promise.all(soundPromises).then(sounds => { |
|
sprite.sounds = sounds; |
|
|
|
sprite.soundBank = soundBank || null; |
|
}); |
|
return Promise.all(costumePromises.concat(soundPromises)).then(() => target); |
|
}; |
|
|
|
const deserializeMonitor = function (monitorData, runtime, targets, extensions) { |
|
|
|
const xOffset = (runtime.stageWidth - 480) / 2; |
|
const yOffset = (runtime.stageHeight - 360) / 2; |
|
monitorData.x += xOffset; |
|
monitorData.y += yOffset; |
|
monitorData.x = MathUtil.clamp(monitorData.x, 0, runtime.stageWidth); |
|
monitorData.y = MathUtil.clamp(monitorData.y, 0, runtime.stageHeight); |
|
|
|
|
|
|
|
|
|
if (monitorData.spriteName) { |
|
const filteredTargets = targets.filter(t => t.sprite.name === monitorData.spriteName); |
|
if (filteredTargets && filteredTargets.length > 0) { |
|
monitorData.targetId = filteredTargets[0].id; |
|
} else { |
|
log.warn(`Tried to deserialize sprite specific monitor ${ |
|
monitorData.opcode} but could not find sprite ${monitorData.spriteName}.`); |
|
} |
|
} |
|
|
|
|
|
|
|
const monitorBlockInfo = runtime.monitorBlockInfo[monitorData.opcode]; |
|
|
|
|
|
|
|
if (monitorData.opcode === 'data_listcontents') { |
|
const listTarget = monitorData.targetId ? |
|
targets.find(t => t.id === monitorData.targetId) : |
|
targets.find(t => t.isStage); |
|
if ( |
|
listTarget && |
|
Object.prototype.hasOwnProperty.call(listTarget.variables, monitorData.id) |
|
) { |
|
monitorData.params.LIST = listTarget.variables[monitorData.id].name; |
|
} |
|
} |
|
|
|
|
|
const fields = {}; |
|
for (const paramKey in monitorData.params) { |
|
const field = { |
|
name: paramKey, |
|
value: monitorData.params[paramKey] |
|
}; |
|
if (typeof monitorData.params[paramKey] === 'object') { |
|
field.id = monitorData.params[paramKey].id; |
|
field.value = monitorData.params[paramKey].name; |
|
} |
|
fields[paramKey] = field; |
|
} |
|
|
|
|
|
|
|
|
|
if (monitorData.opcode !== 'data_variable' && monitorData.opcode !== 'data_listcontents' && |
|
monitorBlockInfo && monitorBlockInfo.isSpriteSpecific) { |
|
monitorData.id = monitorBlockInfo.getId( |
|
monitorData.targetId, fields); |
|
} else { |
|
|
|
|
|
|
|
|
|
monitorData.id = StringUtil.replaceUnsafeChars(monitorData.id); |
|
} |
|
|
|
|
|
|
|
const existingMonitorBlock = runtime.monitorBlocks._blocks[monitorData.id]; |
|
if (existingMonitorBlock) { |
|
|
|
|
|
existingMonitorBlock.isMonitored = monitorData.visible; |
|
existingMonitorBlock.targetId = monitorData.targetId; |
|
} else { |
|
|
|
|
|
const monitorBlock = { |
|
id: monitorData.id, |
|
opcode: monitorData.opcode, |
|
inputs: {}, |
|
fields: fields, |
|
topLevel: true, |
|
next: null, |
|
parent: null, |
|
shadow: false, |
|
x: 0, |
|
y: 0, |
|
isMonitored: monitorData.visible, |
|
targetId: monitorData.targetId |
|
}; |
|
|
|
|
|
|
|
|
|
if (monitorData.opcode === 'data_variable') { |
|
const field = monitorBlock.fields.VARIABLE; |
|
field.id = monitorData.id; |
|
field.variableType = Variable.SCALAR_TYPE; |
|
} else if (monitorData.opcode === 'data_listcontents') { |
|
const field = monitorBlock.fields.LIST; |
|
field.id = monitorData.id; |
|
field.variableType = Variable.LIST_TYPE; |
|
} else if (monitorData.variableId) { |
|
const field = Object.values(monitorBlock.fields)[0]; |
|
field.id = monitorData.variableId; |
|
field.variableType = monitorData.variableType; |
|
} |
|
|
|
runtime.monitorBlocks.createBlock(monitorBlock); |
|
} |
|
|
|
runtime.requestAddMonitor(MonitorRecord(monitorData)); |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const replaceUnsafeCharsInVariableIds = function (targets) { |
|
const allVarRefs = VariableUtil.getAllVarRefsForTargets(targets, true); |
|
|
|
targets.forEach(t => { |
|
Object.keys(t.variables).forEach(id => { |
|
const newId = StringUtil.replaceUnsafeChars(id); |
|
if (newId === id) return; |
|
t.variables[id].id = newId; |
|
t.variables[newId] = t.variables[id]; |
|
delete t.variables[id]; |
|
}); |
|
}); |
|
|
|
|
|
for (const id in allVarRefs) { |
|
const newId = StringUtil.replaceUnsafeChars(id); |
|
if (id === newId) continue; |
|
|
|
|
|
|
|
VariableUtil.updateVariableIdentifiers(allVarRefs[id], newId); |
|
} |
|
return targets; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const deserialize = function (json, runtime, zip, isSingleSprite) { |
|
const extensionPatcher = new OldExtensions(runtime); |
|
extensionPatcher.registerExtensions(ExtensionPatches); |
|
const extensions = { |
|
extensionIDs: new Set(json.extensions), |
|
extensionURLs: new Map(), |
|
extensionData: {}, |
|
patcher: extensionPatcher |
|
}; |
|
|
|
|
|
if (json.meta) { |
|
if (json.meta.origin) runtime.origin = json.meta.origin; |
|
else if (json.meta.platform) runtime.origin = json.meta.platform.name; |
|
else runtime.origin = null; |
|
} else { |
|
runtime.origin = null; |
|
} |
|
|
|
|
|
if (json.extensionURLs) { |
|
extensions.extensionURLs = new Map(Object.entries(json.extensionURLs)); |
|
} |
|
if (json.extensionData || json.extensionStorage) { |
|
extensions.extensionData = Object.assign(json.extensionStorage ?? {}, json.extensionData); |
|
|
|
} |
|
|
|
|
|
let fontPromise; |
|
if (json.customFonts) { |
|
fontPromise = runtime.fontManager.deserialize(json.customFonts, zip, isSingleSprite); |
|
} else { |
|
fontPromise = Promise.resolve(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
const targetObjects = ((isSingleSprite ? [json] : json.targets) || []) |
|
.map((t, i) => Object.assign(t, {targetPaneOrder: i})) |
|
.sort((a, b) => a.layerOrder - b.layerOrder); |
|
|
|
const monitorObjects = json.monitors || []; |
|
|
|
return fontPromise.then(() => targetObjects.map(target => parseScratchAssets(target, runtime, zip))) |
|
|
|
|
|
.then(assets => Promise.resolve(assets)) |
|
.then(assets => Promise.all(targetObjects |
|
.map((target, index) => |
|
parseScratchObject(target, runtime, extensions, zip, assets[index])))) |
|
.then(targets => targets |
|
.map((t, i) => { |
|
|
|
|
|
|
|
t.layerOrder = i; |
|
return t; |
|
}) |
|
.sort((a, b) => a.targetPaneOrder - b.targetPaneOrder) |
|
.map(t => { |
|
|
|
|
|
delete t.targetPaneOrder; |
|
return t; |
|
})) |
|
.then(targets => replaceUnsafeCharsInVariableIds(targets)) |
|
.then(targets => { |
|
|
|
if (runtime.origin === 'TurboWarp') runtime.origin = null; |
|
|
|
|
|
const stage = targets.find(t => t.isStage); |
|
if (stage) { |
|
|
|
let projectOptsComment; |
|
for (const comment of Object.values(stage?.comments)) { |
|
if (comment.text.includes(" // _twconfig_")) { |
|
projectOptsComment = comment; |
|
break; |
|
} |
|
} |
|
|
|
if (projectOptsComment) runtime.parseProjectOptions(projectOptsComment); |
|
} |
|
|
|
monitorObjects.map(monitorDesc => deserializeMonitor(monitorDesc, runtime, targets, extensions)); |
|
return targets; |
|
}) |
|
.then(targets => ({ |
|
targets, |
|
extensions |
|
})); |
|
}; |
|
|
|
module.exports = { |
|
serialize: serialize, |
|
deserialize: deserialize, |
|
deserializeBlocks: deserializeBlocks, |
|
serializeBlocks: serializeBlocks, |
|
deserializeStandaloneBlocks: deserializeStandaloneBlocks, |
|
serializeStandaloneBlocks: serializeStandaloneBlocks, |
|
getExtensionIdForOpcode: getExtensionIdForOpcode |
|
}; |
|
|