Skip to content

Commit

Permalink
feat: export more modules and icons, save cache in fs, improve typings
Browse files Browse the repository at this point in the history
  • Loading branch information
PalmDevs committed Dec 26, 2024
1 parent 0fd85f3 commit 457a47b
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 20 deletions.
3 changes: 2 additions & 1 deletion libraries/modules/src/common/components/icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ export type IconComponentProps = {
color?: string
}

export const CopyIcon = wrapIcon(findProp<IconComponent>('CopyIcon')!)
export const CheckmarkLargeIcon = wrapIcon(findProp<IconComponent>('CheckmarkLargeIcon')!)
export const CopyIcon = wrapIcon(findProp<IconComponent>('CopyIcon')!)
export const FolderIcon = wrapIcon(findProp<IconComponent>('FolderIcon')!)
5 changes: 4 additions & 1 deletion libraries/modules/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require('!deps-shim!')
import { lazyDestructure } from '@revenge-mod/utils/lazy'
import { findByFilePath, findByName, findByProps } from '../finders'

import type { StackNavigationOptions } from '@react-navigation/stack'
import type { ReactNativeInternals } from '@revenge-mod/revenge'
import type { ComponentType } from 'react'
import type { DiscordModules } from '../types'
Expand Down Expand Up @@ -67,7 +68,7 @@ export const NavigationNative = findByProps<
>('NavigationContainer')!

export type NavigationNativeStackParamList = {
RevengeCustomPage: { title: string; render: ComponentType }
RevengeCustomPage: { render: ComponentType } & StackNavigationOptions
// biome-ignore lint/suspicious/noExplicitAny: https://github.com/react-navigation/react-navigation/issues/9037
[Page: string]: any
}
Expand Down Expand Up @@ -100,3 +101,5 @@ export const semver = findByProps<typeof import('semver')>('SEMVER_SPEC_VERSION'
export const xxhash64 = findByProps<typeof import('@intrnl/xxhash64')>('XXH64')!
export const nobleHashesUtils = findByProps<typeof import('@noble/hashes/utils')>('randomBytes')!
export * from 'events'

export const _ = findByProps<typeof import('lodash')>('cloneDeep')!
10 changes: 5 additions & 5 deletions libraries/modules/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ export const MetroCacheVersion = 2
/**
* The storage key for the Metro cache
*/
export const MetroCacheKey = 'RevengeMetroCache'
export const MetroCacheRelativeFilePath = 'RevengeMetroCache'

/**
* The key for a module's file path (using a symbol does not work for some reason)
* The key for the first asset type registered
*/
export const MetroModuleFilePathKey = '__moduleFilePath'
export const FirstAssetTypeRegisteredKey = '__firstAssetTypeRegistered'

/**
* The key for an asset's first registered type
* The key for a module's file path (using a symbol does not work for some reason)
*/
export const FirstAssetTypeRegisteredKey = '__firstTypeRegistered'
export const MetroModuleFilePathKey = '__moduleFilePath'

/**
* The symbol to access asset caches using indexes instead of names
Expand Down
18 changes: 11 additions & 7 deletions libraries/modules/src/metro/caches.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import {
FirstAssetTypeRegisteredKey,
IndexMetroModuleId,
MetroCacheKey,
MetroCacheRelativeFilePath,
MetroCacheVersion,
MetroModuleFlags,
MetroModuleLookupFlags,
assetCacheIndexSymbol,
} from '../constants'
import { byProps } from '../filters'
import { findId } from '../finders'
import { CacheModule, ClientInfoModule } from '../native'
import { ClientInfoModule, FileModule } from '../native'
import { logger } from '../shared'
import {
blacklistModule,
Expand Down Expand Up @@ -76,8 +76,10 @@ export async function restoreCache() {
// For testing:
// invalidateCache()

const savedCache = await CacheModule.getItem(MetroCacheKey)
if (!savedCache) return false
const path = `${FileModule.getConstants().CacheDirPath}/${MetroCacheRelativeFilePath}`

if (!(await FileModule.fileExists(path))) return false
const savedCache = await FileModule.readFile(path, 'utf8')

const storedCache = JSON.parse(savedCache) as MetroCacheObject
logger.log(
Expand Down Expand Up @@ -145,8 +147,9 @@ let saveCacheDebounceTimeoutId: number
export function saveCache() {
if (saveCacheDebounceTimeoutId) clearTimeout(saveCacheDebounceTimeoutId)
saveCacheDebounceTimeoutId = setTimeout(() => {
CacheModule.setItem(
MetroCacheKey,
FileModule.writeFile(
'cache',
MetroCacheRelativeFilePath,
JSON.stringify({
v: MetroCacheVersion,
b: ClientInfoModule.Build,
Expand All @@ -156,6 +159,7 @@ export function saveCache() {
a: cache.assetModules,
p: cache.patchableModules,
} satisfies MetroCacheObject),
'utf8',
)

logger.log(`Cache saved (${cache.totalModules} modules)`)
Expand All @@ -164,7 +168,7 @@ export function saveCache() {

/** @internal */
export function invalidateCache() {
CacheModule.removeItem(MetroCacheKey)
FileModule.removeFile('cache', MetroCacheRelativeFilePath)
logger.warn('Cache invalidated')
}

Expand Down
2 changes: 0 additions & 2 deletions libraries/modules/src/metro/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import { initializeModulePatches } from './patches'
import type { Metro } from '../types'

export {
type MetroCacheObject,
type MetroLookupCacheRegistry,
cacheAsset,
cacheModuleAsBlacklisted,
cacherFor,
Expand Down
3 changes: 3 additions & 0 deletions libraries/shared/src/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ const BaseDirectory = 'revenge'

export const SettingsFilePath = `${BaseDirectory}/settings.json`

export const TrustedKeysDirectoryPath = `${BaseDirectory}/trusted_keys`
export const TrustedKeysDataFilePath = `${TrustedKeysDirectoryPath}/data.json`

export const PluginsDirectoryPath = `${BaseDirectory}/plugins`
export const PluginsStatesFilePath = `${PluginsDirectoryPath}/states.json`
export const PluginStoragePath = (id: string) => `${PluginsDirectoryPath}/${id}/storage.json`
5 changes: 1 addition & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ async function initialize() {
const ModulesLibrary = await ModulesLibraryPromise

const [
{
startCorePlugins,
startPluginsMetroModuleSubscriptions: startCorePluginsMetroModuleSubscriptions,
},
{ startCorePlugins, startPluginsMetroModuleSubscriptions: startCorePluginsMetroModuleSubscriptions },
{ awaitStorage },
{ settings, pluginsStates },
] = await Promise.all([
Expand Down
3 changes: 3 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Buffer as _Buffer } from 'buffer'
import type { AppLibrary } from '@revenge-mod/app'
import type { AssetsLibrary } from '@revenge-mod/assets'
import type { DiscordModules, Metro, ModulesLibrary } from '@revenge-mod/modules'
Expand All @@ -9,6 +10,8 @@ import type MessageQueue from 'react-native/Libraries/BatchedBridge/MessageQueue
// All of these typings are exported, be careful what you export here!

declare global {
var Buffer: typeof _Buffer

var nativeModuleProxy: Record<string, unknown>
var modules: Metro.ModuleList
var __r: Metro.RequireFn
Expand Down

0 comments on commit 457a47b

Please sign in to comment.