Merge pull request #3067 from ericrosenbaum/save-and-load-origin

Save and load origin metadata
This commit is contained in:
Eric Rosenbaum 2021-05-10 12:18:34 -04:00 committed by GitHub
commit 80e25f7b2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 54 additions and 0 deletions

View File

@ -392,6 +392,13 @@ class Runtime extends EventEmitter {
* @type {function}
*/
this.removeCloudVariable = this._initializeRemoveCloudVariable(newCloudDataManager);
/**
* A string representing the origin of the current project from outside of the
* Scratch community, such as CSFirst.
* @type {?string}
*/
this.origin = null;
}
/**

View File

@ -561,6 +561,9 @@ const serialize = function (runtime, targetId) {
const meta = Object.create(null);
meta.semver = '3.0.0';
meta.vm = vmPackage.version;
if (runtime.origin) {
meta.origin = runtime.origin;
}
// Attach full user agent string to metadata if available
meta.agent = 'none';
@ -1235,6 +1238,13 @@ const deserialize = function (json, runtime, zip, isSingleSprite) {
extensionURLs: new Map()
};
// Store the origin field (e.g. project originated at CSFirst) so that we can save it again.
if (json.meta && json.meta.origin) {
runtime.origin = json.meta.origin;
} else {
runtime.origin = null;
}
// First keep track of the current target order in the json,
// then sort by the layer order property before parsing the targets
// so that their corresponding render drawables can be created in

BIN
test/fixtures/origin-absent.sb3 vendored Normal file

Binary file not shown.

BIN
test/fixtures/origin.sb3 vendored Normal file

Binary file not shown.

View File

@ -11,6 +11,8 @@ const commentsSB3NoDupeIds = path.resolve(__dirname, '../fixtures/comments_no_du
const variableReporterSB2ProjectPath = path.resolve(__dirname, '../fixtures/top-level-variable-reporter.sb2');
const topLevelReportersProjectPath = path.resolve(__dirname, '../fixtures/top-level-reporters.sb3');
const draggableSB3ProjectPath = path.resolve(__dirname, '../fixtures/draggable.sb3');
const originSB3ProjectPath = path.resolve(__dirname, '../fixtures/origin.sb3');
const originAbsentSB3ProjectPath = path.resolve(__dirname, '../fixtures/origin-absent.sb3');
const FakeRenderer = require('../fixtures/fake-renderer');
test('serialize', t => {
@ -324,3 +326,38 @@ test('(#1850) sprite draggability state read when loading SB3 file', t => {
t.end();
});
});
test('load origin value from SB3 file json metadata', t => {
const vm = new VirtualMachine();
vm.loadProject(readFileToBuffer(originSB3ProjectPath))
.then(() => {
t.type(vm.runtime.origin, 'string');
})
.then(() => vm.loadProject(readFileToBuffer(originAbsentSB3ProjectPath)))
.then(() => {
// After loading a project with an origin, then loading one without an origin,
// origin value should no longer be set.
t.equal(vm.runtime.origin, null);
t.end();
});
});
test('serialize origin value if it is present', t => {
const vm = new VirtualMachine();
vm.loadProject(readFileToBuffer(originSB3ProjectPath))
.then(() => {
const result = sb3.serialize(vm.runtime);
t.type(result.meta.origin, 'string');
t.end();
});
});
test('do not serialize origin value if it is not present', t => {
const vm = new VirtualMachine();
vm.loadProject(readFileToBuffer(originAbsentSB3ProjectPath))
.then(() => {
const result = sb3.serialize(vm.runtime);
t.equal(result.meta.origin, undefined);
t.end();
});
});