mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-07-29 23:49:21 -04:00
Merge pull request #3067 from ericrosenbaum/save-and-load-origin
Save and load origin metadata
This commit is contained in:
commit
80e25f7b2a
5 changed files with 54 additions and 0 deletions
src
test
|
@ -392,6 +392,13 @@ class Runtime extends EventEmitter {
|
||||||
* @type {function}
|
* @type {function}
|
||||||
*/
|
*/
|
||||||
this.removeCloudVariable = this._initializeRemoveCloudVariable(newCloudDataManager);
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -561,6 +561,9 @@ const serialize = function (runtime, targetId) {
|
||||||
const meta = Object.create(null);
|
const meta = Object.create(null);
|
||||||
meta.semver = '3.0.0';
|
meta.semver = '3.0.0';
|
||||||
meta.vm = vmPackage.version;
|
meta.vm = vmPackage.version;
|
||||||
|
if (runtime.origin) {
|
||||||
|
meta.origin = runtime.origin;
|
||||||
|
}
|
||||||
|
|
||||||
// Attach full user agent string to metadata if available
|
// Attach full user agent string to metadata if available
|
||||||
meta.agent = 'none';
|
meta.agent = 'none';
|
||||||
|
@ -1235,6 +1238,13 @@ const deserialize = function (json, runtime, zip, isSingleSprite) {
|
||||||
extensionURLs: new Map()
|
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,
|
// First keep track of the current target order in the json,
|
||||||
// then sort by the layer order property before parsing the targets
|
// then sort by the layer order property before parsing the targets
|
||||||
// so that their corresponding render drawables can be created in
|
// so that their corresponding render drawables can be created in
|
||||||
|
|
BIN
test/fixtures/origin-absent.sb3
vendored
Normal file
BIN
test/fixtures/origin-absent.sb3
vendored
Normal file
Binary file not shown.
BIN
test/fixtures/origin.sb3
vendored
Normal file
BIN
test/fixtures/origin.sb3
vendored
Normal file
Binary file not shown.
|
@ -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 variableReporterSB2ProjectPath = path.resolve(__dirname, '../fixtures/top-level-variable-reporter.sb2');
|
||||||
const topLevelReportersProjectPath = path.resolve(__dirname, '../fixtures/top-level-reporters.sb3');
|
const topLevelReportersProjectPath = path.resolve(__dirname, '../fixtures/top-level-reporters.sb3');
|
||||||
const draggableSB3ProjectPath = path.resolve(__dirname, '../fixtures/draggable.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');
|
const FakeRenderer = require('../fixtures/fake-renderer');
|
||||||
|
|
||||||
test('serialize', t => {
|
test('serialize', t => {
|
||||||
|
@ -324,3 +326,38 @@ test('(#1850) sprite draggability state read when loading SB3 file', t => {
|
||||||
t.end();
|
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();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue