49 lines
1.7 KiB
TypeScript
49 lines
1.7 KiB
TypeScript
import type { Router } from "../router.ts";
|
|
import { readDirDirs } from "../util/readDir.ts";
|
|
import { versionCompat } from "../util/versionCompat.ts";
|
|
import { readBlocks } from "./readers.ts";
|
|
|
|
export const createResourcesRoutes = (router: Router) => {
|
|
router.route("/api/resources/:path*")
|
|
.get(async (req, ctx) => {
|
|
const path = ctx.params.path;
|
|
if (!path) {
|
|
return new Response("no path provided", { status: 400 });
|
|
}
|
|
const format = ctx.url.searchParams.get("format");
|
|
if (!format) {
|
|
return new Response("no format provided", { status: 400 });
|
|
}
|
|
const packVersion = await Deno.readTextFile(
|
|
"./pack_versions/" + format + ".json",
|
|
);
|
|
const packVersionJson = JSON.parse(packVersion);
|
|
const mcVersion = packVersionJson.mcVersion;
|
|
const resourceVersions = await readDirDirs("./resources");
|
|
console.log("resourceVersions", resourceVersions);
|
|
for (const resourceVersion of resourceVersions) {
|
|
if (versionCompat(resourceVersion, mcVersion)) {
|
|
const resourcePath = "./resources/" + resourceVersion;
|
|
const splitPath = path.split("/");
|
|
switch (splitPath[0]) {
|
|
case "block":
|
|
case "blocks": {
|
|
return new Response(
|
|
JSON.stringify(await readBlocks(resourcePath)),
|
|
);
|
|
}
|
|
case "item":
|
|
case "items": {
|
|
return new Response(
|
|
JSON.stringify(await readBlocks(resourcePath)),
|
|
);
|
|
}
|
|
default: {
|
|
return new Response("invalid path", { status: 400 });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
};
|