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