-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusergrp.js
More file actions
152 lines (140 loc) · 5.21 KB
/
Copy pathusergrp.js
File metadata and controls
152 lines (140 loc) · 5.21 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
/* Inject user-provided game data from launcher (IndexedDB, same origin). */
(function (global) {
'use strict';
var DB_NAME = 'begw-showcase-v1';
var STORE = 'payload';
function openDb() {
return new Promise(function (resolve, reject) {
var req = indexedDB.open(DB_NAME, 1);
req.onupgradeneeded = function () {
req.result.createObjectStore(STORE);
};
req.onsuccess = function () { resolve(req.result); };
req.onerror = function () { reject(req.error); };
});
}
function readPayload() {
return openDb().then(function (db) {
return new Promise(function (resolve, reject) {
var tx = db.transaction(STORE, 'readonly');
var req = tx.objectStore(STORE).get('active');
req.onsuccess = function () { resolve(req.result || null); };
req.onerror = function () { reject(req.error); };
});
});
}
function normalizeMountPath(relPath) {
var norm = relPath.replace(/\\/g, '/');
var leaf = norm.split('/').pop() || '';
if (leaf.toUpperCase() === 'VACATION.GRP') {
return norm.slice(0, norm.length - leaf.length) + 'vacation.grp';
}
if (leaf.toUpperCase() === 'LIQUID.PAK' || leaf.toUpperCase() === 'LIQUID.GRP') {
return norm.slice(0, norm.length - leaf.length) + 'liquid.grp';
}
if (leaf.toUpperCase() === 'PIRATE.GRP') {
return norm.slice(0, norm.length - leaf.length) + 'pirate.grp';
}
if (leaf.toUpperCase() === 'SW.GRP') {
return norm.slice(0, norm.length - leaf.length) + 'sw.grp';
}
// Loose-file ports (Witchaven, TekWar, …) use lowercase paths in the Emscripten VFS.
var parts = norm.split('/');
for (var i = 0; i < parts.length; i++) {
if (/\.(art|map|dat|snd|cfg|ini|bat|nfo|pal)$/i.test(parts[i]))
parts[i] = parts[i].toLowerCase();
}
return parts.join('/');
}
function writeFile(Module, relPath, bytes) {
relPath = normalizeMountPath(relPath);
var parts = relPath.replace(/\\/g, '/').split('/').filter(Boolean);
var name = parts.pop();
var dir = '/';
for (var i = 0; i < parts.length; i++) {
var seg = parts[i];
var probe = dir === '/' ? '/' + seg : dir + '/' + seg;
try { Module.FS.lookupPath(probe); }
catch (e) {
try { Module.FS.mkdir(probe); } catch (e2) {}
}
dir = probe;
}
var out = dir === '/' ? '/' + name : dir + '/' + name;
try { Module.FS.unlink(out); } catch (e3) {}
Module.FS.writeFile(out, bytes);
return out;
}
var pending = null;
var mounted = false;
function prepare() {
if (!/usergrp=1/.test(location.search)) {
return Promise.resolve(null);
}
return readPayload().then(function (payload) {
if (!payload || !payload.files || !payload.files.length) {
var hint = 'Open the launcher at http://127.0.0.1:8760/ on the SAME port, select files, then Launch.';
if (location.port && location.port !== '8760') {
hint = 'Wrong server port (' + location.port + '). Use one server: start-servers.ps1 → http://127.0.0.1:8760/';
}
throw new Error('No game files in launcher storage. ' + hint);
}
if (payload.gameId && /[?&]game=/.test(location.search) === false) {
var qGame = new URLSearchParams(location.search).get('game');
if (qGame && qGame !== payload.gameId) {
console.warn('[BEGW] Launcher saved data for ' + payload.gameId + ' but opening ' + qGame);
}
}
pending = payload;
mounted = false;
return payload;
});
}
function mount(Module) {
if (!pending || !Module || !Module.FS || mounted) return;
var n = 0;
for (var i = 0; i < pending.files.length; i++) {
var f = pending.files[i];
if (!f || !f.name || !f.data) continue;
try {
writeFile(Module, f.name, new Uint8Array(f.data));
n++;
} catch (e) {
console.warn('[BEGW] write failed:', f.name, e);
}
}
mounted = true;
console.log('[BEGW] Installed ' + n + ' user file(s) for ' + (pending.gameId || 'game'));
}
function wrapArgs(args, payload) {
if (!payload || !payload.gamegrpArg) return args;
var out = args.slice();
var has = false;
for (var i = 0; i < out.length; i++) {
if (out[i] === '-gamegrp' || out[i] === '-g') { has = true; break; }
}
if (!has) out.push('-gamegrp', payload.gamegrpArg);
return out;
}
function patchModuleOptions(opts) {
if (!pending) return opts;
opts = opts || {};
var pre = function () { mount(this); };
if (Array.isArray(opts.preRun)) opts.preRun.push(pre);
else if (opts.preRun) opts.preRun = [opts.preRun, pre];
else opts.preRun = [pre];
var origInit = opts.onRuntimeInitialized;
opts.onRuntimeInitialized = function () {
mount(this);
if (typeof origInit === 'function') origInit.call(this);
};
return opts;
}
global.BEGW_UserGrp = {
prepare: prepare,
mount: mount,
wrapArgs: wrapArgs,
patchModuleOptions: patchModuleOptions,
getPending: function () { return pending; }
};
})(typeof window !== 'undefined' ? window : globalThis);