aboutsummaryrefslogtreecommitdiff
path: root/scripts/patcher/common.js
blob: f750601589f0f70f84f0fb71f505fd41d7a81fae (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
const path = require("path");
const readline = require("readline");
const fs = require("fs");
const menu = require("console-menu");

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`,
    "/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) {
    let menuItems = installations.map(info => ({
        title: info.patched ? "[MODIFIED] " + info.location : info.location,
        info,
    }));

    if (menuItems.length === 0) {
        console.log("No Discord installations found.");
        process.exit(1);
    }

    const result = await menu(
        [...menuItems, { 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.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));

        let 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));

        let 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));
            }

            let 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 = {
    BRANCH_NAMES,
    MACOS_DISCORD_DIRS,
    LINUX_DISCORD_DIRS,
    FLATPAK_NAME_MAPPING,
    ENTRYPOINT,
    question,
    getMenuItem,
    getWindowsDirs,
    getDarwinDirs,
    getLinuxDirs,
};