[Global] Tidy usage of native modules

This commit is contained in:
Beef 2023-04-05 20:40:17 +01:00
parent 10618ae217
commit aff1c83e94
9 changed files with 36 additions and 35 deletions

2
src/def.d.ts vendored
View file

@ -239,7 +239,7 @@ interface MMKVManager {
clear: () => void; clear: () => void;
} }
interface DCDFileManager { interface FileManager {
/** /**
* @param path **Full** path to file * @param path **Full** path to file
*/ */

View file

@ -1,6 +1,7 @@
import { RNConstants } from "@types"; import { RNConstants } from "@types";
import { ReactNative as RN } from "@metro/common"; import { ReactNative as RN } from "@metro/common";
import { after } from "@lib/patcher"; import { after } from "@lib/patcher";
import { ClientInfoManager, DeviceManager } from "@lib/native";
import { getAssetIDByName } from "@ui/assets"; import { getAssetIDByName } from "@ui/assets";
import { showToast } from "@ui/toasts"; import { showToast } from "@ui/toasts";
import logger from "@lib/logger"; import logger from "@lib/logger";
@ -47,12 +48,6 @@ export function patchLogHook() {
export const versionHash: string = __vendettaVersion; export const versionHash: string = __vendettaVersion;
export function getDebugInfo() { 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 // Hermes
const hermesProps = window.HermesInternal.getRuntimeProperties(); const hermesProps = window.HermesInternal.getRuntimeProperties();
const hermesVer = hermesProps["OSS Release Version"]; const hermesVer = hermesProps["OSS Release Version"];
@ -68,8 +63,8 @@ export function getDebugInfo() {
loader: window.__vendetta_loader?.name ?? "Unknown", loader: window.__vendetta_loader?.name ?? "Unknown",
}, },
discord: { discord: {
version: InfoDictionaryManager.Version, version: ClientInfoManager.Version,
build: InfoDictionaryManager.Build, build: ClientInfoManager.Build,
}, },
react: { react: {
version: React.version, version: React.version,
@ -104,15 +99,15 @@ export function getDebugInfo() {
manufacturer: PlatformConstants.Manufacturer, manufacturer: PlatformConstants.Manufacturer,
brand: PlatformConstants.Brand, brand: PlatformConstants.Brand,
model: PlatformConstants.Model, model: PlatformConstants.Model,
codename: DCDDeviceManager.device codename: DeviceManager.device
} }
}, },
ios: { ios: {
device: { device: {
manufacturer: DCDDeviceManager.deviceManufacturer, manufacturer: DeviceManager.deviceManufacturer,
brand: DCDDeviceManager.deviceBrand, brand: DeviceManager.deviceBrand,
model: DCDDeviceManager.deviceModel, model: DeviceManager.deviceModel,
codename: DCDDeviceManager.device codename: DeviceManager.device
} }
} }
} }

11
src/lib/native.ts Normal file
View file

@ -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;

View file

