-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-b.js
More file actions
492 lines (427 loc) · 20.2 KB
/
Copy pathserver-b.js
File metadata and controls
492 lines (427 loc) · 20.2 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
const express = require("express");
const http = require("http");
const { WebSocketServer } = require("ws");
const puppeteer = require("puppeteer");
const { spawn } = require("child_process");
const fs = require("fs");
const app = express();
const server = http.createServer(app);
const wss = new WebSocketServer({ server });
const GAME_BASE_URL = process.env.GAME_URL || "https://jdunk4.github.io/ARCADE1/game.html";
const LOADING_URL = process.env.LOADING_URL || "https://jdunk4.github.io/ARCADE1/loading.html";
const TARGET_FPS = 30;
const FRAME_MS = 1000 / TARGET_FPS;
const VIEWPORT_W = 512;
const VIEWPORT_H = 448;
const JPEG_QUALITY = 50;
const LOADING_SCREEN_MS = 20000;
// Path to Pokemon Insurgence Game.exe on the server
// Upload the game folder to Railway via volume or include in repo
const INSURGENCE_PATH = process.env.INSURGENCE_PATH || "/app/insurgence/Game.exe";
const DISPLAY = process.env.DISPLAY || ":99";
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
if (req.method === "OPTIONS") return res.sendStatus(200);
next();
});
app.get("/", (req, res) => res.send("SNES Puppeteer streaming server OK"));
app.get("/debug-screenshot.jpg", (req, res) => {
var p = "/tmp/debug-screenshot.jpg";
if (fs.existsSync(p)) {
res.setHeader("Content-Type", "image/jpeg");
res.sendFile(p);
} else {
res.status(404).send("No screenshot yet");
}
});
// Full virtual display capture — visit /debug-display.jpg while Wine game is running
app.get("/debug-display.jpg", function(req, res) {
var execSync = require("child_process").execSync;
try {
execSync("ffmpeg -y -f x11grab -r 1 -s 1024x768 -i :99.0+0,0 -vframes 1 /tmp/debug-display.jpg 2>/dev/null || true");
var p = "/tmp/debug-display.jpg";
if (fs.existsSync(p)) {
res.setHeader("Content-Type", "image/jpeg");
res.sendFile(p);
} else {
res.status(404).send("No display screenshot — is a Wine session running?");
}
} catch(e) { res.status(500).send("Error: " + e.message); }
});
// ── Key maps ──────────────────────────────────────────────────────────────────
// For EmulatorJS (Puppeteer) sessions
const KEY_MAP = {
up: "ArrowUp", down: "ArrowDown", left: "ArrowLeft", right: "ArrowRight",
a: "z", b: "x", x: "a", y: "s",
start: "Enter", select: "Shift", l: "q", r: "w"
};
// For Wine sessions — xdotool key names
const WINE_KEY_MAP = {
up: "Up",
down: "Down",
left: "Left",
right: "Right",
a: "z", // confirm / interact
b: "x", // cancel / back
x: "a",
y: "s",
start: "Return",
select: "Shift_L",
l: "q",
r: "w"
};
const sessions = new Map();
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// WINE SESSION — Pokemon Insurgence
// Uses: wine Game.exe on virtual display, ffmpeg captures screen + audio
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
async function createWineSession(ws, romId, wallet) {
console.log("[wine] creating session: id=" + romId + " wallet=" + wallet);
// Kill any stale Wine/ffmpeg processes from previous sessions
try { require("child_process").execSync("pkill -f 'Game.exe' 2>/dev/null || true"); } catch(e) {}
try { require("child_process").execSync("pkill -f 'x11grab' 2>/dev/null || true"); } catch(e) {}
await new Promise(function(r) { setTimeout(r, 500); });
ws.send(JSON.stringify({ type: "status", message: "Starting Pokemon Insurgence..." }));
// ── Step 1: Launch Wine process ───────────────────────────────
var wineProc = null;
try {
wineProc = spawn("wine", [INSURGENCE_PATH], {
env: Object.assign({}, process.env, {
DISPLAY: DISPLAY,
WINEDEBUG: "-all" // suppress wine debug spam
}),
stdio: ["ignore", "pipe", "pipe"]
});
wineProc.stdout.on("data", function(d) { console.log("[wine] " + d.toString().trim()); });
wineProc.stderr.on("data", function(d) {
var line = d.toString().trim();
// Only log actual errors, not wine debug noise
if (line.includes("err:") || line.includes("fixme:")) return;
if (line.length > 0) console.log("[wine] " + line);
});
wineProc.on("close", function(code) {
console.log("[wine] process exited code " + code);
destroySession(ws);
});
wineProc.on("error", function(e) {
console.error("[wine] failed to start: " + e.message);
ws.send(JSON.stringify({ type: "error", message: "Wine failed: " + e.message }));
});
console.log("[wine] Game.exe launched (pid " + wineProc.pid + ")");
} catch(e) {
ws.send(JSON.stringify({ type: "error", message: "Could not launch Wine: " + e.message }));
ws.close();
return;
}
// ── Step 2: Wait for game window to appear ────────────────────
console.log("[wine] waiting 8s for game window...");
await new Promise(function(r) { setTimeout(r, 8000); });
ws.send(JSON.stringify({ type: "status", message: "Loading game..." }));
// ── Step 3: ffmpeg — capture screen frames from virtual display ─
// Captures the full virtual display at TARGET_FPS, outputs MJPEG frames
var ffmpegVideo = spawn("ffmpeg", [
"-f", "x11grab",
"-r", String(TARGET_FPS),
"-s", "544x416", // game window only - RPG Maker XP resolution
"-i", DISPLAY + ".0+175,145", // crop to centered game window
"-vf", "scale=" + VIEWPORT_W + ":" + VIEWPORT_H,
"-c:v", "mjpeg",
"-q:v", "5", // quality 1=best 31=worst, 5 is good balance
"-f", "image2pipe",
"-vcodec", "mjpeg",
"pipe:1"
], { stdio: ["ignore", "pipe", "pipe"] });
// Parse MJPEG stream into individual JPEG frames and send over WS
var jpegBuffer = Buffer.alloc(0);
var SOI = Buffer.from([0xFF, 0xD8]); // JPEG start marker
var EOI = Buffer.from([0xFF, 0xD9]); // JPEG end marker
ffmpegVideo.stdout.on("data", function(chunk) {
jpegBuffer = Buffer.concat([jpegBuffer, chunk]);
// Extract complete JPEG frames from the stream
while (true) {
var start = jpegBuffer.indexOf(SOI);
if (start === -1) { jpegBuffer = Buffer.alloc(0); break; }
var end = jpegBuffer.indexOf(EOI, start + 2);
if (end === -1) break; // incomplete frame, wait for more data
end += 2; // include EOI bytes
var frame = jpegBuffer.slice(start, end);
jpegBuffer = jpegBuffer.slice(end);
if (ws.readyState === 1) {
try {
ws.send(JSON.stringify({
image: "data:image/jpeg;base64," + frame.toString("base64")
}));
} catch(e) { console.warn("[wine] frame send error: " + e.message); }
}
}
});
ffmpegVideo.stderr.on("data", function(d) {
var line = d.toString().trim();
if (line.includes("Error") || line.includes("error")) console.log("[ffmpeg-video] " + line);
});
ffmpegVideo.on("close", function(code) { console.log("[ffmpeg-video] exited code " + code); });
ffmpegVideo.on("error", function(e) { console.warn("[ffmpeg-video] failed: " + e.message); });
// Audio handled by m-audio element in MML world — no streaming needed
console.log("[wine] audio handled by world m-audio element");
// Store wine session — no browser/page, just processes
sessions.set(ws, {
type: "wine",
wineProc,
ffmpegVideo,
ffmpegAudio: null,
frameInterval: null,
wallet,
romId
});
ws.send(JSON.stringify({ type: "status", message: "" }));
console.log("[wine] session live: " + wallet + " / " + romId);
}
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// EMULATORJS SESSION — all other ROMs (unchanged)
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
async function createSession(ws, romFile, romCore, romId, wallet) {
console.log("[session] creating: rom=" + romFile + " core=" + romCore + " wallet=" + wallet);
const browser = await puppeteer.launch({
executablePath: process.env.PUPPETEER_EXECUTABLE_PATH || "/usr/bin/chromium",
headless: false,
defaultViewport: { width: VIEWPORT_W, height: VIEWPORT_H },
ignoreDefaultArgs: ["--mute-audio"],
args: [
"--no-sandbox",
"--disable-setuid-sandbox",
"--disable-dev-shm-usage",
"--enable-webgl",
"--enable-webgl2",
"--ignore-gpu-blocklist",
"--ignore-gpu-blacklist",
"--autoplay-policy=no-user-gesture-required",
"--enable-features=SharedArrayBuffer",
"--display=:99",
"--use-fake-ui-for-media-stream"
]
});
const page = await browser.newPage();
await page.setViewport({ width: VIEWPORT_W, height: VIEWPORT_H });
await page.evaluateOnNewDocument(function() {
Object.defineProperty(window, "crossOriginIsolated", { get: function() { return true; } });
if (typeof SharedArrayBuffer === "undefined") window.SharedArrayBuffer = ArrayBuffer;
});
await page.setRequestInterception(true);
page.on("request", function(req) {
var url = req.url();
if (url.includes("cdn.emulatorjs.org") && url.endsWith(".json")) {
req.respond({ status: 200, contentType: "application/json", headers: { "Access-Control-Allow-Origin": "*" }, body: "{}" });
return;
}
req.continue();
});
page.on("console", function(msg) {
var text = msg.text();
if (text.includes("Translation not found")) return;
if (text.includes("Language set to")) return;
console.log("[browser] " + msg.type() + ": " + text);
});
page.on("pageerror", function(err) { console.error("[browser] PAGE ERROR: " + err.message); });
console.log("[session] showing loading screen: " + LOADING_URL);
await page.goto(LOADING_URL, { waitUntil: "domcontentloaded", timeout: 10000 });
var showingLoader = true;
var loadingInterval = setInterval(async function() {
if (ws.readyState !== 1) { clearInterval(loadingInterval); return; }
if (!showingLoader) { clearInterval(loadingInterval); return; }
try {
var imageBase64 = await page.screenshot({ type: "jpeg", quality: JPEG_QUALITY, encoding: "base64" });
if (ws.readyState === 1) ws.send(JSON.stringify({ image: "data:image/jpeg;base64," + imageBase64 }));
} catch(e) { clearInterval(loadingInterval); }
}, FRAME_MS);
await new Promise(function(r) { setTimeout(r, LOADING_SCREEN_MS); });
showingLoader = false;
clearInterval(loadingInterval);
var gameUrl = GAME_BASE_URL
+ "?rom=" + encodeURIComponent(romFile)
+ "&core=" + encodeURIComponent(romCore)
+ "&id=" + encodeURIComponent(romId)
+ "&wallet=" + encodeURIComponent(wallet);
console.log("[session] navigating to game: " + gameUrl);
var keepalive = setInterval(function() {
if (ws.readyState === 1) ws.send(JSON.stringify({ type: "status", message: "Loading emulator..." }));
}, 3000);
await page.goto(gameUrl, { waitUntil: "domcontentloaded", timeout: 30000 });
var canvasFound = false;
try {
await page.waitForSelector("canvas", { timeout: 60000 });
canvasFound = true;
console.log("[session] canvas found");
} catch(e) {
console.warn("[session] canvas not found within 60s");
try { await page.screenshot({ path: "/tmp/debug-screenshot.jpg", type: "jpeg", quality: 80 }); } catch(se) {}
}
clearInterval(keepalive);
if (!canvasFound) {
ws.send(JSON.stringify({ type: "error", message: "Emulator failed to load" }));
await browser.close();
return;
}
await new Promise(function(r) { setTimeout(r, 8000); });
var allClickable = await page.evaluate(function() {
var results = [];
var els = document.querySelectorAll("button, [role='button'], span, div");
for (var i = 0; i < els.length; i++) {
var el = els[i];
var text = (el.innerText || "").trim();
if (text && text.length < 30) {
var rect = el.getBoundingClientRect();
if (rect.width > 0 && rect.height > 0) {
results.push({ text: text, x: Math.round(rect.left + rect.width/2), y: Math.round(rect.top + rect.height/2) });
}
}
}
return results.slice(0, 30);
});
var playEl = allClickable.find(function(el) { return el.text === "Play"; });
if (playEl) {
console.log("[session] clicking Play at " + playEl.x + "," + playEl.y);
await page.mouse.click(playEl.x, playEl.y);
await new Promise(function(r) { setTimeout(r, 1000); });
}
await page.mouse.click(VIEWPORT_W / 2, VIEWPORT_H / 2);
await new Promise(function(r) { setTimeout(r, 500); });
var ffmpegProc = null;
try {
console.log("[session] starting ffmpeg audio capture from PulseAudio...");
ffmpegProc = spawn("ffmpeg", [
"-f", "pulse",
"-i", "virtual_speaker.monitor",
"-c:a", "libopus",
"-b:a", "64k",
"-vn",
"-f", "webm",
"-cluster_size_limit", "2M",
"-cluster_time_limit", "100",
"pipe:1"
], { stdio: ["ignore", "pipe", "pipe"] });
ffmpegProc.stdout.on("data", function(chunk) {
if (ws.readyState !== 1) return;
try { ws.send(JSON.stringify({ type: "audio", data: chunk.toString("base64") })); }
catch(e) { console.warn("[ffmpeg] send error: " + e.message); }
});
ffmpegProc.stderr.on("data", function(d) {
var line = d.toString().trim();
if (line.includes("Stream") || line.includes("Error") || line.includes("error")) {
console.log("[ffmpeg] " + line);
}
});
ffmpegProc.on("close", function(code) { console.log("[ffmpeg] exited code " + code); });
ffmpegProc.on("error", function(e) { console.warn("[ffmpeg] failed: " + e.message); });
console.log("[session] ffmpeg audio capture started");
} catch(e) {
console.warn("[session] ffmpeg setup failed: " + e.message);
}
var sendingFrame = false;
var frameInterval = setInterval(async function() {
if (ws.readyState !== 1) { clearInterval(frameInterval); return; }
if (sendingFrame) return;
sendingFrame = true;
try {
var canvasEl = await page.$("canvas");
var imageBase64;
if (canvasEl) {
imageBase64 = await canvasEl.screenshot({ type: "jpeg", quality: JPEG_QUALITY, encoding: "base64" });
} else {
imageBase64 = await page.screenshot({ type: "jpeg", quality: JPEG_QUALITY, encoding: "base64" });
}
ws.send(JSON.stringify({ image: "data:image/jpeg;base64," + imageBase64 }), function(err) {
sendingFrame = false;
if (err) console.warn("[session] send error: " + err.message);
});
} catch(e) {
sendingFrame = false;
console.error("[session] screenshot failed: " + e.message);
clearInterval(frameInterval);
destroySession(ws);
}
}, FRAME_MS);
sessions.set(ws, { type: "emulator", browser, page, frameInterval, ffmpegProc, wallet, romId });
console.log("[session] live: " + wallet + " / " + romId);
}
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// DESTROY SESSION — handles both types
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
async function destroySession(ws) {
var session = sessions.get(ws);
if (!session) return;
if (session.type === "wine") {
try { if (session.ffmpegVideo) session.ffmpegVideo.kill("SIGKILL"); } catch(e) {}
try { if (session.ffmpegAudio) session.ffmpegAudio.kill("SIGKILL"); } catch(e) {}
try { if (session.wineProc) session.wineProc.kill("SIGKILL"); } catch(e) {}
// Force kill any stray processes to prevent audio bleed into other games
try { require("child_process").execSync("pkill -9 -f x11grab 2>/dev/null || true"); } catch(e) {}
try { require("child_process").execSync("pkill -9 -f Game.exe 2>/dev/null || true"); } catch(e) {}
try { require("child_process").execSync("pkill -9 -f wineserver 2>/dev/null || true"); } catch(e) {}
} else {
clearInterval(session.frameInterval);
try { if (session.ffmpegProc) session.ffmpegProc.kill("SIGTERM"); } catch(e) {}
try { await session.browser.close(); } catch(e) {}
}
sessions.delete(ws);
console.log("[session] destroyed: " + session.wallet + " / " + session.romId);
}
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// WEBSOCKET CONNECTION HANDLER
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
wss.on("connection", async function(ws, req) {
var url = new URL(req.url, "http://localhost");
var romFile = url.searchParams.get("rom") || "Kaizo Mario (English).sfc";
var romCore = url.searchParams.get("core") || "snes";
var romId = url.searchParams.get("id") || url.searchParams.get("rom") || "kaizo-mario-world-1";
var wallet = url.searchParams.get("wallet") || "anonymous";
console.log("[ws] connected: rom=" + romFile + " core=" + romCore + " id=" + romId + " wallet=" + wallet);
ws.send(JSON.stringify({ type: "status", message: "Launching..." }));
try {
// ── Route to Wine session if core=wine ──────────────────────
if (romCore === "wine") {
await createWineSession(ws, romId, wallet);
} else {
await createSession(ws, romFile, romCore, romId, wallet);
}
if (sessions.has(ws)) ws.send(JSON.stringify({ type: "status", message: "" }));
} catch(e) {
console.error("[ws] session creation failed: " + e.message);
ws.send(JSON.stringify({ type: "error", message: "Failed to start: " + e.message }));
ws.close();
return;
}
ws.on("message", async function(data) {
var session = sessions.get(ws);
if (!session) return;
try {
var msg = JSON.parse(data);
// ── Wine input: use xdotool to send keystrokes ────────────
if (session.type === "wine") {
var wineKey = WINE_KEY_MAP[msg.key];
if (!wineKey) return;
var action = msg.type === "keyDown" ? "keydown" : "keyup";
spawn("xdotool", [action, "--clearmodifiers", wineKey], {
env: Object.assign({}, process.env, { DISPLAY: DISPLAY })
});
return;
}
// ── Emulator input: send via Puppeteer keyboard ───────────
var key = KEY_MAP[msg.key];
if (!key) return;
if (msg.type === "keyDown") await session.page.keyboard.down(key);
else if (msg.type === "keyUp") await session.page.keyboard.up(key);
} catch(e) { console.warn("[ws] input error: " + e.message); }
});
ws.on("close", function() { console.log("[ws] disconnected: " + wallet); destroySession(ws); });
ws.on("error", function(e) { console.error("[ws] error: " + e.message); destroySession(ws); });
});
var PORT = process.env.PORT || 8081;
server.listen(PORT, function() {
console.log("Puppeteer SNES server on port " + PORT);
console.log("Base game URL: " + GAME_BASE_URL);
console.log("Loading URL: " + LOADING_URL);
console.log("Loading screen duration:" + LOADING_SCREEN_MS + "ms");
console.log("Target FPS: " + TARGET_FPS);
console.log("JPEG quality: " + JPEG_QUALITY);
console.log("Insurgence path: " + INSURGENCE_PATH);
});