diff --git a/src/plugins/devCompanion.dev/util.tsx b/src/plugins/devCompanion.dev/util.tsx index 841d49bc..15468742 100644 --- a/src/plugins/devCompanion.dev/util.tsx +++ b/src/plugins/devCompanion.dev/util.tsx @@ -9,27 +9,20 @@ import { Settings } from "@api/Settings"; import { canonicalizeMatch } from "@utils/patches"; import { CodeFilter, stringMatches, wreq } from "@webpack"; import { Toasts } from "@webpack/common"; -import { AnyWebpackRequire } from "webpack"; +import { getFactoryPatchedSource, getOriginalFactory } from "@webpack/utils"; 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 = wreq.m[id]; - const patchedSource = getFactoryPatchedSource(id); - if (!patchedSource) + const module = getFactoryPatchedSource(id); + if (!module) throw new Error("No patched module found for module id " + id); - return patchedSource; + return module; } /** @@ -41,12 +34,11 @@ 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); - return patched ? patchedSource ?? original : original; + 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(); } export function parseNode(node: Recieve.FindNode): any { diff --git a/src/webpack/index.ts b/src/webpack/index.ts index 6f1fd25b..93032f1e 100644 --- a/src/webpack/index.ts +++ b/src/webpack/index.ts @@ -17,5 +17,6 @@ */ 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 b4676d14..33931787 100644 --- a/src/webpack/patchWebpack.ts +++ b/src/webpack/patchWebpack.ts @@ -15,10 +15,8 @@ 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]>; @@ -78,19 +76,6 @@ 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 new file mode 100644 index 00000000..94fe1dc8 --- /dev/null +++ b/src/webpack/utils/getFactories.ts @@ -0,0 +1,23 @@ +/* + * 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 new file mode 100644 index 00000000..92844be1 --- /dev/null +++ b/src/webpack/utils/index.ts @@ -0,0 +1,8 @@ +/* + * 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 new file mode 100644 index 00000000..9b496e96 --- /dev/null +++ b/src/webpack/utils/symbols.ts @@ -0,0 +1,9 @@ +/* + * 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 dbc45105..9cffd7c8 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 "./patchWebpack"; +import { SYM_ORIGINAL_FACTORY, SYM_PATCHED_BY, SYM_PATCHED_SOURCE } from "./utils/symbols"; export type ModuleExports = any; diff --git a/tsconfig.json b/tsconfig.json index feeaa8b4..62e6282a 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 +}