2025-08-13 01:27:49 +07:00
import {
lookupModule ,
lookupModules ,
waitForModules ,
} from '@revenge-mod/modules/finders'
2025-07-13 19:29:44 +07:00
import {
byDependencies ,
byName ,
byProps ,
} from '@revenge-mod/modules/finders/filters'
2025-07-17 16:55:08 +07:00
import {
mInitializingId ,
mUninitialized ,
} from '@revenge-mod/modules/metro/patches'
2025-05-09 19:05:31 +07:00
import { getModuleDependencies } from '@revenge-mod/modules/metro/utils'
2025-07-13 18:10:12 +07:00
import { proxify } from '@revenge-mod/utils/proxy'
2025-07-13 19:52:32 +07:00
import { aOverrides } from './_internal'
2025-06-08 22:07:40 +07:00
import { cacheAsset , cached } from './caches'
2025-08-04 09:41:18 +07:00
import type { Metro } from '@revenge-mod/modules/types'
2025-05-08 23:41:55 +07:00
import type { ReactNative } from '@revenge-mod/react/types'
2025-06-07 15:14:28 +07:00
import type { Asset , PackagerAsset } from './types'
2025-05-08 23:41:55 +07:00
2025-07-13 19:29:44 +07:00
const { relative } = byDependencies
const [ , _classCallCheckId ] = lookupModule ( byName ( '_classCallCheck' ) )
const [ , _createClassId ] = lookupModule ( byName ( '_createClass' ) )
// https://github.com/facebook/react-native/blob/main/packages/react-native/Libraries/Image/AssetSourceResolver.js
const byAssetSourceResolver = byDependencies ( [
_classCallCheckId ,
_createClassId ,
relative ( 1 ) ,
relative ( 2 ) ,
undefined ,
2025-08-05 20:37:06 +07:00
undefined ,
2025-07-13 19:29:44 +07:00
] )
2025-07-13 18:10:12 +07:00
2025-07-31 19:15:08 +07:00
const cachedOnly = { cached : true }
2025-05-08 23:41:55 +07:00
// Tracking/caching assets
2025-06-03 16:24:58 +07:00
const unsubAR = waitForModules (
byProps < ReactNative.AssetsRegistry > ( 'registerAsset' ) ,
( exports , id ) = > {
2025-06-07 15:14:28 +07:00
AssetsRegistryModuleId = id
AssetsRegistry = exports as ReactNative . AssetsRegistry
2025-06-03 16:24:58 +07:00
// 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.
2025-05-08 23:41:55 +07:00
2025-06-03 16:24:58 +07:00
// 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 ( )
2025-05-08 23:41:55 +07:00
2025-06-08 21:37:54 +07:00
// TODO(lib/assets/caches): Ideally we should not wait for the cache promise here, see the promise impl for more info.
2025-06-08 22:07:40 +07:00
cached . then ( cached = > {
if ( ! cached ) {
2025-08-22 02:37:18 +07:00
// Resolve reference once and keep in closure
const metroRequire = __r
2025-06-08 21:37:54 +07:00
// More fragile way, but also more performant:
// There is exactly one asset before the reexported asset registry :/
const firstAssetModuleId = id - 1
2025-07-05 13:12:57 +07:00
for ( const mId of mUninitialized ) {
2025-06-08 21:37:54 +07:00
if ( mId < firstAssetModuleId ) continue
const deps = getModuleDependencies ( mId ) !
2025-08-22 02:37:18 +07:00
if ( deps . length === 1 && deps [ 0 ] === id )
metroRequire ( mId )
2025-06-08 21:37:54 +07:00
}
}
} )
2025-05-08 23:41:55 +07:00
2025-06-03 16:24:58 +07:00
// We already patched the original asset registry, so we don't need to patch again.
return
2025-05-08 23:41:55 +07:00
}
2025-06-03 16:24:58 +07:00
const orig = exports . registerAsset
exports . registerAsset = ( asset : Asset ) = > {
2025-06-07 15:14:28 +07:00
const result = orig ( asset as PackagerAsset )
2025-05-08 23:41:55 +07:00
2025-06-07 15:14:28 +07:00
// Cache and set moduleId for packager assets only
if ( ( asset as PackagerAsset ) . __packager_asset ) {
2025-07-30 20:17:12 +07:00
asset . moduleId = mInitializingId
2025-07-05 13:12:57 +07:00
cacheAsset ( asset , mInitializingId ! )
2025-06-03 16:24:58 +07:00
}
2025-05-08 23:41:55 +07:00
2025-06-07 15:14:28 +07:00
return ( asset . id = result )
2025-05-08 23:41:55 +07:00
}
2025-06-03 16:24:58 +07:00
} ,
2025-07-31 19:15:08 +07:00
cachedOnly ,
2025-06-03 16:24:58 +07:00
)
2025-05-08 23:41:55 +07:00
2025-07-14 09:54:23 +07:00
/ * *
2025-08-04 09:41:18 +07:00
* If you need to use this ID , unproxify { @link AssetsRegistry } first .
2025-07-14 09:54:23 +07:00
*
* ` ` ` js
* preinit() {
2025-08-04 09:41:18 +07:00
* unproxify ( AssetsRegistry )
2025-07-14 09:54:23 +07:00
* // Module ID will now be set!
* AssetsRegistryModuleId // ...
* }
* ` ` `
* /
2025-08-04 09:41:18 +07:00
export let AssetsRegistryModuleId : Metro.ModuleID | undefined
2025-07-14 09:54:23 +07:00
export let AssetsRegistry : ReactNative.AssetsRegistry = proxify ( ( ) = > {
for ( const [ , id ] of lookupModules ( byDependencies ( [ [ ] ] ) , {
initialize : false ,
uninitialized : true ,
} ) ) {
const deps = getModuleDependencies ( id ) !
if ( deps . length !== 1 ) continue
// The module next to assets-registry is AssetSourceResolver
if ( byAssetSourceResolver ( deps [ 0 ] + 1 ) ) {
const module = __r ( id )
// ID will be set by the wait below
if ( module ? . registerAsset ) return ( AssetsRegistry = module )
}
}
throw new Error ( 'assets-registry not found' )
} )
2025-05-08 23:41:55 +07:00
// Asset overrides
const unsubRAS = waitForModules (
byName < {
2025-06-03 16:24:58 +07:00
addCustomSourceTransformer : (
2025-06-07 15:14:28 +07:00
transformer : ( arg : { asset : Asset } ) = > PackagerAsset ,
2025-06-03 16:24:58 +07:00
) = > void
2025-05-08 23:41:55 +07:00
} > ( '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 } ) = > {
2025-07-01 17:30:53 +07:00
if ( ! ( asset as PackagerAsset ) . __packager_asset ) return asset
2025-05-08 23:41:55 +07:00
} )
// Asset overrides
// @ts-expect-error
2025-07-05 13:12:57 +07:00
rAS . addCustomSourceTransformer ( ( { asset } ) = > aOverrides . get ( asset ) )
2025-05-08 23:41:55 +07:00
} ,
2025-07-31 19:15:08 +07:00
cachedOnly ,
2025-05-08 23:41:55 +07:00
)