From 457a47b33d8cd9c4414659b8cb1dd2306072bc11 Mon Sep 17 00:00:00 2001 From: PalmDevs Date: Fri, 27 Dec 2024 01:35:06 +0700 Subject: [PATCH] feat: export more modules and icons, save cache in fs, improve typings --- .../modules/src/common/components/icons.ts | 3 ++- libraries/modules/src/common/index.ts | 5 ++++- libraries/modules/src/constants.ts | 10 +++++----- libraries/modules/src/metro/caches.ts | 18 +++++++++++------- libraries/modules/src/metro/index.ts | 2 -- libraries/shared/src/paths.ts | 3 +++ src/index.ts | 5 +---- types.d.ts | 3 +++ 8 files changed, 29 insertions(+), 20 deletions(-) diff --git a/libraries/modules/src/common/components/icons.ts b/libraries/modules/src/common/components/icons.ts index 664e4ef..c7baf04 100644 --- a/libraries/modules/src/common/components/icons.ts +++ b/libraries/modules/src/common/components/icons.ts @@ -15,5 +15,6 @@ export type IconComponentProps = { color?: string } -export const CopyIcon = wrapIcon(findProp('CopyIcon')!) export const CheckmarkLargeIcon = wrapIcon(findProp('CheckmarkLargeIcon')!) +export const CopyIcon = wrapIcon(findProp('CopyIcon')!) +export const FolderIcon = wrapIcon(findProp('FolderIcon')!) diff --git a/libraries/modules/src/common/index.ts b/libraries/modules/src/common/index.ts index b90af69..7818324 100644 --- a/libraries/modules/src/common/index.ts +++ b/libraries/modules/src/common/index.ts @@ -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' @@ -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 } @@ -100,3 +101,5 @@ export const semver = findByProps('SEMVER_SPEC_VERSION' export const xxhash64 = findByProps('XXH64')! export const nobleHashesUtils = findByProps('randomBytes')! export * from 'events' + +export const _ = findByProps('cloneDeep')! diff --git a/libraries/modules/src/constants.ts b/libraries/modules/src/constants.ts index 27b3c2e..5f5da27 100644 --- a/libraries/modules/src/constants.ts +++ b/libraries/modules/src/constants.ts @@ -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 diff --git a/libraries/modules/src/metro/caches.ts b/libraries/modules/src/metro/caches.ts index a271cf9..0f00960 100644 --- a/libraries/modules/src/metro/caches.ts +++ b/libraries/modules/src/metro/caches.ts @@ -1,7 +1,7 @@ import { FirstAssetTypeRegisteredKey, IndexMetroModuleId, - MetroCacheKey, + MetroCacheRelativeFilePath, MetroCacheVersion, MetroModuleFlags, MetroModuleLookupFlags, @@ -9,7 +9,7 @@ import { } 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, @@ -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( @@ -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, @@ -156,6 +159,7 @@ export function saveCache() { a: cache.assetModules, p: cache.patchableModules, } satisfies MetroCacheObject), + 'utf8', ) logger.log(`Cache saved (${cache.totalModules} modules)`) @@ -164,7 +168,7 @@ export function saveCache() { /** @internal */ export function invalidateCache() { - CacheModule.removeItem(MetroCacheKey) + FileModule.removeFile('cache', MetroCacheRelativeFilePath) logger.warn('Cache invalidated') } diff --git a/libraries/modules/src/metro/index.ts b/libraries/modules/src/metro/index.ts index afd3af0..053d370 100644 --- a/libraries/modules/src/metro/index.ts +++ b/libraries/modules/src/metro/index.ts @@ -21,8 +21,6 @@ import { initializeModulePatches } from './patches' import type { Metro } from '../types' export { - type MetroCacheObject, - type MetroLookupCacheRegistry, cacheAsset, cacheModuleAsBlacklisted, cacherFor, diff --git a/libraries/shared/src/paths.ts b/libraries/shared/src/paths.ts index 036f500..f1630a1 100644 --- a/libraries/shared/src/paths.ts +++ b/libraries/shared/src/paths.ts @@ -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` diff --git a/src/index.ts b/src/index.ts index 70c2055..74b3b50 100644 --- a/src/index.ts +++ b/src/index.ts @@ -35,10 +35,7 @@ async function initialize() { const ModulesLibrary = await ModulesLibraryPromise const [ - { - startCorePlugins, - startPluginsMetroModuleSubscriptions: startCorePluginsMetroModuleSubscriptions, - }, + { startCorePlugins, startPluginsMetroModuleSubscriptions: startCorePluginsMetroModuleSubscriptions }, { awaitStorage }, { settings, pluginsStates }, ] = await Promise.all([ diff --git a/types.d.ts b/types.d.ts index cc92569..e64171b 100644 --- a/types.d.ts +++ b/types.d.ts @@ -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' @@ -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 var modules: Metro.ModuleList var __r: Metro.RequireFn