···
import { app, nativeTheme } from "electron";
2
+
import * as path from "node:path";
3
+
import * as fs from "node:fs/promises";
4
+
import * as fsSync from "node:fs";
5
+
import { parseTarGzip } from "nanotar";
7
+
const logger = moonlightHost.getLogger("nativeFixes/host");
const enabledFeatures = app.commandLine.getSwitchValue("enable-features").split(",");
moonlightHost.events.on("window-created", function (browserWindow) {
···
app.commandLine.appendSwitch("enable-features", [...new Set(enabledFeatures)].join(","));
53
+
if (process.platform === "linux" && moonlightHost.getConfigOption<boolean>("nativeFixes", "linuxUpdater")) {
54
+
const exePath = app.getPath("exe");
55
+
const appName = path.basename(exePath);
56
+
const targetDir = path.dirname(exePath);
57
+
const { releaseChannel }: { releaseChannel: string } = JSON.parse(
58
+
fsSync.readFileSync(path.join(targetDir, "resources", "build_info.json"), "utf8")
61
+
const updaterModule = require(path.join(moonlightHost.asarPath, "app_bootstrap", "hostUpdater.js"));
62
+
const updater = updaterModule.constructor;
64
+
async function doUpdate(cb: (percent: number) => void) {
65
+
logger.debug("Extracting to", targetDir);
67
+
const exists = (path: string) =>
71
+
.catch(() => false);
73
+
const url = `https://discord.com/api/download/${releaseChannel}?platform=linux&format=tar.gz`;
74
+
const resp = await fetch(url, {
78
+
const reader = resp.body!.getReader();
79
+
const contentLength = parseInt(resp.headers.get("Content-Length") ?? "0");
80
+
logger.info(`Expecting ${contentLength} bytes for the update`);
81
+
const bytes = new Uint8Array(contentLength);
83
+
let lastPercent = 0;
86
+
const { done, value } = await reader.read();
90
+
bytes.set(value, pos);
91
+
pos += value.length;
93
+
const newPercent = Math.floor((pos / contentLength) * 100);
94
+
if (lastPercent !== newPercent) {
95
+
lastPercent = newPercent;
101
+
const files = await parseTarGzip(bytes);
103
+
for (const file of files) {
104
+
if (!file.data) continue;
105
+
// @ts-expect-error What do you mean their own types are wrong
106
+
if (file.type !== "file") continue;
108
+
// Discord update files are inside of a main "Discord(PTB|Canary)" folder
109
+
const filePath = file.name.replace(`${appName}/`, "");
110
+
logger.info("Extracting", filePath);
112
+
let targetFilePath = path.join(targetDir, filePath);
113
+
if (filePath === "resources/app.asar") {
115
+
targetFilePath = path.join(targetDir, "resources", "_app.asar");
116
+
} else if (filePath === appName) {
117
+
// Can't write over the executable? Just move it! 4head
118
+
if (await exists(targetFilePath)) {
119
+
await fs.rename(targetFilePath, targetFilePath + ".bak");
120
+
await fs.unlink(targetFilePath + ".bak");
123
+
const targetFileDir = path.dirname(targetFilePath);
125
+
if (!(await exists(targetFileDir))) await fs.mkdir(targetFileDir, { recursive: true });
126
+
await fs.writeFile(targetFilePath, file.data);
128
+
const mode = file.attrs?.mode;
129
+
if (mode != null) {
130
+
// Not sure why this slice is needed
131
+
await fs.chmod(targetFilePath, mode.slice(-3));
135
+
logger.debug("Done updating");
138
+
const realEmit = updater.prototype.emit;
139
+
updater.prototype.emit = function (event: string, ...args: any[]) {
140
+
// Arrow functions don't bind `this` :D
141
+
const call = (event: string, ...args: any[]) => realEmit.call(this, event, ...args);
143
+
if (event === "update-manually") {
144
+
const latestVerStr: string = args[0];
145
+
logger.debug("update-manually called, intercepting", latestVerStr);
146
+
call("update-available");
150
+
await doUpdate((progress) => {
151
+
call("update-progress", progress);
153
+
// Copied from the win32 updater
154
+
this.updateVersion = latestVerStr;
156
+
"update-downloaded",
162
+
this.quitAndInstall.bind(this)
165
+
logger.error("Error updating", e);
171
+
return realEmit.call(this, event, ...args);