From 66272929a5db0e2400dfc4c9fe426af42a3bbfbb Mon Sep 17 00:00:00 2001 From: Alvin Dimas Praditya Date: Thu, 25 Jul 2024 15:45:46 +0700 Subject: [PATCH] discord patch --- bun.lockb | Bin 80955 -> 80955 bytes index.ts | 6 +++--- package.json | 4 ++-- src/app.ts | 12 ++++++----- src/apppatch.ts | 15 -------------- src/commandline.ts | 2 +- src/{ => patches}/amdfriend.ts | 7 +++---- src/patches/apppatch.ts | 19 +++++++++++++++++ src/{ => patches}/chromium.ts | 2 +- src/patches/discord.ts | 36 +++++++++++++++++++++++++++++++++ src/update.ts | 2 +- src/utils.ts | 20 ++++++++++++++++++ tsconfig.json | 6 ++++++ webpack.config.js | 4 +++- 14 files changed, 102 insertions(+), 33 deletions(-) delete mode 100644 src/apppatch.ts rename src/{ => patches}/amdfriend.ts (91%) create mode 100644 src/patches/apppatch.ts rename src/{ => patches}/chromium.ts (98%) create mode 100644 src/patches/discord.ts diff --git a/bun.lockb b/bun.lockb index ba4658d22cae67562757d20f54f14eb19969fdbd..62c8872181aad098ab9afb0defda314e7e217a95 100755 GIT binary patch delta 24 gcmdn}gJt&*mJRc7b27#mn(Gk delta 24 bcmdn}gJt&*mJRc7b22eN!R8gWCno>^db this.appPatches.forEach(patch => patch.supported() && patch.patch()); diff --git a/src/apppatch.ts b/src/apppatch.ts deleted file mode 100644 index 3f0d071..0000000 --- a/src/apppatch.ts +++ /dev/null @@ -1,15 +0,0 @@ -export default class AppPatch { - appName: string; - appPath: string; - constructor(appPath: string) { - this.appPath = appPath; - this.appName = appPath.split("/").pop()!.replace(/.app/g, ''); - } - patched(): number { - return -1; - } - patch(){} - supported(): boolean { - return false; - } -} \ No newline at end of file diff --git a/src/commandline.ts b/src/commandline.ts index c07ef21..35571c1 100644 --- a/src/commandline.ts +++ b/src/commandline.ts @@ -1,6 +1,6 @@ import fs from "fs"; import chalk from "chalk"; -import App from "./app"; +import App from "@src/app"; import inquirer from "inquirer"; // @ts-ignore import clear from "console-clear"; diff --git a/src/amdfriend.ts b/src/patches/amdfriend.ts similarity index 91% rename from src/amdfriend.ts rename to src/patches/amdfriend.ts index 7839b10..6855907 100644 --- a/src/amdfriend.ts +++ b/src/patches/amdfriend.ts @@ -5,11 +5,10 @@ import {walkDirectory} from "amdfriend/src/utils"; import path, {resolve} from "path"; import type {PatchOptions} from "amdfriend/src/types"; import {patchFile} from "amdfriend/src"; -import {isRoot} from "./utils"; -import AppPatch from "./apppatch"; +import {isRoot, patchOptions} from "@src/utils"; +import AppPatch from "@patches/apppatch"; const amdfriends = ["Adobe Photoshop", "CorelDRAW"]; -const patchOptions: PatchOptions = {backup: false, clearXA: false, dryRun: false, inPlace: true, sign: true }; export default class Amdfriend extends AppPatch { patchedPath: string; @@ -17,7 +16,7 @@ export default class Amdfriend extends AppPatch { super(appPath); this.patchedPath = path.join(this.appPath, "Contents", ".amdhelper"); } - patched(): number { + patched() { const fileExists = fs.existsSync(this.patchedPath); if(!isRoot() && !fileExists) return -1; return (fileExists ? 1 : 0); diff --git a/src/patches/apppatch.ts b/src/patches/apppatch.ts new file mode 100644 index 0000000..e637fc7 --- /dev/null +++ b/src/patches/apppatch.ts @@ -0,0 +1,19 @@ +type Patched = 1 | 0 | -1; + +export default class AppPatch { + appName: string; + appPath: string; + constructor(appPath: string) { + this.appPath = appPath; + this.appName = appPath.split("/").pop()!.replace(/.app/g, ''); + } + patched(): Patched { + throw new Error(`Method "patched" must be implemented on class ${this.constructor.name}`); + } + patch() { + throw new Error(`Method "patch" must be implemented on class ${this.constructor.name}`); + } + supported(): boolean { + throw new Error(`Method "supported" must be implemented on class ${this.constructor.name}`); + } +} \ No newline at end of file diff --git a/src/chromium.ts b/src/patches/chromium.ts similarity index 98% rename from src/chromium.ts rename to src/patches/chromium.ts index 95af1d8..2769fbf 100644 --- a/src/chromium.ts +++ b/src/patches/chromium.ts @@ -1,7 +1,7 @@ import path from "path"; import os from "os"; import fs from "fs"; -import AppPatch from "./apppatch"; +import AppPatch from "@patches/apppatch"; interface ChromiumConfig { browser?: { diff --git a/src/patches/discord.ts b/src/patches/discord.ts new file mode 100644 index 0000000..82f29d3 --- /dev/null +++ b/src/patches/discord.ts @@ -0,0 +1,36 @@ +import AppPatch from "@patches/apppatch"; +import path from "path"; +import {homedir} from "os"; +import {isRoot, patchOptions, searchFile} from "@src/utils"; +import fs from "fs"; +import {patchFile} from "amdfriend/src"; + +const discordPath = path.join(homedir(), "Library", "Application Support", "discord") +export default class Discord extends AppPatch { + patchedPath: string; + krispPath: string; + constructor(appPath: string) { + super(appPath); + this.krispPath = path.join( + discordPath, + fs.readdirSync(discordPath) + .filter(a => a != ".DS_Store") + .sort((a, b) => a.localeCompare(b))[0] + , "modules", "discord_krisp", "discord_krisp.node"); + this.patchedPath = path.join(this.krispPath, "..", ".amdhelper"); + } + patched() { + const fileExists = fs.existsSync(this.patchedPath); + return (fileExists ? 1 : 0); + } + supported(): boolean { + return this.appName === "Discord"; + } + + async patch(){ + if(this.patched() === 1) return console.log(`${this.appName} already patched. Ignoring...`); + await patchFile(this.krispPath, patchOptions); + + fs.writeFileSync(this.patchedPath, ""); + } +} \ No newline at end of file diff --git a/src/update.ts b/src/update.ts index 8968a50..df8c7a9 100644 --- a/src/update.ts +++ b/src/update.ts @@ -1,4 +1,4 @@ -import {exec} from "./utils"; +import {exec} from "@src/utils"; interface UpdateResponse { tag_name: string diff --git a/src/utils.ts b/src/utils.ts index 0c7f6fc..2ca25eb 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,7 +1,27 @@ // @ts-ignore import {promisify} from "util"; import child_process from "child_process"; +import type {PatchOptions} from "amdfriend/src/types"; +import fs from "fs"; +import path from "path"; export const isRoot = () => process!.getuid() === 0 ; export const exec = promisify(child_process.exec); +export const patchOptions: PatchOptions = {backup: false, clearXA: false, dryRun: false, inPlace: true, sign: true }; + +export function searchFile(dirPath: string, fileName: string): string | null { + const files = fs.readdirSync(dirPath); + for (const file of files) { + const filePath = path.join(dirPath, file); + + const fileStat = fs.statSync(filePath); + if (fileStat.isDirectory()) { + const result = searchFile(filePath, fileName); + if (result) return result; + } else if (file === fileName) { + return filePath; + } + } + return null; +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 185cda5..bcf18f3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,6 +5,7 @@ "module": "commonjs", "moduleDetection": "force", "allowJs": false, + "baseUrl": "./", "allowImportingTsExtensions": false, "verbatimModuleSyntax": false, @@ -15,6 +16,11 @@ "noImplicitAny": false, "forceConsistentCasingInFileNames": true, "esModuleInterop": true, + + "paths": { + "@src/*": ["src/*"], + "@patches/*": ["src/patches/*"] + } }, "ts-loader": { "allowTsInNodeModules": true diff --git a/webpack.config.js b/webpack.config.js index f8860a1..2de47b6 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -19,7 +19,9 @@ module.exports = { resolve: { extensions: ['.js', '.ts'], alias: { - "amdfriend/src": "amdfriend/dist" + "amdfriend/src": "amdfriend/dist", + "@src": path.resolve(__dirname, "src"), + "@patches": path.resolve(__dirname, "src", "patches"), } }, };