mirror of
https://github.com/Equicord/Equicord.git
synced 2025-02-24 00:59:09 -05:00
parent
936ac1ebd8
commit
f5cad98f78
8 changed files with 37 additions and 55 deletions
|
@ -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 {
|
||||
|
|
|
@ -17,6 +17,5 @@
|
|||
*/
|
||||
|
||||
export * as Common from "./common";
|
||||
export * from "./utils/getFactories";
|
||||
export * from "./webpack";
|
||||
export * from "./wreq.d";
|
||||
|
|
|
@ -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<AnyWebpackRequire>();
|
||||
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.
|
||||
|
|
|
@ -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];
|
||||
}
|
|
@ -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";
|
|
@ -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");
|
2
src/webpack/wreq.d.ts
vendored
2
src/webpack/wreq.d.ts
vendored
|
@ -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;
|
||||
|
||||
|
|
|
@ -42,12 +42,12 @@
|
|||
"@webpack/common": [
|
||||
"./webpack/common"
|
||||
],
|
||||
"@webpack/utils": [
|
||||
"./webpack/utils"
|
||||
],
|
||||
"@webpack": [
|
||||
"./webpack/webpack"
|
||||
],
|
||||
"@webpack/patch": [
|
||||
"./webpack/patchWebpack"
|
||||
],
|
||||
"@plugins": [
|
||||
"./plugins"
|
||||
],
|
||||
|
|
Loading…
Add table
Reference in a new issue