Confirm extension in use in sb2 serialization (#1643)

Add a check when serializing sb2 projects to see whether an extension is actually in use in a block or a visible monitor.
This commit is contained in:
Katie Broida 2018-10-17 10:49:08 -04:00 committed by GitHub
parent 17874030e6
commit 899ce56214
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 111 additions and 2 deletions

View file

@ -1596,6 +1596,14 @@ class Runtime extends EventEmitter {
return count;
}
/**
*
* @return {OrderedMap} The current state of monitor blocks.
*/
getMonitorState () {
return this._monitorState;
}
/**
* Queue monitor blocks to sequencer to be run.
*/

View file

@ -179,7 +179,8 @@ class Scratch3VideoSensingBlocks {
if (stage) {
return stage.videoState;
}
return VideoState.ON;
// Default to off to prevent a flash of video while the project is loading
return VideoState.OFF;
}
set globalVideoState (state) {

View file

@ -334,6 +334,55 @@ const parseMonitorObject = (object, runtime, targets, extensions) => {
}));
};
const confirmTargetExtensions = function (targets, extensions) {
// Ensure only extensions used by blocks or visible monitors are enabled
const extensionsInUse = new Set();
for (const ext of extensions.extensionIDs) {
let extensionConfirmed = false;
for (const target of targets) {
const targetBlocks = Object.entries(target.blocks._blocks);
// Make sure there is a block that uses the currently set extensions
extensionConfirmed = targetBlocks.some(block => {
const opcode = block[1].opcode;
if (opcode && (ext === opcode.split('_')[0])) {
return true;
}
return false;
});
if (extensionConfirmed) {
extensionsInUse.add(ext);
break;
}
const monitorState = target.runtime.getMonitorState();
// Check if a visible monitor uses this extension
extensionConfirmed = monitorState.some(monitor => {
if (!monitor.visible) {
return false;
}
if (monitor.opcode && (ext === monitor.opcode.split('_')[0])) {
return true;
}
return false;
});
if (extensionConfirmed) {
extensionsInUse.add(ext);
break;
}
}
}
extensions.extensionIDs = extensionsInUse;
return extensions;
};
/**
* Parse a single "Scratch object" and create all its in-memory VM objects.
* TODO: parse the "info" section, especially "savedExtensions"
@ -598,6 +647,8 @@ const parseScratchObject = function (object, runtime, extensions, topLevel, zip)
if (object.info.hasOwnProperty('videoOn')) {
if (object.info.videoOn) {
target.videoState = RenderedTarget.VIDEO_STATE.ON;
} else {
target.videoState = RenderedTarget.VIDEO_STATE.OFF;
}
}
}
@ -686,6 +737,9 @@ const parseScratchObject = function (object, runtime, extensions, topLevel, zip)
for (let n = 0; n < deferredMonitors.length; n++) {
parseMonitorObject(deferredMonitors[n], runtime, targets, extensions);
}
extensions = confirmTargetExtensions(targets, extensions);
return targets;
})
);
@ -705,7 +759,6 @@ const reorderParsedTargets = function (targets) {
return targets;
};
/**
* Top-level handler. Parse provided JSON,
* and process the top-level object (the stage object).

BIN
test/fixtures/extensions-in-blocks.sb2 vendored Normal file

Binary file not shown.

BIN
test/fixtures/extensions-in-monitors.sb2 vendored Normal file

Binary file not shown.

Binary file not shown.

View file

@ -102,3 +102,50 @@ test('Ordering', t => {
t.end();
});
});
test('Extensions used in blocks serialized', t => {
const uri = path.resolve(__dirname, '../fixtures/extensions-in-blocks.sb2');
const json = extractProjectJson(uri);
const rt = new Runtime();
const expectedExtensionIDs = new Set(['videoSensing', 'wedo2', 'pen']);
// Make sure any extensions loaded in a block are added to the project
sb2.deserialize(json, rt).then(({extensions}) => {
t.deepEquals(extensions.extensionIDs, expectedExtensionIDs);
t.end();
});
});
test('Extensions used in visible monitors serialized', t => {
const uri = path.resolve(__dirname, '../fixtures/extensions-in-monitors.sb2');
const json = extractProjectJson(uri);
const rt = new Runtime();
sb2.deserialize(json, rt).then(({extensions}) => {
const monitorState = rt.getMonitorState();
const monitor = monitorState.first();
const monitorOpcode = monitor.opcode.split('_')[0];
t.ok(extensions.extensionIDs.has(monitorOpcode));
t.equals(monitor.visible, true);
t.end();
});
});
test('No extensions serialized', t => {
// This test project has had the video motion monitor checked, saved,
// then unchecked and then saved
const uri = path.resolve(__dirname, '../fixtures/extensions-not-serialized.sb2');
const json = extractProjectJson(uri);
const rt = new Runtime();
sb2.deserialize(json, rt).then(({extensions}) => {
const monitorState = rt.getMonitorState();
const monitor = monitorState.first();
const monitorOpcode = monitor.opcode.split('_')[0];
t.equals(monitor.visible, false);
t.notOk(extensions.extensionIDs.has(monitorOpcode));
t.end();
});
});