diff --git a/lib/assets/src/_internal.ts b/lib/assets/src/_internal.ts index 0c1128a..22d1cf1 100644 --- a/lib/assets/src/_internal.ts +++ b/lib/assets/src/_internal.ts @@ -1,3 +1,5 @@ -import type { Asset } from '.' +import type { Asset, AssetId } from '.' -export const _assets: Map]> = new Map() +export const _assets = new Map]>() +export const _overrides = new Map() +export const _ids = new WeakMap() diff --git a/lib/assets/src/index.ts b/lib/assets/src/index.ts index 37dea46..f617c30 100644 --- a/lib/assets/src/index.ts +++ b/lib/assets/src/index.ts @@ -1,6 +1,6 @@ import { ReactNative } from '@revenge-mod/react' -import { _assets } from './_internal' +import { _assets, _ids, _overrides } from './_internal' import { AssetRegistry } from './patches' import type { ReactNative as RN } from '@revenge-mod/types' @@ -20,6 +20,15 @@ export function getAssetById(id: AssetId): Asset | undefined { return AssetRegistry.getAssetByID(id) } +/** + * Get the ID of an asset. + * + * @param asset The asset to get the ID for. + */ +export function getAssetId(asset: Asset) { + return _ids.get(asset) +} + /** * Get an asset by its name. * If more than one asset is registered with the same name, this will return the one with the preferred type, or the first registered one. @@ -30,7 +39,7 @@ export function getAssetByName(name: string): Asset | undefined { const reg = _assets.get(name) if (!reg) return - return reg[1][_preferredType] ?? reg[0] + return _overrides.get(name) ?? reg[1][_preferredType] ?? reg[0] } /** diff --git a/lib/assets/src/patches.ts b/lib/assets/src/patches.ts index 975f94e..ab65bd9 100644 --- a/lib/assets/src/patches.ts +++ b/lib/assets/src/patches.ts @@ -3,7 +3,7 @@ import { waitForModules } from '@revenge-mod/modules/finders' import { byProps } from '@revenge-mod/modules/finders/filters' import { getModuleDependencies } from '@revenge-mod/modules/metro' -import { _assets } from './_internal' +import { _assets, _ids } from './_internal' import type { ReactNative } from '@revenge-mod/types' import type { Asset } from '.' @@ -24,7 +24,9 @@ const unsubForAssetRegistry = waitForModules(byProps('registerAsset'), (arId, ex const reg = _assets.get(name)! reg[1][type] = asset - return (asset.id = orig(asset)) + const result = orig(asset) + _ids.set(asset, result) + return result } return @@ -33,15 +35,13 @@ const unsubForAssetRegistry = waitForModules(byProps('registerAsset'), (arId, ex unsubForAssetRegistry() // TODO(assets/patches): conditionally run this if cache does not exist - if (true) { - // More fragile way, but more performant: - // There is exactly one asset before the reexported asset registry, thanks Discord! - const firstAssetModuleId = arId - 1 - for (const id of _mUninited) { - if (id < firstAssetModuleId) continue + // More fragile way, but more performant: + // There is exactly one asset before the reexported asset registry, thanks Discord! + const firstAssetModuleId = arId - 1 + for (const id of _mUninited) { + if (id < firstAssetModuleId) continue - const deps = getModuleDependencies(id)! - if (deps.length === 1 && deps[0] === arId) __r(id) - } + const deps = getModuleDependencies(id)! + if (deps.length === 1 && deps[0] === arId) __r(id) } }) diff --git a/lib/types/types.d.ts b/lib/types/types.d.ts index ed7b293..57476ce 100644 --- a/lib/types/types.d.ts +++ b/lib/types/types.d.ts @@ -108,11 +108,6 @@ export namespace ReactNative { type: string resolver?: AssetDestPathResolver } - - // Revenge-added - export interface PackagerAsset { - id: number - } } export interface AssetsRegistry { diff --git a/src/components/icons/AssetIcon.tsx b/src/components/icons/AssetIcon.tsx index dd8d48a..22f5911 100644 --- a/src/components/icons/AssetIcon.tsx +++ b/src/components/icons/AssetIcon.tsx @@ -1,9 +1,10 @@ -import { getAssetByName } from '@revenge-mod/assets' -import type { DiscordModules } from '@revenge-mod/discord/types' +import { getAssetByName, getAssetId } from '@revenge-mod/assets' import { Components } from '@revenge-mod/discord/ui' +import type { DiscordModules } from '@revenge-mod/discord/types' + export default function AssetIcon(props: AssetIconProps) { - return + return } export interface AssetIconProps extends Omit { diff --git a/src/plugins/developer-settings/devtools.ts b/src/plugins/developer-settings/devtools.ts index 1e67d05..c44c8c5 100644 --- a/src/plugins/developer-settings/devtools.ts +++ b/src/plugins/developer-settings/devtools.ts @@ -1,4 +1,4 @@ -import { getAssetByName } from '@revenge-mod/assets' +import { getAssetByName, getAssetId } from '@revenge-mod/assets' import { ToastActionCreators } from '@revenge-mod/discord/actions' import { React } from '@revenge-mod/react' import { getErrorStack } from '@revenge-mod/utils/errors' @@ -28,7 +28,7 @@ subscriptions.add((e, err) => { ToastActionCreators.open({ key: 'DEVTOOLS_ERROR', - icon: getAssetByName('CircleXIcon-primary')!.id, + icon: getAssetId(getAssetByName('CircleXIcon-primary')!)!, content: actualError, }) }