aboutsummaryrefslogtreecommitdiff
path: root/scripts/patcher/common.js
blob: 05523e5aaa58b104e266232f2e9eda53264b9886 (plain)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
 * Vencord, a modification for Discord's desktop app
 * Copyright (c) 2022 Vendicated and contributors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

const path = require("path");
const readline = require("readline");
const fs = require("fs");
const menu = require("console-menu");

function pathToBranch(dir) {
    dir = dir.toLowerCase();
    if (dir.endsWith("development")) {
        return "development";
    }
    if (dir.endsWith("canary")) {
        return "canary";
    }
    if (dir.endsWith("ptb")) {
        return "ptb";
    }
    return "stable";
}

const BRANCH_NAMES = [
    "Discord",
    "DiscordPTB",
    "DiscordCanary",
    "DiscordDevelopment",
    "discord",
    "discordptb",
    "discordcanary",
    "discorddevelopment",
    "discord-ptb",
    "discord-canary",
    "discord-development",
    // Flatpak
    "com.discordapp.Discord",
    "com.discordapp.DiscordPTB",
    "com.discordapp.DiscordCanary",
    "com.discordapp.DiscordDevelopment",
];

const MACOS_DISCORD_DIRS = [
    "Discord.app",
    "Discord PTB.app",
    "Discord Canary.app",
    "Discord Development.app",
];

if (process.platform === "linux" && process.env.SUDO_USER) {
    process.env.HOME = fs
        .readFileSync("/etc/passwd", "utf-8")
        .match(new RegExp(`^${process.env.SUDO_USER}.+$`, "m"))[0]
        .split(":")[5];
}

const LINUX_DISCORD_DIRS = [
    "/usr/share",
    "/usr/lib64",
    "/opt",
    `${process.env.HOME}/.local/share`,
    `${process.env.HOME}/.dvm`,
    "/var/lib/flatpak/app",
    `${process.env.HOME}/.local/share/flatpak/app`,
];

const FLATPAK_NAME_MAPPING = {
    DiscordCanary: "discord-canary",
    DiscordPTB: "discord-ptb",
    DiscordDevelopment: "discord-development",
    Discord: "discord",
};

const ENTRYPOINT = path
    .join(process.cwd(), "dist", "patcher.js")
    .replace(/\\/g, "/");

function question(question) {
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout,
        terminal: false,
    });

    return new Promise(resolve => {
        rl.question(question, answer => {
            rl.close();
            resolve(answer);
        });
    });
}

async function getMenuItem(installations) {
    const menuItems = installations.map(info => ({
        title: info.patched ? "[MODIFIED] " + info.location : info.location,
        info,
    }));

    const result = await menu(
        [
            ...menuItems,
            { title: "Specify custom path", info: "custom" },
            { title: "Exit without patching", exit: true }
        ],
        {
            header: "Select a Discord installation to patch:",
            border: true,
            helpMessage:
                "Use the up/down arrow keys to select an option. " +
                "Press ENTER to confirm.",
        }
    );

    if (!result || !result.info || result.exit) {
        console.log("No installation selected.");
        process.exit(0);
    }

    if (result.info === "custom") {
        const customPath = await question("Please enter the path: ");
        if (!customPath || !fs.existsSync(customPath)) {
            console.log("No such Path or not specifed.");
            process.exit();
        }

        const resourceDir = path.join(customPath, "resources");
        if (!fs.existsSync(path.join(resourceDir, "app.asar"))) {
            console.log("Unsupported Install. resources/app.asar not found");
            process.exit();
        }

        const appDir = path.join(resourceDir, "app");
        result.info = {
            branch: "unknown",
            patched: fs.existsSync(appDir),
            location: customPath,
            versions: [{
                path: appDir,
                name: null
            }],
            arch: process.platform === "linux" ? "linux" : "win32",
            isFlatpak: false,
        };
    }

    if (result.info.patched) {
        const answer = await question(
            "This installation has already been modified. Overwrite? [Y/n]: "
        );

        if (!["y", "yes", "yeah", ""].includes(answer.toLowerCase())) {
            console.log("Not patching.");
            process.exit(0);
        }
    }

    return result.info;
}

