From 0250243db744a57ca2681db2548f416941ce6712 Mon Sep 17 00:00:00 2001 From: sadan <117494111+sadan4@users.noreply.github.com> Date: Fri, 15 Nov 2024 02:23:56 -0500 Subject: [PATCH] more horror code --- .gitignore | 3 + src/api/Notices.ts | 2 +- src/plugins/devCompanion.dev/initWs.tsx | 165 +++++++++++++++++++- src/plugins/devCompanion.dev/types/index.ts | 5 + src/plugins/devCompanion.dev/util.tsx | 4 - 5 files changed, 166 insertions(+), 13 deletions(-) create mode 100644 src/plugins/devCompanion.dev/types/index.ts diff --git a/.gitignore b/.gitignore index 135673a6d..d01825b1b 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,6 @@ src/userplugins ExtensionCache/ settings/ + +# vencord companion module cache +.modules diff --git a/src/api/Notices.ts b/src/api/Notices.ts index 6d20087a7..5bd2f9b20 100644 --- a/src/api/Notices.ts +++ b/src/api/Notices.ts @@ -18,7 +18,7 @@ import { waitFor } from "@webpack"; -let NoticesModule: any; +export let NoticesModule: any; waitFor(m => m.show && m.dismiss && !m.suppressAll, m => NoticesModule = m); export const noticesQueue = [] as any[]; diff --git a/src/plugins/devCompanion.dev/initWs.tsx b/src/plugins/devCompanion.dev/initWs.tsx index 1cb67fa75..d846e9c5d 100644 --- a/src/plugins/devCompanion.dev/initWs.tsx +++ b/src/plugins/devCompanion.dev/initWs.tsx @@ -4,14 +4,17 @@ * SPDX-License-Identifier: GPL-3.0-or-later */ +import { popNotice, showNotice } from "@api/Notices"; +import ErrorBoundary from "@components/ErrorBoundary"; import { canonicalizeMatch, canonicalizeReplace } from "@utils/patches"; import { filters, findAll, search, wreq } from "@webpack"; -import { Toasts } from "@webpack/common"; +import { React, Toasts, useState } from "@webpack/common"; +import { loadLazyChunks } from "debug/loadLazyChunks"; import { reporterData } from "debug/reporterData"; import { Settings } from "Vencord"; import { logger, PORT, settings } from "."; -import { extractModule, extractOrThrow, FindData, findModuleId, FindType, mkRegexFind, parseNode, PatchData, SendData, toggleEnabled, } from "./util"; +import { extractModule, extractOrThrow, FindData, findModuleId, mkRegexFind, parseNode, PatchData, SendData, toggleEnabled, } from "./util"; export function stopWs() { socket?.close(1000, "Plugin Stopped"); @@ -155,14 +158,14 @@ export function initWs(isManual = false) { } case "search": { let moduleId; - if (data.findType === FindType.STRING) + if (data.findType === "string") moduleId = +findModuleId([idOrSearch.toString()]); else moduleId = +findModuleId(mkRegexFind(idOrSearch)); const p = extractOrThrow(moduleId); const p2 = extractModule(moduleId, false); - console.log(p, p2, "done"); + replyData({ type: "diff", ok: true, @@ -205,7 +208,7 @@ export function initWs(isManual = false) { } case "search": { let moduleId; - if (data.findType === FindType.STRING) + if (data.findType === "string") moduleId = +findModuleId([idOrSearch.toString()]); else @@ -280,9 +283,8 @@ export function initWs(isManual = false) { } case "testPatch": { const { find, replacement } = data as PatchData; - let candidates; - if (data.findType === FindType.STRING) + if (data.findType === "string") candidates = search(find.toString()); else @@ -319,7 +321,6 @@ export function initWs(isManual = false) { return reply(`Replacement ${i} failed: ${err}`); } } - reply(); break; } @@ -368,6 +369,138 @@ export function initWs(isManual = false) { reply(); break; } + case "allModules": { + const { promise, resolve, reject } = Promise.withResolvers(); + // wrap in try/catch to prevent crashing if notice api is not loaded + try { + let closed = false; + const close = () => { + if (closed) return; + closed = true; + popNotice(); + }; + // @ts-expect-error it accepts react components + showNotice(, "OK", () => { + closed = true; + popNotice(); + }); + } catch (e) { + console.error(e); + } + loadLazyChunks() + .then(() => { + resolve(); + replyData({ + type: "allModules", + data: Object.keys(wreq.m), + ok: true + }); + }) + .catch(e => { + console.error(e); + replyData({ + type: "allModules", + ok: false, + data: e.toString() + }); + reject(e); + }); + break; + } + // FIXME: this is just extract but with a different name + case "rawContent": { + try { + const { extractType, idOrSearch } = data; + switch (extractType) { + case "id": { + if (typeof idOrSearch !== "number") + throw new Error("Id is not a number, got :" + typeof idOrSearch); + + else + replyData({ + type: "rawContent", + ok: true, + data: extractModule(idOrSearch), + moduleNumber: idOrSearch + }); + + break; + } + case "search": { + let moduleId; + if (data.findType === "string") + moduleId = +findModuleId([idOrSearch.toString()]); + + else + moduleId = +findModuleId(mkRegexFind(idOrSearch)); + replyData({ + type: "rawContent", + ok: true, + data: extractModule(moduleId), + moduleNumber: moduleId + }); + break; + } + case "find": { + const { findType, findArgs } = data; + try { + var parsedArgs = findArgs.map(parseNode); + } catch (err) { + return reply("Failed to parse args: " + err); + } + + try { + let results: any[]; + switch (findType.replace("find", "").replace("Lazy", "")) { + case "": + case "Component": + results = findAll(parsedArgs[0]); + break; + case "ByProps": + results = findAll(filters.byProps(...parsedArgs)); + break; + case "Store": + results = findAll(filters.byStoreName(parsedArgs[0])); + break; + case "ByCode": + results = findAll(filters.byCode(...parsedArgs)); + break; + case "ModuleId": + results = Object.keys(search(parsedArgs[0])); + break; + case "ComponentByCode": + results = findAll(filters.componentByCode(...parsedArgs)); + break; + default: + return reply("Unknown Find Type " + findType); + } + + const uniqueResultsCount = new Set(results).size; + if (uniqueResultsCount === 0) throw "No results"; + if (uniqueResultsCount > 1) throw "Found more than one result! Make this filter more specific"; + // best name ever + const foundFind: string = [...results][0].toString(); + replyData({ + type: "rawContent", + ok: true, + find: true, + data: foundFind, + moduleNumber: +findModuleId([foundFind]) + }); + } catch (err) { + return reply("Failed to find: " + err); + } + break; + } + default: + reply(`Unknown Extract type. Got: ${extractType}`); + break; + } + } catch (error) { + reply(String(error)); + } + break; + } default: reply("Unknown Type " + type); break; @@ -375,3 +508,19 @@ export function initWs(isManual = false) { }); } +interface AllModulesNotiProps { + done: Promise; + close: () => void; +} + +const AllModulesNoti = ErrorBoundary.wrap(function ({ done, close }: AllModulesNotiProps) { + const [state, setState] = useState<0 | 1 | -1>(0); + done.then(setState.bind(null, 1)).catch(setState.bind(null, -1)); + console.log("test"); + if (state === 1) setTimeout(close, 5000); + return (<> + {state === 0 && "Loading lazy modules, restarting could lead to errors"} + {state === 1 && "Loaded all lazy modules"} + {state === -1 && "Failed to load lazy modules, check console for errors"} + ); +}, { noop: true }); diff --git a/src/plugins/devCompanion.dev/types/index.ts b/src/plugins/devCompanion.dev/types/index.ts new file mode 100644 index 000000000..3beb3e42c --- /dev/null +++ b/src/plugins/devCompanion.dev/types/index.ts @@ -0,0 +1,5 @@ +/* + * Vencord, a Discord client mod + * Copyright (c) 2024 Vendicated and contributors + * SPDX-License-Identifier: GPL-3.0-or-later + */ diff --git a/src/plugins/devCompanion.dev/util.tsx b/src/plugins/devCompanion.dev/util.tsx index f18496aba..9765ae908 100644 --- a/src/plugins/devCompanion.dev/util.tsx +++ b/src/plugins/devCompanion.dev/util.tsx @@ -26,10 +26,6 @@ export interface RegexNode { flags: string; }; } -export enum FindType { - STRING, - REGEX -} export interface FunctionNode { type: "function"; value: string;