Skip to content

Commit

Permalink
Refactor platform serialization to make it easier for forks
Browse files Browse the repository at this point in the history
Also allows extensions an easy way to access it
  • Loading branch information
GarboMuffin committed May 9, 2024
1 parent 6a9bed0 commit 7efdf51
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/engine/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const xmlEscape = require('../util/xml-escape');
const ScratchLinkWebSocket = require('../util/scratch-link-websocket');
const FontManager = require('./tw-font-manager');
const fetchWithTimeout = require('../util/fetch-with-timeout');
const platform = require('./tw-platform.js');

// Virtual I/O devices.
const Clock = require('../io/clock');
Expand Down Expand Up @@ -439,6 +440,11 @@ class Runtime extends EventEmitter {
*/
this.origin = null;

/**
* Metadata about the platform this VM is part of.
*/
this.platform = Object.assign({}, platform);

this._initScratchLink();

this.resetRunId();
Expand Down
7 changes: 7 additions & 0 deletions src/engine/tw-platform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Forks should change this.
// This can be accessed externally on `vm.runtime.platform`

module.exports = {
name: 'TurboWarp',
url: 'https://turbowarp.org/'
};
7 changes: 2 additions & 5 deletions src/serialization/sb3.js
Original file line number Diff line number Diff line change
Expand Up @@ -794,11 +794,8 @@ const serialize = function (runtime, targetId, {allowOptimization = true} = {})
// TW: Never include full user agent to slightly improve user privacy
// if (typeof navigator !== 'undefined') meta.agent = navigator.userAgent;

// TW: Attach platform information (should be changed for forks of TurboWarp)
const platform = Object.create(null);
platform.name = 'TurboWarp';
platform.url = 'https://turbowarp.org/';
meta.platform = platform;
// TW: Attach copy of platform information
meta.platform = Object.assign({}, runtime.platform);

// Assemble payload and return
obj.meta = meta;
Expand Down
26 changes: 26 additions & 0 deletions test/integration/tw_platform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const {test} = require('tap');
const VM = require('../../src/virtual-machine');
const platform = require('../../src/engine/tw-platform');

test('the internal object', t => {
// the idea with this test is to make it harder for forks to screw up modifying the file
t.type(platform.name, 'string');
t.type(platform.url, 'string');
t.end();
});

test('vm property', t => {
const vm = new VM();
t.same(vm.runtime.platform, platform, 'copy of tw-platform.js');
t.not(vm.runtime.platform, platform, 'not the same object as tw-platform.js');
t.end();
});

test('sanitize', t => {
const vm = new VM();
vm.runtime.platform.name += ' - test';
const json = JSON.parse(vm.toJSON());
t.same(json.meta.platform, vm.runtime.platform, 'copy of runtime.platform');
t.not(json.meta.platform, vm.runtime.platform, 'not the same object as runtime.platform');
t.end();
});

0 comments on commit 7efdf51

Please sign in to comment.