[Assets] Implement

This commit is contained in:
Beef 2022-10-22 23:26:06 +01:00
parent cc634c7805
commit 62b843f9f6
3 changed files with 57 additions and 0 deletions

18
src/def.d.ts vendored
View file

@ -28,6 +28,15 @@ interface FindInTreeOptions {
maxDepth?: number; maxDepth?: number;
} }
interface Asset {
name: string;
id: number;
}
interface Assets {
[id: string]: Asset;
}
interface VendettaObject { interface VendettaObject {
patcher: { patcher: {
after: typeof _spitroast.after; after: typeof _spitroast.after;
@ -49,6 +58,15 @@ interface VendettaObject {
AsyncStorage: typeof _AsyncStorage; AsyncStorage: typeof _AsyncStorage;
}; };
}; };
ui: {
assets: {
all: Assets;
find: (filter: (a: any) => void) => Asset | null | undefined;
getAssetByName: (name: string) => Asset;
getAssetByID: (name: string) => Asset;
getAssetIDByName: (name: string) => number;
}
};
logger: Logger; logger: Logger;
} }

View file

@ -2,6 +2,8 @@ import patcher from "@lib/patcher";
import logger from "@lib/logger"; import logger from "@lib/logger";
import * as metro from "@metro/filters"; import * as metro from "@metro/filters";
import * as common from "@metro/common"; import * as common from "@metro/common";
import { all, find, getAssetByID, getAssetByName, getAssetIDByName } from "@ui/assets";
import patchAssets from "@ui/assets";
import initSettings from "@ui/settings"; import initSettings from "@ui/settings";
import { patchLogHook } from "@lib/debug"; import { patchLogHook } from "@lib/debug";
@ -10,12 +12,22 @@ let erroredOnLoad = false;
try { try {
initSettings(); initSettings();
patchAssets();
patchLogHook(); patchLogHook();
window.vendetta = { window.vendetta = {
patcher: patcher, patcher: patcher,
metro: { ...metro, common: common }, metro: { ...metro, common: common },
logger: logger, logger: logger,
ui: {
assets: {
all: all,
find: find,
getAssetByID: getAssetByID,
getAssetByName: getAssetByName,
getAssetIDByName: getAssetIDByName,
},
},
}; };
} catch (e: Error | any) { } catch (e: Error | any) {
erroredOnLoad = true; erroredOnLoad = true;

27
src/ui/assets.ts Normal file
View file

@ -0,0 +1,27 @@
import { Asset, Assets } from "@types";
import { after } from "@lib/patcher";
import { findByProps } from "@metro/filters";
const assetsModule = findByProps("registerAsset");
export const all: Assets = {};
export default function patchAssets() {
try {
after("registerAsset", assetsModule, (args: Asset[], id: number) => {
const asset = args[0];
all[asset.name] = { ...asset, id };
});
for (let id = 1; ; id++) {
const asset = assetsModule.getAssetByID(id);
if (!asset) break;
if (all[asset.name]) continue;
};
} catch {};
}
export const find = (filter: (a: any) => void): Asset | null | undefined => Object.values(all).find(filter);
export const getAssetByName = (name: string): Asset => all[name];
export const getAssetByID = (name: string): Asset => assetsModule.getAssetByID(name);
export const getAssetIDByName = (name: string) => all[name].id;