this repo has no description
1import { join, dirname } from "node:path";
2import { mkdirSync, renameSync, existsSync, copyFileSync, readdirSync } from "node:fs";
3import Logger from "./util/logger";
4
5const logger = new Logger("core/persist");
6
7export default function persist(asarPath: string) {
8 try {
9 if (process.platform === "win32") {
10 persistWin32(asarPath);
11 }
12 } catch (e) {
13 logger.error(`Failed to persist moonlight: ${e}`);
14 }
15}
16
17function persistWin32(asarPath: string) {
18 const updaterModule = require(join(asarPath, "common", "updater"));
19 const updater = updaterModule.Updater;
20
21 const currentAppDir = join(dirname(asarPath), "app");
22
23 const realEmit = updater.prototype.emit;
24 updater.prototype.emit = function (event: string, ...args: any[]) {
25 if (event === "host-updated") {
26 const versions = this.queryCurrentVersionsSync();
27
28 const newRootDir = join(this.rootPath, "app-" + versions.current_host.map((v: number) => v.toString()).join("."));
29 logger.info(`Persisting moonlight - new root dir: ${newRootDir}`);
30
31 const newResources = join(newRootDir, "resources");
32
33 // app.asar -> _app.asar
34 const newAsar = join(newResources, "app.asar");
35 const newRenamedAsar = join(newResources, "_app.asar");
36 if (!existsSync(newRenamedAsar)) renameSync(newAsar, newRenamedAsar);
37
38 // copy the already existing app dir so we don't have to figure out the moonlight dir
39 const newAppDir = join(newResources, "app");
40 if (!existsSync(newAppDir)) mkdirSync(newAppDir);
41
42 for (const file of readdirSync(currentAppDir)) {
43 copyFileSync(join(currentAppDir, file), join(newAppDir, file));
44 }
45 }
46
47 return realEmit.call(this, event, ...args);
48 };
49}