fixing some chunking issues

This commit is contained in:
2024-10-29 00:14:21 -06:00
parent a8a903d581
commit 1f306485a8
10 changed files with 274 additions and 88 deletions

View File

@@ -2,13 +2,7 @@ import { encodeBase64 } from "@std/encoding/base64";
import { readDirFiles } from "../util/readDir.ts";
import { createIsometricCube } from "./renderer.ts";
interface BlockItem {
name: string;
resourceLocation: string;
images: string[];
}
export const readBlocks = async (path: string) => {
export const readBlocks = async (path: string, read?: (b: BlockItem, i: number) => void) => {
const blocks: BlockItem[] =
(await readDirFiles(path + "/assets/minecraft/blockstates"))
.map((b) => ({
@@ -300,10 +294,16 @@ export const readBlocks = async (path: string) => {
block.images.push(await createIsometricCube(b64, b64, b64));
}
}
let i = 0;
for (const block of blocks) {
read?.(block, i);
i++;
}
return blocks;
};
export const readItems = async (path: string) => {
export const readItems = async (path: string, read?: (b: BlockItem) => void) => {
const items: BlockItem[] =
(await readDirFiles(path + "/assets/minecraft/models/item")).map((i) => ({
name: i.replace(".json", ""),
@@ -348,6 +348,7 @@ export const readItems = async (path: string) => {
}
}
}
read?.(item);
}
return items;

View File

@@ -1,7 +1,8 @@
import { ensureDir } from "@std/fs/ensure-dir";
import type { Router } from "../router.ts";
import { readDirDirs } from "../util/readDir.ts";
import { versionCompat } from "../util/versionCompat.ts";
import { readBlocks } from "./readers.ts";
import { readBlocks, readItems } from "./readers.ts";
export const createResourcesRoutes = (router: Router) => {
router.route("/api/resources/:path*")
@@ -10,38 +11,92 @@ export const createResourcesRoutes = (router: Router) => {
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;
await ensureDir("./resources");
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)),
let items: BlockItem[] = [];
const batch = (
controller: ReadableStreamDefaultController,
res: BlockItem,
) => {
items.push(res);
if (items.length > 4) {
controller.enqueue(
new TextEncoder().encode(JSON.stringify(items) + "\n"),
);
items = [];
}
case "item":
case "items": {
return new Response(
JSON.stringify(await readBlocks(resourcePath)),
);
}
default: {
return new Response("invalid path", { status: 400 });
}
}
};
const body = new ReadableStream({
async start(controller) {
switch (path) {
case "block":
case "blocks": {
await readBlocks(resourcePath, (res, i) => {
console.log(i);
batch(controller, res);
});
controller.close();
break;
}
case "item":
case "items": {
await readItems(resourcePath, (res) => {
batch(controller, res);
});
controller.close();
break;
}
default: {
controller.close();
break;
}
}
},
});
return new Response(body, {
headers: {
"content-type": "application/json",
"access-control-allow-origin": "*",
},
});
// 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 });
// }
// }
}
}
});