Skip to content

Commit

Permalink
add new patch for chromium browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
alvindimas05 committed Aug 26, 2024
1 parent 69cc3d1 commit d72aecb
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 19 deletions.
10 changes: 3 additions & 7 deletions src/commandline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ import fs from "fs";
import chalk from "chalk";
import App from "@src/app";
import inquirer from "inquirer";
// @ts-ignore
import clear from "console-clear";
import path from "path";
import {PatchType} from "@src/types";


export default class CommandLine {
basePath = "/Applications/";
// @ts-ignore
supportedApps: App[];
async start(){
this.getSupportedApplication();

// @ts-ignore
console.log(`${chalk.red("AMD")}Helper\n`);
console.log(`Applications that can be patched:`)
this.logSupportedApps();
Expand Down Expand Up @@ -60,14 +56,14 @@ export default class CommandLine {

switch(app.patched()){
case PatchType.PATCHED:
// @ts-ignore
status = chalk.green("PATCHED");
break;
case PatchType.UNPATCHED:
// @ts-ignore
status = chalk.red("NOT PATCHED");
break;
// @ts-ignore
case PatchType.OLD_PATCH:
status = chalk.blue("NEW PATCH");
break;
default: status = chalk.yellow("UNDETECTED");
}

Expand Down
75 changes: 65 additions & 10 deletions src/patches/chromium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ import os from "os";
import fs from "fs";
import AppPatch from "@patches/apppatch";
import {PatchType} from "@src/types";
import {
amdhelperChromiumBash,
amdhelperChromiumBashName,
amdhelperChromiumPlist,
amdhelperChromiumPlistName
} from "@src/scripts/chromium";
import {escapePathSpaces, exec} from "@src/utils";
import child_process from "child_process";

interface ChromiumConfig {
browser?: {
Expand All @@ -11,11 +19,14 @@ interface ChromiumConfig {
}

const chromiumBrowsers = ["Chromium", "Google Chrome", "Arc", "Microsoft Edge", "Brave"];

const exeName = "amdhelper_executable"
export default class Chromium extends AppPatch {
configPath: string | null = null;
patchValue = "enable-gpu-rasterization@2";
// @ts-ignore
patchValue = "use-angle@1";
oldPatchValues = ["enable-gpu-rasterization@1", "enable-gpu-rasterization@2"];
exePath = path.join(this.appPath, "Contents", "MacOS", exeName);
bashPath = path.join("/", "Library", "amdhelper", amdhelperChromiumBashName);
plistPath = path.join("/", "Library", "LaunchAgents", amdhelperChromiumPlistName);
config: ChromiumConfig;
constructor(appPath: string) {
super(appPath);
Expand All @@ -41,34 +52,38 @@ export default class Chromium extends AppPatch {
return;
}
this.configPath = null;

}
setConfig(){
// @ts-ignore
this.config = JSON.parse(fs.readFileSync(this.configPath));
this.config = JSON.parse(fs.readFileSync(this.configPath).toString("utf8"));
}
supported() {
return chromiumBrowsers.includes(this.appName) && this.configPath != null;
}
patched() {
return (this.config.browser !== undefined &&
this.config.browser.enabled_labs_experiments !== undefined &&
this.config.browser.enabled_labs_experiments.includes(this.patchValue)) ?
PatchType.PATCHED : PatchType.UNPATCHED;
if(this.isOldPatch()) return PatchType.OLD_PATCH;
return fs.existsSync(this.bashPath) ? PatchType.PATCHED : PatchType.UNPATCHED;
// return fs.existsSync(this.exePath) ?
// PatchType.PATCHED : PatchType.UNPATCHED;
}
async patch(){
if(this.configPath == undefined || this.config == undefined){
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}...`);

this.apply();
this.save();
await this.addLaunchAgent();
}
apply(){
if(this.config.browser === undefined) this.config.browser = { enabled_labs_experiments: [] };
Expand All @@ -79,4 +94,44 @@ export default class Chromium extends AppPatch {
save(){
fs.writeFileSync(this.configPath!, JSON.stringify(this.config));
}
isOldPatch(){
return (this.config.browser !== undefined &&
this.config.browser.enabled_labs_experiments !== undefined &&
this.config.browser.enabled_labs_experiments.some(value => this.oldPatchValues.includes(value)))
}
async addLaunchAgent(){
fs.mkdirSync(path.join(this.bashPath, ".."), { recursive: true });
fs.writeFileSync(this.bashPath,
amdhelperChromiumBash(this.appName));
await exec(`sudo chmod +x ${escapePathSpaces(this.bashPath)}`);

fs.writeFileSync(this.plistPath, amdhelperChromiumPlist);
child_process.spawn("bash", [this.bashPath], { detached: true});
}
// async addExecutable(){
// const command = `./${escapePathSpaces(this.appName)} --enable-angle-features=disableBlendFuncExtended`;
// fs.writeFileSync(this.exePath, command);
// await exec(`sudo chmod +x ${escapePathSpaces(this.exePath)}`);
//
// const plistPath = path.join(this.appPath, "Contents", "Info.plist");
// let plist = fs.readFileSync(plistPath).toString("utf8")
// const n = plist.lastIndexOf("<key>CFBundleExecutable</key>");
// plist = plist.slice(0, n) + plist.slice(n).replace(`<string>${this.appName}</string>`,
// `<string>${exeName}</string>`);
// fs.writeFileSync(plistPath, plist);
// }
// oldApply(){
// 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.oldPatchValue);
// console.log(`Patch applied to ${this.appName}!`)
// }
removeOldPatch(){
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}!`)
}
}
19 changes: 19 additions & 0 deletions src/scripts/amdhelper_chromium.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
appname="Google Chrome"
is_running=0
while [ 1 ]; do
if [[ $(pgrep -f "$appname") ]]; then
if [[ $is_running == 0 ]]; then
pkill -x "$appname"
sleep 1
open -a /Applications/"$appname".app --args --enable-angle-features=disableBlendFuncExtended
echo "Detected user opening $appname..."
is_running=1
fi
else
if [[ $is_running == 1 ]]; then
echo "Detected user exiting $appname..."
is_running=0
fi
fi
sleep 0.1
done
43 changes: 43 additions & 0 deletions src/scripts/chromium.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
export const amdhelperChromiumBashName = "amdhelper_chromium";
export const amdhelperChromiumBash = (appname: string) => `
appname="${appname}"
is_running=0
while [ 1 ]; do
if [[ $(pgrep -f "$appname") ]]; then
if [[ $is_running == 0 ]]; then
pkill -x "$appname"
sleep 1
open -a /Applications/"$appname".app --args --enable-angle-features=disableBlendFuncExtended
echo "Detected user opening $appname..."
is_running=1
fi
else
if [[ $is_running == 1 ]]; then
echo "Detected user exiting $appname..."
is_running=0
fi
fi
sleep 0.1
done
`;

export const amdhelperChromiumPlistName = "org.alvindimas05.amdhelper_chromium.plist";
export const amdhelperChromiumPlist = `
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.alvindimas05.amdhelper_chromium</string>
<key>LaunchOnlyOnce</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/bin/sh</string>
<string>/Library/amdhelper/amdhelper_chromium</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
`;
17 changes: 17 additions & 0 deletions src/scripts/org.alvindimas05.amdhelper_chromium.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.alvindimas05.amdhelper_chromium</string>
<key>LaunchOnlyOnce</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/bin/sh</string>
<string>/Library/amdhelper/amdhelper_chromium</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export enum PatchType {
PATCHED = 1,
UNPATCHED = 0,
UNDETECTED = -1
UNDETECTED = -1,
OLD_PATCH = -2,
}
3 changes: 2 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// @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 escapePathSpaces = (path: string) => path.replace(/(\s+)/g, '\\$1');

// export const spawn = promisify(child_process.spawn);
export const exec = promisify(child_process.exec);
export const patchOptions: PatchOptions = {backup: false, clearXA: false, dryRun: false, inPlace: true, sign: true };

Expand Down

0 comments on commit d72aecb

Please sign in to comment.