Vencord/src/debug/runReporter.ts

130 lines
4.8 KiB
TypeScript
Raw Normal View History

2024-05-31 23:28:58 -03:00
/*
* Vencord, a Discord client mod
* Copyright (c) 2024 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { SYM_LAZY_COMPONENT_INNER } from "@utils/lazyReact";
2024-05-31 23:28:58 -03:00
import { Logger } from "@utils/Logger";
import { SYM_PROXY_INNER_GET, SYM_PROXY_INNER_VALUE } from "@utils/proxyInner";
2024-05-31 23:28:58 -03:00
import * as Webpack from "@webpack";
import { patches } from "plugins";
2024-06-01 17:51:50 -03:00
import { loadLazyChunks } from "./loadLazyChunks";
2024-05-31 23:28:58 -03:00
const ReporterLogger = new Logger("Reporter");
async function runReporter() {
try {
2024-06-01 17:51:50 -03:00
ReporterLogger.log("Starting test...");
2024-05-31 23:28:58 -03:00
2024-06-01 17:51:50 -03:00
let loadLazyChunksResolve: (value: void | PromiseLike<void>) => void;
const loadLazyChunksDone = new Promise<void>(r => loadLazyChunksResolve = r);
2024-05-31 23:28:58 -03:00
2024-06-01 18:10:45 -03:00
Webpack.beforeInitListeners.add(() => loadLazyChunks().then((loadLazyChunksResolve)));
2024-06-01 17:51:50 -03:00
await loadLazyChunksDone;
2024-05-31 23:28:58 -03:00
for (const patch of patches) {
if (!patch.all) {
new Logger("WebpackInterceptor").warn(`Patch by ${patch.plugin} found no module (Module id is -): ${patch.find}`);
}
}
await Promise.all(Webpack.webpackSearchHistory.map(async ([searchType, args]) => {
args = [...args];
2024-05-31 23:28:58 -03:00
try {
let result = null as any;
switch (searchType) {
case "webpackDependantLazy":
case "webpackDependantLazyComponent": {
const [factory] = args;
result = factory();
break;
}
case "extractAndLoadChunks": {
const [code, matcher] = args;
2024-05-31 23:28:58 -03:00
result = await Webpack.extractAndLoadChunks(code, matcher);
if (result === false) {
result = null;
}
2024-05-31 23:28:58 -03:00
break;
}
default: {
const findResult = args.shift();
2024-05-31 23:28:58 -03:00
if (findResult != null) {
if (findResult.$$vencordCallbackCalled != null && findResult.$$vencordCallbackCalled()) {
result = findResult;
}
2024-05-31 23:28:58 -03:00
if (findResult[SYM_PROXY_INNER_GET] != null) {
result = findResult[SYM_PROXY_INNER_VALUE];
}
if (findResult[SYM_LAZY_COMPONENT_INNER] != null) {
result = findResult[SYM_LAZY_COMPONENT_INNER]();
}
}
break;
}
2024-05-31 23:28:58 -03:00
}
if (result == null) {
throw "a rock at ben shapiro";
}
2024-05-31 23:28:58 -03:00
} catch (e) {
let logMessage = searchType;
let filterName = "";
let parsedArgs = args;
if (args[0].$$vencordProps != null) {
if (["find", "findComponent", "waitFor"].includes(searchType)) {
filterName = args[0].$$vencordProps[0];
}
parsedArgs = args[0].$$vencordProps.slice(1);
}
// if parsedArgs is the same as args, it means vencordProps of the filter was not available (like in normal filter functions),
// so log the filter function instead
if (
parsedArgs === args &&
["waitFor", "find", "findComponent", "webpackDependantLazy", "webpackDependantLazyComponent"].includes(searchType)
) {
let filter = parsedArgs[0].toString();
if (filter.length > 150) {
filter = filter.slice(0, 147) + "...";
}
logMessage += `(${filter})`;
} else if (searchType === "extractAndLoadChunks") {
let regexStr: string;
if (parsedArgs[1] === Webpack.DefaultExtractAndLoadChunksRegex) {
regexStr = "DefaultExtractAndLoadChunksRegex";
} else {
regexStr = parsedArgs[1].toString();
}
logMessage += `([${parsedArgs[0].map((arg: any) => `"${arg}"`).join(", ")}], ${regexStr})`;
} else {
logMessage += `(${filterName.length ? `${filterName}(` : ""}${parsedArgs.map(arg => `"${arg}"`).join(", ")})${filterName.length ? ")" : ""}`;
}
2024-05-31 23:28:58 -03:00
ReporterLogger.log("Webpack Find Fail:", logMessage);
}
}));
2024-05-31 23:28:58 -03:00
ReporterLogger.log("Finished test");
} catch (e) {
ReporterLogger.log("A fatal error occurred:", e);
}
}
runReporter();