From aff1c83e94e3e13c05cd307b64cc311b7a9ffa98 Mon Sep 17 00:00:00 2001 From: Beef Date: Wed, 5 Apr 2023 20:40:17 +0100 Subject: [PATCH] [Global] Tidy usage of native modules --- src/def.d.ts | 2 +- src/lib/debug.ts | 21 ++++++++------------- src/lib/native.ts | 11 +++++++++++ src/lib/plugins.ts | 5 ++--- src/lib/storage/backends.ts | 15 ++++++--------- src/lib/storage/index.ts | 6 ++---- src/lib/themes.ts | 3 +-- src/ui/settings/components/ThemeCard.tsx | 5 +++-- src/ui/settings/pages/General.tsx | 3 ++- 9 files changed, 36 insertions(+), 35 deletions(-) create mode 100644 src/lib/native.ts diff --git a/src/def.d.ts b/src/def.d.ts index cd2d1dc..9976131 100644 --- a/src/def.d.ts +++ b/src/def.d.ts @@ -239,7 +239,7 @@ interface MMKVManager { clear: () => void; } -interface DCDFileManager { +interface FileManager { /** * @param path **Full** path to file */ diff --git a/src/lib/debug.ts b/src/lib/debug.ts index 9ba126d..07acd6a 100644 --- a/src/lib/debug.ts +++ b/src/lib/debug.ts @@ -1,6 +1,7 @@ import { RNConstants } from "@types"; import { ReactNative as RN } from "@metro/common"; import { after } from "@lib/patcher"; +import { ClientInfoManager, DeviceManager } from "@lib/native"; import { getAssetIDByName } from "@ui/assets"; import { showToast } from "@ui/toasts"; import logger from "@lib/logger"; @@ -47,12 +48,6 @@ export function patchLogHook() { export const versionHash: string = __vendettaVersion; export function getDebugInfo() { - // Discord - //! 173.13 renamed this to RTNClientInfoManager. - const InfoDictionaryManager = RN.NativeModules.InfoDictionaryManager ?? RN.NativeModules.RTNClientInfoManager; - //! 173.14 renamed this to RTNDeviceManager. - const DCDDeviceManager = RN.NativeModules.DCDDeviceManager ?? RN.NativeModules.RTNDeviceManager; - // Hermes const hermesProps = window.HermesInternal.getRuntimeProperties(); const hermesVer = hermesProps["OSS Release Version"]; @@ -68,8 +63,8 @@ export function getDebugInfo() { loader: window.__vendetta_loader?.name ?? "Unknown", }, discord: { - version: InfoDictionaryManager.Version, - build: InfoDictionaryManager.Build, + version: ClientInfoManager.Version, + build: ClientInfoManager.Build, }, react: { version: React.version, @@ -104,15 +99,15 @@ export function getDebugInfo() { manufacturer: PlatformConstants.Manufacturer, brand: PlatformConstants.Brand, model: PlatformConstants.Model, - codename: DCDDeviceManager.device + codename: DeviceManager.device } }, ios: { device: { - manufacturer: DCDDeviceManager.deviceManufacturer, - brand: DCDDeviceManager.deviceBrand, - model: DCDDeviceManager.deviceModel, - codename: DCDDeviceManager.device + manufacturer: DeviceManager.deviceManufacturer, + brand: DeviceManager.deviceBrand, + model: DeviceManager.deviceModel, + codename: DeviceManager.device } } } diff --git a/src/lib/native.ts b/src/lib/native.ts new file mode 100644 index 0000000..2665bdf --- /dev/null +++ b/src/lib/native.ts @@ -0,0 +1,11 @@ +import { MMKVManager as _MMKVManager, FileManager as _FileManager } from "@types"; +const nmp = window.nativeModuleProxy; + +export const MMKVManager = nmp.MMKVManager as _MMKVManager; +//! 173.10 renamed this to RTNFileManager. +export const FileManager = (nmp.DCDFileManager ?? nmp.RTNFileManager) as _FileManager; +//! 173.13 renamed this to RTNClientInfoManager. +export const ClientInfoManager = nmp.InfoDictionaryManager ?? nmp.RTNClientInfoManager; +//! 173.14 renamed this to RTNDeviceManager. +export const DeviceManager = nmp.DCDDeviceManager ?? nmp.RTNDeviceManager; +export const BundleUpdaterManager = nmp.BundleUpdaterManager; \ No newline at end of file diff --git a/src/lib/plugins.ts b/src/lib/plugins.ts index 0ca7210..538fe08 100644 --- a/src/lib/plugins.ts +++ b/src/lib/plugins.ts @@ -1,10 +1,9 @@ -import { Indexable, PluginManifest, Plugin, MMKVManager } from "@types"; +import { Indexable, PluginManifest, Plugin } from "@types"; import { awaitSyncWrapper, createMMKVBackend, createStorage, wrapSync } from "@lib/storage"; +import { MMKVManager } from "@lib/native"; import logger, { logModule } from "@lib/logger"; import safeFetch from "@utils/safeFetch"; -const MMKVManager = window.nativeModuleProxy.MMKVManager as MMKVManager; - type EvaledPlugin = { onLoad?(): void; onUnload(): void; diff --git a/src/lib/storage/backends.ts b/src/lib/storage/backends.ts index 40af37a..500ad94 100644 --- a/src/lib/storage/backends.ts +++ b/src/lib/storage/backends.ts @@ -1,9 +1,6 @@ -import { DCDFileManager, MMKVManager, StorageBackend } from "@types"; +import { StorageBackend } from "@types"; import { ReactNative as RN } from "@metro/common"; - -const MMKVManager = window.nativeModuleProxy.MMKVManager as MMKVManager; -//! 173.10 renamed this to RTNFileManager. -const DCDFileManager = (window.nativeModuleProxy.DCDFileManager ?? window.nativeModuleProxy.RTNFileManager) as DCDFileManager; +import { MMKVManager, FileManager } from "@lib/native"; export const createMMKVBackend = (store: string): StorageBackend => ({ get: async () => JSON.parse((await MMKVManager.getItem(store)) ?? "{}"), @@ -20,10 +17,10 @@ export const createFileBackend = (file: string): StorageBackend => { let created: boolean; return { get: async () => { - const path = `${DCDFileManager.getConstants().DocumentsDirPath}/${file}`; - if (!created && !(await DCDFileManager.fileExists(path))) return (created = true), DCDFileManager.writeFile("documents", filePathFixer(file), "{}", "utf8"); - return JSON.parse(await DCDFileManager.readFile(path, "utf8")); + const path = `${FileManager.getConstants().DocumentsDirPath}/${file}`; + if (!created && !(await FileManager.fileExists(path))) return (created = true), FileManager.writeFile("documents", filePathFixer(file), "{}", "utf8"); + return JSON.parse(await FileManager.readFile(path, "utf8")); }, - set: (data) => void DCDFileManager.writeFile("documents", filePathFixer(file), JSON.stringify(data), "utf8"), + set: (data) => void FileManager.writeFile("documents", filePathFixer(file), JSON.stringify(data), "utf8"), }; }; \ No newline at end of file diff --git a/src/lib/storage/index.ts b/src/lib/storage/index.ts index adbae25..7e4601a 100644 --- a/src/lib/storage/index.ts +++ b/src/lib/storage/index.ts @@ -1,8 +1,6 @@ -import { Emitter, MMKVManager, StorageBackend } from "@types"; +import { Emitter, StorageBackend } from "@types"; import createEmitter from "../emitter"; -const MMKVManager = window.nativeModuleProxy.MMKVManager as MMKVManager; - const emitterSymbol = Symbol("emitter accessor"); const syncAwaitSymbol = Symbol("wrapSync promise accessor"); @@ -115,4 +113,4 @@ export function wrapSync>(store: T): Awaited { export const awaitSyncWrapper = (store: any) => new Promise((res) => store[syncAwaitSymbol](res)); -export * from "./backends"; +export * from "@lib/storage/backends"; diff --git a/src/lib/themes.ts b/src/lib/themes.ts index 2a3c757..8e3b2e3 100644 --- a/src/lib/themes.ts +++ b/src/lib/themes.ts @@ -1,4 +1,4 @@ -import { DCDFileManager, Indexable, Theme, ThemeData } from "@types"; +import { Indexable, Theme, ThemeData } from "@types"; import { findByProps } from "@metro/filters"; import { ReactNative, chroma } from "@metro/common"; import { instead } from "@lib/patcher"; @@ -9,7 +9,6 @@ import { safeFetch } from "@utils"; // Somehow, this is late enough, though? export const color = findByProps("SemanticColor"); -const DCDFileManager = window.nativeModuleProxy.DCDFileManager as DCDFileManager; export const themes = wrapSync(createStorage>(createMMKVBackend("VENDETTA_THEMES"))); async function writeTheme(theme: Theme | {}) { diff --git a/src/ui/settings/components/ThemeCard.tsx b/src/ui/settings/components/ThemeCard.tsx index 5924644..f6c1111 100644 --- a/src/ui/settings/components/ThemeCard.tsx +++ b/src/ui/settings/components/ThemeCard.tsx @@ -1,6 +1,7 @@ import { ButtonColors, Theme } from "@types"; import { ReactNative as RN, clipboard } from "@metro/common"; import { fetchTheme, removeTheme, selectTheme } from "@lib/themes"; +import { BundleUpdaterManager } from "@lib/native"; import { getAssetIDByName } from "@ui/assets"; import { showConfirmationAlert } from "@ui/alerts"; import { showToast } from "@ui/toasts"; @@ -13,7 +14,7 @@ interface ThemeCardProps { async function selectAndReload(value: boolean, id: string) { await selectTheme(value ? id : "default"); - RN.NativeModules.BundleUpdaterManager.reload(); + BundleUpdaterManager.reload(); } export default function ThemeCard({ theme, index }: ThemeCardProps) { @@ -48,7 +49,7 @@ export default function ThemeCard({ theme, index }: ThemeCardProps) { confirmText: "Reload", cancelText: "Cancel", confirmColor: ButtonColors.RED, - onConfirm: () => RN.NativeModules.BundleUpdaterManager.reload(), + onConfirm: () => BundleUpdaterManager.reload(), }) } else { showToast("Successfully refetched theme.", getAssetIDByName("toast_image_saved")); diff --git a/src/ui/settings/pages/General.tsx b/src/ui/settings/pages/General.tsx index 400f481..7392e03 100644 --- a/src/ui/settings/pages/General.tsx +++ b/src/ui/settings/pages/General.tsx @@ -4,6 +4,7 @@ import { Forms, Summary } from "@ui/components"; import { DISCORD_SERVER, GITHUB } from "@lib/constants"; import { getDebugInfo } from "@lib/debug"; import { useProxy } from "@lib/storage"; +import { BundleUpdaterManager } from "@lib/native"; import settings from "@lib/settings"; import Version from "@ui/settings/components/Version"; import ErrorBoundary from "@ui/components/ErrorBoundary"; @@ -102,7 +103,7 @@ export default function General() { } - onPress={() => RN.NativeModules.BundleUpdaterManager.reload()} + onPress={() => BundleUpdaterManager.reload()} />