Revert "Fix Dev Companion"

This reverts commit cb6a8502fd.
This commit is contained in:
thororen1234 2025-02-11 13:53:43 -05:00
parent 936ac1ebd8
commit f5cad98f78
No known key found for this signature in database
8 changed files with 37 additions and 55 deletions

View file

@ -9,20 +9,27 @@ import { Settings } from "@api/Settings";
import { canonicalizeMatch } from "@utils/patches"; import { canonicalizeMatch } from "@utils/patches";
import { CodeFilter, stringMatches, wreq } from "@webpack"; import { CodeFilter, stringMatches, wreq } from "@webpack";
import { Toasts } from "@webpack/common"; import { Toasts } from "@webpack/common";
import { getFactoryPatchedSource, getOriginalFactory } from "@webpack/utils"; import { AnyWebpackRequire } from "webpack";
import { settings as companionSettings } from "."; import { settings as companionSettings } from ".";
import { Recieve } from "./types"; 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 * extracts the patched module, if there is no patched module, throws an error
* @param id module id * @param id module id
*/ */
export function extractOrThrow(id: number): string { export function extractOrThrow(id: number): string {
const module = getFactoryPatchedSource(id); const module = wreq.m[id];
if (!module) const patchedSource = getFactoryPatchedSource(id);
if (!patchedSource)
throw new Error("No patched module found for module id " + id); 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 * @param patched return the patched module
*/ */
export function extractModule(id: number, patched = companionSettings.store.usePatchedModule): string { 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 patchedSource = getFactoryPatchedSource(id);
const original = getOriginalFactory(id); return patched ? patchedSource ?? original : original;
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 { export function parseNode(node: Recieve.FindNode): any {

View file

@ -17,6 +17,5 @@
*/ */
export * as Common from "./common"; export * as Common from "./common";
export * from "./utils/getFactories";
export * from "./webpack"; export * from "./webpack";
export * from "./wreq.d"; export * from "./wreq.d";

View file

@ -15,8 +15,10 @@ import { reporterData } from "debug/reporterData";
import { traceFunctionWithResults } from "../debug/Tracer"; import { traceFunctionWithResults } from "../debug/Tracer";
import { patches } from "../plugins"; import { patches } from "../plugins";
import { _initWebpack, _shouldIgnoreModule, AnyModuleFactory, AnyWebpackRequire, factoryListeners, findModuleId, MaybeWrappedModuleFactory, ModuleExports, moduleListeners, waitForSubscriptions, WebpackRequire, WrappedModuleFactory, wreq } from "."; 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 */ /** A set with all the Webpack instances */
export const allWebpackInstances = new Set<AnyWebpackRequire>(); export const allWebpackInstances = new Set<AnyWebpackRequire>();
export const patchTimings = [] as Array<[plugin: string, moduleId: PropertyKey, match: string | RegExp, totalTime: number]>; 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 // 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 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. // 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.

View file

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

View file

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

View file

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

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: GPL-3.0-or-later * 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; export type ModuleExports = any;

View file

@ -42,12 +42,12 @@
"@webpack/common": [ "@webpack/common": [
"./webpack/common" "./webpack/common"
], ],
"@webpack/utils": [
"./webpack/utils"
],
"@webpack": [ "@webpack": [
"./webpack/webpack" "./webpack/webpack"
], ],
"@webpack/patch": [
"./webpack/patchWebpack"
],
"@plugins": [ "@plugins": [
"./plugins" "./plugins"
], ],