Skip to content

Commit

Permalink
improve chromium apps patch
Browse files Browse the repository at this point in the history
  • Loading branch information
alvindimas05 committed Sep 10, 2024
1 parent 3d5e16a commit 45b73a8
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 64 deletions.
2 changes: 2 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ async function main(){
await check_update();
if(!isRoot()) console.log("Please run as sudo to detect apps that's not patched.");

global.chromiumApps = [];

const cli = new CommandLine();
await cli.start();
}
Expand Down
12 changes: 9 additions & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@ export default class App {
constructor(path: string) {
this.path = path;
this.name = path.split("/").pop()!.replace(/.app/g, '');

global.electronApps = [];
this.appPatches = [
new Chromium(path),
new Amdfriend(path),
new Discord(path),
new Firefox(path),
new FirefoxDev(path),
];
if(global.commandlineChromiumMode){
this.appPatches = [new Chromium(path)];
}
}
getAppPatch(): AppPatch {
for(const app of this.appPatches){
if(!app.supported()) continue;
return app;
}
}
async patch() {
for(const app of this.appPatches){
Expand Down
67 changes: 58 additions & 9 deletions src/commandline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import inquirer from "inquirer";
import clear from "console-clear";
import path from "path";
import {PatchType} from "@src/types";
import Chromium, { bashPath, patchChromiumApps, removePatchChromiumApps } from "./patches/chromium";
import { exec } from "./utils";
import child_process from "child_process";

export default class CommandLine {
basePath = "/Applications/";
Expand All @@ -15,9 +18,19 @@ export default class CommandLine {

console.log(`Applications that can be patched:`)
this.logSupportedApps();
console.log("\n(A) Patch all apps")
console.log(`(G) ${global.disableGpuMode ? "Disable" : "Enable"} disable-gpu-rasterization patch instead of disableBlendFuncExtended`)
console.log(`(E) ${global.patchElectronApps ? "Disable" : "Enable"} patch Electron apps (${chalk.rgb(255,99,71)("EXPERIMENTAL")})`)
console.log("");
if(global.commandlineChromiumMode){
if (global.disableGpuMode) {
console.log(`(G) Use disableBlendFuncExtended patch instead of disable-gpu-rasterization`)
} else {
console.log(`(G) Use disable-gpu-rasterization patch instead of disableBlendFuncExtended`)
}
if(global.chromiumApps.length > 0) console.log("(P) Patch selected chromium apps.")
console.log("(R) Remove chromium apps patch")
} else {
console.log("(A) Patch all apps")
}
console.log(`(C) ${global.commandlineChromiumMode ? "Exit" : "Enter"} Chromium apps mode (${chalk.rgb(255,99,71)("EXPERIMENTAL")})`)
console.log("(Q) Quit")

// @ts-ignore
Expand All @@ -33,19 +46,52 @@ export default class CommandLine {
console.log("Bye!");
process.exit();
break;
case "e":
global.patchElectronApps = !global.patchElectronApps;
case "c":
global.commandlineChromiumMode = !global.commandlineChromiumMode;
break;
case "g":
global.disableGpuMode = !global.disableGpuMode;
break;
case "a":
await this.patchAllApps();
break;
if(!global.commandlineChromiumMode){
await this.patchAllApps();
break;
}
case "p":
if(global.chromiumApps.length > 0){
console.log("Applying chromium apps patch...")

const macosVersion = (await exec("sw_vers -productVersion")).stdout;
const majorVersion = parseInt(macosVersion.split(".")[0]);
const minorVersion = parseInt(macosVersion.split(".")[1]);
if(majorVersion > 13 && minorVersion > 3){
console.log("You are running on MacOS Sonoma 14.3.1 or above. Please restart or run the command below to start.");
console.log(`bash ${bashPath} &`)
} else {
child_process.spawn("bash", [bashPath], { stdio: "ignore", detached: true }).unref();
}

patchChromiumApps();
break;
}
case "r":
if(global.commandlineChromiumMode){
removePatchChromiumApps();
break;
}
default:
const val = parseInt(value);
if(!isNaN(val) && val <= this.supportedApps.length + 1 && val > 0)
await this.supportedApps[val-1].patch()
if (isNaN(val) || val < 1 || val > this.supportedApps.length + 1) break;
const app = this.supportedApps[val - 1];

if (global.commandlineChromiumMode) {
// @ts-ignore
const appPatch: Chromium = app.getAppPatch();
if(appPatch.selected()) break;
global.chromiumApps.push(app);
} else {
await app.patch()
}
}

const cli = new CommandLine();
Expand Down Expand Up @@ -73,6 +119,9 @@ export default class CommandLine {
case PatchType.EXPERIMENTAL:
status = chalk.rgb(255,99,71)("EXPERIMENTAL");
break;
case PatchType.SELECTED:
status = chalk.cyan("SELECTED");
break;
default: status = chalk.yellow("UNDETECTED");
}

Expand Down
9 changes: 9 additions & 0 deletions src/globals.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import App from "./app";

export {}

declare global {
var commandlineChromiumMode: boolean;
var disableGpuMode: boolean;
var chromiumApps: App[];
}
82 changes: 32 additions & 50 deletions src/patches/chromium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ interface ChromiumConfig {
}

const chromiumBrowsers = ["Chromium", "Google Chrome", "Arc", "Microsoft Edge", "Brave"];
export const bashPath = path.join("/", "Library", "amdhelper", amdhelperChromiumBashName);
export const plistPath = path.join("/", "Library", "LaunchAgents", amdhelperChromiumPlistName);

export default class Chromium extends AppPatch {
configPath: string | null = null;
// patchValue = "use-angle@1";
oldPatchValues = ["enable-gpu-rasterization@1", "enable-gpu-rasterization@2"];
bashPath = path.join("/", "Library", "amdhelper", amdhelperChromiumBashName);
plistPath = path.join("/", "Library", "LaunchAgents", amdhelperChromiumPlistName);
config: ChromiumConfig;
constructor(appPath: string) {
super(appPath);
Expand Down Expand Up @@ -59,47 +59,20 @@ export default class Chromium extends AppPatch {
}
supported() {
return (chromiumBrowsers.includes(this.appName) && this.configPath != null) ||
(global.patchElectronApps && this.supportElectron());
this.supportElectron();
}
supportElectron(){
return fs.existsSync(path.join("/Applications", `${this.appName}.app`, "Contents", "Frameworks", "Electron Framework.framework"))
}
selected(): boolean {
return global.chromiumApps.findIndex(fapp => fapp.name === this.appName) !== -1
}
patched() {
if(this.selected()) return PatchType.SELECTED;
if(this.isOldPatch()) return PatchType.OLD_PATCH;
if(this.supportElectron()) return PatchType.EXPERIMENTAL;
return fs.existsSync(this.bashPath) ?
PatchType.PATCHED : PatchType.UNPATCHED;
// return (fs.existsSync(this.bashPath) && this.config.browser !== undefined &&
// this.config.browser.enabled_labs_experiments !== undefined &&
// this.config.browser.enabled_labs_experiments.includes(this.patchValue))
// ? PatchType.PATCHED : PatchType.UNPATCHED;
return fs.existsSync(bashPath) ? PatchType.PATCHED : PatchType.UNPATCHED;
}
async patch(){
// if(this.configPath == undefined || this.config == null){
// console.error(`Can't apply patch to ${this.appName}! Config not found.`)
// return;
// }

if(this.isOldPatch()) this.removeOldPatch();

// if(this.patched()){
// console.log(`${this.appName} already patched. Ignoring...`);
// return;
// }

console.log(`Applying patch to ${this.appName}...`);
if(global.electronApps.indexOf(this.appName) === -1) global.electronApps.push(this.appName);

// this.apply();
// this.save();
await this.addLaunchAgent();
}
// apply(){
// if(this.config.browser === undefined) this.config.browser = { enabled_labs_experiments: [] };
// if(this.config.browser.enabled_labs_experiments === undefined) this.config.browser.enabled_labs_experiments = [];
// this.config.browser!.enabled_labs_experiments.push(this.patchValue);
// console.log(`Patch applied to ${this.appName}!`)
// }
save(){
fs.writeFileSync(this.configPath!, JSON.stringify(this.config));
}
Expand All @@ -108,27 +81,36 @@ export default class Chromium extends AppPatch {
this.config.browser.enabled_labs_experiments !== undefined &&
this.config.browser.enabled_labs_experiments.some(value => this.oldPatchValues.includes(value)))
}
async addLaunchAgent(){
try {
await exec(`pkill -f bash`);
} catch {}

let apps = chromiumBrowsers;
if(global.patchElectronApps) apps.push(...global.electronApps);

fs.mkdirSync(path.join(this.bashPath, ".."), { recursive: true });
fs.writeFileSync(this.bashPath, amdhelperChromiumBash(apps, global.disableGpuMode));
await exec(`sudo chmod +x ${escapePathSpaces(this.bashPath)}`);

fs.writeFileSync(this.plistPath, amdhelperChromiumPlist);
child_process.spawn("bash", [this.bashPath], { detached: true });
}
removeOldPatch(){
if (!this.isOldPatch()) return;
if(this.config.browser === undefined) return;
if(this.config.browser.enabled_labs_experiments === undefined) return;
this.config.browser!.enabled_labs_experiments =
this.config.browser!.enabled_labs_experiments.filter(val => !this.oldPatchValues.includes(val));
this.save();
console.log(`Removing old patch from ${this.appName}!`)
}
}

async function killPatchProcess(){
try { await exec(`pkill -f bash`) } catch {}
try { await exec(`pkill -f amdhelper_chromium`) } catch {}
}

export async function patchChromiumApps(){
killPatchProcess();
const apps = global.chromiumApps.map(app => app.name);

fs.mkdirSync(path.join(bashPath, ".."), { recursive: true });
fs.writeFileSync(bashPath, amdhelperChromiumBash(apps, global.disableGpuMode));
await exec(`sudo chmod +x ${escapePathSpaces(bashPath)}`);

fs.writeFileSync(plistPath, amdhelperChromiumPlist);
}

export async function removePatchChromiumApps(){
console.log("Removing chromium apps patch...");
killPatchProcess();
try { fs.rmSync(bashPath) } catch {}
try { fs.rmSync(plistPath) } catch {}
}
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export enum PatchType {
UNPATCHED = 0,
UNDETECTED = -1,
OLD_PATCH = -2,
EXPERIMENTAL = -3
EXPERIMENTAL = -3,
SELECTED = -4,
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
},
"include": [
"index.ts",
"src/globals.d.ts",
"src/**/*.ts",
"src/globals.d.ts"
],
"exclude": [
"node_modules",
Expand Down

0 comments on commit 45b73a8

Please sign in to comment.