GoonCord/src/lib/api/assets.ts

31 lines
935 B
TypeScript
Raw Normal View History

import { after } from "@lib/api/patcher";
2024-03-06 01:57:59 +08:00
import { assets } from "@metro/common";
2022-10-22 23:26:06 +01:00
export const all: Record<string, Asset> = {};
2022-10-22 23:26:06 +01:00
2024-02-23 15:11:35 +08:00
export interface Asset {
name: string;
id: number;
}
export function _patchAssets() {
const unpatch = after("registerAsset", assets, (args: Asset[], id: number) => {
const asset = args[0];
all[asset.name] = { ...asset, id: id };
});
2022-10-22 23:26:06 +01:00
for (let id = 1; ; id++) {
const asset = assets.getAssetByID(id);
if (!asset) break;
if (all[asset.name]) continue;
all[asset.name] = { ...asset, id: id };
2024-03-06 01:57:59 +08:00
}
return unpatch;
2022-10-22 23:26:06 +01:00
}
export const find = (filter: (a: any) => void): Asset | null | undefined => Object.values(all).find(filter);
export const getAssetByName = (name: string): Asset => all[name];
2023-01-07 03:13:16 +00:00
export const getAssetByID = (id: number): Asset => assets.getAssetByID(id);
2024-03-06 01:57:59 +08:00
export const getAssetIDByName = (name: string) => all[name]?.id;