Skip to content

Commit

Permalink
modconf and modfs improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
DerGoogler committed Feb 24, 2024
1 parent f8a4fab commit df1a6a4
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 50 deletions.
6 changes: 4 additions & 2 deletions Website/src/activitys/ModConfPlaygroundActivity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -114,8 +116,8 @@ const editorDidMount = (editor: monacoEditor.editor.IStandaloneCodeEditor, monac
const ModConfPlaygroundActivity = () => {
const { context, extra } = useActivity<PlaygroundExtra>();
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)");
Expand Down
15 changes: 15 additions & 0 deletions Website/src/activitys/ModFSActivity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,21 @@ function ModFSActivity() {
),
confKey: "LOCAL_INSTALL",
},
{
text: "ModConf Playground Root",
dialogDesc: (
<>
<Typography>
Check the{" "}
<Anchor href="https://github.com/DerGoogler/MMRL/tree/master/docs" noIcon>
ModConf documentations
</Anchor>{" "}
for more informations!
</Typography>
</>
),
confKey: "MODCONF_PLAYGROUND",
},
],
},
],
Expand Down
75 changes: 31 additions & 44 deletions Website/src/components/ConfigureView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 "";
}
}
Expand All @@ -73,32 +75,10 @@ const scope = {

export const ConfigureView = React.forwardRef<any, { children: string; modid: string }>((props, ref) => {
const { theme } = useTheme();
const { modFS: modConf } = useModFS();
const { modFS, _modFS } = useModFS();

const log = useLog(`Config-${props.modid}`);
const format = React.useCallback<<K extends keyof ModFS>(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<<K extends keyof ModFS>(key: K) => ModFS[K]>((key) => modFS(key, { MODID: props.modid }), []);

const box = React.useCallback(
(code: string) => {
Expand All @@ -121,26 +101,27 @@ export const ConfigureView = React.forwardRef<any, { children: string; modid: st
});
},
},
document: document,
require: customRequire,
include(file: string, opt?: { ignoreCwd: boolean }) {
const __raw__filename = !opt?.ignoreCwd ? `${format("CONFCWD")}/${file}` : file;
const __file = new SuFile(__raw__filename);
if (__file.exist()) {
if (__raw__filename.endsWith(".jsx") || __raw__filename.endsWith(".js")) {
return box(__file.read());
} else if (__raw__filename.endsWith(".yaml") || __raw__filename.endsWith(".yml")) {
return yaml.parse(__file.read());
} else if (__raw__filename.endsWith(".json")) {
return JSON.parse(__file.read());
} else if (__raw__filename.endsWith(".prop") || __raw__filename.endsWith(".properties") || __raw__filename.endsWith(".ini")) {
return ini.parse(__file.read());
} else {
return __file.read();
require: (file: string) => {
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,
Expand All @@ -156,7 +137,13 @@ export const ConfigureView = React.forwardRef<any, { children: string; modid: st
return <Component />;
} else {
return (
<Page>
<Page
renderToolbar={() => (
<Toolbar>
<Toolbar.Center>Error</Toolbar.Center>
</Toolbar>
)}
>
<div>An error occurred, either there is a syntax mistake or something</div>
</Page>
);
Expand Down
4 changes: 3 additions & 1 deletion Website/src/hooks/useModFS.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface ModFS {
LOCAL_INSTALL: string;
CONFCWD: string;
CONFINDEX: string;
MODCONF_PLAYGROUND: string;
}

export const INITIAL_MOD_CONF: ModFS = {
Expand Down Expand Up @@ -85,6 +86,7 @@ export const INITIAL_MOD_CONF: ModFS = {
EXPLORE_INSTALL: "<MMRLINI>/system/usr/share/mmrl/bin/mmrl_explore_install_v6",
LOCAL_INSTALL: "<MMRLINI>/system/usr/share/mmrl/bin/mmrl_local_install_v6",
CONFINDEX: "<CONFCWD>/index.jsx",
MODCONF_PLAYGROUND: "/data/adb/mmrl/modconf-playground.jsx",
};

export interface ModConfContext {
Expand All @@ -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(
() => ({
Expand Down
8 changes: 5 additions & 3 deletions Website/src/hooks/useNativeFileStorage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { useLog } from "./native/useLog";
import { SuFile } from "@Native/SuFile";
import { SetValue, parseJSON } from "./useNativeStorage";

export function useNativeFileStorage<T>(key: string, initialValue: T): [T, SetValue<T>] {
export function useNativeFileStorage<T>(key: string, initialValue: T, opt: { json: boolean } = { json: true }): [T, SetValue<T>] {
const log = useLog("useNativeStorage");

const { json } = opt;

const file = new SuFile(key);

const readValue = useCallback((): T => {
Expand All @@ -14,7 +16,7 @@ export function useNativeFileStorage<T>(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}`);

Expand All @@ -31,7 +33,7 @@ export function useNativeFileStorage<T>(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}`);
Expand Down

0 comments on commit df1a6a4

Please sign in to comment.