[Metro] Fixups and tidying

This commit is contained in:
Beef 2023-02-14 18:28:27 +00:00
parent 97176f9d3f
commit eb90b0a21d
3 changed files with 10 additions and 8 deletions

2
src/def.d.ts vendored
View file

@ -280,7 +280,7 @@ interface VendettaObject {
findByPropsAll: PropsFinderAll;
findByDisplayName: (name: string, defaultExp?: boolean) => any;
findByDisplayNameAll: (name: string, defaultExp?: boolean) => any[];
findByStoreName: (storeName: string) => any;
findByStoreName: (name: string) => any;
common: {
constants: PropIntellisense<"API_HOST">;
channels: PropIntellisense<"getVoiceChannelId">;

View file

@ -17,7 +17,7 @@ function blacklist(id: number) {
// Blacklist any "bad-actor" modules, e.g. the dreaded null proxy, the window itself, or undefined modules
for (const key in window.modules) {
const id = Number(key);
const module = window.modules[id].publicModule.exports;
const module = window.modules[id]?.publicModule?.exports;
if (!module || module === window || module["proxygone"] === null) {
blacklist(id);
@ -34,7 +34,7 @@ const filterModules = (modules: MetroModules, single = false) => (filter: (m: an
for (const key in modules) {
const id = Number(key);
const module = modules[id].publicModule.exports;
const module = modules[id]?.publicModule?.exports;
if (!modules[id].isInitialized) try {
__r(id);
@ -58,7 +58,7 @@ const filterModules = (modules: MetroModules, single = false) => (filter: (m: an
else found.push(module);
}
} catch (e: Error | any) {
console.error(`Failed to getModule... ${e.stack || e.toString()}`);
console.error(`Failed to filter modules... ${e.stack || e.toString()}`);
}
}
@ -70,10 +70,12 @@ export const find = filterModules(modules, true);
export const findAll = filterModules(modules);
const propsFilter = (props: (string | symbol)[]) => (m: any) => props.every((p) => m[p] !== undefined);
const dNameFilter = (name: string, defaultExp: boolean) => (defaultExp ? (m: any) => m.name === name : (m: any) => m?.default?.name === name);
// TODO: This uses .name, not .displayName. We should fix this SOON. Changing it directly WILL break plugins, though.
const dNameFilter = (name: string, defaultExp: boolean) => (defaultExp ? (m: any) => m?.name === name : (m: any) => m?.default?.name === name);
const storeFilter = (name: string) => (m: any) => m.getName && m.getName.length === 0 && m.getName() === name;
export const findByProps: PropsFinder = (...props) => find(propsFilter(props));
export const findByPropsAll: PropsFinderAll = (...props) => findAll(propsFilter(props));
export const findByDisplayName = (name: string, defaultExp = true) => find(dNameFilter(name, defaultExp));
export const findByDisplayNameAll = (name: string, defaultExp = true) => findAll(dNameFilter(name, defaultExp));
export const findByStoreName = (storeName: string) => find((m: any) => m.getName?.() === storeName);
export const findByStoreName = (name: string) => find(storeFilter(name));

View file

@ -2,7 +2,7 @@
// This used to be in filters.ts, but things became convoluted
// Early find logic
const basicFind = (prop: string) => Object.values(window.modules).find(m => m.publicModule.exports?.[prop]).publicModule.exports;
const basicFind = (prop: string) => Object.values(window.modules).find(m => m?.publicModule.exports?.[prop])?.publicModule?.exports;
// Hoist React on window
window.React = basicFind("createElement") as typeof import("react");;
@ -11,7 +11,7 @@ window.React = basicFind("createElement") as typeof import("react");;
export const ReactNative = basicFind("Text") as typeof import("react-native");
// Export Discord's constants
export const constants = basicFind("ThemeColorMap");
export const constants = basicFind("AbortCodes");
// Export moment
export const moment = basicFind("isMoment");