Basic mod manager

This commit is contained in:
2023-10-05 12:55:50 -06:00
parent 5779cd9efc
commit 1e9868348d
5 changed files with 185 additions and 37 deletions

View File

@@ -1,5 +1,6 @@
import { AppProps } from "$fresh/server.ts";
import { Nav } from "../components/nav/index.tsx";
import { StatusManager } from "../islands/statusManager.tsx";
export default function App({ Component }: AppProps) {
return (
@@ -44,6 +45,10 @@ export default function App({ Component }: AppProps) {
<Component />
</div>
</div>
<div class="fixed bottom-0 right-0 rounded-tl-3xl border-t-4 border-l-4 border-sky px-4 py-2 bg-smoke-600">
<StatusManager />
</div>
</body>
</html>
);

View File

@@ -2,34 +2,158 @@ import { FunctionComponent } from "preact";
import { Content } from "../../components/Content.tsx";
import { SERVER_STATE } from "../../state/serverState.ts";
import { FileUploader } from "../../islands/fileUploader.tsx";
import { Handlers, PageProps } from "$fresh/server.ts";
import { ensureFile } from "$std/fs/ensure_file.ts";
import { ensureDir } from "$std/fs/ensure_dir.ts";
import { Button } from "../../components/Button.tsx";
export default async function ModsFolder() {
const files: string[] = [];
export const handler: Handlers = {
async POST(req, _ctx) {
const formData = await req.formData();
const filePath = formData.get("filePath") as string;
if (typeof filePath !== "string") {
throw "File path not included in mod enable/disable";
}
if (formData.get("delete")) {
await Deno.remove(filePath);
return Response.redirect(req.url);
}
const fileName = filePath.split("/").at(-1);
if (!fileName) throw "Unable to infer filename in mod enable/disable";
try {
await Deno.lstat(filePath);
} catch {
return Response.redirect(req.url);
}
if (filePath.includes("disabled")) {
await ensureFile("./server/mods/" + fileName);
await Deno.rename(filePath, "./server/mods/" + fileName);
} else {
await ensureFile("./server/disabled-mods/" + fileName);
await Deno.rename(filePath, "./server/disabled-mods/" + fileName);
}
return Response.redirect(req.url);
},
};
export default async function ModsFolder({ url }: PageProps) {
const activeMods: string[] = [];
const disabledMods: string[] = [];
if (
SERVER_STATE.serverType !== "unset" && SERVER_STATE.serverType !== "vanilla"
) {
for await (const fileEntry of Deno.readDir("./server/mods")) {
if (fileEntry.isFile) {
files.push(fileEntry.name);
activeMods.push(fileEntry.name);
}
}
ensureDir("./server/disabled-mods");
for await (const fileEntry of Deno.readDir("./server/disabled-mods")) {
if (fileEntry.isFile) {
disabledMods.push(fileEntry.name);
}
}
}
return (
<div className="container p-8">
<div className="container p-8 flex flex-col gap-8">
<Content>
<h2 class="font-pixel text-xl">Active Mods</h2>
<FileUploader path="./server/mods">
<div className="relative grid lg:grid-cols-3 min-h-[100px]">
{!files.length && (
<div class="absolute place-self-center">Drop files here to upload</div>
)}
{files.map((f) => (
<div class="flex gap-2 items-center">
<FileIcon fileName={f} />
{f}
</div>
))}
<div class="min-h-[100px]">
<div className="relative grid lg:grid-cols-3 gap-8">
{!activeMods.length && (
<div class="absolute place-self-center">
Drop files here to upload
</div>
)}
{activeMods.map((f) => (
<div class="flex justify-between">
<div class="flex gap-2 items-center">
<FileIcon fileName={f} />
{f}
</div>
<div class="flex gap-2">
<form action={url.pathname} method="POST">
<input
type="hidden"
name="filePath"
value={"./server/mods/" + f}
/>
<Button type="submit">Disable</Button>
</form>
<form action={url.pathname} method="POST">
<input
type="hidden"
name="filePath"
value={"./server/disabled-mods/" + f}
/>
<input
type="hidden"
name="delete"
value="true"
/>
<Button color="fire" type="submit">
<i class="fas fa-trash"></i>
</Button>
</form>
</div>
</div>
))}
</div>
</div>
</FileUploader>
</Content>
<Content>
<h2 class="font-pixel text-xl">Disabled Mods</h2>
<FileUploader path="./server/disabled-mods">
<div class="min-h-[100px]">
<div className="relative grid lg:grid-cols-3 gap-8">
{!disabledMods.length && (
<div class="absolute place-self-center">
Drop files here to upload
</div>
)}
{disabledMods.map((f) => (
<div class="flex justify-between">
<div class="flex gap-2 items-center">
<FileIcon fileName={f} />
{f}
</div>
<div class="flex gap-2">
<form action={url.pathname} method="POST">
<input
type="hidden"
name="filePath"
value={"./server/disabled-mods/" + f}
/>
<Button type="submit">Enable</Button>
</form>
<form action={url.pathname} method="POST">
<input
type="hidden"
name="filePath"
value={"./server/disabled-mods/" + f}
/>
<input
type="hidden"
name="delete"
value="true"
/>
<Button color="fire" type="submit">
<i class="fas fa-trash"></i>
</Button>
</form>
</div>
</div>
))}
</div>
</div>
</FileUploader>
</Content>
@@ -39,9 +163,9 @@ export default async function ModsFolder() {
const FileIcon: FunctionComponent<{ fileName: string }> = ({ fileName }) => {
let icon;
switch (fileName.split(".")[1]) {
switch (fileName.split(".").at(-1)) {
case "jar":
icon = "fa-brand fa-java";
icon = "fab fa-java";
break;
case "tmp":
case "temp":