@ -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 { awaitSyncWrapper, createMMKVBackend, createStorage, wrapSync } from "@lib/storage";
import { MMKVManager } from "@lib/native";
import logger, { logModule } from "@lib/logger"; import logger, { logModule } from "@lib/logger";
import safeFetch from "@utils/safeFetch"; import safeFetch from "@utils/safeFetch";
const MMKVManager = window.nativeModuleProxy.MMKVManager as MMKVManager;
type EvaledPlugin = { type EvaledPlugin = {
onLoad?(): void; onLoad?(): void;
onUnload(): void; onUnload(): void;

View file

@ -1,9 +1,6 @@
import { DCDFileManager, MMKVManager, StorageBackend } from "@types"; import { StorageBackend } from "@types";
import { ReactNative as RN } from "@metro/common"; import { ReactNative as RN } from "@metro/common";
import { MMKVManager, FileManager } from "@lib/native";
const MMKVManager = window.nativeModuleProxy.MMKVManager as MMKVManager;
//! 173.10 renamed this to RTNFileManager.
const DCDFileManager = (window.nativeModuleProxy.DCDFileManager ?? window.nativeModuleProxy.RTNFileManager) as DCDFileManager;
export const createMMKVBackend = (store: string): StorageBackend => ({ export const createMMKVBackend = (store: string): StorageBackend => ({
get: async () => JSON.parse((await MMKVManager.getItem(store)) ?? "{}"), get: async () => JSON.parse((await MMKVManager.getItem(store)) ?? "{}"),
@ -20,10 +17,10 @@ export const createFileBackend = (file: string): StorageBackend => {
let created: boolean; let created: boolean;
return { return {
get: async () => { get: async () => {
const path = `${DCDFileManager.getConstants().DocumentsDirPath}/${file}`; const path = `${FileManager.getConstants().DocumentsDirPath}/${file}`;
if (!created && !(await DCDFileManager.fileExists(path))) return (created = true), DCDFileManager.writeFile("documents", filePathFixer(file), "{}", "utf8"); if (!created && !(await FileManager.fileExists(path))) return (created = true), FileManager.writeFile("documents", filePathFixer(file), "{}", "utf8");
return JSON.parse(await DCDFileManager.readFile(path, "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"),
}; };
}; };

View file

@ -1,8 +1,6 @@
import { Emitter, MMKVManager, StorageBackend } from "@types"; import { Emitter, StorageBackend } from "@types";
import createEmitter from "../emitter"; import createEmitter from "../emitter";
const MMKVManager = window.nativeModuleProxy.MMKVManager as MMKVManager;
const emitterSymbol = Symbol("emitter accessor"); const emitterSymbol = Symbol("emitter accessor");
const syncAwaitSymbol = Symbol("wrapSync promise accessor"); const syncAwaitSymbol = Symbol("wrapSync promise accessor");
@ -115,4 +113,4 @@ export function wrapSync<T extends Promise<any>>(store: T): Awaited<T> {
export const awaitSyncWrapper = (store: any) => new Promise<void>((res) => store[syncAwaitSymbol](res)); export const awaitSyncWrapper = (store: any) => new Promise<void>((res) => store[syncAwaitSymbol](res));
export * from "./backends"; export * from "@lib/storage/backends";

View file

@ -1,4 +1,4 @@
import { DCDFileManager, Indexable, Theme, ThemeData } from "@types"; import { Indexable, Theme, ThemeData } from "@types";
import { findByProps } from "@metro/filters"; import { findByProps } from "@metro/filters";
import { ReactNative, chroma } from "@metro/common"; import { ReactNative, chroma } from "@metro/common";
import { instead } from "@lib/patcher"; import { instead } from "@lib/patcher";
@ -9,7 +9,6 @@ import { safeFetch } from "@utils";
// Somehow, this is late enough, though? // Somehow, this is late enough, though?
export const color = findByProps("SemanticColor"); export const color = findByProps("SemanticColor");
const DCDFileManager = window.nativeModuleProxy.DCDFileManager as DCDFileManager;
export const themes = wrapSync(createStorage<Indexable<Theme>>(createMMKVBackend("VENDETTA_THEMES"))); export const themes = wrapSync(createStorage<Indexable<Theme>>(createMMKVBackend("VENDETTA_THEMES")));
async function writeTheme(theme: Theme | {}) { async function writeTheme(theme: Theme | {}) {

View file

@ -1,6 +1,7 @@
import { ButtonColors, Theme } from "@types"; import { ButtonColors, Theme } from "@types";
import { ReactNative as RN, clipboard } from "@metro/common"; import { ReactNative as RN, clipboard } from "@metro/common";
import { fetchTheme, removeTheme, selectTheme } from "@lib/themes"; import { fetchTheme, removeTheme, selectTheme } from "@lib/themes";
import { BundleUpdaterManager } from "@lib/native";
import { getAssetIDByName } from "@ui/assets"; import { getAssetIDByName } from "@ui/assets";
import { showConfirmationAlert } from "@ui/alerts"; import { showConfirmationAlert } from "@ui/alerts";
import { showToast } from "@ui/toasts"; import { showToast } from "@ui/toasts";
@ -13,7 +14,7 @@ interface ThemeCardProps {
async function selectAndReload(value: boolean, id: string) { async function selectAndReload(value: boolean, id: string) {
await selectTheme(value ? id : "default"); await selectTheme(value ? id : "default");
RN.NativeModules.BundleUpdaterManager.reload(); BundleUpdaterManager.reload();
} }
export default function ThemeCard({ theme, index }: ThemeCardProps) { export default function ThemeCard({ theme, index }: ThemeCardProps) {
@ -48,7 +49,7 @@ export default function ThemeCard({ theme, index }: ThemeCardProps) {
confirmText: "Reload", confirmText: "Reload",
cancelText: "Cancel", cancelText: "Cancel",
confirmColor: ButtonColors.RED, confirmColor: ButtonColors.RED,
onConfirm: () => RN.NativeModules.BundleUpdaterManager.reload(), onConfirm: () => BundleUpdaterManager.reload(),
}) })
} else { } else {
showToast("Successfully refetched theme.", getAssetIDByName("toast_image_saved")); showToast("Successfully refetched theme.", getAssetIDByName("toast_image_saved"));

View file

@ -4,6 +4,7 @@ import { Forms, Summary } from "@ui/components";
import { DISCORD_SERVER, GITHUB } from "@lib/constants"; import { DISCORD_SERVER, GITHUB } from "@lib/constants";
import { getDebugInfo } from "@lib/debug"; import { getDebugInfo } from "@lib/debug";
import { useProxy } from "@lib/storage"; import { useProxy } from "@lib/storage";
import { BundleUpdaterManager } from "@lib/native";
import settings from "@lib/settings"; import settings from "@lib/settings";
import Version from "@ui/settings/components/Version"; import Version from "@ui/settings/components/Version";
import ErrorBoundary from "@ui/components/ErrorBoundary"; import ErrorBoundary from "@ui/components/ErrorBoundary";
@ -102,7 +103,7 @@ export default function General() {
<FormRow <FormRow
label="Reload Discord" label="Reload Discord"
leading={<FormRow.Icon source={getAssetIDByName("ic_message_retry")} />} leading={<FormRow.Icon source={getAssetIDByName("ic_message_retry")} />}
onPress={() => RN.NativeModules.BundleUpdaterManager.reload()} onPress={() => BundleUpdaterManager.reload()}
/> />
<FormDivider /> <FormDivider />
<FormSwitchRow <FormSwitchRow