function getWindowsDirs() {
    const dirs = [];
    for (const dir of fs.readdirSync(process.env.LOCALAPPDATA)) {
        if (!BRANCH_NAMES.includes(dir)) continue;

        const location = path.join(process.env.LOCALAPPDATA, dir);
        if (!fs.statSync(location).isDirectory()) continue;

        const appDirs = fs
            .readdirSync(location, { withFileTypes: true })
            .filter(file => file.isDirectory())
            .filter(file => file.name.startsWith("app-"))
            .map(file => path.join(location, file.name));

        const versions = [];
        let patched = false;

        for (const fqAppDir of appDirs) {
            const resourceDir = path.join(fqAppDir, "resources");
            if (!fs.existsSync(path.join(resourceDir, "app.asar"))) {
                continue;
            }
            const appDir = path.join(resourceDir, "app");
            if (fs.existsSync(appDir)) {
                patched = true;
            }
            versions.push({
                path: appDir,
                name: /app-([0-9.]+)/.exec(fqAppDir)[1],
            });
        }

        if (appDirs.length) {
            dirs.push({
                branch: dir,
                patched,
                location,
                versions,
                arch: "win32",
                flatpak: false,
            });
        }
    }
    return dirs;
}

function getDarwinDirs() {
    const dirs = [];
    for (const dir of fs.readdirSync("/Applications")) {
        if (!MACOS_DISCORD_DIRS.includes(dir)) continue;

        const location = path.join("/Applications", dir, "Contents");
        if (!fs.existsSync(location)) continue;
        if (!fs.statSync(location).isDirectory()) continue;

        const appDirs = fs
            .readdirSync(location, { withFileTypes: true })
            .filter(file => file.isDirectory())
            .filter(file => file.name.startsWith("Resources"))
            .map(file => path.join(location, file.name));

        const versions = [];
        let patched = false;

        for (const resourceDir of appDirs) {
            if (!fs.existsSync(path.join(resourceDir, "app.asar"))) {
                continue;
            }
            const appDir = path.join(resourceDir, "app");
            if (fs.existsSync(appDir)) {
                patched = true;
            }

            versions.push({
                path: appDir,
                name: null, // MacOS installs have no version number
            });
        }

        if (appDirs.length) {
            dirs.push({
                branch: dir,
                patched,
                location,
                versions,
                arch: "win32",
            });
        }
    }
    return dirs;
}

function getLinuxDirs() {
    const dirs = [];
    for (const dir of LINUX_DISCORD_DIRS) {
        if (!fs.existsSync(dir)) continue;
        for (const branch of fs.readdirSync(dir)) {
            if (!BRANCH_NAMES.includes(branch)) continue;

            const location = path.join(dir, branch);
            if (!fs.statSync(location).isDirectory()) continue;

            const isFlatpak = location.includes("/flatpak/");

            let appDirs = [];

            if (isFlatpak) {
                const fqDir = path.join(location, "current", "active", "files");
                if (!/com\.discordapp\.(\w+)\//.test(fqDir)) continue;
                const branchName = /com\.discordapp\.(\w+)\//.exec(fqDir)[1];
                if (!Object.keys(FLATPAK_NAME_MAPPING).includes(branchName)) {
                    continue;
                }
                const appDir = path.join(
                    fqDir,
                    FLATPAK_NAME_MAPPING[branchName]
                );

                if (!fs.existsSync(appDir)) continue;
                if (!fs.statSync(appDir).isDirectory()) continue;

                const resourceDir = path.join(appDir, "resources");

                appDirs.push(resourceDir);
            } else {
                appDirs = fs
                    .readdirSync(location, { withFileTypes: true })
                    .filter(file => file.isDirectory())
                    .filter(
                        file =>
                            file.name.startsWith("app-") ||
                            file.name === "resources"
                    )
                    .map(file => path.join(location, file.name));
            }

            const versions = [];
            let patched = false;

            for (const resourceDir of appDirs) {
                if (!fs.existsSync(path.join(resourceDir, "app.asar"))) {
                    continue;
                }
                const appDir = path.join(resourceDir, "app");
                if (fs.existsSync(appDir)) {
                    patched = true;
                }

                const version = /app-([0-9.]+)/.exec(resourceDir);

                versions.push({
                    path: appDir,
                    name: version && version.length > 1 ? version[1] : null,
                });
            }

            if (appDirs.length) {
                dirs.push({
                    branch,
                    patched,
                    location,
                    versions,
                    arch: "linux",
                    isFlatpak,
                });
            }
        }
    }
    return dirs;
}

module.exports = {
    pathToBranch,
    BRANCH_NAMES,
    MACOS_DISCORD_DIRS,
    LINUX_DISCORD_DIRS,
    FLATPAK_NAME_MAPPING,
    ENTRYPOINT,
    question,
    getMenuItem,
    getWindowsDirs,
    getDarwinDirs,
    getLinuxDirs,
};