Repository navigation
Expand file tree
/
Copy pathpatch.ts
More file actions
230 lines (216 loc) · 9.64 KB
/
Copy pathpatch.ts
File metadata and controls
230 lines (216 loc) · 9.64 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
#!/usr/bin/env bun
// glikvm-mod - patch the GLKVM Windows desktop client (Electron) so sessions can
// live in separate windows and the local clipboard can be pasted straight into
// the remote machine.
//
// bun patch.ts build extract + patch into ./build/app (no install)
// bun patch.ts install build + install a side-by-side copy (no admin)
// bun patch.ts install --inplace build + drop resources/app into the stock install (needs elevated shell)
// bun patch.ts uninstall [--inplace] remove the mod
// bun patch.ts run [-- args] launch the modded client
// bun patch.ts status
//
// Options: --src <dir> stock install (default: C:\Program Files\GLKVM)
// --dest <dir> side-by-side install dir (default: %LOCALAPPDATA%\Programs\GLKVM-mod)
import fs from "node:fs";
import path from "node:path";
import crypto from "node:crypto";
import { extractAll } from "@electron/asar";
import { allPatches, MOD_VERSION, SUPPORTED_APP_VERSIONS } from "./src/patches";
const argv = process.argv.slice(2);
const cmd = argv.find((a) => !a.startsWith("--")) ?? "help";
const flag = (name: string) => argv.includes(`--${name}`);
const opt = (name: string, def: string) => {
const i = argv.indexOf(`--${name}`);
return i !== -1 && argv[i + 1] ? argv[i + 1] : def;
};
const passthrough = argv.includes("--") ? argv.slice(argv.indexOf("--") + 1) : [];
const HERE = path.dirname(new URL(import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, "$1"));
const SRC = opt("src", "C:\\Program Files\\GLKVM");
const DEST = opt("dest", path.join(process.env.LOCALAPPDATA ?? "", "Programs", "GLKVM-mod"));
const INPLACE = flag("inplace");
const BUILD = path.join(HERE, "build");
const BUILD_APP = path.join(BUILD, "app");
const SHORTCUT = path.join(process.env.APPDATA ?? "", "Microsoft", "Windows", "Start Menu", "Programs", "GLKVM (mod).lnk");
const MARKER = "glikvm-mod.json";
const log = (...a: unknown[]) => console.log("•", ...a);
const die = (msg: string): never => {
console.error("✗", msg);
process.exit(1);
};
function sha256(file: string) {
return crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex");
}
function stockAsar() {
const p = path.join(SRC, "resources", "app.asar");
if (!fs.existsSync(p)) die(`stock app.asar not found at ${p} (use --src <dir>)`);
return p;
}
function syntaxCheck(file: string) {
const code = fs.readFileSync(file, "utf8");
new Bun.Transpiler({ loader: "js" }).transformSync(code); // throws on parse error
}
function build() {
const asar = stockAsar();
try {
fs.rmSync(BUILD_APP, { recursive: true, force: true });
} catch (e: any) {
if (e.code !== "EBUSY" && e.code !== "EPERM") throw e;
// something (a shell?) has build/app as its cwd - move it aside and carry on
const aside = `${BUILD_APP}.old-${Date.now()}`;
fs.renameSync(BUILD_APP, aside);
try {
fs.rmSync(aside, { recursive: true, force: true });
} catch {
log(`(could not delete ${aside}; remove it by hand later)`);
}
}
fs.mkdirSync(BUILD_APP, { recursive: true });
log(`extracting ${asar}`);
extractAll(asar, BUILD_APP);
const pkg = JSON.parse(fs.readFileSync(path.join(BUILD_APP, "package.json"), "utf8"));
log(`stock client version ${pkg.version}`);
if (!SUPPORTED_APP_VERSIONS.includes(pkg.version)) {
const supported = SUPPORTED_APP_VERSIONS.join(", ");
const newerClient = pkg.version.localeCompare(SUPPORTED_APP_VERSIONS[SUPPORTED_APP_VERSIONS.length - 1], undefined, { numeric: true }) > 0;
const hint = newerClient
? `Your GLKVM is newer than the mod - check ${"https://github.com/emaspa/glikvm-mod"} for an updated mod (git pull) before patching.`
: `Your GLKVM is older than what this mod targets - update the client, or check out an older mod tag.`;
if (!flag("force")) {
die(
`glikvm-mod ${MOD_VERSION} supports GLKVM ${supported}, but the installed client is ${pkg.version}.\n ${hint}\n To try anyway: add --force (patching still aborts safely if the code moved).`,
);
}
log(`WARNING: GLKVM ${pkg.version} is not a supported version (${supported}); continuing because of --force`);
}
const patches = allPatches(BUILD_APP);
const touched = new Set<string>();
for (const p of patches) {
const file = path.join(BUILD_APP, p.file);
const before = fs.readFileSync(file, "utf8");
const after = p.apply(before);
if (after === before) die(`patch produced no change: ${p.what}`);
fs.writeFileSync(file, after);
touched.add(file);
log(`patched: ${p.what}`);
}
for (const f of touched) syntaxCheck(f);
log(`syntax OK for ${touched.size} patched files`);
fs.writeFileSync(
path.join(BUILD_APP, MARKER),
JSON.stringify({ modVersion: MOD_VERSION, appVersion: pkg.version, sourceAsarSha256: sha256(asar), builtAt: new Date().toISOString() }, null, 2),
);
return pkg.version as string;
}
function robocopy(src: string, dst: string, extra: string[]) {
const r = Bun.spawnSync(["robocopy", src, dst, ...extra, "/R:2", "/W:1", "/NJH", "/NJS", "/NDL", "/NFL", "/NP"], { stdout: "pipe", stderr: "pipe" });
const code = r.exitCode ?? 99;
if (code >= 8) {
console.error(r.stdout.toString(), r.stderr.toString());
die(`robocopy ${src} -> ${dst} failed (exit ${code}). Is the modded client still running?`);
}
}
function makeShortcut(exe: string) {
const ps = [
`$s = (New-Object -ComObject WScript.Shell).CreateShortcut('${SHORTCUT.replace(/'/g, "''")}')`,
`$s.TargetPath = '${exe.replace(/'/g, "''")}'`,
`$s.WorkingDirectory = '${path.dirname(exe).replace(/'/g, "''")}'`,
`$s.IconLocation = '${exe.replace(/'/g, "''")},0'`,
`$s.Description = 'GLKVM with glikvm-mod (multi-window sessions, clipboard paste)'`,
`$s.Save()`,
].join("; ");
const r = Bun.spawnSync(["powershell", "-NoProfile", "-NonInteractive", "-Command", ps], { stdout: "pipe", stderr: "pipe" });
if (r.exitCode !== 0) log(`(could not create Start Menu shortcut: ${r.stderr.toString().trim()})`);
else log(`Start Menu shortcut: ${SHORTCUT}`);
}
function install() {
const version = build();
if (INPLACE) {
const target = path.join(SRC, "resources", "app");
log(`installing in place -> ${target}`);
try {
fs.rmSync(target, { recursive: true, force: true });
fs.cpSync(BUILD_APP, target, { recursive: true });
} catch (e: any) {
die(`cannot write to ${target} (${e.code}). Run this from an elevated (Administrator) shell, or use the default side-by-side install.`);
}
log(`done. Electron loads resources/app in preference to app.asar, so the stock GLKVM.exe now runs the mod.`);
log(`NOTE: after a GLKVM update, re-run 'bun patch.ts install --inplace' (or uninstall) - a stale resources/app would shadow the new app.asar.`);
return;
}
log(`installing side-by-side -> ${DEST}`);
fs.mkdirSync(DEST, { recursive: true });
// 1) stock binaries (Electron runtime, helpers, locales) - everything except the stock app payload
robocopy(SRC, DEST, ["/E", "/XF", "app.asar", "Uninstall GLKVM.exe", "/XD", "app.asar.unpacked", "app"]);
// 2) patched app
robocopy(BUILD_APP, path.join(DEST, "resources", "app"), ["/MIR"]);
const exe = path.join(DEST, "GLKVM.exe");
makeShortcut(exe);
log(`installed glikvm-mod ${MOD_VERSION} on GLKVM ${version}`);
log(`run it: bun patch.ts run (or the "GLKVM (mod)" Start Menu entry, or ${exe})`);
log(`it shares login/settings with the stock client (same %APPDATA%\\gl-kvm). If the stock client is running you will be asked to close it.`);
}
function uninstall() {
if (INPLACE) {
const target = path.join(SRC, "resources", "app");
if (!fs.existsSync(target)) return log(`nothing to remove at ${target}`);
try {
fs.rmSync(target, { recursive: true, force: true });
} catch (e: any) {
die(`cannot remove ${target} (${e.code}); run from an elevated shell`);
}
return log(`removed ${target}; stock GLKVM.exe is back to app.asar`);
}
if (fs.existsSync(DEST)) {
fs.rmSync(DEST, { recursive: true, force: true });
log(`removed ${DEST}`);
} else log(`nothing at ${DEST}`);
if (fs.existsSync(SHORTCUT)) {
fs.rmSync(SHORTCUT);
log(`removed ${SHORTCUT}`);
}
}
function run() {
const exe = INPLACE ? path.join(SRC, "GLKVM.exe") : path.join(DEST, "GLKVM.exe");
if (!fs.existsSync(exe)) die(`${exe} not found - run 'bun patch.ts install' first`);
const child = Bun.spawn([exe, ...passthrough], { cwd: path.dirname(exe), stdio: ["ignore", "ignore", "ignore"], detached: true } as any);
child.unref();
log(`launched ${exe} ${passthrough.join(" ")}`.trim());
}
function status() {
const asar = path.join(SRC, "resources", "app.asar");
log(`stock install: ${SRC} ${fs.existsSync(asar) ? "(found)" : "(app.asar MISSING)"}`);
for (const [label, dir] of [
["side-by-side", path.join(DEST, "resources", "app")],
["in-place", path.join(SRC, "resources", "app")],
]) {
const marker = path.join(dir, MARKER);
if (!fs.existsSync(marker)) {
log(`${label}: not installed`);
continue;
}
const m = JSON.parse(fs.readFileSync(marker, "utf8"));
const stale = fs.existsSync(asar) && sha256(asar) !== m.sourceAsarSha256;
log(`${label}: mod ${m.modVersion} built from GLKVM ${m.appVersion} at ${m.builtAt}${stale ? " ⚠ stock app.asar changed since - re-run install" : ""}`);
}
}
switch (cmd) {
case "build":
build();
log(`build ready at ${BUILD_APP}`);
break;
case "install":
install();
break;
case "uninstall":
uninstall();
break;
case "run":
run();
break;
case "status":
status();
break;
default:
console.log(fs.readFileSync(new URL(import.meta.url), "utf8").split("\n").slice(1, 15).map((l) => l.replace(/^\/\/ ?/, "")).join("\n"));
}