From 18270829c04c807896ddc066229b3f69a7bdf6c3 Mon Sep 17 00:00:00 2001 From: pylixonly <82711525+pylixonly@users.noreply.github.com> Date: Thu, 14 Nov 2024 07:57:36 +0800 Subject: [PATCH] refactor(native): use new native module names --- src/core/vendetta/storage.ts | 42 +++++++++---------- .../addons/themes/colors/patches/resolver.ts | 4 +- src/lib/api/debug.ts | 16 +++---- src/lib/api/native/fs.ts | 18 ++++---- src/lib/api/native/modules/index.ts | 11 ++--- src/metro/internals/caches.ts | 10 ++--- 6 files changed, 51 insertions(+), 50 deletions(-) diff --git a/src/core/vendetta/storage.ts b/src/core/vendetta/storage.ts index f49451b..a5ec242 100644 --- a/src/core/vendetta/storage.ts +++ b/src/core/vendetta/storage.ts @@ -1,5 +1,5 @@ import { Emitter, EmitterEvent, EmitterListener, EmitterListenerData } from "@core/vendetta/Emitter"; -import { FileManager, MMKVManager } from "@lib/api/native/modules"; +import { NativeCacheModule, NativeFileModule } from "@lib/api/native/modules"; import { Platform } from "react-native"; const emitterSymbol = Symbol.for("vendetta.storage.emitter"); @@ -156,7 +156,7 @@ const ILLEGAL_CHARS_REGEX = /[<>:"/\\|?*]/g; const filePathFixer = (file: string): string => Platform.select({ default: file, - ios: FileManager.saveFileToGallery ? file : `Documents/${file}`, + ios: NativeFileModule.saveFileToGallery ? file : `Documents/${file}`, }); const getMMKVPath = (name: string): string => { @@ -169,13 +169,13 @@ const getMMKVPath = (name: string): string => { }; export const purgeStorage = async (store: string) => { - if (await MMKVManager.getItem(store)) { - MMKVManager.removeItem(store); + if (await NativeCacheModule.getItem(store)) { + NativeCacheModule.removeItem(store); } const mmkvPath = getMMKVPath(store); - if (await FileManager.fileExists(`${FileManager.getConstants().DocumentsDirPath}/${mmkvPath}`)) { - await FileManager.removeFile?.("documents", mmkvPath); + if (await NativeFileModule.fileExists(`${NativeFileModule.getConstants().DocumentsDirPath}/${mmkvPath}`)) { + await NativeFileModule.removeFile?.("documents", mmkvPath); } }; @@ -184,16 +184,16 @@ export const createMMKVBackend = (store: string, defaultData = {}) => { const defaultStr = JSON.stringify(defaultData); return createFileBackend(mmkvPath, defaultData, (async () => { - const path = `${FileManager.getConstants().DocumentsDirPath}/${mmkvPath}`; - if (await FileManager.fileExists(path)) return; + const path = `${NativeFileModule.getConstants().DocumentsDirPath}/${mmkvPath}`; + if (await NativeFileModule.fileExists(path)) return; - let oldData = await MMKVManager.getItem(store) ?? defaultStr; + let oldData = await NativeCacheModule.getItem(store) ?? defaultStr; // From the testing on Android, it seems to return this if the data is too large if (oldData === "!!LARGE_VALUE!!") { - const cachePath = `${FileManager.getConstants().CacheDirPath}/mmkv/${store}`; - if (await FileManager.fileExists(cachePath)) { - oldData = await FileManager.readFile(cachePath, "utf8"); + const cachePath = `${NativeFileModule.getConstants().CacheDirPath}/mmkv/${store}`; + if (await NativeFileModule.fileExists(cachePath)) { + oldData = await NativeFileModule.readFile(cachePath, "utf8"); } else { console.log(`${store}: Experienced data loss :(`); oldData = defaultStr; @@ -207,9 +207,9 @@ export const createMMKVBackend = (store: string, defaultData = {}) => { oldData = defaultStr; } - await FileManager.writeFile("documents", filePathFixer(mmkvPath), oldData, "utf8"); - if (await MMKVManager.getItem(store) !== null) { - MMKVManager.removeItem(store); + await NativeFileModule.writeFile("documents", filePathFixer(mmkvPath), oldData, "utf8"); + if (await NativeCacheModule.getItem(store) !== null) { + NativeCacheModule.removeItem(store); console.log(`Successfully migrated ${store} store from MMKV storage to fs`); } })()); @@ -219,10 +219,10 @@ export const createFileBackend = (file: string, defaultData = {}, migratePromise return { get: async () => { await migratePromise; - const path = `${FileManager.getConstants().DocumentsDirPath}/${file}`; + const path = `${NativeFileModule.getConstants().DocumentsDirPath}/${file}`; - if (await FileManager.fileExists(path)) { - const content = await FileManager.readFile(path, "utf8"); + if (await NativeFileModule.fileExists(path)) { + const content = await NativeFileModule.readFile(path, "utf8"); try { return JSON.parse(content); } catch { @@ -230,12 +230,12 @@ export const createFileBackend = (file: string, defaultData = {}, migratePromise } } - await FileManager.writeFile("documents", filePathFixer(file), JSON.stringify(defaultData), "utf8"); - return JSON.parse(await FileManager.readFile(path, "utf8")); + await NativeFileModule.writeFile("documents", filePathFixer(file), JSON.stringify(defaultData), "utf8"); + return JSON.parse(await NativeFileModule.readFile(path, "utf8")); }, set: async data => { await migratePromise; - await FileManager.writeFile("documents", filePathFixer(file), JSON.stringify(data), "utf8"); + await NativeFileModule.writeFile("documents", filePathFixer(file), JSON.stringify(data), "utf8"); } }; }; diff --git a/src/lib/addons/themes/colors/patches/resolver.ts b/src/lib/addons/themes/colors/patches/resolver.ts index 949c83d..103f5ce 100644 --- a/src/lib/addons/themes/colors/patches/resolver.ts +++ b/src/lib/addons/themes/colors/patches/resolver.ts @@ -1,5 +1,5 @@ import { _colorRef } from "@lib/addons/themes/colors/updater"; -import { ThemeManager } from "@lib/api/native/modules"; +import { NativeThemeModule } from "@lib/api/native/modules"; import { before, instead } from "@lib/api/patcher"; import { findByProps } from "@metro"; import { byMutableProp } from "@metro/filters"; @@ -39,7 +39,7 @@ export default function patchDefinitionAndResolver() { const unpatches = [ before("isThemeDark", isThemeModule, callback), before("isThemeLight", isThemeModule, callback), - before("updateTheme", ThemeManager, callback), + before("updateTheme", NativeThemeModule, callback), instead("resolveSemanticColor", tokenReference.default.meta ?? tokenReference.default.internal, (args: any[], orig: any) => { if (!_colorRef.current) return orig(...args); if (args[0] !== _colorRef.key) return orig(...args); diff --git a/src/lib/api/debug.ts b/src/lib/api/debug.ts index 05de186..0bdd3f1 100644 --- a/src/lib/api/debug.ts +++ b/src/lib/api/debug.ts @@ -1,7 +1,7 @@ import { getThemeFromLoader, selectTheme, themes } from "@lib/addons/themes"; import { findAssetId } from "@lib/api/assets"; import { getLoaderName, getLoaderVersion, isThemeSupported } from "@lib/api/native/loader"; -import { BundleUpdaterManager, ClientInfoManager, DeviceManager } from "@lib/api/native/modules"; +import { BundleUpdaterManager, NativeClientInfoModule, NativeDeviceModule } from "@lib/api/native/modules"; import { after } from "@lib/api/patcher"; import { settings } from "@lib/api/settings"; import { logger } from "@lib/utils/logger"; @@ -113,8 +113,8 @@ export function getDebugInfo() { } }, discord: { - version: ClientInfoManager.Version, - build: ClientInfoManager.Build, + version: NativeClientInfoModule.Version, + build: NativeClientInfoModule.Build, }, react: { version: React.version, @@ -149,15 +149,15 @@ export function getDebugInfo() { manufacturer: PlatformConstants.Manufacturer, brand: PlatformConstants.Brand, model: PlatformConstants.Model, - codename: DeviceManager.device + codename: NativeDeviceModule.device } }, ios: { device: { - manufacturer: DeviceManager.deviceManufacturer, - brand: DeviceManager.deviceBrand, - model: DeviceManager.deviceModel, - codename: DeviceManager.device + manufacturer: NativeDeviceModule.deviceManufacturer, + brand: NativeDeviceModule.deviceBrand, + model: NativeDeviceModule.deviceModel, + codename: NativeDeviceModule.device } } } diff --git a/src/lib/api/native/fs.ts b/src/lib/api/native/fs.ts index d32b835..ff6e407 100644 --- a/src/lib/api/native/fs.ts +++ b/src/lib/api/native/fs.ts @@ -1,12 +1,12 @@ -import { FileManager } from "./modules"; +import { NativeFileModule } from "./modules"; /** * Removes all files in a directory from the path given * @param path Path to the targeted directory */ export async function clearFolder(path: string, { prefix = "pyoncord/" } = {}) { - if (typeof FileManager.clearFolder !== "function") throw new Error("'fs.clearFolder' is not supported"); - return void await FileManager.clearFolder("documents", `${prefix}${path}`); + if (typeof NativeFileModule.clearFolder !== "function") throw new Error("'fs.clearFolder' is not supported"); + return void await NativeFileModule.clearFolder("documents", `${prefix}${path}`); } /** @@ -14,8 +14,8 @@ export async function clearFolder(path: string, { prefix = "pyoncord/" } = {}) { * @param path Path to the file */ export async function removeFile(path: string, { prefix = "pyoncord/" } = {}) { - if (typeof FileManager.removeFile !== "function") throw new Error("'fs.removeFile' is not supported"); - return void await FileManager.removeFile("documents", `${prefix}${path}`); + if (typeof NativeFileModule.removeFile !== "function") throw new Error("'fs.removeFile' is not supported"); + return void await NativeFileModule.removeFile("documents", `${prefix}${path}`); } /** @@ -23,7 +23,7 @@ export async function removeFile(path: string, { prefix = "pyoncord/" } = {}) { * @param path Path to the file */ export async function fileExists(path: string, { prefix = "pyoncord/" } = {}) { - return await FileManager.fileExists(`${FileManager.getConstants().DocumentsDirPath}/${prefix}${path}`); + return await NativeFileModule.fileExists(`${NativeFileModule.getConstants().DocumentsDirPath}/${prefix}${path}`); } /** @@ -33,7 +33,7 @@ export async function fileExists(path: string, { prefix = "pyoncord/" } = {}) { */ export async function writeFile(path: string, data: string, { prefix = "pyoncord/" } = {}): Promise { if (typeof data !== "string") throw new Error("Argument 'data' must be a string"); - return void await FileManager.writeFile("documents", `${prefix}${path}`, data, "utf8"); + return void await NativeFileModule.writeFile("documents", `${prefix}${path}`, data, "utf8"); } /** @@ -43,7 +43,7 @@ export async function writeFile(path: string, data: string, { prefix = "pyoncord */ export async function readFile(path: string, { prefix = "pyoncord/" } = {}): Promise { try { - return await FileManager.readFile(`${FileManager.getConstants().DocumentsDirPath}/${prefix}${path}`, "utf8"); + return await NativeFileModule.readFile(`${NativeFileModule.getConstants().DocumentsDirPath}/${prefix}${path}`, "utf8"); } catch (err) { throw new Error(`An error occured while writing to '${path}'`, { cause: err }); } @@ -63,5 +63,5 @@ export async function downloadFile(url: string, path: string, { prefix = "pyonco const arrayBuffer = await response.arrayBuffer(); const data = Buffer.from(arrayBuffer).toString("base64"); - return void await FileManager.writeFile("documents", `${prefix}${path}`, data, "base64"); + await NativeFileModule.writeFile("documents", `${prefix}${path}`, data, "base64"); } diff --git a/src/lib/api/native/modules/index.ts b/src/lib/api/native/modules/index.ts index a31dead..fdc2419 100644 --- a/src/lib/api/native/modules/index.ts +++ b/src/lib/api/native/modules/index.ts @@ -2,9 +2,10 @@ import { RNModules } from "./types"; const nmp = window.nativeModuleProxy; -export const MMKVManager = (nmp.NativeCacheModule ?? nmp.MMKVManager) as RNModules.MMKVManager; -export const FileManager = (nmp.NativeFileModule ?? nmp.RTNFileManager ?? nmp.DCDFileManager) as RNModules.FileManager; -export const ClientInfoManager = nmp.NativeClientInfoModule ?? nmp.RTNClientInfoManager ?? nmp.InfoDictionaryManager; -export const DeviceManager = nmp.NativeDeviceModule ?? nmp.RTNDeviceManager ?? nmp.DCDDeviceManager; +export const NativeCacheModule = (nmp.NativeCacheModule ?? nmp.MMKVManager) as RNModules.MMKVManager; +export const NativeFileModule = (nmp.NativeFileModule ?? nmp.RTNFileManager ?? nmp.DCDFileManager) as RNModules.FileManager; +export const NativeClientInfoModule = nmp.NativeClientInfoModule ?? nmp.RTNClientInfoManager ?? nmp.InfoDictionaryManager; +export const NativeDeviceModule = nmp.NativeDeviceModule ?? nmp.RTNDeviceManager ?? nmp.DCDDeviceManager; +export const NativeThemeModule = nmp.NativeThemeModule ?? nmp.RTNThemeManager ?? nmp.DCDTheme; + export const { BundleUpdaterManager } = nmp; -export const ThemeManager = nmp.NativeThemeModule ?? nmp.RTNThemeManager ?? nmp.DCDTheme; diff --git a/src/metro/internals/caches.ts b/src/metro/internals/caches.ts index 66f5df8..c803de7 100644 --- a/src/metro/internals/caches.ts +++ b/src/metro/internals/caches.ts @@ -1,4 +1,4 @@ -import { ClientInfoManager, MMKVManager } from "@lib/api/native/modules"; +import { NativeCacheModule, NativeClientInfoModule } from "@lib/api/native/modules"; import { debounce } from "es-toolkit"; import { ModuleFlags, ModulesMapInternal } from "./enums"; @@ -17,7 +17,7 @@ export const getMetroCache = window.__getMetroCache = () => _metroCache; function buildInitCache() { const cache = { _v: CACHE_VERSION, - _buildNumber: ClientInfoManager.Build as number, + _buildNumber: NativeClientInfoModule.Build as number, _modulesCount: Object.keys(window.modules).length, exportsIndex: {} as Record, findIndex: {} as Record, @@ -40,7 +40,7 @@ function buildInitCache() { // TODO: Store in file system... is a better idea? /** @internal */ export async function initMetroCache() { - const rawCache = await MMKVManager.getItem(BUNNY_METRO_CACHE_KEY); + const rawCache = await NativeCacheModule.getItem(BUNNY_METRO_CACHE_KEY); if (rawCache == null) return void buildInitCache(); try { @@ -49,7 +49,7 @@ export async function initMetroCache() { _metroCache = null!; throw "cache invalidated; cache version outdated"; } - if (_metroCache._buildNumber !== ClientInfoManager.Build) { + if (_metroCache._buildNumber !== NativeClientInfoModule.Build) { _metroCache = null!; throw "cache invalidated; version mismatch"; } @@ -63,7 +63,7 @@ export async function initMetroCache() { } const saveCache = debounce(() => { - MMKVManager.setItem(BUNNY_METRO_CACHE_KEY, JSON.stringify(_metroCache)); + NativeCacheModule.setItem(BUNNY_METRO_CACHE_KEY, JSON.stringify(_metroCache)); }, 1000); function extractExportsFlags(moduleExports: any) {