diff --git a/src/debug/runReporter.ts b/src/debug/runReporter.ts index 24bc138df..33ef2e97b 100644 --- a/src/debug/runReporter.ts +++ b/src/debug/runReporter.ts @@ -140,7 +140,7 @@ async function runReporter() { if (filter.$$vencordProps != null) { const filterName = filter.$$vencordProps[0]; - parsedFilter = `${filterName}(${filter.$$vencordProps.slice(1).map((arg: any) => JSON.stringify(arg)).join(", ")})`; + parsedFilter = `${filterName}(${filter.$$vencordProps.slice(1).map((arg: any) => arg instanceof RegExp ? String(arg) : JSON.stringify(arg)).join(", ")})`; } else { parsedFilter = String(filter).slice(0, 147) + "..."; } @@ -150,7 +150,7 @@ async function runReporter() { logMessage += `(${JSON.stringify(code)}, {\n${parsedFailedMappers.map(([key, parsedFilter]) => `\t${key}: ${parsedFilter}`).join(",\n")}\n})`; } else { - logMessage += `(${filterName.length ? `${filterName}(` : ""}${parsedArgs.map(arg => JSON.stringify(arg)).join(", ")})${filterName.length ? ")" : ""}`; + logMessage += `(${filterName.length ? `${filterName}(` : ""}${parsedArgs.map(arg => arg instanceof RegExp ? String(arg) : JSON.stringify(arg)).join(", ")})${filterName.length ? ")" : ""}`; } ReporterLogger.log("Webpack Find Fail:", logMessage); diff --git a/src/webpack/api.tsx b/src/webpack/api.tsx index be1bc9344..2bc027ba9 100644 --- a/src/webpack/api.tsx +++ b/src/webpack/api.tsx @@ -62,13 +62,24 @@ if (IS_DEV && IS_DISCORD_DESKTOP) { }, 0); } +export type PropsFilter = Array; +export type CodeFilter = Array; +export type StoreNameFilter = string; + export type FilterFn = ((module: any) => boolean) & { - $$vencordProps?: string[]; + $$vencordProps?: Array; $$vencordIsFactoryFilter?: boolean; }; +const stringMatches = (s: string, filter: CodeFilter) => + filter.every(f => + typeof f === "string" + ? s.includes(f) + : (f.global && (f.lastIndex = 0), f.test(s)) + ); + export const filters = { - byProps: (...props: string[]): FilterFn => { + byProps: (...props: PropsFilter): FilterFn => { const filter: FilterFn = props.length === 1 ? m => m?.[props[0]] !== void 0 : m => props.every(p => m?.[p] !== void 0); @@ -77,28 +88,25 @@ export const filters = { return filter; }, - byCode: (...code: string[]): FilterFn => { + byCode: (...code: CodeFilter): FilterFn => { + code = code.map(canonicalizeMatch); const filter: FilterFn = m => { if (typeof m !== "function") return false; - const s = String(m); - for (const c of code) { - if (!s.includes(c)) return false; - } - return true; + return stringMatches(String(m), code); }; filter.$$vencordProps = ["byCode", ...code]; return filter; }, - byStoreName: (name: string): FilterFn => { + byStoreName: (name: StoreNameFilter): FilterFn => { const filter: FilterFn = m => m?.constructor?.displayName === name; filter.$$vencordProps = ["byStoreName", name]; return filter; }, - byComponentCode: (...code: string[]): FilterFn => { + byComponentCode: (...code: CodeFilter): FilterFn => { const byCodeFilter = filters.byCode(...code); const filter: FilterFn = m => { let inner = m; @@ -118,7 +126,7 @@ export const filters = { return filter; }, - byFactoryCode: (...code: string[]): FilterFn => { + byFactoryCode: (...code: CodeFilter): FilterFn => { const byCodeFilter = filters.byCode(...code); byCodeFilter.$$vencordProps = ["byFactoryCode", ...code]; @@ -133,7 +141,7 @@ export const webpackSearchHistory = [] as Array<["waitFor" | "find" | "findCompo function printFilter(filter: FilterFn) { if (filter.$$vencordProps != null) { const props = filter.$$vencordProps; - return `${props[0]}(${props.slice(1).map(arg => JSON.stringify(arg)).join(", ")})`; + return `${props[0]}(${props.slice(1).map(arg => arg instanceof RegExp ? String(arg) : JSON.stringify(arg)).join(", ")})`; } return String(filter); @@ -263,9 +271,9 @@ export function findComponent(filter: FilterFn, parse: ( * @param parse A function that takes the found component as its first argument and returns a component. Useful if you want to wrap the found component in something. Defaults to the original component * @returns The component if found, or a noop component */ -export function findExportedComponent(...props: string[] | [...string[], (component: any) => LazyComponentType]) { +export function findExportedComponent(...props: PropsFilter | [...PropsFilter, (component: any) => LazyComponentType]) { const parse = (typeof props.at(-1) === "function" ? props.pop() : m => m) as (component: any) => LazyComponentType; - const newProps = props as string[]; + const newProps = props as PropsFilter; const filter = filters.byProps(...newProps); @@ -308,9 +316,9 @@ export function findExportedComponent(...props: string[] * @param parse A function that takes the found component as its first argument and returns a component. Useful if you want to wrap the found component in something. Defaults to the original component * @returns The component if found, or a noop component */ -export function findComponentByCode(...code: string[] | [...string[], (component: any) => LazyComponentType]) { +export function findComponentByCode(...code: CodeFilter | [...CodeFilter, (component: any) => LazyComponentType]) { const parse = (typeof code.at(-1) === "function" ? code.pop() : m => m) as (component: any) => LazyComponentType; - const newCode = code as string[]; + const newCode = code as CodeFilter; const ComponentResult = findComponent(filters.byComponentCode(...newCode), parse, { isIndirect: true }); @@ -327,9 +335,9 @@ export function findComponentByCode(...code: string[] | * @param props A list of props to search the module or exports for * @param parse A function that takes the find result as its first argument and returns something. Useful if you want to use a value from the find result, instead of all of it. Defaults to the find result itself */ -export function findByProps(...props: string[] | [...string[], (mod: any) => T]) { +export function findByProps(...props: PropsFilter | [...PropsFilter, (mod: any) => T]) { const parse = (typeof props.at(-1) === "function" ? props.pop() : m => m) as (mod: any) => T; - const newProps = props as string[]; + const newProps = props as PropsFilter; const result = find(filters.byProps(...newProps), parse, { isIndirect: true }); @@ -348,9 +356,9 @@ export function findByProps(...props: string[] | [...string[], (mod: an * @param props A list of props to search the module or exports for * @param parse A function that takes the find result as its first argument and returns something. Useful if you want to use a value from the find result, instead of all of it. Defaults to the find result itself */ -export function findByPropsAndExtract(...props: string[] | [...string[], (mod: any) => T]) { +export function findByPropsAndExtract(...props: PropsFilter | [...PropsFilter, (mod: any) => T]) { const parse = (typeof props.at(-1) === "function" ? props.pop() : m => m) as (mod: any) => T; - const newProps = props as string[]; + const newProps = props as PropsFilter; const result = find(filters.byProps(...newProps), m => parse(m[newProps[0]]), { isIndirect: true }); @@ -367,9 +375,9 @@ export function findByPropsAndExtract(...props: string[] | [...string[] * @param code A list of code to search each export for * @param parse A function that takes the find result as its first argument and returns something. Useful if you want to use a value from the find result, instead of all of it. Defaults to the find result itself */ -export function findByCode(...code: string[] | [...string[], (mod: any) => T]) { +export function findByCode(...code: CodeFilter | [...CodeFilter, (mod: any) => T]) { const parse = (typeof code.at(-1) === "function" ? code.pop() : m => m) as (mod: any) => T; - const newCode = code as string[]; + const newCode = code as CodeFilter; const result = find(filters.byCode(...newCode), parse, { isIndirect: true }); @@ -385,7 +393,7 @@ export function findByCode(...code: string[] | [...string[], (mod: any) * * @param name The store name */ -export function findStore(name: string) { +export function findStore(name: StoreNameFilter) { const result = find(filters.byStoreName(name), m => m, { isIndirect: true }); if (IS_REPORTER) { @@ -401,9 +409,9 @@ export function findStore(name: string) { * @param code A list of code to search each factory for * @param parse A function that takes the find result as its first argument and returns something. Useful if you want to use a value from the find result, instead of all of it. Defaults to the find result itself */ -export function findByFactoryCode(...code: string[] | [...string[], (mod: any) => T]) { +export function findByFactoryCode(...code: CodeFilter | [...CodeFilter, (mod: any) => T]) { const parse = (typeof code.at(-1) === "function" ? code.pop() : m => m) as (mod: any) => T; - const newCode = code as string[]; + const newCode = code as CodeFilter; const result = find(filters.byFactoryCode(...newCode), parse, { isIndirect: true }); @@ -428,7 +436,7 @@ export function findByFactoryCode(...code: string[] | [...string[], (mo * @param mappers Mappers to create the non mangled exports object * @returns Unmangled exports as specified in mappers */ -export function mapMangledModule(code: string | string[], mappers: Record) { +export function mapMangledModule(code: string | RegExp | CodeFilter, mappers: Record) { const mapping = {} as Record; const setters = {} as Record void>; @@ -481,7 +489,7 @@ export function mapMangledModule(code: string | string[], /** * Find the first module factory that includes all the given code. */ -export function findModuleFactory(...code: string[]) { +export function findModuleFactory(...code: CodeFilter) { const filter = filters.byFactoryCode(...code); const [proxy, setInnerValue] = proxyInner(`Webpack module factory find matched no module. Filter: ${printFilter(filter)}`, "Webpack find with proxy called on a primitive value."); @@ -533,7 +541,7 @@ export const ChunkIdsRegex = /\("([^"]+?)"\)/g; * @param matcher A RegExp that returns the chunk ids array as the first capture group and the entry point id as the second. Defaults to a matcher that captures the first lazy chunk loading found in the module factory * @returns A function that returns a promise that resolves with a boolean whether the chunks were loaded, on first call */ -export function extractAndLoadChunksLazy(code: string | string[], matcher: RegExp = DefaultExtractAndLoadChunksRegex) { +export function extractAndLoadChunksLazy(code: string | RegExp | CodeFilter, matcher: RegExp = DefaultExtractAndLoadChunksRegex) { const module = findModuleFactory(...Array.isArray(code) ? code : [code]); const extractAndLoadChunks = makeLazy(async () => { @@ -805,16 +813,9 @@ export const cacheFindBulk = traceFunction("cacheFindBulk", function cacheFindBu /** * Find the id of the first already loaded module factory that includes all the given code. */ -export const cacheFindModuleId = traceFunction("cacheFindModuleId", function cacheFindModuleId(...code: string[]) { - outer: +export const cacheFindModuleId = traceFunction("cacheFindModuleId", function cacheFindModuleId(...code: CodeFilter) { for (const id in wreq.m) { - const str = String(wreq.m[id]); - - for (const c of code) { - if (!str.includes(c)) continue outer; - } - - return id; + if (stringMatches(String(wreq.m[id]), code)) return id; } const err = new Error("Didn't find module with code(s):\n" + code.join("\n")); @@ -829,12 +830,12 @@ export const cacheFindModuleId = traceFunction("cacheFindModuleId", function cac /** * Find the first already loaded module factory that includes all the given code. */ -export function cacheFindModuleFactory(...code: string[]) { +export const cacheFindModuleFactory = traceFunction("cacheFindModuleFactory", function cacheFindModuleFactory(...code: CodeFilter) { const id = cacheFindModuleId(...code); if (id == null) return; return wreq.m[id]; -} +}); /** * Search modules by keyword. This searches the factory methods, @@ -843,18 +844,16 @@ export function cacheFindModuleFactory(...code: string[]) { * @param filters One or more strings or regexes * @returns Mapping of found modules */ -export function search(...filters: Array) { +export function search(...filters: CodeFilter) { const results = {} as Record; const factories = wreq.m; - outer: + for (const id in factories) { const factory = factories[id]; - const factoryStr = String(factory); - for (const filter of filters) { - if (typeof filter === "string" && !factoryStr.includes(filter)) continue outer; - if (filter instanceof RegExp && !filter.test(factoryStr)) continue outer; + + if (stringMatches(String(factory), filters)) { + results[id] = factory; } - results[id] = factory; } return results;