2024-05-10 02:15:32 +08:00
|
|
|
// TODO: Rewrite the whole thing to fit new metro :3
|
|
|
|
|
|
2024-02-28 14:09:42 +08:00
|
|
|
import { after } from "@lib/api/patcher";
|
2024-05-20 15:36:33 +08:00
|
|
|
import { getMetroCache, registerAssetCacheId } from "@metro/caches";
|
|
|
|
|
import { getImportingModuleId, requireModule } from "@metro/modules";
|
2022-10-22 23:26:06 +01:00
|
|
|
|
2023-04-15 03:04:48 +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;
|
2024-05-10 02:15:32 +08:00
|
|
|
moduleId: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface AssetModule {
|
|
|
|
|
registerAsset(assetDefinition: any): number;
|
|
|
|
|
getAssetByID(id: number): any;
|
2024-02-23 15:11:35 +08:00
|
|
|
}
|
|
|
|
|
|
2024-05-10 02:15:32 +08:00
|
|
|
let assetsModule: AssetModule;
|
|
|
|
|
|
2024-03-11 15:04:11 +08:00
|
|
|
/**
|
|
|
|
|
* @internal
|
|
|
|
|
*/
|
2024-05-10 02:15:32 +08:00
|
|
|
export function patchAssets(module: AssetModule) {
|
|
|
|
|
if (assetsModule) return;
|
|
|
|
|
assetsModule = module;
|
|
|
|
|
|
|
|
|
|
const unpatch = after("registerAsset", assetsModule, (args: Asset[], id: number) => {
|
2023-02-04 02:30:46 +00:00
|
|
|
const asset = args[0];
|
2024-05-10 02:15:32 +08:00
|
|
|
|
|
|
|
|
const moduleId = Number(getImportingModuleId());
|
|
|
|
|
|
|
|
|
|
registerAssetCacheId(asset.name, moduleId);
|
|
|
|
|
|
|
|
|
|
all[asset.name] = {
|
|
|
|
|
...asset,
|
|
|
|
|
id,
|
|
|
|
|
moduleId
|
|
|
|
|
};
|
2023-02-04 02:30:46 +00:00
|
|
|
});
|
2022-10-22 23:26:06 +01:00
|
|
|
|
2023-02-04 02:30:46 +00:00
|
|
|
for (let id = 1; ; id++) {
|
2024-05-10 02:15:32 +08:00
|
|
|
const asset = assetsModule.getAssetByID(id);
|
2023-02-04 02:30:46 +00:00
|
|
|
if (!asset) break;
|
|
|
|
|
if (all[asset.name]) continue;
|
|
|
|
|
all[asset.name] = { ...asset, id: id };
|
2024-03-06 01:57:59 +08:00
|
|
|
}
|
2023-02-04 02:30:46 +00: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];
|
2024-05-10 02:15:32 +08:00
|
|
|
export const getAssetByID = (id: number): Asset => assetsModule.getAssetByID(id);
|
|
|
|
|
export const getAssetIDByName = (name: string) => all[name]?.id ?? requireModule(getMetroCache().assetsCache[name]);
|