Skip to content

Commit

Permalink
Merge pull request scratchfoundation#3067 from ericrosenbaum/save-and…
Browse files Browse the repository at this point in the history
…-load-origin

Save and load origin metadata
  • Loading branch information
ericrosenbaum authored May 10, 2021
2 parents 65f22c1 + 7a2393b commit 80e25f7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/engine/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/serialization/sb3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down
Binary file added test/fixtures/origin-absent.sb3
Binary file not shown.
Binary file added test/fixtures/origin.sb3
Binary file not shown.
37 changes: 37 additions & 0 deletions test/unit/serialization_sb3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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();
});
});

0 comments on commit 80e25f7

Please sign in to comment.