@wasm-gaming/rsdkv5-wasm 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +106 -6
- package/dist/rsdkv5/app/app.css +176 -0
- package/dist/rsdkv5/app/app.js +527 -0
- package/dist/rsdkv5/app/assets/player-knuckles.svg +757 -0
- package/dist/rsdkv5/app/assets/player-sonic.svg +720 -0
- package/dist/rsdkv5/app/assets/player-sonictails.svg +1414 -0
- package/dist/rsdkv5/app/assets/player-tails.svg +979 -0
- package/dist/rsdkv5/engine-specs.js +382 -0
- package/dist/rsdkv5/rsdkv5.js +1 -1
- package/dist/rsdkv5/rsdkv5.options.d.ts +61 -13
- package/dist/rsdkv5/rsdkv5.options.d.ts.map +1 -1
- package/dist/rsdkv5/rsdkv5.options.js +35 -21
- package/dist/rsdkv5/rsdkv5.options.js.map +1 -1
- package/dist/rsdkv5/rsdkv5.sdk.d.ts +137 -32
- package/dist/rsdkv5/rsdkv5.sdk.d.ts.map +1 -1
- package/dist/rsdkv5/rsdkv5.sdk.js +474 -181
- package/dist/rsdkv5/rsdkv5.sdk.js.map +1 -1
- package/dist/rsdkv5/rsdkv5.wasm +0 -0
- package/package.json +9 -15
- package/src/app/app.css +176 -0
- package/src/app/app.js +527 -0
- package/src/app/assets/player-knuckles.svg +757 -0
- package/src/app/assets/player-sonic.svg +720 -0
- package/src/app/assets/player-sonictails.svg +1414 -0
- package/src/app/assets/player-tails.svg +979 -0
- package/src/rsdkv5.options.ts +96 -43
- package/src/rsdkv5.sdk.ts +562 -203
- package/dist/manifest.json +0 -69
- package/dist/rsdkv5/rsdkv5.manifest.d.ts +0 -4
- package/dist/rsdkv5/rsdkv5.manifest.d.ts.map +0 -1
- package/dist/rsdkv5/rsdkv5.manifest.js +0 -39
- package/dist/rsdkv5/rsdkv5.manifest.js.map +0 -1
- package/src/rsdkv5.manifest.ts +0 -44
|
@@ -1,35 +1,120 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
/**
|
|
2
|
+
* The engine package's entry point: an `EngineSDK` for RSDKv5 (Sonic Mania).
|
|
3
|
+
*
|
|
4
|
+
* ```js
|
|
5
|
+
* import Rsdkv5SDK from '@wasm-gaming/rsdkv5-wasm';
|
|
6
|
+
*
|
|
7
|
+
* const play = await new Rsdkv5SDK()
|
|
8
|
+
* .assets({ data: () => readDataRsdk() }) // bytes, or a thunk
|
|
9
|
+
* .storage({ namespace: 'sonic-mania' }) // → OPFS rsdkv5/sonic-mania/
|
|
10
|
+
* .mount(document.querySelector('#stage'))
|
|
11
|
+
* .start();
|
|
12
|
+
*
|
|
13
|
+
* play.pause('menu'); play.resume('menu');
|
|
14
|
+
* play.devMenu.getStageList(); // the host draws its own stage select
|
|
15
|
+
* await play.destroy(); // save flushed, loop stopped, audio closed
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* One game-agnostic `rsdkv5.wasm`: `Data.rsdk` is written into the filesystem at
|
|
19
|
+
* runtime, never baked in. The working dir `/data/rsdkv5/<namespace>` is in memory;
|
|
20
|
+
* Sonic Mania writes its user files there (`SaveData.bin`, `TimeAttackDB.bin`,
|
|
21
|
+
* `ReplayDB.bin`, `Replay_*.bin`). The SDK seeds them from OPFS `rsdkv5/<namespace>/`
|
|
22
|
+
* before boot and mirrors back whatever changed (every 10 s, on pause, on page hide
|
|
23
|
+
* and on destroy).
|
|
24
|
+
*/
|
|
25
|
+
import { EnginePlayBase, EngineSDKBase, EventEmitter, } from './engine-specs.js';
|
|
26
|
+
import { RSDKV5_SPEC, RSDKV5_VIDEO, } from './rsdkv5.options.js';
|
|
27
|
+
export { RSDKV5_SPEC, RSDKV5_VIDEO } from './rsdkv5.options.js';
|
|
28
|
+
const ENGINE_ID = 'rsdkv5';
|
|
6
29
|
const DEFAULT_STORAGE_NAMESPACE = 'default';
|
|
30
|
+
const WORK_ROOT = '/data';
|
|
31
|
+
/** Inputs the SDK writes itself; everything else in the working dir is the game's. */
|
|
32
|
+
const NOT_SAVES = new Set(['Data.rsdk', 'Settings.ini', 'log.txt']);
|
|
33
|
+
const SAVE_INTERVAL_MS = 10_000;
|
|
34
|
+
/** Mania's characters, by the `characterID` its save slots store. */
|
|
35
|
+
export const MANIA_CHARACTERS = ['Sonic & Tails', 'Sonic', 'Tails', 'Knuckles', 'Mighty', 'Ray'];
|
|
36
|
+
/** Mania Mode's zones, by the `zoneID` its save slots store. */
|
|
37
|
+
export const MANIA_ZONES = [
|
|
38
|
+
'Green Hill', 'Chemical Plant', 'Studiopolis', 'Flying Battery', 'Press Garden', 'Stardust Speedway',
|
|
39
|
+
'Hydrocity', 'Mirage Saloon', 'Oil Ocean', 'Lava Reef', 'Metallic Madness', 'Titanic Monarch', 'Egg Reverie',
|
|
40
|
+
];
|
|
41
|
+
/** Mania Plus's eight regular save slots in `SaveData.bin` (Encore slots are elsewhere). */
|
|
42
|
+
export const MANIA_SAVE_SLOTS = 8;
|
|
43
|
+
const SLOT_BYTES = 0x400;
|
|
44
|
+
const SLOT_FIELDS = 0x58; // SaveRAM: the entity header Mania keeps before the fields
|
|
45
|
+
/**
|
|
46
|
+
* Mania Mode's save slots from `SaveData.bin` (`globals->saveRAM`, int32 LE): slot `s`
|
|
47
|
+
* is the 0x400-byte block at `0x400 * s`, fields from +0x58. A missing or short file
|
|
48
|
+
* reads as empty slots.
|
|
49
|
+
*/
|
|
50
|
+
export function parseManiaSaves(bytes) {
|
|
51
|
+
const view = bytes ? new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength) : null;
|
|
52
|
+
return Array.from({ length: MANIA_SAVE_SLOTS }, (_, slot) => {
|
|
53
|
+
const base = slot * SLOT_BYTES + SLOT_FIELDS;
|
|
54
|
+
const field = (i) => (view && base + i * 4 + 4 <= view.byteLength ? view.getInt32(base + i * 4, true) : 0);
|
|
55
|
+
const state = field(0) === 1 ? 'progress' : field(0) === 2 ? 'complete' : 'empty';
|
|
56
|
+
const character = Math.max(0, Math.min(MANIA_CHARACTERS.length - 1, field(1)));
|
|
57
|
+
const zone = field(2);
|
|
58
|
+
let emeralds = 0;
|
|
59
|
+
for (let mask = field(6) & 0x7f; mask; mask >>= 1)
|
|
60
|
+
emeralds += mask & 1;
|
|
61
|
+
return {
|
|
62
|
+
slot,
|
|
63
|
+
state,
|
|
64
|
+
character: state === 'empty' ? 0 : character,
|
|
65
|
+
zone: state === 'empty' ? 0 : zone,
|
|
66
|
+
zoneName: state === 'complete' ? 'Complete' : state === 'empty' ? '' : (MANIA_ZONES[zone] ?? 'Unknown zone'),
|
|
67
|
+
lives: state === 'empty' ? 3 : field(3),
|
|
68
|
+
score: state === 'empty' ? 0 : field(4),
|
|
69
|
+
emeralds: state === 'empty' ? 0 : emeralds,
|
|
70
|
+
continues: state === 'empty' ? 0 : field(7),
|
|
71
|
+
};
|
|
72
|
+
});
|
|
73
|
+
}
|
|
7
74
|
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* `
|
|
12
|
-
*
|
|
75
|
+
* Where an engine keeps one namespace: `<engineId>/<namespace>`, relative to the OPFS
|
|
76
|
+
* root. Identical in every wasm-gaming engine, written to move verbatim into
|
|
77
|
+
* engine-specs: segments cleaned, `.` and `..` dropped, anything outside
|
|
78
|
+
* `[A-Za-z0-9._-]` → `_`, nothing usable → `default`; a leading `<engineId>/` is
|
|
79
|
+
* stripped once.
|
|
13
80
|
*/
|
|
14
|
-
function
|
|
15
|
-
const
|
|
81
|
+
export function storagePath(engineId, namespace) {
|
|
82
|
+
const segments = typeof namespace === 'string'
|
|
83
|
+
? namespace
|
|
84
|
+
.split('/')
|
|
85
|
+
.map((segment) => segment.trim())
|
|
86
|
+
.filter((segment) => segment && segment !== '.' && segment !== '..')
|
|
87
|
+
.map((segment) => segment.replace(/[^A-Za-z0-9._-]/g, '_'))
|
|
88
|
+
: [];
|
|
89
|
+
const prefixed = segments.length > 1 && segments[0] === engineId;
|
|
90
|
+
if (prefixed)
|
|
91
|
+
segments.shift();
|
|
92
|
+
const cleaned = segments.join('/') || DEFAULT_STORAGE_NAMESPACE;
|
|
93
|
+
return { namespace: cleaned, path: `${engineId}/${cleaned}`, prefixed };
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Serialise the engine config into RSDKv5U's `Settings.ini` (capital S — the engine
|
|
97
|
+
* opens exactly that name, and the Emscripten FS is case-sensitive). The v5 dialect:
|
|
98
|
+
* lowercase keys, 0/1 booleans, `dataFile=` naming the pack relative to the working dir.
|
|
99
|
+
*/
|
|
100
|
+
export function buildSettingsIni(config) {
|
|
16
101
|
const b = (v) => (v ? '1' : '0');
|
|
17
102
|
return [
|
|
18
103
|
'[Game]',
|
|
19
104
|
'dataFile=Data.rsdk',
|
|
20
|
-
`devMenu=${b(
|
|
21
|
-
`language=${
|
|
22
|
-
`region=${
|
|
105
|
+
`devMenu=${b(config.devMenu)}`,
|
|
106
|
+
`language=${(config.language ?? 0) | 0}`,
|
|
107
|
+
`region=${(config.region ?? -1) | 0}`,
|
|
23
108
|
'',
|
|
24
109
|
'[Video]',
|
|
25
110
|
'windowed=1',
|
|
26
111
|
'border=1',
|
|
27
112
|
'exclusiveFS=0',
|
|
28
|
-
`vsync=${b(
|
|
113
|
+
`vsync=${b(config.vsync ?? true)}`,
|
|
29
114
|
'tripleBuffering=0',
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
115
|
+
`pixWidth=${RSDKV5_VIDEO.width}`,
|
|
116
|
+
`winWidth=${RSDKV5_VIDEO.width}`,
|
|
117
|
+
`winHeight=${RSDKV5_VIDEO.height}`,
|
|
33
118
|
'fsWidth=0',
|
|
34
119
|
'fsHeight=0',
|
|
35
120
|
'refreshRate=60',
|
|
@@ -43,192 +128,400 @@ function buildSettingsIni(options = {}) {
|
|
|
43
128
|
'',
|
|
44
129
|
].join('\n');
|
|
45
130
|
}
|
|
46
|
-
function
|
|
47
|
-
if (
|
|
131
|
+
async function opfsDir(path, create) {
|
|
132
|
+
if (typeof navigator === 'undefined' || !navigator.storage?.getDirectory)
|
|
48
133
|
return null;
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
return
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
134
|
+
try {
|
|
135
|
+
let dir = await navigator.storage.getDirectory();
|
|
136
|
+
for (const segment of path.split('/').filter(Boolean)) {
|
|
137
|
+
dir = await dir.getDirectoryHandle(segment, { create });
|
|
138
|
+
}
|
|
139
|
+
return dir;
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
58
144
|
}
|
|
59
|
-
|
|
60
|
-
function
|
|
61
|
-
if (
|
|
62
|
-
return
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
.filter(Boolean)
|
|
69
|
-
.join('/');
|
|
70
|
-
return cleaned || DEFAULT_STORAGE_NAMESPACE;
|
|
145
|
+
async function resolveAsset(asset) {
|
|
146
|
+
const value = typeof asset === 'function' ? await asset() : asset;
|
|
147
|
+
if (value == null)
|
|
148
|
+
return null;
|
|
149
|
+
if (value instanceof Uint8Array)
|
|
150
|
+
return value;
|
|
151
|
+
if (value instanceof ArrayBuffer)
|
|
152
|
+
return new Uint8Array(value);
|
|
153
|
+
throw new TypeError('rsdkv5: an asset must be a Uint8Array, an ArrayBuffer or a thunk returning one');
|
|
71
154
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
155
|
+
function sameBytes(a, b) {
|
|
156
|
+
if (!a || a.length !== b.length)
|
|
157
|
+
return false;
|
|
158
|
+
for (let i = 0; i < a.length; i++)
|
|
159
|
+
if (a[i] !== b[i])
|
|
160
|
+
return false;
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
function mkdirp(Module, path) {
|
|
75
164
|
let current = '';
|
|
76
|
-
for (const part of
|
|
165
|
+
for (const part of path.split('/').filter(Boolean)) {
|
|
77
166
|
current += `/${part}`;
|
|
78
167
|
try {
|
|
79
168
|
Module.FS.mkdir(current);
|
|
80
169
|
}
|
|
81
170
|
catch {
|
|
82
|
-
/*
|
|
171
|
+
/* exists */
|
|
83
172
|
}
|
|
84
173
|
}
|
|
85
174
|
}
|
|
86
|
-
/**
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
175
|
+
/** The live engine: one booted RSDKv5 and its session. */
|
|
176
|
+
export class Rsdkv5Play extends EnginePlayBase {
|
|
177
|
+
#module = null;
|
|
178
|
+
#destroyed = false;
|
|
179
|
+
#pauseOwner = null;
|
|
180
|
+
#muted = false;
|
|
181
|
+
#canvas = null;
|
|
182
|
+
#ownsCanvas = false;
|
|
183
|
+
#box = null;
|
|
184
|
+
#boxObserver = null;
|
|
185
|
+
#namespace = DEFAULT_STORAGE_NAMESPACE;
|
|
186
|
+
#storagePath = `${ENGINE_ID}/${DEFAULT_STORAGE_NAMESPACE}`;
|
|
187
|
+
#workDir = `${WORK_ROOT}/${ENGINE_ID}/${DEFAULT_STORAGE_NAMESPACE}`;
|
|
188
|
+
#persist = true;
|
|
189
|
+
#saveTimer = null;
|
|
190
|
+
#lastSaved = new Map();
|
|
191
|
+
#flushing = null;
|
|
192
|
+
#onHidden = () => {
|
|
193
|
+
if (document.visibilityState === 'hidden')
|
|
194
|
+
void this.flushSaves();
|
|
195
|
+
};
|
|
196
|
+
constructor(init, events) {
|
|
197
|
+
super(RSDKV5_SPEC, init, events);
|
|
97
198
|
}
|
|
98
|
-
|
|
99
|
-
return
|
|
199
|
+
get namespace() {
|
|
200
|
+
return this.#namespace;
|
|
100
201
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
if (!fileExists(Module, path))
|
|
105
|
-
return false;
|
|
106
|
-
try {
|
|
107
|
-
Module.FS.unlink(path);
|
|
108
|
-
return true;
|
|
202
|
+
/** Where this session's files live, relative to the OPFS root: `rsdkv5/<namespace>`. */
|
|
203
|
+
get storagePath() {
|
|
204
|
+
return this.#storagePath;
|
|
109
205
|
}
|
|
110
|
-
|
|
111
|
-
return
|
|
206
|
+
get pausedBy() {
|
|
207
|
+
return this.#pauseOwner;
|
|
112
208
|
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
209
|
+
/** Mania Mode's save slots — see {@link ManiaSaves}. */
|
|
210
|
+
get saves() {
|
|
211
|
+
const Module = this.#module;
|
|
212
|
+
const workDir = this.#workDir;
|
|
213
|
+
const available = !!Module?._web_mania_save_status && !!Module._web_mania_start_slot;
|
|
214
|
+
return {
|
|
215
|
+
available,
|
|
216
|
+
characters: available && Module?._web_mania_characters ? Module._web_mania_characters() : 4,
|
|
217
|
+
list: () => {
|
|
218
|
+
let bytes = null;
|
|
219
|
+
try {
|
|
220
|
+
bytes = Module ? new Uint8Array(Module.FS.readFile(`${workDir}/SaveData.bin`)) : null;
|
|
221
|
+
}
|
|
222
|
+
catch {
|
|
223
|
+
bytes = null; // no save file yet: eight empty slots
|
|
224
|
+
}
|
|
225
|
+
return parseManiaSaves(bytes);
|
|
226
|
+
},
|
|
227
|
+
ready: async (timeoutMs = 5000) => {
|
|
228
|
+
if (!available || !Module)
|
|
229
|
+
return false;
|
|
230
|
+
const deadline = performance.now() + timeoutMs;
|
|
231
|
+
for (;;) {
|
|
232
|
+
const status = Module._web_mania_save_status();
|
|
233
|
+
if (status === 200)
|
|
234
|
+
return true;
|
|
235
|
+
if ((status !== 0 && status !== 100) || performance.now() > deadline)
|
|
236
|
+
return false;
|
|
237
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
238
|
+
}
|
|
239
|
+
},
|
|
240
|
+
start: (slot, character = 0) => {
|
|
241
|
+
if (!available || !Module)
|
|
242
|
+
return false;
|
|
243
|
+
if (!Module._web_mania_start_slot(slot | 0, character | 0))
|
|
244
|
+
return false;
|
|
245
|
+
// Whatever drove this is done with the engine: hand it back running.
|
|
246
|
+
if (this.#pauseOwner)
|
|
247
|
+
this.resume(this.#pauseOwner);
|
|
248
|
+
return true;
|
|
249
|
+
},
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
/** RSDKv5's stage list, for a host that draws its own stage select. */
|
|
253
|
+
get devMenu() {
|
|
254
|
+
const Module = () => this.#module;
|
|
255
|
+
return {
|
|
256
|
+
getStageList() {
|
|
257
|
+
try {
|
|
258
|
+
return JSON.parse(Module()?.web_devmenu_get_stage_list?.() ?? '[]');
|
|
259
|
+
}
|
|
260
|
+
catch {
|
|
261
|
+
return [];
|
|
262
|
+
}
|
|
263
|
+
},
|
|
264
|
+
loadStage(category, stage) {
|
|
265
|
+
Module()?.web_devmenu_load_stage?.(category | 0, stage | 0);
|
|
266
|
+
},
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Freeze the game and its sound. The first caller owns the pause; only that
|
|
271
|
+
* owner's {@link resume} lifts it. The save is flushed.
|
|
272
|
+
*/
|
|
273
|
+
pause(owner = 'host') {
|
|
274
|
+
const Module = this.#module;
|
|
275
|
+
if (!Module || this.#pauseOwner)
|
|
276
|
+
return;
|
|
277
|
+
this.#pauseOwner = owner;
|
|
278
|
+
Module.web_devmenu_set_paused?.(true);
|
|
279
|
+
// This build has no native pause hook (web_devmenu_* are not compiled in), so the
|
|
280
|
+
// game is frozen by stopping emscripten's main loop: no frame runs until resume().
|
|
281
|
+
Module.pauseMainLoop?.();
|
|
282
|
+
void Module.SDL2?.audioContext?.suspend();
|
|
283
|
+
void this.flushSaves();
|
|
284
|
+
this.emit('pause', { owner });
|
|
285
|
+
}
|
|
286
|
+
resume(owner = 'host') {
|
|
287
|
+
const Module = this.#module;
|
|
288
|
+
if (!Module || this.#pauseOwner !== owner)
|
|
289
|
+
return;
|
|
290
|
+
this.#pauseOwner = null;
|
|
291
|
+
if (!this.#muted)
|
|
292
|
+
void Module.SDL2?.audioContext?.resume();
|
|
293
|
+
Module.web_devmenu_set_paused?.(false);
|
|
294
|
+
Module.resumeMainLoop?.();
|
|
295
|
+
this.emit('resume', { owner });
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Silence the game without stopping it (the frames keep running). A host uses it to
|
|
299
|
+
* let Mania get somewhere — load its save file — behind its own screen. Independent
|
|
300
|
+
* of {@link pause}: a paused game stays silent either way.
|
|
301
|
+
*/
|
|
302
|
+
mute(muted = true) {
|
|
303
|
+
this.#muted = muted;
|
|
304
|
+
const context = this.#module?.SDL2?.audioContext;
|
|
305
|
+
if (muted)
|
|
306
|
+
void context?.suspend();
|
|
307
|
+
else if (!this.#pauseOwner)
|
|
308
|
+
void context?.resume();
|
|
309
|
+
}
|
|
310
|
+
get muted() {
|
|
311
|
+
return this.#muted;
|
|
312
|
+
}
|
|
313
|
+
/** Copy the game's changed user files to OPFS `rsdkv5/<namespace>/`; emits `saves`. */
|
|
314
|
+
flushSaves() {
|
|
315
|
+
this.#flushing ??= this.#flush().finally(() => {
|
|
316
|
+
this.#flushing = null;
|
|
317
|
+
});
|
|
318
|
+
return this.#flushing;
|
|
319
|
+
}
|
|
320
|
+
// ---- Contract hooks ----
|
|
321
|
+
mounted(target) {
|
|
322
|
+
if (target instanceof HTMLCanvasElement) {
|
|
323
|
+
this.#canvas = target;
|
|
324
|
+
this.#ownsCanvas = false;
|
|
325
|
+
this.#box = target.parentElement;
|
|
126
326
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
window
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
canvas
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
settingsBytes = toUint8(await config.settingsProvider());
|
|
165
|
-
}
|
|
166
|
-
if (settingsBytes) {
|
|
167
|
-
Module.FS.writeFile(settingsPath, settingsBytes);
|
|
168
|
-
}
|
|
169
|
-
else if (!fileExists(Module, settingsPath)) {
|
|
170
|
-
Module.FS.writeFile(settingsPath, new TextEncoder().encode(buildSettingsIni(options)));
|
|
171
|
-
}
|
|
172
|
-
Module.FS.chdir(workDir);
|
|
173
|
-
const setPaused = (paused) => {
|
|
174
|
-
if (typeof Module.web_devmenu_set_paused === 'function')
|
|
175
|
-
Module.web_devmenu_set_paused(paused);
|
|
176
|
-
};
|
|
177
|
-
const devMenu = {
|
|
178
|
-
getStageList() {
|
|
179
|
-
if (typeof Module.web_devmenu_get_stage_list !== 'function')
|
|
180
|
-
return [];
|
|
327
|
+
else {
|
|
328
|
+
this.#canvas = document.createElement('canvas');
|
|
329
|
+
this.#ownsCanvas = true;
|
|
330
|
+
this.#box = target;
|
|
331
|
+
target.appendChild(this.#canvas);
|
|
332
|
+
}
|
|
333
|
+
// Emscripten's SDL2 port finds its window with querySelector('#canvas').
|
|
334
|
+
this.#canvas.id = 'canvas';
|
|
335
|
+
this.#canvas.tabIndex = 0;
|
|
336
|
+
this.#canvas.style.display = 'block';
|
|
337
|
+
this.#canvas.style.imageRendering = 'pixelated';
|
|
338
|
+
this.#canvas.addEventListener('contextmenu', (event) => event.preventDefault());
|
|
339
|
+
if (this.#ownsCanvas && this.#box) {
|
|
340
|
+
this.#boxObserver?.disconnect();
|
|
341
|
+
this.#boxObserver = new ResizeObserver(() => this.#fit());
|
|
342
|
+
this.#boxObserver.observe(this.#box);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
async open(_options) {
|
|
346
|
+
if (this.#destroyed || this.#module) {
|
|
347
|
+
// RSDKv5 cannot restart in process: main() runs once and owns the loop.
|
|
348
|
+
throw new Error('rsdkv5: the engine cannot be restarted in process — destroy() and start a new page (or iframe)');
|
|
349
|
+
}
|
|
350
|
+
await this.#boot();
|
|
351
|
+
}
|
|
352
|
+
patched(_kind, _patch) {
|
|
353
|
+
// Nothing in RSDKV5_SPEC is live: Settings.ini is read once, at boot.
|
|
354
|
+
}
|
|
355
|
+
async close() {
|
|
356
|
+
if (this.#saveTimer)
|
|
357
|
+
clearInterval(this.#saveTimer);
|
|
358
|
+
this.#saveTimer = null;
|
|
359
|
+
document.removeEventListener('visibilitychange', this.#onHidden);
|
|
360
|
+
window.removeEventListener('pagehide', this.#onHidden);
|
|
361
|
+
const Module = this.#module;
|
|
362
|
+
if (Module) {
|
|
363
|
+
await this.flushSaves();
|
|
181
364
|
try {
|
|
182
|
-
|
|
365
|
+
Module.web_devmenu_set_paused?.(true);
|
|
366
|
+
Module.pauseMainLoop?.();
|
|
183
367
|
}
|
|
184
|
-
catch
|
|
185
|
-
|
|
186
|
-
return [];
|
|
368
|
+
catch {
|
|
369
|
+
/* already stopped */
|
|
187
370
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
if (typeof Module.web_devmenu_load_stage === 'function') {
|
|
191
|
-
Module.web_devmenu_load_stage(categoryIdx | 0, stageIdx | 0);
|
|
371
|
+
try {
|
|
372
|
+
await Module.SDL2?.audioContext?.close();
|
|
192
373
|
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
};
|
|
196
|
-
// No 'UsingCWD' argv — that's a v4-ism; v5U's main() doesn't parse it. The
|
|
197
|
-
// engine resolves Settings.ini/dataFile relative to the FS CWD set above.
|
|
198
|
-
Module.callMain([]);
|
|
199
|
-
emit({ type: 'ready' });
|
|
200
|
-
return {
|
|
201
|
-
start() { },
|
|
202
|
-
pause() { setPaused(true); },
|
|
203
|
-
resume() { setPaused(false); },
|
|
204
|
-
reset() {
|
|
205
|
-
throw new Error('rsdkv5: reset() is not supported — destroy() and load() again');
|
|
206
|
-
},
|
|
207
|
-
setInput(preset) {
|
|
208
|
-
if (typeof window !== 'undefined') {
|
|
209
|
-
window.__gamepadKeyMap = preset ?? manifest.input;
|
|
374
|
+
catch {
|
|
375
|
+
/* already closed */
|
|
210
376
|
}
|
|
211
|
-
}
|
|
212
|
-
|
|
377
|
+
}
|
|
378
|
+
this.#boxObserver?.disconnect();
|
|
379
|
+
this.#boxObserver = null;
|
|
380
|
+
if (this.#ownsCanvas)
|
|
381
|
+
this.#canvas?.remove();
|
|
382
|
+
this.#canvas = null;
|
|
383
|
+
this.#module = null;
|
|
384
|
+
this.#pauseOwner = null;
|
|
385
|
+
this.#destroyed = true;
|
|
386
|
+
}
|
|
387
|
+
// ---- Boot ----
|
|
388
|
+
async #boot() {
|
|
389
|
+
const config = await this.#values('config');
|
|
390
|
+
const assets = await this.#values('assets');
|
|
391
|
+
const storage = await this.#values('storage');
|
|
392
|
+
if (!this.#canvas)
|
|
393
|
+
throw new Error('rsdkv5: mount() an element (or a canvas) before start()');
|
|
394
|
+
const data = await resolveAsset(assets.data);
|
|
395
|
+
if (!data)
|
|
396
|
+
throw new Error('rsdkv5: no Data.rsdk — hand one over as assets.data (bytes or a thunk)');
|
|
397
|
+
const settings = await resolveAsset(assets.settings);
|
|
398
|
+
const resolved = storagePath(ENGINE_ID, storage.namespace);
|
|
399
|
+
if (resolved.prefixed) {
|
|
400
|
+
console.warn(`[rsdkv5] storage.namespace no longer needs the "${ENGINE_ID}/" prefix. Pass "${resolved.namespace}".`);
|
|
401
|
+
}
|
|
402
|
+
this.#namespace = resolved.namespace;
|
|
403
|
+
this.#storagePath = resolved.path;
|
|
404
|
+
this.#workDir = `${WORK_ROOT}/${resolved.path}`;
|
|
405
|
+
this.#persist = storage.persist !== null;
|
|
406
|
+
const jsUrl = config.jsUrl ?? new URL('./rsdkv5.js', import.meta.url).href;
|
|
407
|
+
const wasmUrl = config.wasmUrl ?? new URL('./rsdkv5.wasm', import.meta.url).href;
|
|
408
|
+
const { default: createRSDKv5 } = (await import(/* @vite-ignore */ jsUrl));
|
|
409
|
+
let failed = null;
|
|
410
|
+
const Module = await createRSDKv5({
|
|
411
|
+
canvas: this.#canvas,
|
|
412
|
+
noInitialRun: true,
|
|
413
|
+
locateFile: (path) => (path.endsWith('.wasm') ? wasmUrl : new URL(path, jsUrl).href),
|
|
414
|
+
print: (text) => console.log('[rsdkv5]', text),
|
|
415
|
+
printErr: (text) => console.warn('[rsdkv5]', text),
|
|
416
|
+
onAbort: (what) => failed?.(new Error(`rsdkv5: aborted — ${String(what)}`)),
|
|
417
|
+
});
|
|
418
|
+
this.#module = Module;
|
|
419
|
+
failed = (error) => this.emit('error', error);
|
|
420
|
+
mkdirp(Module, this.#workDir);
|
|
421
|
+
Module.FS.writeFile(`${this.#workDir}/Data.rsdk`, data);
|
|
422
|
+
Module.FS.writeFile(`${this.#workDir}/Settings.ini`, settings ?? new TextEncoder().encode(buildSettingsIni(config)));
|
|
423
|
+
if (this.#persist)
|
|
424
|
+
await this.#restoreSave();
|
|
425
|
+
Module.FS.chdir(this.#workDir);
|
|
426
|
+
// RSDKv5U resolves Settings.ini and dataFile relative to the CWD set above.
|
|
427
|
+
Module.callMain([]);
|
|
428
|
+
if (this.#persist) {
|
|
429
|
+
this.#saveTimer = setInterval(() => void this.flushSaves(), SAVE_INTERVAL_MS);
|
|
430
|
+
document.addEventListener('visibilitychange', this.#onHidden);
|
|
431
|
+
window.addEventListener('pagehide', this.#onHidden);
|
|
432
|
+
}
|
|
433
|
+
this.#fit();
|
|
434
|
+
}
|
|
435
|
+
async #values(kind) {
|
|
436
|
+
const described = (await this[kind]());
|
|
437
|
+
const out = {};
|
|
438
|
+
for (const [key, property] of Object.entries(described))
|
|
439
|
+
out[key] = property.value;
|
|
440
|
+
return out;
|
|
441
|
+
}
|
|
442
|
+
// ---- Save mirror ----
|
|
443
|
+
async #restoreSave() {
|
|
444
|
+
const dir = await opfsDir(this.#storagePath, false);
|
|
445
|
+
if (!dir || !this.#module)
|
|
446
|
+
return;
|
|
447
|
+
const iterable = dir;
|
|
448
|
+
for await (const [name, handle] of iterable.entries()) {
|
|
449
|
+
if (handle.kind !== 'file' || NOT_SAVES.has(name))
|
|
450
|
+
continue;
|
|
451
|
+
const bytes = new Uint8Array(await (await handle.getFile()).arrayBuffer());
|
|
452
|
+
this.#module.FS.writeFile(`${this.#workDir}/${name}`, bytes);
|
|
453
|
+
this.#lastSaved.set(name, bytes);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
async #flush() {
|
|
457
|
+
const Module = this.#module;
|
|
458
|
+
if (!Module || !this.#persist)
|
|
459
|
+
return;
|
|
460
|
+
const changed = [];
|
|
461
|
+
let names;
|
|
462
|
+
try {
|
|
463
|
+
names = Module.FS.readdir(this.#workDir);
|
|
464
|
+
}
|
|
465
|
+
catch {
|
|
466
|
+
return;
|
|
467
|
+
}
|
|
468
|
+
for (const name of names) {
|
|
469
|
+
if (name === '.' || name === '..' || NOT_SAVES.has(name))
|
|
470
|
+
continue;
|
|
471
|
+
const path = `${this.#workDir}/${name}`;
|
|
472
|
+
if (!Module.FS.isFile(Module.FS.stat(path).mode))
|
|
473
|
+
continue;
|
|
474
|
+
const bytes = new Uint8Array(Module.FS.readFile(path));
|
|
475
|
+
if (!sameBytes(this.#lastSaved.get(name) ?? null, bytes))
|
|
476
|
+
changed.push([name, bytes]);
|
|
477
|
+
}
|
|
478
|
+
if (changed.length === 0)
|
|
479
|
+
return;
|
|
480
|
+
const dir = await opfsDir(this.#storagePath, true);
|
|
481
|
+
if (!dir)
|
|
482
|
+
return;
|
|
483
|
+
const written = [];
|
|
484
|
+
for (const [name, bytes] of changed) {
|
|
213
485
|
try {
|
|
214
|
-
|
|
486
|
+
const writable = await (await dir.getFileHandle(name, { create: true })).createWritable();
|
|
487
|
+
await writable.write(bytes);
|
|
488
|
+
await writable.close();
|
|
489
|
+
this.#lastSaved.set(name, bytes);
|
|
490
|
+
written.push(name);
|
|
215
491
|
}
|
|
216
|
-
catch {
|
|
217
|
-
|
|
218
|
-
setPaused(true);
|
|
492
|
+
catch (e) {
|
|
493
|
+
console.warn(`[rsdkv5] could not persist ${name}`, e);
|
|
219
494
|
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
}
|
|
231
|
-
|
|
495
|
+
}
|
|
496
|
+
if (written.length)
|
|
497
|
+
this.emit('saves', { files: written });
|
|
498
|
+
}
|
|
499
|
+
// ---- Picture ----
|
|
500
|
+
#fit() {
|
|
501
|
+
const canvas = this.#canvas;
|
|
502
|
+
const box = this.#box;
|
|
503
|
+
if (!canvas || !box || !this.#ownsCanvas)
|
|
504
|
+
return;
|
|
505
|
+
const { width, height } = box.getBoundingClientRect();
|
|
506
|
+
if (!width || !height)
|
|
507
|
+
return;
|
|
508
|
+
const scale = Math.min(width / RSDKV5_VIDEO.width, height / RSDKV5_VIDEO.height);
|
|
509
|
+
canvas.style.width = `${Math.floor(RSDKV5_VIDEO.width * scale)}px`;
|
|
510
|
+
canvas.style.height = `${Math.floor(RSDKV5_VIDEO.height * scale)}px`;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
/** The factory a host constructs: static identity, the configuration chain, `start()`. */
|
|
514
|
+
export class Rsdkv5SDK extends EngineSDKBase {
|
|
515
|
+
static id = ENGINE_ID;
|
|
516
|
+
/** Kept in step with package.json by `node scripts/sync-version.mjs`. */
|
|
517
|
+
static version = '0.2.0';
|
|
518
|
+
static name = 'Retro Software Development Kit v3';
|
|
519
|
+
static description = 'RSDKv5U (Sonic Mania decompilation) compiled to WebAssembly. Ships none of the game: the player provides Data.rsdk, and it never leaves their browser.';
|
|
520
|
+
createPlay(init, events) {
|
|
521
|
+
return new Rsdkv5Play(init, events);
|
|
522
|
+
}
|
|
232
523
|
}
|
|
233
|
-
|
|
524
|
+
/** CI's conformance line: if this class stops satisfying the contract, `make test` fails. */
|
|
525
|
+
export const RSDKV5_ENGINE = Rsdkv5SDK;
|
|
526
|
+
export default Rsdkv5SDK;
|
|
234
527
|
//# sourceMappingURL=rsdkv5.sdk.js.map
|