aboutsummaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authorVen <vendicated@riseup.net>2022-10-01 02:27:28 +0200
committerGitHub <noreply@github.com>2022-10-01 02:27:28 +0200
commitef353f1d66dbf1d14e528830d267aac518ed1beb (patch)
tree67d13f5ce3cf42150884376c5f8d9b98f7300b38 /src/utils
parent3e64054283acbbe2697c9fc583bba7a06e1003b2 (diff)
downloadVencord-ef353f1d66dbf1d14e528830d267aac518ed1beb.tar.gz
Vencord-ef353f1d66dbf1d14e528830d267aac518ed1beb.tar.bz2
Vencord-ef353f1d66dbf1d14e528830d267aac518ed1beb.zip
Better authors field (#18)
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/IpcEvents.ts2
-rw-r--r--src/utils/constants.ts20
-rw-r--r--src/utils/patchWebpack.ts148
-rw-r--r--src/utils/types.ts7
4 files changed, 27 insertions, 150 deletions
diff --git a/src/utils/IpcEvents.ts b/src/utils/IpcEvents.ts
index b0a53f2..24e52b9 100644
--- a/src/utils/IpcEvents.ts
+++ b/src/utils/IpcEvents.ts
@@ -8,7 +8,7 @@ function strEnum<T extends Record<string, string>>(obj: T): T {
o[key] = obj[key] as any;
o[obj[key]] = key as any;
};
- return o;
+ return Object.freeze(o);
}
export default strEnum({
diff --git a/src/utils/constants.ts b/src/utils/constants.ts
index 3d60e29..57cd9b7 100644
--- a/src/utils/constants.ts
+++ b/src/utils/constants.ts
@@ -1,2 +1,22 @@
export const WEBPACK_CHUNK = "webpackChunkdiscord_app";
export const REACT_GLOBAL = "Vencord.Webpack.Common.React";
+
+// Add yourself here if you made more than one plugin
+export const Devs = Object.freeze({
+ Ven: {
+ name: "Vendicated",
+ id: 343383572805058560n
+ },
+ Arjix: {
+ name: "ArjixWasTaken",
+ id: 674710789138939916n
+ },
+ Cyn: {
+ name: "Cynosphere",
+ id: 150745989836308480n
+ },
+ Megu: {
+ name: "Megumin",
+ id: 545581357812678656n
+ }
+});
diff --git a/src/utils/patchWebpack.ts b/src/utils/patchWebpack.ts
deleted file mode 100644
index 0508968..0000000
--- a/src/utils/patchWebpack.ts
+++ /dev/null
@@ -1,148 +0,0 @@
-import { WEBPACK_CHUNK } from './constants';
-import Logger from "./logger";
-import { _initWebpack } from "../webpack";
-
-let webpackChunk: any[];
-
-const logger = new Logger("WebpackInterceptor", "#8caaee");
-
-Object.defineProperty(window, WEBPACK_CHUNK, {
- get: () => webpackChunk,
- set: (v) => {
- if (v?.push !== Array.prototype.push) {
- logger.info(`Patching ${WEBPACK_CHUNK}.push`);
- _initWebpack(v);
- patchPush();
- // @ts-ignore
- delete window[WEBPACK_CHUNK];
- window[WEBPACK_CHUNK] = v;
- }
- webpackChunk = v;
- },
- configurable: true
-});
-
-function patchPush() {
- function handlePush(chunk) {
- try {
- const modules = chunk[1];
- const { subscriptions, listeners } = Vencord.Webpack;
- const { patches } = Vencord.Plugins;
-
- for (const id in modules) {
- let mod = modules[id];
- // Discords Webpack chunks for some ungodly reason contain random
- // newlines. Cyn recommended this workaround and it seems to work fine,
- // however this could potentially break code, so if anything goes weird,
- // this is probably why.
- // Additionally, `[actual newline]` is one less char than "\n", so if Discord
- // ever targets newer browsers, the minifier could potentially use this trick and
- // cause issues.
- let code = mod.toString().replaceAll("\n", "");
- const originalMod = mod;
- const patchedBy = new Set();
-
- modules[id] = function (module, exports, require) {
- try {
- mod(module, exports, require);
- } catch (err) {
- // Just rethrow discord errors
- if (mod === originalMod) throw err;
-
- logger.error("Error in patched chunk", err);
- return originalMod(module, exports, require);
- }
-
- // There are (at the time of writing) 11 modules exporting the window
- // Make these non enumerable to improve webpack search performance
- if (module.exports === window) {
- Object.defineProperty(require.c, id, {
- value: require.c[id],
- enumerable: false,
- configurable: true,
- writable: true
- });
- return;
- }
-
- for (const callback of listeners) {
- try {
- callback(exports);
- } catch (err) {
- logger.error("Error in webpack listener", err);
- }
- }
- for (const [filter, callback] of subscriptions) {
- try {
- if (filter(exports)) {
- subscriptions.delete(filter);
- callback(exports);
- } else if (typeof exports === "object") {
- if (exports.default && filter(exports.default)) {
- subscriptions.delete(filter);
- callback(exports.default);
- }
-
- for (const nested in exports) if (nested.length < 3) {
- if (exports[nested] && filter(exports[nested])) {
- subscriptions.delete(filter);
- callback(exports[nested]);
- }
- }
- }
- } catch (err) {
- logger.error("Error while firing callback for webpack chunk", err);
- }
- }
- };
- modules[id].toString = () => mod.toString();
- modules[id].original = originalMod;
-
- for (let i = 0; i < patches.length; i++) {
- const patch = patches[i];
- if (code.includes(patch.find)) {
- patchedBy.add(patch.plugin);
- // @ts-ignore we change all patch.replacement to array in plugins/index
- for (const replacement of patch.replacement) {
- const lastMod = mod;
- const lastCode = code;
- try {
- const newCode = code.replace(replacement.match, replacement.replace);
- if (newCode === code) {
- logger.warn(`Patch by ${patch.plugin} had no effect: ${replacement.match}`);
- logger.debug("Function Source:\n", code);
- } else {
- code = newCode;
- mod = (0, eval)(`// Webpack Module ${id} - Patched by ${[...patchedBy].join(", ")}\n${newCode}\n//# sourceURL=WebpackModule${id}`);
- }
- } catch (err) {
- // TODO - More meaningful errors. This probably means moving away from string.replace
- // in favour of manual matching. Then cut out the context and log some sort of
- // diff
- logger.error("Failed to apply patch of", patch.plugin, err);
- logger.debug("Original Source\n", lastCode);
- logger.debug("Patched Source\n", code);
- code = lastCode;
- mod = lastMod;
- patchedBy.delete(patch.plugin);
- }
- }
- patches.splice(i--, 1);
- }
- }
- }
- } catch (err) {
- logger.error("oopsie", err);
- }
-
- return handlePush.original.call(window[WEBPACK_CHUNK], chunk);
- }
-
- handlePush.original = window[WEBPACK_CHUNK].push;
- Object.defineProperty(window[WEBPACK_CHUNK], "push", {
- get: () => handlePush,
- set: (v) => (handlePush.original = v),
- configurable: true
- });
-}
-
diff --git a/src/utils/types.ts b/src/utils/types.ts
index 05441e8..a6b79c5 100644
--- a/src/utils/types.ts
+++ b/src/utils/types.ts
@@ -14,6 +14,11 @@ export interface Patch {
replacement: PatchReplacement | PatchReplacement[];
}
+export interface PluginAuthor {
+ name: string;
+ id: BigInt;
+}
+
export interface Plugin extends PluginDef {
patches?: Patch[];
started: boolean;
@@ -22,7 +27,7 @@ export interface Plugin extends PluginDef {
interface PluginDef {
name: string;
description: string;
- author: string;
+ authors: PluginAuthor[];
start?(): void;
stop?(): void;
patches?: Omit<Patch, "plugin">[];