this repo has no description

Support \i everywhere

Changed files
+38 -17
packages
core
src
core-extensions
src
spacepack
webpackModules
+3 -10
packages/core-extensions/src/spacepack/webpackModules/spacepack.ts
···
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;
···
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];
···
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
);
},
+5 -7
packages/core/src/patch.ts
···
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");
···
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);
}
···
patch.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 modules
const shouldRemove = typeof patch.find === "string" ? true : !patch.find.global;
···
const 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 works
let replaced;
+30
packages/core/src/util/patch.ts
···
+
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);
+
}