diff --git a/src/plugins/devCompanion.dev/util.tsx b/src/plugins/devCompanion.dev/util.tsx index 15468742..841d49bc 100644 --- a/src/plugins/devCompanion.dev/util.tsx +++ b/src/plugins/devCompanion.dev/util.tsx @@ -9,20 +9,27 @@ import { Settings } from "@api/Settings"; import { canonicalizeMatch } from "@utils/patches"; import { CodeFilter, stringMatches, wreq } from "@webpack"; import { Toasts } from "@webpack/common"; -import { getFactoryPatchedSource, getOriginalFactory } from "@webpack/utils"; +import { AnyWebpackRequire } from "webpack"; import { settings as companionSettings } from "."; import { Recieve } from "./types"; +const SYM_PATCHED_SOURCE = Symbol("WebpackPatcher.patchedSource"); + +function getFactoryPatchedSource(id: PropertyKey, webpackRequire = wreq as AnyWebpackRequire) { + return webpackRequire.m[id]?.[SYM_PATCHED_SOURCE]; +} + /** * extracts the patched module, if there is no patched module, throws an error * @param id module id */ export function extractOrThrow(id: number): string { - const module = getFactoryPatchedSource(id); - if (!module) + const module = wreq.m[id]; + const patchedSource = getFactoryPatchedSource(id); + if (!patchedSource) throw new Error("No patched module found for module id " + id); - return module; + return patchedSource; } /** @@ -34,11 +41,12 @@ export function extractOrThrow(id: number): string { * @param patched return the patched module */ export function extractModule(id: number, patched = companionSettings.store.usePatchedModule): string { + const module = wreq.m[id]; + if (!module) + throw new Error("No module found for module id:" + id); + const original = module.toString(); const patchedSource = getFactoryPatchedSource(id); - const original = getOriginalFactory(id); - if (patched && !patchedSource || !original) - throw new Error("No module found for module id: " + id); - return patched ? patchedSource ?? original.toString() : original.toString(); + return patched ? patchedSource ?? original : original; } export function parseNode(node: Recieve.FindNode): any { diff --git a/src/webpack/index.ts b/src/webpack/index.ts index 93032f1e..6f1fd25b 100644 --- a/src/webpack/index.ts +++ b/src/webpack/index.ts @@ -17,6 +17,5 @@ */ export * as Common from "./common"; -export * from "./utils/getFactories"; export * from "./webpack"; export * from "./wreq.d"; diff --git a/src/webpack/patchWebpack.ts b/src/webpack/patchWebpack.ts index c039aaad..7d6b1676 100644 --- a/src/webpack/patchWebpack.ts +++ b/src/webpack/patchWebpack.ts @@ -15,8 +15,10 @@ import { reporterData } from "debug/reporterData"; import { traceFunctionWithResults } from "../debug/Tracer"; import { patches } from "../plugins"; import { _initWebpack, _shouldIgnoreModule, AnyModuleFactory, AnyWebpackRequire, factoryListeners, findModuleId, MaybeWrappedModuleFactory, ModuleExports, moduleListeners, waitForSubscriptions, WebpackRequire, WrappedModuleFactory, wreq } from "."; -import { SYM_ORIGINAL_FACTORY, SYM_PATCHED_BY, SYM_PATCHED_SOURCE } from "./utils/symbols"; +export const SYM_ORIGINAL_FACTORY = Symbol("WebpackPatcher.originalFactory"); +export const SYM_PATCHED_SOURCE = Symbol("WebpackPatcher.patchedSource"); +export const SYM_PATCHED_BY = Symbol("WebpackPatcher.patchedBy"); /** A set with all the Webpack instances */ export const allWebpackInstances = new Set(); export const patchTimings = [] as Array<[plugin: string, moduleId: PropertyKey, match: string | RegExp, totalTime: number]>; @@ -76,6 +78,19 @@ const define: Define = (target, p, attributes) => { }); }; +export function getOriginalFactory(id: PropertyKey, webpackRequire = wreq as AnyWebpackRequire) { + const moduleFactory = webpackRequire.m[id]; + return (moduleFactory?.[SYM_ORIGINAL_FACTORY] ?? moduleFactory) as AnyModuleFactory | undefined; +} + +export function getFactoryPatchedSource(id: PropertyKey, webpackRequire = wreq as AnyWebpackRequire) { + return webpackRequire.m[id]?.[SYM_PATCHED_SOURCE]; +} + +export function getFactoryPatchedBy(id: PropertyKey, webpackRequire = wreq as AnyWebpackRequire) { + return webpackRequire.m[id]?.[SYM_PATCHED_BY]; +} + // wreq.m is the Webpack object containing module factories. It is pre-populated with module factories, and is also populated via webpackGlobal.push // We use this setter to intercept when wreq.m is defined and apply the patching in its module factories. // We wrap wreq.m with our proxy, which is responsible for patching the module factories when they are set, or defining getters for the patched versions. diff --git a/src/webpack/utils/getFactories.ts b/src/webpack/utils/getFactories.ts deleted file mode 100644 index 94fe1dc8..00000000 --- a/src/webpack/utils/getFactories.ts +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Vencord, a Discord client mod - * Copyright (c) 2025 Vendicated and contributors - * SPDX-License-Identifier: GPL-3.0-or-later - */ - -import { wreq } from "@webpack"; -import { AnyModuleFactory, AnyWebpackRequire } from "webpack/wreq"; - -import { SYM_ORIGINAL_FACTORY, SYM_PATCHED_BY, SYM_PATCHED_SOURCE } from "./symbols"; - -export function getOriginalFactory(id: PropertyKey, webpackRequire = wreq as AnyWebpackRequire) { - const moduleFactory = webpackRequire.m[id]; - return (moduleFactory?.[SYM_ORIGINAL_FACTORY] ?? moduleFactory) as AnyModuleFactory | undefined; -} - -export function getFactoryPatchedSource(id: PropertyKey, webpackRequire = wreq as AnyWebpackRequire) { - return webpackRequire.m[id]?.[SYM_PATCHED_SOURCE]; -} - -export function getFactoryPatchedBy(id: PropertyKey, webpackRequire = wreq as AnyWebpackRequire) { - return webpackRequire.m[id]?.[SYM_PATCHED_BY]; -} diff --git a/src/webpack/utils/index.ts b/src/webpack/utils/index.ts deleted file mode 100644 index 92844be1..00000000 --- a/src/webpack/utils/index.ts +++ /dev/null @@ -1,8 +0,0 @@ -/* - * Vencord, a Discord client mod - * Copyright (c) 2025 Vendicated and contributors - * SPDX-License-Identifier: GPL-3.0-or-later - */ - -export * from "./getFactories"; -export * from "./symbols"; diff --git a/src/webpack/utils/symbols.ts b/src/webpack/utils/symbols.ts deleted file mode 100644 index 9b496e96..00000000 --- a/src/webpack/utils/symbols.ts +++ /dev/null @@ -1,9 +0,0 @@ -/* - * Vencord, a Discord client mod - * Copyright (c) 2025 Vendicated and contributors - * SPDX-License-Identifier: GPL-3.0-or-later - */ - -export const SYM_ORIGINAL_FACTORY = Symbol.for("WebpackPatcher.originalFactory"); -export const SYM_PATCHED_SOURCE = Symbol.for("WebpackPatcher.patchedSource"); -export const SYM_PATCHED_BY = Symbol.for("WebpackPatcher.patchedBy"); diff --git a/src/webpack/wreq.d.ts b/src/webpack/wreq.d.ts index 9cffd7c8..dbc45105 100644 --- a/src/webpack/wreq.d.ts +++ b/src/webpack/wreq.d.ts @@ -4,7 +4,7 @@ * SPDX-License-Identifier: GPL-3.0-or-later */ -import { SYM_ORIGINAL_FACTORY, SYM_PATCHED_BY, SYM_PATCHED_SOURCE } from "./utils/symbols"; +import { SYM_ORIGINAL_FACTORY, SYM_PATCHED_BY, SYM_PATCHED_SOURCE } from "./patchWebpack"; export type ModuleExports = any; diff --git a/tsconfig.json b/tsconfig.json index 62e6282a..feeaa8b4 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -42,12 +42,12 @@ "@webpack/common": [ "./webpack/common" ], - "@webpack/utils": [ - "./webpack/utils" - ], "@webpack": [ "./webpack/webpack" ], + "@webpack/patch": [ + "./webpack/patchWebpack" + ], "@plugins": [ "./plugins" ], @@ -70,4 +70,4 @@ "scripts/**/*", "eslint.config.mjs" ], -} +} \ No newline at end of file