this repo has no description

Support \i everywhere

+38 -17
+3 -10
packages/core-extensions/src/spacepack/webpackModules/spacepack.ts
··· 1 1 import { WebpackModule, WebpackModuleFunc, WebpackRequireType } from "@moonlight-mod/types"; 2 2 import { Spacepack } from "@moonlight-mod/types/coreExtensions/spacepack"; 3 + import { processFind, testFind } from "@moonlight-mod/core/util/patch"; 3 4 4 5 const webpackRequire = require as unknown as WebpackRequireType; 5 6 const cache = webpackRequire.c; ··· 42 43 43 44 findByCode: (...args: (string | RegExp)[]) => { 44 45 return Object.entries(modules) 45 - .filter( 46 - ([id, mod]) => 47 - !args.some( 48 - (item) => !(item instanceof RegExp ? item.test(mod.toString()) : mod.toString().indexOf(item) !== -1) 49 - ) 50 - ) 46 + .filter(([id, mod]) => !args.some((item) => !testFind(mod.toString(), processFind(item)))) 51 47 .map(([id]) => { 52 48 //if (!(id in cache)) require(id); 53 49 //return cache[id]; ··· 137 133 return ( 138 134 Object.entries(exports).filter( 139 135 ([index, func]) => 140 - typeof func === "function" && 141 - !strings.some( 142 - (query) => !(query instanceof RegExp ? func.toString().match(query) : func.toString().includes(query)) 143 - ) 136 + typeof func === "function" && !strings.some((query) => !testFind(func.toString(), processFind(query))) 144 137 )?.[0]?.[1] ?? null 145 138 ); 146 139 },
+5 -7
packages/core/src/patch.ts
··· 13 13 import calculateDependencies, { Dependency } from "./util/dependency"; 14 14 import WebpackRequire from "@moonlight-mod/types/discord/require"; 15 15 import { EventType } from "@moonlight-mod/types/core/event"; 16 + import { processFind, processReplace, testFind } from "./util/patch"; 16 17 17 18 const logger = new Logger("core/patch"); 18 19 ··· 24 25 const moduleLoadSubscriptions: Map<string, ((moduleId: string) => void)[]> = new Map(); 25 26 26 27 export function registerPatch(patch: IdentifiedPatch) { 28 + patch.find = processFind(patch.find); 29 + processReplace(patch.replace); 30 + 27 31 patches.push(patch); 28 32 moonlight.unpatched.add(patch); 29 33 } ··· 113 117 patch.find.lastIndex = 0; 114 118 } 115 119 116 - // indexOf is faster than includes by 0.25% lmao 117 - const match = 118 - typeof patch.find === "string" ? moduleString.indexOf(patch.find) !== -1 : patch.find.test(moduleString); 120 + const match = testFind(moduleString, patch.find); 119 121 120 122 // Global regexes apply to all modules 121 123 const shouldRemove = typeof patch.find === "string" ? true : !patch.find.global; ··· 125 127 const replace = patch.replace as PatchReplace; 126 128 127 129 if (replace.type === undefined || replace.type === PatchReplaceType.Normal) { 128 - // Add support for \i to match rspack's minified names 129 - if (typeof replace.match !== "string") { 130 - replace.match = new RegExp(replace.match.source.replace(/\\i/g, "[A-Za-z_$][\\w$]*"), replace.match.flags); 131 - } 132 130 // tsc fails to detect the overloads for this, so I'll just do this 133 131 // Verbose, but it works 134 132 let replaced;
+30
packages/core/src/util/patch.ts
··· 1 + import { PatchReplace, PatchReplaceType } from "@moonlight-mod/types"; 2 + 3 + type SingleFind = string | RegExp; 4 + type Find = SingleFind | SingleFind[]; 5 + 6 + export function processFind<T extends Find>(find: T): T { 7 + if (Array.isArray(find)) { 8 + return find.map(processFind) as T; 9 + } else if (find instanceof RegExp) { 10 + // Add support for \i to match rspack's minified names 11 + return new RegExp(find.source.replace(/\\i/g, "[A-Za-z_$][\\w$]*"), find.flags) as T; 12 + } else { 13 + return find; 14 + } 15 + } 16 + 17 + export function processReplace(replace: PatchReplace | PatchReplace[]) { 18 + if (Array.isArray(replace)) { 19 + replace.forEach(processReplace); 20 + } else { 21 + if (replace.type === undefined || replace.type === PatchReplaceType.Normal) { 22 + replace.match = processFind(replace.match); 23 + } 24 + } 25 + } 26 + 27 + export function testFind(src: string, find: SingleFind) { 28 + // indexOf is faster than includes by 0.25% lmao 29 + return typeof find === "string" ? src.indexOf(find) !== -1 : find.test(src); 30 + }