|
| 1 | +import fs from "node:fs"; |
| 2 | + |
| 3 | +const wasmPath = process.argv[2]; |
| 4 | +if (!wasmPath) { |
| 5 | + throw new Error("usage: node tests/tiberian_sun_runtime.mjs <tiberian-sun.wasm>"); |
| 6 | +} |
| 7 | + |
| 8 | +const moduleBase = 0x10000000n; |
| 9 | +const splashPointer = 0x20000000n; |
| 10 | +const decoder = new TextDecoder(); |
| 11 | +const encoder = new TextEncoder(); |
| 12 | +const processNames = []; |
| 13 | +const moduleNames = []; |
| 14 | +let instance; |
| 15 | +let processOpen = true; |
| 16 | +let timerState = 0; |
| 17 | +let isPlaying = 1; |
| 18 | +let mainMenuIndex = 1; |
| 19 | +let gameState = 0; |
| 20 | +let splashVisible = 0; |
| 21 | +let splashText = ""; |
| 22 | +let starts = 0; |
| 23 | +let splits = 0; |
| 24 | +let pauses = 0; |
| 25 | +let resumes = 0; |
| 26 | +let detaches = 0; |
| 27 | + |
| 28 | +const text = (pointer, length) => decoder.decode( |
| 29 | + new Uint8Array(instance.exports.memory.buffer, pointer, length), |
| 30 | +); |
| 31 | + |
| 32 | +function writeSplash(destination) { |
| 33 | + const output = new Uint8Array(instance.exports.memory.buffer, destination, 20); |
| 34 | + output.fill(0); |
| 35 | + const encoded = encoder.encode(splashText); |
| 36 | + if (encoded.length > output.length) { |
| 37 | + throw new Error(`fixture splash text exceeds string20: ${splashText}`); |
| 38 | + } |
| 39 | + output.set(encoded); |
| 40 | +} |
| 41 | + |
| 42 | +const env = { |
| 43 | + timer_get_state: () => timerState, |
| 44 | + timer_start() { starts += 1; timerState = 1; }, |
| 45 | + timer_split() { splits += 1; }, |
| 46 | + timer_reset() {}, |
| 47 | + timer_set_game_time() {}, |
| 48 | + timer_pause_game_time() { pauses += 1; }, |
| 49 | + timer_resume_game_time() { resumes += 1; }, |
| 50 | + timer_set_variable() {}, |
| 51 | + runtime_set_tick_rate() {}, |
| 52 | + runtime_print_message() {}, |
| 53 | + process_attach(pointer, length) { |
| 54 | + const name = text(pointer, length); |
| 55 | + processNames.push(name); |
| 56 | + return name === "game.exe" ? 1n : 0n; |
| 57 | + }, |
| 58 | + process_detach() { detaches += 1; }, |
| 59 | + process_is_open: () => processOpen ? 1 : 0, |
| 60 | + process_get_module_address(_process, pointer, length) { |
| 61 | + const name = text(pointer, length); |
| 62 | + moduleNames.push(name); |
| 63 | + return name === "game.exe" ? moduleBase : 0n; |
| 64 | + }, |
| 65 | + process_read(_process, address, destination, size) { |
| 66 | + const view = new DataView(instance.exports.memory.buffer); |
| 67 | + if (address === moduleBase + 0x3e48fcn && size === 1) { |
| 68 | + view.setUint8(destination, isPlaying); |
| 69 | + } else if (address === moduleBase + 0x408c4cn && size === 1) { |
| 70 | + view.setUint8(destination, mainMenuIndex); |
| 71 | + } else if (address === moduleBase + 0x3e224cn && size === 1) { |
| 72 | + view.setUint8(destination, gameState); |
| 73 | + } else if (address === moduleBase + 0x34c5f4n && size === 8) { |
| 74 | + view.setBigUint64(destination, splashPointer, true); |
| 75 | + } else if (address === splashPointer + 0x14n && size === 1) { |
| 76 | + view.setUint8(destination, splashVisible); |
| 77 | + } else if (address === splashPointer + 0x14n && size === 20) { |
| 78 | + writeSplash(destination); |
| 79 | + } else { |
| 80 | + throw new Error(`unexpected Tiberian Sun read ${address.toString(16)} (${size})`); |
| 81 | + } |
| 82 | + return 1; |
| 83 | + }, |
| 84 | + user_settings_add_bool: () => 1, |
| 85 | + user_settings_add_title() {}, |
| 86 | + user_settings_add_choice() {}, |
| 87 | + user_settings_add_choice_option: () => 0, |
| 88 | + user_settings_add_file_select() {}, |
| 89 | + user_settings_add_file_select_name_filter() {}, |
| 90 | + user_settings_add_file_select_mime_filter() {}, |
| 91 | + user_settings_set_tooltip() {}, |
| 92 | + settings_map_load: () => 1n, |
| 93 | + settings_map_free() {}, |
| 94 | + settings_map_get: () => 0n, |
| 95 | + setting_value_free() {}, |
| 96 | + setting_value_get_bool: () => 0, |
| 97 | + setting_value_get_string: () => 0, |
| 98 | +}; |
| 99 | + |
| 100 | +({ instance } = await WebAssembly.instantiate(fs.readFileSync(wasmPath), { env })); |
| 101 | +instance.exports._start(); |
| 102 | +instance.exports.update(); |
| 103 | + |
| 104 | +mainMenuIndex = 0; |
| 105 | +gameState = 255; |
| 106 | +instance.exports.update(); |
| 107 | +if (starts !== 1) throw new Error(`start transition differed: ${starts}`); |
| 108 | + |
| 109 | +isPlaying = 0; |
| 110 | +instance.exports.update(); |
| 111 | +isPlaying = 1; |
| 112 | +instance.exports.update(); |
| 113 | + |
| 114 | +splashText = "MISSION ACCOMPLISHED"; |
| 115 | +splashVisible = 1; |
| 116 | +instance.exports.update(); |
| 117 | +splashVisible = 0; |
| 118 | +instance.exports.update(); |
| 119 | +splashText = "MISION CUMPLIDA"; |
| 120 | +splashVisible = 1; |
| 121 | +instance.exports.update(); |
| 122 | +splashVisible = 0; |
| 123 | +instance.exports.update(); |
| 124 | +splashText = "MISSION FAILED"; |
| 125 | +splashVisible = 1; |
| 126 | +instance.exports.update(); |
| 127 | + |
| 128 | +if (splits !== 2 || pauses !== 1 || resumes !== 6) { |
| 129 | + throw new Error(`timer behavior differed: ${JSON.stringify({ splits, pauses, resumes })}`); |
| 130 | +} |
| 131 | +if (processNames.join(",") !== "game.exe" || moduleNames.some(name => name !== "game.exe")) { |
| 132 | + throw new Error(`process/module identity differed: ${JSON.stringify({ processNames, moduleNames })}`); |
| 133 | +} |
| 134 | + |
| 135 | +processOpen = false; |
| 136 | +instance.exports.update(); |
| 137 | +if (detaches !== 1) throw new Error(`detach behavior differed: ${detaches}`); |
| 138 | + |
| 139 | +console.log(JSON.stringify({ starts, splits, pauses, resumes, detaches, moduleLookups: moduleNames.length })); |
0 commit comments