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"); for (const resourceVersion of resourceVersions) { if (versionCompat(resourceVersion, mcVersion)) { const resourcePath = "./resources/" + resourceVersion; const splitPath = path.split("/"); switch (splitPath[0]) { case "blocks": { return new Response( JSON.stringify(await readBlocks(resourcePath)), ); } } } } }); };