Update src/serialization/sb3.js
Browse files- src/serialization/sb3.js +67 -2
src/serialization/sb3.js
CHANGED
@@ -1280,6 +1280,57 @@ const parseScratchAssets = function (object, runtime, zip) {
|
|
1280 |
return assets;
|
1281 |
};
|
1282 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1283 |
/**
|
1284 |
* Parse a single "Scratch object" and create all its in-memory VM objects.
|
1285 |
* @param {!object} object From-JSON "Scratch object:" sprite, stage, watcher.
|
@@ -1322,11 +1373,20 @@ const parseScratchObject = function (object, runtime, extensions, zip, assets) {
|
|
1322 |
|
1323 |
deserializeBlocks(object.blocks);
|
1324 |
// Take a second pass to create objects and add extensions
|
|
|
1325 |
for (const blockId in object.blocks) {
|
1326 |
if (!object.blocks.hasOwnProperty(blockId)) continue;
|
1327 |
const blockJSON = object.blocks[blockId];
|
|
|
|
|
|
|
|
|
|
|
1328 |
blocks.createBlock(blockJSON);
|
1329 |
}
|
|
|
|
|
|
|
1330 |
}
|
1331 |
// Costumes from JSON.
|
1332 |
const {costumePromises} = assets;
|
@@ -1656,8 +1716,10 @@ const deserialize = function (json, runtime, zip, isSingleSprite) {
|
|
1656 |
};
|
1657 |
|
1658 |
// Store the origin field (e.g. project originated at CSFirst) so that we can save it again.
|
1659 |
-
if (json.meta
|
1660 |
-
runtime.origin = json.meta.origin;
|
|
|
|
|
1661 |
} else {
|
1662 |
runtime.origin = null;
|
1663 |
}
|
@@ -1713,6 +1775,9 @@ const deserialize = function (json, runtime, zip, isSingleSprite) {
|
|
1713 |
}))
|
1714 |
.then(targets => replaceUnsafeCharsInVariableIds(targets))
|
1715 |
.then(targets => {
|
|
|
|
|
|
|
1716 |
// at this point, stage size has not been set by 'runtime.parseProjectOptions'
|
1717 |
const stage = targets.find(t => t.isStage);
|
1718 |
if (stage) {
|
|
|
1280 |
return assets;
|
1281 |
};
|
1282 |
|
1283 |
+
/**
|
1284 |
+
* Convert a Procedure Block to a PenguinMod-acceptable format (TurboWarp compatibility)
|
1285 |
+
* @param {!object} blockJSON - blockJSON for a block
|
1286 |
+
* @param {!object} blocks - all blocks in the sprite container
|
1287 |
+
*/
|
1288 |
+
const convertProcedureCompat = function (blockJSON, blocks) {
|
1289 |
+
if (blockJSON.opcode === 'procedures_return') {
|
1290 |
+
blockJSON.inputs.return = blockJSON.inputs.VALUE;
|
1291 |
+
blockJSON.inputs.return.name = "return";
|
1292 |
+
delete blockJSON.inputs.VALUE;
|
1293 |
+
|
1294 |
+
// climb stack tree to change the procedure to returnable
|
1295 |
+
let thisBlock = blockJSON;
|
1296 |
+
let parent = thisBlock.parent;
|
1297 |
+
while (parent !== null) {
|
1298 |
+
if (parent) {
|
1299 |
+
thisBlock = blocks._blocks[parent];
|
1300 |
+
parent = thisBlock?.parent ?? null;
|
1301 |
+
}
|
1302 |
+
}
|
1303 |
+
if (thisBlock && thisBlock.opcode === 'procedures_definition') {
|
1304 |
+
thisBlock.opcode = 'procedures_definition_return';
|
1305 |
+
const proto = blocks._blocks[thisBlock.inputs.custom_block.block];
|
1306 |
+
proto.mutation.returns = 'true';
|
1307 |
+
}
|
1308 |
+
} else if (blockJSON.opcode === 'procedures_call') {
|
1309 |
+
const defineId = blocks.getProcedureDefinition(blockJSON.mutation.proccode);
|
1310 |
+
if (defineId) {
|
1311 |
+
const protoId = blocks._blocks[defineId].inputs.custom_block.block;
|
1312 |
+
if (blocks._blocks[protoId].mutation.returns === 'true') {
|
1313 |
+
blockJSON.mutation.returns = 'true';
|
1314 |
+
}
|
1315 |
+
}
|
1316 |
+
|
1317 |
+
// check if we're not in a reporter slot
|
1318 |
+
const parent = blocks._blocks[blockJSON.parent];
|
1319 |
+
if (parent) {
|
1320 |
+
if (parent.next === blockJSON.id) blockJSON.mutation.returns = 'false';
|
1321 |
+
else {
|
1322 |
+
// we could be in a branch
|
1323 |
+
for (const input of Object.values(parent.inputs)) {
|
1324 |
+
if (input.block === blockJSON.id && input.name.startsWith('SUBSTACK')) {
|
1325 |
+
blockJSON.mutation.returns = 'false';
|
1326 |
+
break;
|
1327 |
+
}
|
1328 |
+
}
|
1329 |
+
}
|
1330 |
+
}
|
1331 |
+
}
|
1332 |
+
};
|
1333 |
+
|
1334 |
/**
|
1335 |
* Parse a single "Scratch object" and create all its in-memory VM objects.
|
1336 |
* @param {!object} object From-JSON "Scratch object:" sprite, stage, watcher.
|
|
|
1373 |
|
1374 |
deserializeBlocks(object.blocks);
|
1375 |
// Take a second pass to create objects and add extensions
|
1376 |
+
const _converterCache = [];
|
1377 |
for (const blockId in object.blocks) {
|
1378 |
if (!object.blocks.hasOwnProperty(blockId)) continue;
|
1379 |
const blockJSON = object.blocks[blockId];
|
1380 |
+
if (runtime.origin === 'TurboWarp') {
|
1381 |
+
if (blockJSON.opcode === 'procedures_call' || blockJSON.opcode === 'procedures_return') {
|
1382 |
+
_converterCache.push(blockJSON);
|
1383 |
+
}
|
1384 |
+
}
|
1385 |
blocks.createBlock(blockJSON);
|
1386 |
}
|
1387 |
+
|
1388 |
+
// convert TurboWarp custom reporters to PenguinMod's format
|
1389 |
+
if (runtime.origin === 'TurboWarp') for (const block of _converterCache) convertProcedureCompat(block, blocks);
|
1390 |
}
|
1391 |
// Costumes from JSON.
|
1392 |
const {costumePromises} = assets;
|
|
|
1716 |
};
|
1717 |
|
1718 |
// Store the origin field (e.g. project originated at CSFirst) so that we can save it again.
|
1719 |
+
if (json.meta) {
|
1720 |
+
if (json.meta.origin) runtime.origin = json.meta.origin;
|
1721 |
+
else if (json.meta.platform) runtime.origin = json.meta.platform.name;
|
1722 |
+
else runtime.origin = null;
|
1723 |
} else {
|
1724 |
runtime.origin = null;
|
1725 |
}
|
|
|
1775 |
}))
|
1776 |
.then(targets => replaceUnsafeCharsInVariableIds(targets))
|
1777 |
.then(targets => {
|
1778 |
+
// all blocks have been created, its safe to reset the origin from Turbowarp
|
1779 |
+
if (runtime.origin === 'TurboWarp') runtime.origin = null;
|
1780 |
+
|
1781 |
// at this point, stage size has not been set by 'runtime.parseProjectOptions'
|
1782 |
const stage = targets.find(t => t.isStage);
|
1783 |
if (stage) {
|