From df1a6a4a111b7d5b2e6296f9d6f4169ad756ecaa Mon Sep 17 00:00:00 2001 From: Der_Googler <54764558+DerGoogler@users.noreply.github.com> Date: Sat, 24 Feb 2024 16:42:22 +0100 Subject: [PATCH] modconf and modfs improvements --- .../activitys/ModConfPlaygroundActivity.tsx | 6 +- Website/src/activitys/ModFSActivity.tsx | 15 ++++ .../src/components/ConfigureView/index.tsx | 75 ++++++++----------- Website/src/hooks/useModFS.tsx | 4 +- Website/src/hooks/useNativeFileStorage.tsx | 8 +- 5 files changed, 58 insertions(+), 50 deletions(-) diff --git a/Website/src/activitys/ModConfPlaygroundActivity.tsx b/Website/src/activitys/ModConfPlaygroundActivity.tsx index 5cd0605c..665d2c47 100644 --- a/Website/src/activitys/ModConfPlaygroundActivity.tsx +++ b/Website/src/activitys/ModConfPlaygroundActivity.tsx @@ -12,6 +12,8 @@ import editorTheme from "@Util/editorTheme"; import { ConfigureView } from "@Components/ConfigureView"; import { useNativeStorage } from "@Hooks/useNativeStorage"; import { useStrings } from "@Hooks/useStrings"; +import { useNativeFileStorage } from "@Hooks/useNativeFileStorage"; +import { useModFS } from "@Hooks/useModFS"; export interface PlaygroundExtra { title: string; @@ -114,8 +116,8 @@ const editorDidMount = (editor: monacoEditor.editor.IStandaloneCodeEditor, monac const ModConfPlaygroundActivity = () => { const { context, extra } = useActivity(); const { strings } = useStrings(); - - const [description, setDescription] = useNativeStorage("module-configure-playground", extra.defaultText || ""); + const { modFS } = useModFS(); + const [description, setDescription] = useNativeFileStorage(modFS("MODCONF_PLAYGROUND"), extra.defaultText || "", { json: false }); const [errBoundKey, setErrBoundKey] = React.useState(0); const isLargeScreen = useMediaQuery("(min-width:600px)"); diff --git a/Website/src/activitys/ModFSActivity.tsx b/Website/src/activitys/ModFSActivity.tsx index 7777147c..8ca0c17d 100644 --- a/Website/src/activitys/ModFSActivity.tsx +++ b/Website/src/activitys/ModFSActivity.tsx @@ -236,6 +236,21 @@ function ModFSActivity() { ), confKey: "LOCAL_INSTALL", }, + { + text: "ModConf Playground Root", + dialogDesc: ( + <> + + Check the{" "} + + ModConf documentations + {" "} + for more informations! + + + ), + confKey: "MODCONF_PLAYGROUND", + }, ], }, ], diff --git a/Website/src/components/ConfigureView/index.tsx b/Website/src/components/ConfigureView/index.tsx index 30e7d90b..1451a6d4 100644 --- a/Website/src/components/ConfigureView/index.tsx +++ b/Website/src/components/ConfigureView/index.tsx @@ -19,6 +19,8 @@ import ini from "ini"; import yaml from "yaml"; import { useLog } from "@Hooks/native/useLog"; import { extname } from "@Util/extname"; +import { formatString } from "@Util/stringFormat"; +import { Toolbar } from "@Components/onsenui/Toolbar"; function plugin({ types: t }): PluginObj { return { @@ -50,7 +52,7 @@ function parseCode(data: string): string { }); return code as string; } catch (err) { - console.debug(err as any); + console.info((err as Error).message); return ""; } } @@ -73,32 +75,10 @@ const scope = { export const ConfigureView = React.forwardRef((props, ref) => { const { theme } = useTheme(); - const { modFS: modConf } = useModFS(); + const { modFS, _modFS } = useModFS(); const log = useLog(`Config-${props.modid}`); - const format = React.useCallback<(key: K) => ModFS[K]>((key) => modConf(key, { MODID: props.modid }), []); - - const customRequire = React.useCallback((file: string, opt?: any) => { - const isLocalFile = /^[./]/.test(file); - const absolutePath = file; - - if (SuFile.exist(absolutePath)) { - const fileExt = opt && opt.ignoreExt ? extname(absolutePath) : ""; - - if (fileExt === ".json") { - return JSON.parse(SuFile.read(absolutePath)); - } else if (fileExt === ".yaml" || fileExt === ".yml") { - return yaml.parse(SuFile.read(absolutePath)); - } else if (fileExt === ".ini" || fileExt === ".props") { - return ini.parse(SuFile.read(absolutePath)); - } else { - const code = SuFile.read(absolutePath); - return box(code); - } - } else { - return libraries.find((lib) => absolutePath === lib.name)?.__esModule; - } - }, []); + const format = React.useCallback<(key: K) => ModFS[K]>((key) => modFS(key, { MODID: props.modid }), []); const box = React.useCallback( (code: string) => { @@ -121,26 +101,27 @@ export const ConfigureView = React.forwardRef { + const impFile = new SuFile(formatString(file, { MODID: props.modid, ..._modFS })); + + if (impFile.exist()) { + switch (extname(file)) { + case ".json": + return JSON.parse(impFile.read()); + case ".yml": + case ".yaml": + return yaml.parse(impFile.read()); + case ".prop": + case ".ini": + return ini.parse(impFile.read()); + case ".js": + case ".jsx": + return box(impFile.read()); + default: + return impFile.read(); } } else { - log.e(__raw__filename + " not found"); - return undefined; + return libraries.find((lib) => file === lib.name)?.__esModule; } }, ...scope, @@ -156,7 +137,13 @@ export const ConfigureView = React.forwardRef; } else { return ( - + ( + + Error + + )} + >
An error occurred, either there is a syntax mistake or something
); diff --git a/Website/src/hooks/useModFS.tsx b/Website/src/hooks/useModFS.tsx index db11c966..c62d278c 100644 --- a/Website/src/hooks/useModFS.tsx +++ b/Website/src/hooks/useModFS.tsx @@ -44,6 +44,7 @@ export interface ModFS { LOCAL_INSTALL: string; CONFCWD: string; CONFINDEX: string; + MODCONF_PLAYGROUND: string; } export const INITIAL_MOD_CONF: ModFS = { @@ -85,6 +86,7 @@ export const INITIAL_MOD_CONF: ModFS = { EXPLORE_INSTALL: "/system/usr/share/mmrl/bin/mmrl_explore_install_v6", LOCAL_INSTALL: "/system/usr/share/mmrl/bin/mmrl_local_install_v6", CONFINDEX: "/index.jsx", + MODCONF_PLAYGROUND: "/data/adb/mmrl/modconf-playground.jsx", }; export interface ModConfContext { @@ -108,7 +110,7 @@ export const useModFS = () => { }; export const ModFSProvider = (props: React.PropsWithChildren) => { - const [modFS, setModFS] = useNativeFileStorage("/data/adb/mmrl/modfs.v6.json", INITIAL_MOD_CONF); + const [modFS, setModFS] = useNativeFileStorage("/data/adb/mmrl/modfs.v7.json", INITIAL_MOD_CONF); const contextValue = React.useMemo( () => ({ diff --git a/Website/src/hooks/useNativeFileStorage.tsx b/Website/src/hooks/useNativeFileStorage.tsx index 763f1b7f..7d197331 100644 --- a/Website/src/hooks/useNativeFileStorage.tsx +++ b/Website/src/hooks/useNativeFileStorage.tsx @@ -3,9 +3,11 @@ import { useLog } from "./native/useLog"; import { SuFile } from "@Native/SuFile"; import { SetValue, parseJSON } from "./useNativeStorage"; -export function useNativeFileStorage(key: string, initialValue: T): [T, SetValue] { +export function useNativeFileStorage(key: string, initialValue: T, opt: { json: boolean } = { json: true }): [T, SetValue] { const log = useLog("useNativeStorage"); + const { json } = opt; + const file = new SuFile(key); const readValue = useCallback((): T => { @@ -14,7 +16,7 @@ export function useNativeFileStorage(key: string, initialValue: T): [T, SetVa } try { - return file.exist() ? (parseJSON(file.read()) as T) : initialValue; + return file.exist() ? (json ? (parseJSON(file.read()) as T) : (file.read() as T)) : initialValue; } catch (error) { log.w(`Error reading file “${key}”: ${error}`); @@ -31,7 +33,7 @@ export function useNativeFileStorage(key: string, initialValue: T): [T, SetVa try { const newValue = value instanceof Function ? value(storedValue) : value; - file.write(JSON.stringify(newValue)); + file.write(json ? JSON.stringify(newValue) : String(newValue)); setStoredValue(newValue); } catch (error) { log.w(`Error writing file “${key}”: ${error}`);