From 4a5d480acc0a971a94daf5802cd57aebe7d3b65b Mon Sep 17 00:00:00 2001 From: sadan <117494111+sadan4@users.noreply.github.com> Date: Tue, 20 Aug 2024 16:03:02 -0400 Subject: [PATCH] support regex finds and bit more work on reporter gui --- scripts/build/build.mjs | 3 ++- scripts/build/common.mjs | 1 + src/debug/runReporter.ts | 30 ++++++++++++++++++-------- src/globals.d.ts | 1 + src/plugins/devCompanion.dev/index.tsx | 23 ++++++++++++++++---- src/plugins/devCompanion.dev/util.tsx | 10 +++++++++ src/webpack/patchWebpack.ts | 21 +++++++++++++++++- 7 files changed, 74 insertions(+), 15 deletions(-) diff --git a/scripts/build/build.mjs b/scripts/build/build.mjs index 623f9f940..5799ce168 100755 --- a/scripts/build/build.mjs +++ b/scripts/build/build.mjs @@ -21,12 +21,13 @@ import esbuild from "esbuild"; import { readdir } from "fs/promises"; import { join } from "path"; -import { BUILD_TIMESTAMP, commonOpts, exists, globPlugins, IS_DEV, IS_REPORTER, IS_STANDALONE, IS_UPDATER_DISABLED, resolvePluginName, VERSION, commonRendererPlugins, watch } from "./common.mjs"; +import { BUILD_TIMESTAMP, commonOpts, exists, globPlugins, IS_DEV, IS_REPORTER, IS_STANDALONE, IS_UPDATER_DISABLED, resolvePluginName, VERSION, commonRendererPlugins, watch, IS_COMPANION_TEST } from "./common.mjs"; const defines = { IS_STANDALONE, IS_DEV, IS_REPORTER, + IS_COMPANION_TEST, IS_UPDATER_DISABLED, IS_WEB: false, IS_EXTENSION: false, diff --git a/scripts/build/common.mjs b/scripts/build/common.mjs index 247557e3c..d6ecb0c8a 100644 --- a/scripts/build/common.mjs +++ b/scripts/build/common.mjs @@ -41,6 +41,7 @@ export const watch = process.argv.includes("--watch"); export const IS_DEV = watch || process.argv.includes("--dev"); export const IS_REPORTER = process.argv.includes("--reporter"); export const IS_STANDALONE = process.argv.includes("--standalone"); +export const IS_COMPANION_TEST = process.argv.includes("--companion-test"); export const IS_UPDATER_DISABLED = process.argv.includes("--disable-updater"); export const gitHash = process.env.VENCORD_HASH || execSync("git rev-parse --short HEAD", { encoding: "utf-8" }).trim(); diff --git a/src/debug/runReporter.ts b/src/debug/runReporter.ts index 2cce25bd1..5c135bf50 100644 --- a/src/debug/runReporter.ts +++ b/src/debug/runReporter.ts @@ -5,24 +5,31 @@ */ import { Logger } from "@utils/Logger"; +import { Patch } from "@utils/types"; import * as Webpack from "@webpack"; import { patches } from "plugins"; import { loadLazyChunks } from "./loadLazyChunks"; const ReporterLogger = new Logger("Reporter"); +interface EvaledPatch extends Patch { + id: number | string; +} interface ReporterData { failedPatches: { - /** - * pluginName > array of failed modules - */ - foundNoModule: Record; + foundNoModule: Patch[]; + hadNoEffect: EvaledPatch[]; + undoingPatchGroup: EvaledPatch[]; + erroredPatch: EvaledPatch[]; }; failedWebpack: Record; } -const reporterData: ReporterData = { +export const reporterData: ReporterData = { failedPatches: { - foundNoModule: {} + foundNoModule: [], + hadNoEffect: [], + undoingPatchGroup: [], + erroredPatch: [] }, failedWebpack: { find: [[]], @@ -54,6 +61,8 @@ async function runReporter() { for (const patch of patches) { if (!patch.all) { new Logger("WebpackInterceptor").warn(`Patch by ${patch.plugin} found no module (Module id is -): ${patch.find}`); + if (IS_COMPANION_TEST) + reporterData.failedPatches.foundNoModule[patch.plugin].push(String(patch.find)); } } @@ -99,7 +108,8 @@ async function runReporter() { logMessage += `("${args[0]}", {\n${failedMappings.map(mapping => `\t${mapping}: ${args[1][mapping].toString().slice(0, 147)}...`).join(",\n")}\n})`; } else logMessage += `(${args.map(arg => `"${arg}"`).join(", ")})`; - reporterData.failedWebpack[method].push(args.map(a => String(a))); + if (IS_COMPANION_TEST) + reporterData.failedWebpack[method].push(args.map(a => String(a))); ReporterLogger.log("Webpack Find Fail:", logMessage); } } @@ -108,7 +118,9 @@ async function runReporter() { } catch (e) { ReporterLogger.log("A fatal error occurred:", e); } + console.log(reporterData); } -runReporter(); -console.log(reporterData); +// imported in webpack for reporterData, wrap to avoid running reporter +if (IS_REPORTER) + runReporter(); diff --git a/src/globals.d.ts b/src/globals.d.ts index e20ca4b71..547bcaaeb 100644 --- a/src/globals.d.ts +++ b/src/globals.d.ts @@ -38,6 +38,7 @@ declare global { export var IS_UPDATER_DISABLED: boolean; export var IS_DEV: boolean; export var IS_REPORTER: boolean; + export var IS_COMPANION_TEST: boolean; export var IS_DISCORD_DESKTOP: boolean; export var IS_VESKTOP: boolean; export var VERSION: string; diff --git a/src/plugins/devCompanion.dev/index.tsx b/src/plugins/devCompanion.dev/index.tsx index 4a479ce54..961c8dbf8 100644 --- a/src/plugins/devCompanion.dev/index.tsx +++ b/src/plugins/devCompanion.dev/index.tsx @@ -24,7 +24,7 @@ import { canonicalizeMatch, canonicalizeReplace } from "@utils/patches"; import definePlugin, { OptionType, ReporterTestable } from "@utils/types"; import { filters, findAll, search, wreq } from "@webpack"; -import { extractModule, extractOrThrow, FindData, findModuleId, parseNode, PatchData, SendData } from "./util"; +import { extractModule, extractOrThrow, FindData, findModuleId, FindType, mkRegexFind, parseNode, PatchData, SendData } from "./util"; const PORT = 8485; const NAV_ID = "dev-companion-reconnect"; @@ -149,7 +149,11 @@ function initWs(isManual = false) { break; } case "search": { - const moduleId = +findModuleId([idOrSearch.toString()]); + let moduleId; + if (data.findType === 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"); @@ -188,7 +192,11 @@ function initWs(isManual = false) { break; } case "search": { - const moduleId = +findModuleId([idOrSearch.toString()]); + let moduleId; + if (data.findType === FindType.STRING) + moduleId = +findModuleId([idOrSearch.toString()]); + else + moduleId = +findModuleId(mkRegexFind(idOrSearch)); replyData({ type: "extract", ok: true, @@ -259,7 +267,13 @@ function initWs(isManual = false) { case "testPatch": { const { find, replacement } = data as PatchData; - const candidates = search(find); + let candidates; + if (data.findType === FindType.STRING) + candidates = search(find.toString()); + else + candidates = search(...mkRegexFind(find)); + + // const candidates = search(find); const keys = Object.keys(candidates); if (keys.length !== 1) return reply("Expected exactly one 'find' matches, found " + keys.length); @@ -368,3 +382,4 @@ export default definePlugin({ socket = void 0; } }); + diff --git a/src/plugins/devCompanion.dev/util.tsx b/src/plugins/devCompanion.dev/util.tsx index a681c6eb2..a623f131e 100644 --- a/src/plugins/devCompanion.dev/util.tsx +++ b/src/plugins/devCompanion.dev/util.tsx @@ -4,6 +4,7 @@ * SPDX-License-Identifier: GPL-3.0-or-later */ +import { canonicalizeMatch } from "@utils/patches"; import { CodeFilter, stringMatches, wreq } from "@webpack"; import { settings } from "."; @@ -21,6 +22,10 @@ export interface RegexNode { flags: string; }; } +export enum FindType { + STRING, + REGEX +} export interface FunctionNode { type: "function"; value: string; @@ -93,4 +98,9 @@ export function findModuleId(find: CodeFilter) { } return matches[0]; } +export function mkRegexFind(idOrSearch: string): RegExp[] { + const regex = idOrSearch.substring(1, idOrSearch.lastIndexOf("/")); + const flags = idOrSearch.substring(idOrSearch.lastIndexOf("/") + 1); + return [canonicalizeMatch(RegExp(regex, flags))]; +} diff --git a/src/webpack/patchWebpack.ts b/src/webpack/patchWebpack.ts index fb640cea8..8793683fa 100644 --- a/src/webpack/patchWebpack.ts +++ b/src/webpack/patchWebpack.ts @@ -20,6 +20,7 @@ import { WEBPACK_CHUNK } from "@utils/constants"; import { Logger } from "@utils/Logger"; import { canonicalizeReplacement } from "@utils/patches"; import { PatchReplacement } from "@utils/types"; +import { reporterData } from "debug/runReporter"; import { WebpackInstance } from "discord-types/other"; import { traceFunction } from "../debug/Tracer"; @@ -287,6 +288,11 @@ function patchFactories(factories: Record