Skip to content

Commit

Permalink
finally
Browse files Browse the repository at this point in the history
  • Loading branch information
DerGoogler committed Aug 1, 2024
1 parent 97b27da commit bf04a0e
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 237 deletions.
50 changes: 0 additions & 50 deletions Website/src/activitys/ModConfActivity.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { useLog } from "@Hooks/native/useLog";
import { ModFS, useModFS } from "@Hooks/useModFS";
import { IsolatedEval, IsolatedEvalOptions } from "@Native/IsolatedEval";
import { os } from "@Native/Os";
import * as React from "react";
import { libraries } from "./libs";

interface ModConfViewProps {
children: string;
modid: string;
indexFile: string;
cwd: string;
standaloneFile?: string;
}

export const ModConfView = (props: ModConfViewProps) => {
const { modFS } = useModFS();

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

const internalFetch = React.useCallback(
(input: string | URL | Request, init?: RequestInit | undefined) => {
return fetch(input, init);
},
[modid]
);

const isoEval = React.useMemo(() => {
const options: IsolatedEvalOptions = {
libraries: libraries,
indexFile: props.indexFile,
cwd: props.cwd,
scope: {
log: log,
__idname: modid,
__filename: props.indexFile,
__dirname: props.cwd,
__modpath: format("MODULECWD"),
window: {
fetch: internalFetch,
open: os.openURL,
},
fetch: internalFetch,

// @deprecated
modid: modid,
modpath: (path: string) => `${format("MODULECWD")}/${path}`,
confpath: (path: string) => `${format("CONFCWD")}/${path}`,
include: (modulePath: string, opt: { isolate: boolean } = { isolate: false }) => {
if (opt.isolate) {
modulePath = `${format("CONFCWD")}/${modulePath}`;
}

return isoEval.require(modulePath);
},
},
standaloneFile: props.standaloneFile,
};

return new IsolatedEval<React.FunctionComponent<any>>(options);
}, [children, modid]);

const box = React.useCallback((code: string) => isoEval.compileTransform(code), []);

const Component = box(children as string);

if (Component.exports.default) {
return <Component.exports.default />;
}

return <></>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { Markup } from "@Components/Markdown";
import { DialogEditTextListItem } from "@Components/DialogEditTextListItem";
import { SearchActivity } from "@Activitys/SearchActivity";
import React from "react";
import { withRequireNewVersion } from "../../hoc/withRequireNewVersion";
import { withRequireNewVersion } from "../../../../hoc/withRequireNewVersion";
import { CodeBlock } from "@Components/CodeBlock";
import { VerifiedIcon } from "@Components/icons/VerifiedIcon";
import { IsolatedFunctionBlockError } from "@Native/IsolatedEval/IsolatedFunctionBlockError";
Expand Down
40 changes: 40 additions & 0 deletions Website/src/activitys/ModConfActivity/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { useActivity } from "@Hooks/useActivity";
import React from "react";
import { SuFile } from "@Native/SuFile";
import { ModConfView } from "@Activitys/ModConfActivity/components/ModConfView";
import { PreviewErrorBoundary } from "../ModConfPlaygroundActivity";

interface ModConfActivityExtra {
/**
* ## This field is required by ModConf
*/
indexFile: string;
/**
* ## This field is required by ModConf
*/
cwd: string;
modId: string;
}

const ModConfActivity = () => {
const { extra } = useActivity<ModConfActivityExtra>();

const config: string = React.useMemo(() => {
const notFound = 'import {Page} from "@mmrl/ui";export default () => <Page>Config file not found</Page>';

const file = new SuFile(extra.indexFile);
if (file.exist()) {
return file.read();
} else {
return notFound;
}
}, []);

return (
<PreviewErrorBoundary>
<ModConfView cwd={extra.cwd} indexFile={extra.indexFile} modid={extra.modId} children={config} />
</PreviewErrorBoundary>
);
};

export { ModConfActivity, ModConfActivityExtra };
11 changes: 9 additions & 2 deletions Website/src/activitys/ModConfPlaygroundActivity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as monacoEditor from "monaco-editor/esm/vs/editor/editor.api";
import Editor, { Monaco } from "@monaco-editor/react";
import { ErrorBoundaryProps, ErrorBoundaryState, errorBoundaryInitialState } from "@Components/ErrorBoundary";
import editorTheme from "@Util/editorTheme";
import { ModConfView } from "@Components/ModConfView";
import { ModConfView } from "@Activitys/ModConfActivity/components/ModConfView";
import { useNativeStorage } from "@Hooks/useNativeStorage";
import { useStrings } from "@Hooks/useStrings";
import { useNativeFileStorage } from "@Hooks/useNativeFileStorage";
Expand Down Expand Up @@ -156,6 +156,8 @@ const ModConfPlaygroundActivity = () => {
modulename: "Preview",
},
props: {
cwd: modFS("MMRLFOL"),
indexFile: modFS("MODCONF_PLAYGROUND"),
modid: modFS("MODCONF_PLAYGROUND_MODID"),
children: description,
},
Expand Down Expand Up @@ -229,7 +231,12 @@ const ModConfPlaygroundActivity = () => {
<Preview>
<Box id="ModConf-Container" component="section" sx={{ position: "relative", width: "100%", height: "100%" }}>
<PreviewErrorBoundary key={"preview_error_bound_key_" + errBoundKey}>
<ModConfView modid={modFS("MODCONF_PLAYGROUND_MODID")} children={description} />
<ModConfView
cwd={modFS("MMRLFOL")}
indexFile={modFS("MODCONF_PLAYGROUND")}
modid={modFS("MODCONF_PLAYGROUND_MODID")}
children={description}
/>
</PreviewErrorBoundary>
</Box>
</Preview>
Expand Down
22 changes: 12 additions & 10 deletions Website/src/activitys/ModConfStandaloneActivity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import { SuFile } from "@Native/SuFile";
import { SuZip } from "@Native/SuZip";
import { path } from "@Util/path";
import { ModConfActivity, ModConfActivityExtra } from "./ModConfActivity";
import ModFS from "modfs";

const ModConfStandaloneActivity = () => {
const { context } = useActivity();
const { modFS } = useModFS();
const { modFS, _modFS } = useModFS();

const renderToolbar = () => {
return (
Expand All @@ -34,15 +35,13 @@ const ModConfStandaloneActivity = () => {
<Page.RelativeContent>
<List>
{mcalone.list().map((item) => {
if (path.extname(item) !== ".zip") return null;

const zipFile = modFS("MCALONE") + "/" + item;
const confFile = new SuFile(modFS("MCALONEMETA", { MODID: item }));
if (!confFile.exist()) return null;

try {
const fs = new SuZip(zipFile, "modconf.json");
const metaData = JSON.parse(fs.read());
const metaData = JSON.parse(confFile.read());

if (!(metaData.id && metaData.index)) return null;
if (!metaData.id && metaData.id !== item) return null;

return (
<>
Expand All @@ -53,9 +52,12 @@ const ModConfStandaloneActivity = () => {
component: ModConfActivity,
key: `${metaData.id}_configure_standalone`,
extra: {
standaloneFile: fs.getZipPath(),
index: metaData.index,
cwd: "/",
indexFile: metaData.main
? ModFS.format(metaData.main, { MODID: item, ..._modFS })
: modFS("MCALONEIDX", { MODID: item }),
cwd: metaData.cwd
? ModFS.format(metaData.cwd, { MODID: item, ..._modFS })
: modFS("MCALONECWD", { MODID: item }),
modId: metaData.id,
},
});
Expand Down
2 changes: 1 addition & 1 deletion Website/src/activitys/fragments/DrawerFragment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { IntentPusher } from "@Hooks/useActivity";
import FetchTextActivity, { FetchTextActivityExtra } from "@Activitys/FetchTextActivity";
import AboutActivity from "@Activitys/AboutActivity";
import PlaygroundsActivity, { PlaygroundExtra } from "@Activitys/PlaygroundsActivity";
import { ModConfView } from "@Components/ModConfView";
import { ModConfView } from "@Activitys/ModConfActivity/components/ModConfView";
import { Markup } from "@Components/Markdown";
import { configureSample } from "@Util/configure-sample";
import { dapiSample } from "@Util/dapi-sample";
Expand Down
74 changes: 0 additions & 74 deletions Website/src/components/ModConfView/index.tsx

This file was deleted.

6 changes: 4 additions & 2 deletions Website/src/components/module/DeviceModule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Chip from "@mui/material/Chip";
import Divider from "@mui/material/Divider";
import Button from "@mui/material/Button";

import { ModConfActivity } from "@Activitys/ModConfActivity";
import { ModConfActivity, ModConfActivityExtra } from "@Activitys/ModConfActivity";

import { Delete, Settings, RefreshRounded, Loop } from "@mui/icons-material";

Expand Down Expand Up @@ -183,10 +183,12 @@ const DeviceModule = React.memo<Props>((props) => {
)}
<Button
onClick={() => {
context.pushPage({
context.pushPage<ModConfActivityExtra, any>({
component: ModConfActivity,
key: `${id}_configure`,
extra: {
indexFile: format("CONFINDEX"),
cwd: format("CONFCWD"),
modId: id,
},
});
Expand Down
10 changes: 7 additions & 3 deletions Website/src/hooks/useModFS.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ export interface ModFS {

// modconf standalone
MCALONE: string;
MCALONEFILE: string;
MCALONECWD: string;
MCALONEMETA: string;
MCALONEIDX: string;

// Installer
EXPLORE_INSTALL: string;
Expand Down Expand Up @@ -98,7 +100,9 @@ export const INITIAL_MOD_CONF: ModFS = {

// modconf standalone
MCALONE: "<MMRLFOL>/modconf",
MCALONEFILE: "<MCALONE>/<MODID>.zip",
MCALONECWD: "<MCALONE>/<MODID>",
MCALONEMETA: "<MCALONECWD>/modconf.json",
MCALONEIDX: "<MCALONECWD>/index.jsx",

// Installer
EXPLORE_INSTALL: 'mmrl install -y "<URL>"',
Expand Down Expand Up @@ -126,7 +130,7 @@ export const useModFS = () => {
};

export const ModFSProvider = (props: React.PropsWithChildren) => {
const [modFS, setModFS] = useNativeFileStorage("/data/adb/mmrl/modfs.json", INITIAL_MOD_CONF, { loader: "json" });
const [modFS, setModFS] = useNativeFileStorage("/data/adb/mmrl/modfs.v2.json", INITIAL_MOD_CONF, { loader: "json" });

const pmodFS = React.useMemo(() => new PModFS(defaultComposer(INITIAL_MOD_CONF, modFS)), [modFS]);

Expand Down
Loading

0 comments on commit bf04a0e

Please sign in to comment.