Support \i everywhere
@@ -1,5 +1,6 @@import { WebpackModule, WebpackModuleFunc, WebpackRequireType } from "@moonlight-mod/types";import { Spacepack } from "@moonlight-mod/types/coreExtensions/spacepack";+import { processFind, testFind } from "@moonlight-mod/core/util/patch";const webpackRequire = require as unknown as WebpackRequireType;const cache = webpackRequire.c;@@ -42,12 +43,7 @@ },findByCode: (...args: (string | RegExp)[]) => {return Object.entries(modules)- .filter(- ([id, mod]) =>- !args.some(- (item) => !(item instanceof RegExp ? item.test(mod.toString()) : mod.toString().indexOf(item) !== -1)- )- )+ .filter(([id, mod]) => !args.some((item) => !testFind(mod.toString(), processFind(item)))).map(([id]) => {//if (!(id in cache)) require(id);//return cache[id];@@ -137,10 +133,7 @@ findFunctionByStrings: (exports: Record<string, any>, ...strings: (string | RegExp)[]) => {return (Object.entries(exports).filter(([index, func]) =>- typeof func === "function" &&- !strings.some(- (query) => !(query instanceof RegExp ? func.toString().match(query) : func.toString().includes(query))- )+ typeof func === "function" && !strings.some((query) => !testFind(func.toString(), processFind(query))))?.[0]?.[1] ?? null);},
MODIFIED
packages/core/src/patch.ts
MODIFIED
packages/core/src/patch.ts
@@ -13,6 +13,7 @@ import Logger from "./util/logger";import calculateDependencies, { Dependency } from "./util/dependency";import WebpackRequire from "@moonlight-mod/types/discord/require";import { EventType } from "@moonlight-mod/types/core/event";+import { processFind, processReplace, testFind } from "./util/patch";const logger = new Logger("core/patch");@@ -24,6 +25,9 @@const moduleLoadSubscriptions: Map<string, ((moduleId: string) => void)[]> = new Map();export function registerPatch(patch: IdentifiedPatch) {+ patch.find = processFind(patch.find);+ processReplace(patch.replace);+patches.push(patch);moonlight.unpatched.add(patch);}@@ -113,9 +117,7 @@ // Reset state because global regexes are stateful for some reasonpatch.find.lastIndex = 0;}- // indexOf is faster than includes by 0.25% lmao- const match =- typeof patch.find === "string" ? moduleString.indexOf(patch.find) !== -1 : patch.find.test(moduleString);+ const match = testFind(moduleString, patch.find);// Global regexes apply to all modulesconst shouldRemove = typeof patch.find === "string" ? true : !patch.find.global;@@ -125,10 +127,6 @@ // We ensured all arrays get turned into normal PatchReplace objects on registerconst replace = patch.replace as PatchReplace;if (replace.type === undefined || replace.type === PatchReplaceType.Normal) {- // Add support for \i to match rspack's minified names- if (typeof replace.match !== "string") {- replace.match = new RegExp(replace.match.source.replace(/\\i/g, "[A-Za-z_$][\\w$]*"), replace.match.flags);- }// tsc fails to detect the overloads for this, so I'll just do this// Verbose, but it workslet replaced;
@@ -0,0 +1,30 @@+import { PatchReplace, PatchReplaceType } from "@moonlight-mod/types";++type SingleFind = string | RegExp;+type Find = SingleFind | SingleFind[];++export function processFind<T extends Find>(find: T): T {+ if (Array.isArray(find)) {+ return find.map(processFind) as T;+ } else if (find instanceof RegExp) {+ // Add support for \i to match rspack's minified names+ return new RegExp(find.source.replace(/\\i/g, "[A-Za-z_$][\\w$]*"), find.flags) as T;+ } else {+ return find;+ }+}++export function processReplace(replace: PatchReplace | PatchReplace[]) {+ if (Array.isArray(replace)) {+ replace.forEach(processReplace);+ } else {+ if (replace.type === undefined || replace.type === PatchReplaceType.Normal) {+ replace.match = processFind(replace.match);+ }+ }+}++export function testFind(src: string, find: SingleFind) {+ // indexOf is faster than includes by 0.25% lmao+ return typeof find === "string" ? src.indexOf(find) !== -1 : find.test(src);+}