support regex finds and bit more work on reporter gui

This commit is contained in:
sadan 2024-08-20 16:03:02 -04:00
parent be739d8774
commit 4a5d480acc
No known key found for this signature in database
7 changed files with 74 additions and 15 deletions

View file

@ -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,

View file

@ -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();

View file

@ -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<string, string[]>;
foundNoModule: Patch[];
hadNoEffect: EvaledPatch[];
undoingPatchGroup: EvaledPatch[];
erroredPatch: EvaledPatch[];
};
failedWebpack: Record<Webpack.TypeWebpackSearchHistory, string[][]>;
}
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();

1
src/globals.d.ts vendored
View file

@ -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;

View file

@ -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;
}
});

View file

@ -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))];
}

View file

@ -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<string, (module: any, exports: any, re
if (IS_DEV) {
logger.debug("Function Source:\n", code);
}
if (IS_COMPANION_TEST)
reporterData.failedPatches.hadNoEffect.push({
...patch,
id
});
}
if (patch.group) {
@ -294,6 +300,11 @@ function patchFactories(factories: Record<string, (module: any, exports: any, re
mod = previousMod;
code = previousCode;
patchedBy.delete(patch.plugin);
if (IS_COMPANION_TEST)
reporterData.failedPatches.undoingPatchGroup.push({
...patch,
id
});
break;
}
@ -304,7 +315,11 @@ function patchFactories(factories: Record<string, (module: any, exports: any, re
mod = (0, eval)(`// Webpack Module ${id} - Patched by ${[...patchedBy].join(", ")}\n${newCode}\n//# sourceURL=WebpackModule${id}`);
} catch (err) {
logger.error(`Patch by ${patch.plugin} errored (Module id is ${id}): ${replacement.match}\n`, err);
if (IS_COMPANION_TEST)
reporterData.failedPatches.erroredPatch.push({
...patch,
id
});
if (IS_DEV) {
const changeSize = code.length - lastCode.length;
const match = lastCode.match(replacement.match)!;
@ -342,6 +357,10 @@ function patchFactories(factories: Record<string, (module: any, exports: any, re
if (patch.group) {
logger.warn(`Undoing patch group ${patch.find} by ${patch.plugin} because replacement ${replacement.match} errored`);
reporterData.failedPatches.undoingPatchGroup.push({
...patch,
id
});
mod = previousMod;
code = previousCode;
break;