feat(lib/assets): add getAssetModuleId

This commit is contained in:
PalmDevs 2025-05-08 18:40:53 +07:00
parent c0a853b2fb
commit 75837d48cc
No known key found for this signature in database
GPG key ID: 6FAE8F8677ACA8BA
3 changed files with 19 additions and 9 deletions

View file

@ -1,5 +1,6 @@
import type { Metro } from '@revenge-mod/modules/types'
import type { Asset, AssetId } from '.'
export const _assets = new Map<string, [first: Asset, all: Record<Asset['type'], Asset>]>()
export const _overrides = new Map<string, Asset>()
export const _ids = new WeakMap<Asset, AssetId>()
export const _metas = new WeakMap<Asset, [assetId: AssetId, moduleId: Metro.ModuleID]>()

View file

@ -1,9 +1,9 @@
import { ReactNative } from '@revenge-mod/react'
import { _assets, _ids, _overrides } from './_internal'
import { _assets, _metas, _overrides } from './_internal'
import { AssetRegistry } from './patches'
import type { ReactNative as RN } from '@revenge-mod/types'
import type { ReactNative as RN } from '@revenge-mod/react/types'
export type Asset = ReactNativeAsset | RevengeAsset
export type AssetId = number
@ -33,7 +33,16 @@ export function getAssetById(id: AssetId): Asset | undefined {
* @param asset The asset to get the ID for.
*/
export function getAssetId(asset: Asset) {
return _ids.get(asset)
return _metas.get(asset)?.[0]
}
/**
* Get the module ID that registered the asset. Returns `-1` if the asset is a custom asset.
*
* @param asset The asset to get the module ID for.
*/
export function getAssetModuleId(asset: Asset) {
return _metas.get(asset)?.[1]
}
/**

View file

@ -1,11 +1,11 @@
import { _mUninited } from '@revenge-mod/modules/_/metro'
import { _mInitingId, _mUninited } from '@revenge-mod/modules/_/metro'
import { waitForModules } from '@revenge-mod/modules/finders'
import { byName, byProps } from '@revenge-mod/modules/finders/filters'
import { getModuleDependencies } from '@revenge-mod/modules/metro'
import { _assets, _ids, _overrides } from './_internal'
import { _assets, _metas, _overrides } from './_internal'
import type { ReactNative } from '@revenge-mod/types'
import type { ReactNative } from '@revenge-mod/react/types'
import type { Asset, ReactNativeAsset } from '.'
export let AssetRegistry: ReactNative.AssetsRegistry
@ -27,7 +27,7 @@ const unsubForAssetRegistry = waitForModules(byProps('registerAsset'), (arId, ex
reg[1][type] = asset
const result = orig(asset)
_ids.set(asset, result)
_metas.set(asset, [result, _mInitingId])
return result
}
@ -58,7 +58,7 @@ const unsubForResolveAssetSource = waitForModules(
unsubForResolveAssetSource()
// Custom assets
// Why do we need to do this? Because RN will attempt to resolve the asset via its path, which we don't provide via custom assets.
// Why do we need to do this? Because RN/Dicord (unsure which) will attempt to resolve the asset via its path, which we don't provide via custom assets.
// This will result in a crash, because it tries to read the path and run checks on it, but the path is undefined.
// @ts-expect-error
resolveAssetSource.addCustomSourceTransformer(({ asset }) => {