forked from auberonedu/env-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartEmu.js
More file actions
151 lines (129 loc) · 3.9 KB
/
startEmu.js
File metadata and controls
151 lines (129 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
async function resolveRomUrl() {
let res;
try {
res = await fetch("./rom.json", { cache: "no-store" });
} catch (err) {
throw {
kind: "rom-json-missing",
detail: `Failed to fetch ./rom.json (${err?.message ?? err})`
};
}
if (!res.ok) {
throw {
kind: "rom-json-missing",
detail: `./rom.json not found (HTTP ${res.status})`
};
}
let data;
try {
data = await res.json();
} catch {
throw {
kind: "rom-json-invalid",
detail: "./rom.json exists but is not valid JSON"
};
}
const rom = data?.rom;
if (typeof rom !== "string" || rom.trim() === "") {
throw {
kind: "rom-json-invalid",
detail: 'Expected: { "rom": "your-game.gba" }'
};
}
const romPath = rom.startsWith("./") ? rom : `./${rom}`;
const romCheck = await fetch(romPath, { method: "HEAD", cache: "no-store" });
if (!romCheck.ok) {
throw {
kind: "rom-missing",
detail: `ROM file ${romPath} not found (HTTP ${romCheck.status})`
};
}
return romPath + "?v=" + String(Date.now());
}
function showErrorOnPage(title, message) {
const box = document.createElement("div");
box.style.maxWidth = "800px";
box.style.padding = "20px";
box.style.background = "#1e1e1e";
box.style.color = "#eee";
box.style.border = "1px solid #444";
box.style.borderRadius = "6px";
box.style.fontFamily = "system-ui, sans-serif";
const h = document.createElement("h2");
h.textContent = title;
const p = document.createElement("p");
p.textContent = message;
const hint = document.createElement("pre");
hint.textContent =
`What to check:
• Did the project compile successfully?
• Are there errors in the GitHub Actions / build logs?
• Was a .gba file produced?
If the build failed, the ROM will not exist.`;
hint.style.background = "#111";
hint.style.padding = "12px";
hint.style.borderRadius = "4px";
hint.style.whiteSpace = "pre-wrap";
box.appendChild(h);
box.appendChild(p);
box.appendChild(hint);
document.body.prepend(box);
}
function configureAndLoadEmulator(romUrl) {
window.EJS_player = "#game";
window.EJS_core = "gba";
window.EJS_gameUrl = romUrl;
window.EJS_gameName = "game";
window.EJS_pathtodata = "https://cdn.emulatorjs.org/latest/data/";
window.EJS_startOnLoaded = true;
window.EJS_disableDatabases = true;
window.EJS_DEBUG_XX = true;
const s = document.createElement("script");
s.src = "https://cdn.emulatorjs.org/latest/data/loader.js";
document.head.appendChild(s);
}
async function startFromButton() {
const btn = document.getElementById("start");
if (btn) {
btn.disabled = true;
btn.textContent = "Loading…";
}
let romUrl;
try {
romUrl = await resolveRomUrl();
} catch (err) {
console.error("[Emulator startup failed]", err);
if (err.kind === "rom-json-missing" || err.kind === "rom-json-invalid") {
showErrorOnPage(
"Game could not be started",
"The emulator could not find rom.json, or it is invalid. Check the build logs for compilation errors."
);
} else if (err.kind === "rom-missing") {
showErrorOnPage(
"Game could not be started",
"The ROM file listed in rom.json could not be found. Check the build logs for compilation errors."
);
} else {
showErrorOnPage(
"Game could not be started",
"An unexpected error occurred while loading the game. Check the build logs for compilation errors."
);
}
if (btn) {
btn.disabled = false;
btn.textContent = "Start";
}
return;
}
// Remove the button (optional) and start emulator
btn?.remove();
configureAndLoadEmulator(romUrl);
}
document.addEventListener("DOMContentLoaded", () => {
const btn = document.getElementById("start");
if (!btn) {
showErrorOnPage("Setup error", "Missing Start button with id=\"start\".");
return;
}
btn.addEventListener("click", startFromButton, { once: true });
});