From 488f133a90500947f26b6311b0c4e2fbc8e2d398 Mon Sep 17 00:00:00 2001 From: Nuckyz <61953774+Nuckyz@users.noreply.github.com> Date: Fri, 24 May 2024 03:44:40 -0300 Subject: [PATCH 1/2] Handle if for some reason modules are set again --- src/webpack/patchWebpack.ts | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/webpack/patchWebpack.ts b/src/webpack/patchWebpack.ts index af27e2294..e0f8ce12c 100644 --- a/src/webpack/patchWebpack.ts +++ b/src/webpack/patchWebpack.ts @@ -21,7 +21,7 @@ const allModuleFactories = new Set(); function defineModuleFactoryGetter(modulesFactories: WebpackRequire["m"], id: PropertyKey, factory: ModuleFactory) { Object.defineProperty(modulesFactories, id, { - get: () => { + get() { // $$vencordOriginal means the factory is already patched // @ts-ignore if (factory.$$vencordOriginal != null) { @@ -31,6 +31,15 @@ function defineModuleFactoryGetter(modulesFactories: WebpackRequire["m"], id: Pr // This patches factories if eagerPatches are disabled return (factory = patchFactory(id, factory)); }, + set(v: ModuleFactory) { + // @ts-ignore + if (factory.$$vencordOriginal != null) { + // @ts-ignore + factory.$$vencordOriginal = v; + } else { + factory = v; + } + }, configurable: true, enumerable: true }); @@ -43,16 +52,24 @@ const moduleFactoriesHandler: ProxyHandler = { return Reflect.set(target, p, newValue, receiver); } + const existingFactory = Reflect.get(target, p, target); + if (!Settings.eagerPatches) { + // If existingFactory exists, its either wrapped in defineModuleFactoryGetter, or it has already been required + // so call Reflect.set with the new original and let the correct logic apply (normal set, or defineModuleFactoryGetter setter) + if (existingFactory != null) { + return Reflect.set(target, p, newValue, receiver); + } + defineModuleFactoryGetter(target, p, newValue); return true; } - const existingFactory = Reflect.get(target, p, target); - // Check if this factory is already patched // @ts-ignore - if (existingFactory?.$$vencordOriginal === newValue) { + if (existingFactory.$$vencordOriginal != null) { + // @ts-ignore + existingFactory.$$vencordOriginal = newValue; return true; } @@ -318,7 +335,8 @@ function patchFactory(id: PropertyKey, factory: ModuleFactory) { const patchedFactory: ModuleFactory = (module, exports, require) => { for (const moduleFactories of allModuleFactories) { Object.defineProperty(moduleFactories, id, { - value: originalFactory, + // @ts-ignore + value: patchFactory.$$vencordOriginal, configurable: true, enumerable: true, writable: true From 86f69e84c17848ff16b071bf2e3b1125df066f62 Mon Sep 17 00:00:00 2001 From: Nuckyz <61953774+Nuckyz@users.noreply.github.com> Date: Fri, 24 May 2024 03:45:46 -0300 Subject: [PATCH 2/2] I'm stupid --- src/webpack/patchWebpack.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/webpack/patchWebpack.ts b/src/webpack/patchWebpack.ts index e0f8ce12c..9b1fcdb83 100644 --- a/src/webpack/patchWebpack.ts +++ b/src/webpack/patchWebpack.ts @@ -67,7 +67,7 @@ const moduleFactoriesHandler: ProxyHandler = { // Check if this factory is already patched // @ts-ignore - if (existingFactory.$$vencordOriginal != null) { + if (existingFactory?.$$vencordOriginal != null) { // @ts-ignore existingFactory.$$vencordOriginal = newValue; return true;