Update src/serialization/sb3.js
Browse files- src/serialization/sb3.js +102 -41
src/serialization/sb3.js
CHANGED
|
@@ -136,7 +136,7 @@ const ExtensionPatches = {
|
|
| 136 |
let blocks = object.blocks;
|
| 137 |
const blockIDs = Object.keys(blocks);
|
| 138 |
const patcher = extensions.patcher;
|
| 139 |
-
|
| 140 |
for (let block, idx = 0; idx < blockIDs.length; idx++) {
|
| 141 |
block = blocks[blockIDs[idx]];
|
| 142 |
if (typeof block !== 'object' || Array.isArray(block)) continue;
|
|
@@ -145,7 +145,7 @@ const ExtensionPatches = {
|
|
| 145 |
block.opcode = uniteReplacments[block.opcode];
|
| 146 |
if (block.opcode === 'sensing_regextest' || block.opcode === 'operator_regexmatch') {
|
| 147 |
block.inputs.regrule = [
|
| 148 |
-
INPUT_SAME_BLOCK_SHADOW,
|
| 149 |
[TEXT_PRIMITIVE, "g"]
|
| 150 |
];
|
| 151 |
}
|
|
@@ -158,7 +158,7 @@ const ExtensionPatches = {
|
|
| 158 |
}
|
| 159 |
blocks = Object.assign(blocks, Clone.simple(replacersPatch.blocks));
|
| 160 |
object.variables = Object.assign(object.variables, Clone.simple(replacersPatch.variables));
|
| 161 |
-
const repBlock = block.opcode === 'jwUnite_setReplacer'
|
| 162 |
? "setReplacerToDisplay"
|
| 163 |
: "replaceWithReplacersDisplay";
|
| 164 |
const replacment = Clone.simple(replacersPatch.blocks[repBlock]);
|
|
@@ -554,16 +554,16 @@ const serializeCostume = function (costume) {
|
|
| 554 |
|
| 555 |
obj.bitmapResolution = costumeToSerialize.bitmapResolution;
|
| 556 |
obj.dataFormat = costumeToSerialize.dataFormat.toLowerCase();
|
| 557 |
-
|
| 558 |
obj.assetId = costumeToSerialize.assetId;
|
| 559 |
-
|
| 560 |
// serialize this property with the name 'md5ext' because that's
|
| 561 |
// what it's actually referring to. TODO runtime objects need to be
|
| 562 |
// updated to actually refer to this as 'md5ext' instead of 'md5'
|
| 563 |
// but that change should be made carefully since it is very
|
| 564 |
// pervasive
|
| 565 |
obj.md5ext = costumeToSerialize.md5;
|
| 566 |
-
|
| 567 |
obj.rotationCenterX = costumeToSerialize.rotationCenterX;
|
| 568 |
obj.rotationCenterY = costumeToSerialize.rotationCenterY;
|
| 569 |
|
|
@@ -578,7 +578,7 @@ const serializeCostume = function (costume) {
|
|
| 578 |
const serializeSound = function (sound) {
|
| 579 |
const obj = Object.create(null);
|
| 580 |
obj.name = sound.name;
|
| 581 |
-
|
| 582 |
const soundToSerialize = sound.broken || sound;
|
| 583 |
|
| 584 |
obj.assetId = soundToSerialize.assetId;
|
|
@@ -655,23 +655,27 @@ const serializeVariables = function (obj, runtime, variables) {
|
|
| 655 |
obj.customVars = [];
|
| 656 |
for (const varId in variables) {
|
| 657 |
const v = variables[varId];
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
|
| 662 |
-
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
|
| 669 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 670 |
}
|
| 671 |
-
// else custom variable type
|
| 672 |
-
const varInfo = v.serialize();
|
| 673 |
-
varInfo.unshift(v.type);
|
| 674 |
-
obj.customVars.push(varInfo);
|
| 675 |
}
|
| 676 |
};
|
| 677 |
|
|
@@ -737,6 +741,26 @@ const serializeTarget = function (runtime, target) {
|
|
| 737 |
obj.rotationStyle = target.rotationStyle;
|
| 738 |
}
|
| 739 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 740 |
return obj;
|
| 741 |
};
|
| 742 |
|
|
@@ -768,7 +792,9 @@ const serializeMonitors = function (monitors, runtime) {
|
|
| 768 |
height: monitorData.height,
|
| 769 |
x: monitorData.x - xOffset,
|
| 770 |
y: monitorData.y - yOffset,
|
| 771 |
-
visible: monitorData.visible
|
|
|
|
|
|
|
| 772 |
};
|
| 773 |
if (monitorData.mode !== 'list') {
|
| 774 |
serializedMonitor.sliderMin = monitorData.sliderMin;
|
|
@@ -791,9 +817,11 @@ const serialize = function (runtime, targetId, {allowOptimization = true} = {})
|
|
| 791 |
// Create extension set to hold extension ids found while serializing targets
|
| 792 |
const extensions = getExtensionIDs(runtime);
|
| 793 |
|
| 794 |
-
const originalTargetsToSerialize =
|
| 795 |
-
|
| 796 |
-
|
|
|
|
|
|
|
| 797 |
|
| 798 |
const layerOrdering = getSimplifiedLayerOrdering(originalTargetsToSerialize);
|
| 799 |
|
|
@@ -821,10 +849,13 @@ const serialize = function (runtime, targetId, {allowOptimization = true} = {})
|
|
| 821 |
// add extension datas
|
| 822 |
target.extensionData = {};
|
| 823 |
for (const extension of extensions) {
|
| 824 |
-
if (`ext_${extension}` in runtime) {
|
| 825 |
-
|
| 826 |
-
|
| 827 |
-
|
|
|
|
|
|
|
|
|
|
| 828 |
}
|
| 829 |
}
|
| 830 |
|
|
@@ -841,10 +872,13 @@ const serialize = function (runtime, targetId, {allowOptimization = true} = {})
|
|
| 841 |
// add extension datas
|
| 842 |
obj.extensionData = {};
|
| 843 |
for (const extension of extensions) {
|
| 844 |
-
if (`ext_${extension}` in runtime) {
|
| 845 |
-
|
| 846 |
-
|
| 847 |
-
|
|
|
|
|
|
|
|
|
|
| 848 |
}
|
| 849 |
}
|
| 850 |
|
|
@@ -872,7 +906,7 @@ const serialize = function (runtime, targetId, {allowOptimization = true} = {})
|
|
| 872 |
meta.agent = '';
|
| 873 |
// TW: Never include full user agent to slightly improve user privacy
|
| 874 |
// if (typeof navigator !== 'undefined') meta.agent = navigator.userAgent;
|
| 875 |
-
|
| 876 |
// Attach platform information so TurboWarp and other mods can detect where the file comes from
|
| 877 |
const platform = Object.create(null);
|
| 878 |
platform.name = "PenguinMod";
|
|
@@ -1364,10 +1398,24 @@ const parseScratchObject = function (object, runtime, extensions, zip, assets) {
|
|
| 1364 |
}
|
| 1365 |
}
|
| 1366 |
if (object.hasOwnProperty('customVars')) {
|
| 1367 |
-
for (const
|
| 1368 |
-
|
| 1369 |
-
|
| 1370 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1371 |
}
|
| 1372 |
}
|
| 1373 |
if (object.hasOwnProperty('comments')) {
|
|
@@ -1425,6 +1473,11 @@ const parseScratchObject = function (object, runtime, extensions, zip, assets) {
|
|
| 1425 |
if (object.hasOwnProperty('id') && !existingTargetIds.includes(object.id)) {
|
| 1426 |
target.id = object.id;
|
| 1427 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1428 |
Promise.all(costumePromises).then(costumes => {
|
| 1429 |
sprite.costumes = costumes;
|
| 1430 |
});
|
|
@@ -1483,6 +1536,10 @@ const deserializeMonitor = function (monitorData, runtime, targets, extensions)
|
|
| 1483 |
name: paramKey,
|
| 1484 |
value: monitorData.params[paramKey]
|
| 1485 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1486 |
fields[paramKey] = field;
|
| 1487 |
}
|
| 1488 |
|
|
@@ -1538,6 +1595,10 @@ const deserializeMonitor = function (monitorData, runtime, targets, extensions)
|
|
| 1538 |
const field = monitorBlock.fields.LIST;
|
| 1539 |
field.id = monitorData.id;
|
| 1540 |
field.variableType = Variable.LIST_TYPE;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1541 |
}
|
| 1542 |
|
| 1543 |
runtime.monitorBlocks.createBlock(monitorBlock);
|
|
@@ -1667,4 +1728,4 @@ module.exports = {
|
|
| 1667 |
deserializeStandaloneBlocks: deserializeStandaloneBlocks,
|
| 1668 |
serializeStandaloneBlocks: serializeStandaloneBlocks,
|
| 1669 |
getExtensionIdForOpcode: getExtensionIdForOpcode
|
| 1670 |
-
};
|
|
|
|
| 136 |
let blocks = object.blocks;
|
| 137 |
const blockIDs = Object.keys(blocks);
|
| 138 |
const patcher = extensions.patcher;
|
| 139 |
+
|
| 140 |
for (let block, idx = 0; idx < blockIDs.length; idx++) {
|
| 141 |
block = blocks[blockIDs[idx]];
|
| 142 |
if (typeof block !== 'object' || Array.isArray(block)) continue;
|
|
|
|
| 145 |
block.opcode = uniteReplacments[block.opcode];
|
| 146 |
if (block.opcode === 'sensing_regextest' || block.opcode === 'operator_regexmatch') {
|
| 147 |
block.inputs.regrule = [
|
| 148 |
+
INPUT_SAME_BLOCK_SHADOW,
|
| 149 |
[TEXT_PRIMITIVE, "g"]
|
| 150 |
];
|
| 151 |
}
|
|
|
|
| 158 |
}
|
| 159 |
blocks = Object.assign(blocks, Clone.simple(replacersPatch.blocks));
|
| 160 |
object.variables = Object.assign(object.variables, Clone.simple(replacersPatch.variables));
|
| 161 |
+
const repBlock = block.opcode === 'jwUnite_setReplacer'
|
| 162 |
? "setReplacerToDisplay"
|
| 163 |
: "replaceWithReplacersDisplay";
|
| 164 |
const replacment = Clone.simple(replacersPatch.blocks[repBlock]);
|
|
|
|
| 554 |
|
| 555 |
obj.bitmapResolution = costumeToSerialize.bitmapResolution;
|
| 556 |
obj.dataFormat = costumeToSerialize.dataFormat.toLowerCase();
|
| 557 |
+
|
| 558 |
obj.assetId = costumeToSerialize.assetId;
|
| 559 |
+
|
| 560 |
// serialize this property with the name 'md5ext' because that's
|
| 561 |
// what it's actually referring to. TODO runtime objects need to be
|
| 562 |
// updated to actually refer to this as 'md5ext' instead of 'md5'
|
| 563 |
// but that change should be made carefully since it is very
|
| 564 |
// pervasive
|
| 565 |
obj.md5ext = costumeToSerialize.md5;
|
| 566 |
+
|
| 567 |
obj.rotationCenterX = costumeToSerialize.rotationCenterX;
|
| 568 |
obj.rotationCenterY = costumeToSerialize.rotationCenterY;
|
| 569 |
|
|
|
|
| 578 |
const serializeSound = function (sound) {
|
| 579 |
const obj = Object.create(null);
|
| 580 |
obj.name = sound.name;
|
| 581 |
+
|
| 582 |
const soundToSerialize = sound.broken || sound;
|
| 583 |
|
| 584 |
obj.assetId = soundToSerialize.assetId;
|
|
|
|
| 655 |
obj.customVars = [];
|
| 656 |
for (const varId in variables) {
|
| 657 |
const v = variables[varId];
|
| 658 |
+
|
| 659 |
+
switch (v.type) {
|
| 660 |
+
case Variable.BROADCAST_MESSAGE_TYPE:
|
| 661 |
+
obj.broadcasts[varId] = v.value; // name and value is the same for broadcast msgs
|
| 662 |
+
break;
|
| 663 |
+
case Variable.LIST_TYPE:
|
| 664 |
+
obj.lists[varId] = [v.name, makeSafeForJSON(runtime, v.value)];
|
| 665 |
+
break;
|
| 666 |
+
case Variable.SCALAR_TYPE:
|
| 667 |
+
obj.variables[varId] = [v.name, makeSafeForJSON(runtime, v.value)];
|
| 668 |
+
if (v.isCloud) obj.variables[varId].push(true);
|
| 669 |
+
break;
|
| 670 |
+
default:
|
| 671 |
+
const info = v.serialize();
|
| 672 |
+
const variable_object = {
|
| 673 |
+
type: v.type,
|
| 674 |
+
id: varId,
|
| 675 |
+
info,
|
| 676 |
+
};
|
| 677 |
+
obj.customVars.push(variable_object);
|
| 678 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 679 |
}
|
| 680 |
};
|
| 681 |
|
|
|
|
| 741 |
obj.rotationStyle = target.rotationStyle;
|
| 742 |
}
|
| 743 |
|
| 744 |
+
const extensions = getExtensionIDs(runtime);
|
| 745 |
+
const extensionData = {};
|
| 746 |
+
for (let extension of extensions) {
|
| 747 |
+
if (
|
| 748 |
+
`ext_${extension}` in runtime &&
|
| 749 |
+
(typeof runtime[`ext_${extension}`].serializeForTarget === 'function')
|
| 750 |
+
) {
|
| 751 |
+
extensionData[extension] = runtime[`ext_${extension}`].serializeForTarget(target);
|
| 752 |
+
continue;
|
| 753 |
+
}
|
| 754 |
+
if (extension in (target.extensionStorage ?? {})) {
|
| 755 |
+
extensionData[extension] = target.extensionStorage[extension];
|
| 756 |
+
continue;
|
| 757 |
+
}
|
| 758 |
+
|
| 759 |
+
}
|
| 760 |
+
if (extensionData) {
|
| 761 |
+
obj.extensionData = extensionData;
|
| 762 |
+
}
|
| 763 |
+
|
| 764 |
return obj;
|
| 765 |
};
|
| 766 |
|
|
|
|
| 792 |
height: monitorData.height,
|
| 793 |
x: monitorData.x - xOffset,
|
| 794 |
y: monitorData.y - yOffset,
|
| 795 |
+
visible: monitorData.visible,
|
| 796 |
+
variableType: monitorData.variableType,
|
| 797 |
+
variableId: monitorData.variableId
|
| 798 |
};
|
| 799 |
if (monitorData.mode !== 'list') {
|
| 800 |
serializedMonitor.sliderMin = monitorData.sliderMin;
|
|
|
|
| 817 |
// Create extension set to hold extension ids found while serializing targets
|
| 818 |
const extensions = getExtensionIDs(runtime);
|
| 819 |
|
| 820 |
+
const originalTargetsToSerialize = (
|
| 821 |
+
targetId
|
| 822 |
+
? [runtime.getTargetById(targetId)]
|
| 823 |
+
: runtime.targets.filter(target => target.isOriginal)
|
| 824 |
+
);
|
| 825 |
|
| 826 |
const layerOrdering = getSimplifiedLayerOrdering(originalTargetsToSerialize);
|
| 827 |
|
|
|
|
| 849 |
// add extension datas
|
| 850 |
target.extensionData = {};
|
| 851 |
for (const extension of extensions) {
|
| 852 |
+
if (`ext_${extension}` in runtime && typeof runtime[`ext_${extension}`].serialize === 'function') {
|
| 853 |
+
target.extensionData[extension] = runtime[`ext_${extension}`].serialize();
|
| 854 |
+
continue;
|
| 855 |
+
}
|
| 856 |
+
if (extension in runtime.extensionStorage) {
|
| 857 |
+
target.extensionData[extension] = runtime.extensionStorage[extension]
|
| 858 |
+
continue;
|
| 859 |
}
|
| 860 |
}
|
| 861 |
|
|
|
|
| 872 |
// add extension datas
|
| 873 |
obj.extensionData = {};
|
| 874 |
for (const extension of extensions) {
|
| 875 |
+
if (`ext_${extension}` in runtime && typeof runtime[`ext_${extension}`].serialize === 'function') {
|
| 876 |
+
obj.extensionData[extension] = runtime[`ext_${extension}`].serialize();
|
| 877 |
+
continue;
|
| 878 |
+
}
|
| 879 |
+
if (extension in runtime.extensionStorage) {
|
| 880 |
+
obj.extensionData[extension] = runtime.extensionStorage[extension]
|
| 881 |
+
continue;
|
| 882 |
}
|
| 883 |
}
|
| 884 |
|
|
|
|
| 906 |
meta.agent = '';
|
| 907 |
// TW: Never include full user agent to slightly improve user privacy
|
| 908 |
// if (typeof navigator !== 'undefined') meta.agent = navigator.userAgent;
|
| 909 |
+
|
| 910 |
// Attach platform information so TurboWarp and other mods can detect where the file comes from
|
| 911 |
const platform = Object.create(null);
|
| 912 |
platform.name = "PenguinMod";
|
|
|
|
| 1398 |
}
|
| 1399 |
}
|
| 1400 |
if (object.hasOwnProperty('customVars')) {
|
| 1401 |
+
for (const variable of object.customVars) {
|
| 1402 |
+
if (Array.isArray(variable)) {
|
| 1403 |
+
// Legacy behaviour.
|
| 1404 |
+
const newVar = runtime.newVariableInstance(...variable);
|
| 1405 |
+
target.variables[variable[1]] = newVar;
|
| 1406 |
+
continue;
|
| 1407 |
+
}
|
| 1408 |
+
|
| 1409 |
+
/*
|
| 1410 |
+
{
|
| 1411 |
+
type: v.type,
|
| 1412 |
+
id: varId,
|
| 1413 |
+
name: v.name,
|
| 1414 |
+
info,
|
| 1415 |
+
}
|
| 1416 |
+
*/
|
| 1417 |
+
const newVar = runtime.newVariableInstance(variable.type, ...variable.info);
|
| 1418 |
+
target.variables[variable.id] = newVar;
|
| 1419 |
}
|
| 1420 |
}
|
| 1421 |
if (object.hasOwnProperty('comments')) {
|
|
|
|
| 1473 |
if (object.hasOwnProperty('id') && !existingTargetIds.includes(object.id)) {
|
| 1474 |
target.id = object.id;
|
| 1475 |
}
|
| 1476 |
+
|
| 1477 |
+
if (object.hasOwnProperty('extensionData')) {
|
| 1478 |
+
target.extensionData = object.extensionData;
|
| 1479 |
+
}
|
| 1480 |
+
|
| 1481 |
Promise.all(costumePromises).then(costumes => {
|
| 1482 |
sprite.costumes = costumes;
|
| 1483 |
});
|
|
|
|
| 1536 |
name: paramKey,
|
| 1537 |
value: monitorData.params[paramKey]
|
| 1538 |
};
|
| 1539 |
+
if (typeof monitorData.params[paramKey] === 'object') {
|
| 1540 |
+
field.id = monitorData.params[paramKey].id;
|
| 1541 |
+
field.value = monitorData.params[paramKey].name;
|
| 1542 |
+
}
|
| 1543 |
fields[paramKey] = field;
|
| 1544 |
}
|
| 1545 |
|
|
|
|
| 1595 |
const field = monitorBlock.fields.LIST;
|
| 1596 |
field.id = monitorData.id;
|
| 1597 |
field.variableType = Variable.LIST_TYPE;
|
| 1598 |
+
} else if (monitorData.variableId) {
|
| 1599 |
+
const field = Object.values(monitorBlock.fields)[0];
|
| 1600 |
+
field.id = monitorData.variableId;
|
| 1601 |
+
field.variableType = monitorData.variableType;
|
| 1602 |
}
|
| 1603 |
|
| 1604 |
runtime.monitorBlocks.createBlock(monitorBlock);
|
|
|
|
| 1728 |
deserializeStandaloneBlocks: deserializeStandaloneBlocks,
|
| 1729 |
serializeStandaloneBlocks: serializeStandaloneBlocks,
|
| 1730 |
getExtensionIdForOpcode: getExtensionIdForOpcode
|
| 1731 |
+
};
|