Skip to content

Commit

Permalink
1.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
NotNite committed Jan 27, 2025
1 parent b14997d commit 63e3fcc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2024 moonlight contributors
Copyright (c) 2025 moonlight contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@moonlight-mod/moonmap",
"version": "1.0.3",
"version": "1.0.4",
"license": "MIT",
"repository": {
"type": "git",
Expand Down
12 changes: 9 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ type WebpackModule = (
export type ModuleExport =
| {
type: ModuleExportType.Function;
find: string | RegExp;
find: string | RegExp | (string | RegExp)[];
recursive?: boolean;
}
| {
type: ModuleExportType.Key;
Expand Down Expand Up @@ -202,8 +203,13 @@ export default class Moonmap {
) {
switch (moduleExport.type) {
case ModuleExportType.Function: {
const match = findFunctionByStrings(original, moduleExport.find);
return match?.[0]?.[0];
return findFunctionByStrings(
original,
Array.isArray(moduleExport.find)
? moduleExport.find
: [moduleExport.find],
moduleExport.recursive ?? false
);
}

case ModuleExportType.Key: {
Expand Down
34 changes: 22 additions & 12 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
export function findFunctionByStrings(
exports: Record<string, any>,
...strings: (string | RegExp)[]
strings: (string | RegExp)[],
recursive = false,
state = new WeakSet()
) {
return (
Object.entries(exports).filter(
([index, func]) =>
typeof func === "function" &&
!strings.some(
(query) =>
!(query instanceof RegExp
? func.toString().match(query)
: func.toString().includes(query))
for (const [key, value] of Object.entries(exports)) {
if (typeof value === "function") {
if (
strings.every((query) =>
query instanceof RegExp
? value.toString().match(query)
: value.toString().includes(query)
)
) ?? null
);
) {
return key;
}
} else if (recursive && typeof value === "object" && value !== null) {
if (state.has(value)) continue;
state.add(value);
const result = findFunctionByStrings(value, strings, recursive, state);
if (result != null) return key;
}
}

return null;
}

export function findObjectFromKey(exports: Record<string, any>, key: string) {
Expand Down

0 comments on commit 63e3fcc

Please sign in to comment.