2025-05-09 19:05:31 +07:00
import { _initing , _uninits } from '@revenge-mod/modules/_/metro'
2025-05-08 23:41:55 +07:00
import { byName , byProps } from '@revenge-mod/modules/finders/filters'
2025-05-09 19:05:31 +07:00
import { waitForModules } from '@revenge-mod/modules/finders/wait'
import { getModuleDependencies } from '@revenge-mod/modules/metro/utils'
2025-05-08 23:41:55 +07:00
2025-06-02 15:56:07 +07:00
import { _metas , _overrides } from './_internal'
import { cacheAsset } from './caches'
2025-05-08 23:41:55 +07:00
import type { ReactNative } from '@revenge-mod/react/types'
import type { Asset , ReactNativeAsset } from './types'
export let AssetRegistry : ReactNative.AssetsRegistry
export let AssetRegistryModuleId : number
// Tracking/caching assets
2025-05-18 22:24:48 +07:00
const unsubAR = waitForModules ( byProps < ReactNative.AssetsRegistry > ( 'registerAsset' ) , ( exports , id ) = > {
2025-05-08 23:41:55 +07:00
AssetRegistryModuleId = id
AssetRegistry = exports as ReactNative . AssetsRegistry
// There are two matching exports. One is the original, and one is a re-export.
// The original asset-registry is simply required by the re-exported one with no changes.
// This is the re-exported registry, because it has dependencies.
// We can begin the caching process here, asset registrar modules have the reexported registry as a dependency.
if ( getModuleDependencies ( id ) ! . length ) {
unsubAR ( )
// TODO(assets/patches): conditionally run this if cache does not exist
// More fragile way, but also more performant:
// There is exactly one asset before the reexported asset registry :/
const firstAssetModuleId = id - 1
2025-05-09 19:05:31 +07:00
for ( const mId of _uninits ) {
2025-05-08 23:41:55 +07:00
if ( mId < firstAssetModuleId ) continue
const deps = getModuleDependencies ( mId ) !
if ( deps . length === 1 && deps [ 0 ] === id ) __r ( mId )
}
// We already patched the original asset registry, so we don't need to patch again.
return
}
const orig = exports . registerAsset
exports . registerAsset = ( asset : Asset ) = > {
const result = orig ( asset as ReactNativeAsset )
2025-06-02 15:56:07 +07:00
// Cache packager assets only
if ( ( asset as ReactNativeAsset ) . __packager_asset ) {
cacheAsset ( asset , _initing )
// In-memory cache
_metas . set ( asset , [ result , _initing ] )
2025-05-08 23:41:55 +07:00
}
return result
}
} )
// Asset overrides
const unsubRAS = waitForModules (
byName < {
addCustomSourceTransformer : ( transformer : ( arg : { asset : Asset } ) = > ReactNativeAsset ) = > void
} > ( 'resolveAssetSource' ) ,
2025-05-18 22:24:48 +07:00
rAS = > {
2025-05-08 23:41:55 +07:00
unsubRAS ( )
// Custom assets
2025-06-02 15:56:07 +07:00
// Why do we need to do this? Because RN/Discord (unsure which) will attempt to resolve the asset via its path, which we don't provide via custom assets.
2025-05-08 23:41:55 +07:00
// 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
rAS . addCustomSourceTransformer ( ( { asset } ) = > {
// @ts-expect-error
if ( ! asset . __packager_asset ) return asset
} )
// Asset overrides
// @ts-expect-error
2025-06-02 15:56:07 +07:00
rAS . addCustomSourceTransformer ( ( { asset } ) = > _overrides . get ( asset ) )
2025-05-08 23:41:55 +07:00
} ,
)