Skip to content

Commit

Permalink
discord patch
Browse files Browse the repository at this point in the history
  • Loading branch information
alvindimas05 committed Jul 25, 2024
1 parent f820196 commit 6627292
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 33 deletions.
Binary file modified bun.lockb
Binary file not shown.
6 changes: 3 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import dotenv from "dotenv";
import CommandLine from "./src/commandline";
import {isRoot,exec} from "./src/utils";
import {check_update, update} from "./src/update";
import CommandLine from "@src/commandline";
import {isRoot,exec} from "@src/utils";
import {check_update, update} from "@src/update";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"module": "index.ts",
"type": "commonjs",
"scripts": {
"clean": "bunx rimraf build",
"clean": "rimraf build",
"dev": "bun run index.ts",
"compile": "bunx webpack",
"compile": "webpack",
"build": "bun run clean && bun run compile && bash build.sh"
},
"devDependencies": {
Expand Down
12 changes: 7 additions & 5 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Chromium from "./chromium";
import Amdfriend from "./amdfriend";
import AppPatch from "./apppatch";
import Chromium from "@patches/chromium";
import Amdfriend from "@patches/amdfriend";
import AppPatch from "@patches/apppatch";
import Discord from "@patches/discord";

export default class App {
path: string;
Expand All @@ -11,8 +12,9 @@ export default class App {
this.name = path.split("/").pop()!.replace(/.app/g, '');

this.appPatches = [
new Chromium(this.path),
new Amdfriend(this.path),
new Chromium(path),
new Amdfriend(path),
new Discord(path),
];
}
patch = async () => this.appPatches.forEach(patch => patch.supported() && patch.patch());
Expand Down
15 changes: 0 additions & 15 deletions src/apppatch.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/commandline.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
7 changes: 3 additions & 4 deletions src/amdfriend.ts → src/patches/amdfriend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@ 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;
constructor(appPath: string) {
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);
Expand Down
19 changes: 19 additions & 0 deletions src/patches/apppatch.ts
Original file line number Diff line number Diff line change
@@ -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}`);
}
}
2 changes: 1 addition & 1 deletion src/chromium.ts → src/patches/chromium.ts
Original file line number Diff line number Diff line change
@@ -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?: {
Expand Down
36 changes: 36 additions & 0 deletions src/patches/discord.ts
Original file line number Diff line number Diff line change
@@ -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, "");
}
}
2 changes: 1 addition & 1 deletion src/update.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {exec} from "./utils";
import {exec} from "@src/utils";

interface UpdateResponse {
tag_name: string
Expand Down
20 changes: 20 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 6 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"module": "commonjs",
"moduleDetection": "force",
"allowJs": false,
"baseUrl": "./",

"allowImportingTsExtensions": false,
"verbatimModuleSyntax": false,
Expand All @@ -15,6 +16,11 @@
"noImplicitAny": false,
"forceConsistentCasingInFileNames": true,
"esModuleInterop": true,

"paths": {
"@src/*": ["src/*"],
"@patches/*": ["src/patches/*"]
}
},
"ts-loader": {
"allowTsInNodeModules": true
Expand Down
4 changes: 3 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
},
};

0 comments on commit 6627292

Please sign in to comment.