mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-23 06:23:37 -05:00
Merge pull request #2325 from adroitwhiz/fix-list-monitor-names
Fix old/incorrect list monitor names during deserialization
This commit is contained in:
commit
7fbdf969a1
3 changed files with 66 additions and 0 deletions
|
@ -1098,6 +1098,20 @@ const deserializeMonitor = function (monitorData, runtime, targets, extensions)
|
||||||
// This will be undefined for extension blocks
|
// This will be undefined for extension blocks
|
||||||
const monitorBlockInfo = runtime.monitorBlockInfo[monitorData.opcode];
|
const monitorBlockInfo = runtime.monitorBlockInfo[monitorData.opcode];
|
||||||
|
|
||||||
|
// Due to a bug (see https://github.com/LLK/scratch-vm/pull/2322), renamed list monitors may have been serialized
|
||||||
|
// with an outdated/incorrect LIST parameter. Fix it up to use the current name of the actual corresponding list.
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Convert the serialized monitorData params into the block fields structure
|
// Convert the serialized monitorData params into the block fields structure
|
||||||
const fields = {};
|
const fields = {};
|
||||||
for (const paramKey in monitorData.params) {
|
for (const paramKey in monitorData.params) {
|
||||||
|
|
BIN
test/fixtures/list-monitor-rename.sb3
vendored
Normal file
BIN
test/fixtures/list-monitor-rename.sb3
vendored
Normal file
Binary file not shown.
52
test/integration/list-monitor-rename.js
Normal file
52
test/integration/list-monitor-rename.js
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
const path = require('path');
|
||||||
|
const test = require('tap').test;
|
||||||
|
const makeTestStorage = require('../fixtures/make-test-storage');
|
||||||
|
const readFileToBuffer = require('../fixtures/readProjectFile').readFileToBuffer;
|
||||||
|
const VirtualMachine = require('../../src/index');
|
||||||
|
|
||||||
|
const projectUri = path.resolve(__dirname, '../fixtures/list-monitor-rename.sb3');
|
||||||
|
const project = readFileToBuffer(projectUri);
|
||||||
|
|
||||||
|
test('importing sb3 project with incorrect list monitor name', t => {
|
||||||
|
const vm = new VirtualMachine();
|
||||||
|
vm.attachStorage(makeTestStorage());
|
||||||
|
|
||||||
|
// Evaluate playground data and exit
|
||||||
|
vm.on('playgroundData', () => {
|
||||||
|
const stage = vm.runtime.targets[0];
|
||||||
|
const cat = vm.runtime.targets[1];
|
||||||
|
|
||||||
|
for (const {target, renamedListName} of [
|
||||||
|
{target: stage, renamedListName: 'renamed global'},
|
||||||
|
{target: cat, renamedListName: 'renamed local'}
|
||||||
|
]) {
|
||||||
|
const listId = Object.keys(target.variables).find(k => target.variables[k].name === renamedListName);
|
||||||
|
|
||||||
|
const monitorRecord = vm.runtime._monitorState.get(listId);
|
||||||
|
const monitorBlock = vm.runtime.monitorBlocks.getBlock(listId);
|
||||||
|
t.equal(monitorRecord.opcode, 'data_listcontents');
|
||||||
|
|
||||||
|
// The list name should be properly renamed
|
||||||
|
t.equal(monitorRecord.params.LIST, renamedListName);
|
||||||
|
t.equal(monitorBlock.fields.LIST.value, renamedListName);
|
||||||
|
}
|
||||||
|
|
||||||
|
t.end();
|
||||||
|
process.nextTick(process.exit);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Start VM, load project, and run
|
||||||
|
t.doesNotThrow(() => {
|
||||||
|
vm.start();
|
||||||
|
vm.clear();
|
||||||
|
vm.setCompatibilityMode(false);
|
||||||
|
vm.setTurboMode(false);
|
||||||
|
vm.loadProject(project).then(() => {
|
||||||
|
vm.greenFlag();
|
||||||
|
setTimeout(() => {
|
||||||
|
vm.getPlaygroundData();
|
||||||
|
vm.stopAll();
|
||||||
|
}, 100);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in a new issue