Skip to content

Commit

Permalink
Catch errors in patches
Browse files Browse the repository at this point in the history
  • Loading branch information
Vendicated committed May 30, 2023
1 parent 1fde8b6 commit 5af7423
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 20 deletions.
6 changes: 5 additions & 1 deletion build.mjs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { readFile, writeFile, readdir } from "fs/promises";
import { extname } from "path";
import { extname, resolve } from "path";
import { createHash } from "crypto";

import { rollup } from "rollup";
import esbuild from "rollup-plugin-esbuild";
import commonjs from "@rollup/plugin-commonjs";
import nodeResolve from "@rollup/plugin-node-resolve";
import { typescriptPaths } from "rollup-plugin-typescript-paths";
import swc from "@swc/core";

const extensions = [".js", ".jsx", ".mjs", ".ts", ".tsx", ".cts", ".mts"];

/** @type import("rollup").InputPluginOption */
const plugins = [
typescriptPaths({
preserveExtensions: true
}),
nodeResolve(),
commonjs(),
{
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"esbuild": "^0.16.14",
"rollup": "^3.9.1",
"rollup-plugin-esbuild": "^5.0.0",
"rollup-plugin-typescript-paths": "^1.4.0",
"vendetta-types": "latest"
},
"pnpm": {
Expand Down
10 changes: 3 additions & 7 deletions plugins/ClickableBioLinks/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { findByNameAll, findByProps } from "@vendetta/metro";
import { ReactNative, url as URLOpener, clipboard } from "@vendetta/metro/common";
import { after } from "@vendetta/patcher";
import { storage } from "@vendetta/plugin";
import { getAssetIDByName } from "@vendetta/ui/assets";
import { showToast } from "@vendetta/ui/toasts";
import { after, unpatchAll } from "~/shared/vendetta-wrappers";

function lazy<T>(fn: () => T) {
let v: T;
Expand All @@ -13,8 +13,6 @@ function lazy<T>(fn: () => T) {
const findActionShitter = lazy(() => findByProps("hideActionSheet"));
const findClipboardAsset = lazy(() => getAssetIDByName("ic_message_copy"));

const ups = [];

function walkReactTree(root: any, visit: (node: any) => void) {
if (!root) return;

Expand All @@ -32,7 +30,7 @@ function walkReactTree(root: any, visit: (node: any) => void) {

// WHY DOES DISCORD HAVE TWO OF THESE IM GONNA EXPLODE
for (const BioText of findByNameAll("BioText", false)) {
const up = after("default", BioText, ([props], res) => {
after("default", BioText, ([props], res) => {
if (!res?.props?.children) return;

// make bio text selectable
Expand Down Expand Up @@ -63,10 +61,8 @@ for (const BioText of findByNameAll("BioText", false)) {
</ReactNative.Pressable>
);
});

ups.push(up);
}

export const onUnload = () => ups.forEach(up => up());
export const onUnload = () => unpatchAll();

export { default as settings } from "./settings";
18 changes: 8 additions & 10 deletions plugins/Decap/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { findByProps } from "@vendetta/metro";
import { before } from "@vendetta/patcher";
import { decap } from "./decap";
import { before, unpatchAll } from "~/shared/vendetta-wrappers";

const ups = [];

function bef(obj: any, name: string, cb: (args: any[]) => any) {
ups.push(before(name, obj, cb));
function doDecap(msg?: { content?: string }) {
if (msg?.content) {
msg.content = decap(msg.content);
}
}

const doDecap = (msg?: { content?: string }) => msg?.content && (msg.content = decap(msg.content));

bef(findByProps("editMessage", "sendMessage"), "sendMessage", args => doDecap(args[1]));
before("sendMessage", findByProps("editMessage", "sendMessage"), args => void doDecap(args[1]));

bef(findByProps("uploadLocalFiles"), "uploadLocalFiles", args => doDecap(args[0].parsedMessage));
before("uploadLocalFiles", findByProps("uploadLocalFiles"), args => void doDecap(args[0].parsedMessage));

export const onUnload = () => ups.forEach(up => up());
export const onUnload = () => unpatchAll();
27 changes: 27 additions & 0 deletions shared/vendetta-wrappers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { after as vAfter, before as vBefore, instead as vInstead } from "@vendetta/patcher";

export const unpatches = [] as Array<() => boolean>;

function wrapCb<F extends Function>(type: string, name: string, cb: F): F {
return function () {
try {
return cb.apply(this, arguments);
} catch (err) {
console.error(`Error while running ${type} callback for ${name}:`, err);
}
} as any as F;
}

export const before: (...args: Parameters<typeof vBefore>) => void = (name, obj, cb, once) => {
unpatches.push(vBefore(name, obj, wrapCb("before", name, cb), once));
};

export const after: (...args: Parameters<typeof vAfter>) => void = (name, obj, cb, once) => {
unpatches.push(vAfter(name, obj, wrapCb("after", name, cb), once));
};

export const instead: (...args: Parameters<typeof vInstead>) => void = (name, obj, cb, once) => {
unpatches.push(vInstead(name, obj, wrapCb("instead", name, cb), once));
};

export const unpatchAll = () => unpatches.forEach(u => u());
8 changes: 6 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
"moduleResolution": "node",
"esModuleInterop": true,
"jsx": "react-native",
"allowJs": true
"allowJs": true,
"baseUrl": ".",
"paths": {
"~/shared/*": ["shared/*"]
}
},
"include": ["plugins", "node_modules/vendetta-types", "tests"]
"include": ["plugins", "node_modules/vendetta-types", "tests", "shared"]
}

0 comments on commit 5af7423

Please sign in to comment.