Import textures

This commit is contained in:
2026-10-12 17:39:48 -03:00
parent 355e7a21ca
commit e5baeab193
7 changed files with 419 additions and 152 deletions
+80 -4
View File
@@ -1,10 +1,11 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
import { assert, assertEquals, assertStringIncludes } from "@std/assert";
import { assert, assertEquals, assertStringIncludes, assertThrows } from "@std/assert";
import { validate_model } from "$/common/mod_data.ts";
import type { ModelJson } from "$/common/block_models.ts";
import { import_models } from "$/tools/importer.ts";
import { import_models } from "$/tools/importer/models.ts";
import { convert_mcmeta, import_textures } from "$/tools/importer/textures.ts";
// a java mod's resources in a temporary folder: { "block/gear": {...} } is assets/<namespace>/models/block/gear.json
function resources(models: Record<string, unknown>, namespace = "gears"): string {
@@ -21,7 +22,7 @@ function convert(models: Record<string, unknown>, path: string): ModelJson {
const root = resources(models);
const report = import_models(`${root}/assets/gears/models/${path}.json`);
assertEquals(report.errors, []);
return JSON.parse(report.written[0].text).model;
return JSON.parse(report.written[0].text!).model;
}
const GEAR = {
@@ -128,7 +129,82 @@ Deno.test("--remap renames locations before they become ids", () => {
const report = import_models(`${root}/assets/gears/models/block/gear.json`, {
remaps: { textures: { "gears:block/": "cogs:" }, models: { "gears:block/gear": "cogs:big_gear" } },
});
const model = JSON.parse(report.written[0].text).model;
const model = JSON.parse(report.written[0].text!).model;
assertEquals(model.id, "cogs:big_gear");
assertEquals(model.textures, { "0": "cogs:gear" });
});
// a png with only the header, which is all that's read to size it
function png(width: number, height: number): Uint8Array {
const bytes = new Uint8Array(33);
bytes.set([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 13, 0x49, 0x48, 0x44, 0x52]);
const view = new DataView(bytes.buffer);
view.setUint32(16, width);
view.setUint32(20, height);
return bytes;
}
// textures in a temporary resources folder: { "gears:block/gear": [16, 16] }, with a .png.mcmeta when given
function textures(files: Record<string, { size: [number, number]; mcmeta?: unknown }>): string {
const root = Deno.makeTempDirSync();
for (const [location, { size, mcmeta }] of Object.entries(files)) {
const [namespace, path] = location.split(":");
const file = `${root}/assets/${namespace}/textures/${path}.png`;
Deno.mkdirSync(file.slice(0, file.lastIndexOf("/")), { recursive: true });
Deno.writeFileSync(file, png(...size));
if (mcmeta !== undefined) Deno.writeTextFileSync(`${file}.mcmeta`, JSON.stringify(mcmeta));
}
return root;
}
Deno.test("textures keep their folders, so bworld names them like the models that use them", () => {
const root = textures({
"gears:block/gear": { size: [16, 16] },
"gears:block/belt/scroll": {
size: [16, 64],
mcmeta: { animation: { frametime: 2, interpolate: true }, villager: { hat: "full" } },
},
});
const output = Deno.makeTempDirSync();
const report = import_textures(root, { output });
assertEquals(report.errors, []);
assertEquals(report.written.filter(({ copy }) => copy).map(({ id }) => id).sort(), [
"gears:block_belt_scroll",
"gears:block_gear",
]);
assertEquals(Deno.readFileSync(`${output}/block/gear.png`), png(16, 16));
// the animation is the same format, only renamed, and what bworld can't use is left out
const animation = JSON.parse(Deno.readTextFileSync(`${output}/block/belt/scroll.json`));
assertEquals(animation, { animation: { frametime: 2, interpolate: true } });
assertEquals(report.warnings.length, 1);
});
Deno.test("textures bworld can't use are reported, and minecraft's aren't copied", () => {
const root = textures({
"gears:gui/panorama": { size: [512, 512] },
// 3 rows of 16 don't split into 16×32 frames
"gears:block/uneven": { size: [16, 48], mcmeta: { animation: { height: 32 } } },
"minecraft:block/stone": { size: [16, 16] },
});
const output = Deno.makeTempDirSync();
const report = import_textures(root, { output });
assertEquals(report.skipped.map(({ location }) => location).sort(), [
"gears:gui/panorama",
"minecraft:block/stone",
]);
assertEquals(report.errors.map(({ location }) => location), ["gears:block/uneven"]);
assertEquals(report.written, []);
});
Deno.test("a folder with two mods' textures needs --namespace to pick one", () => {
const root = textures({ "gears:block/gear": { size: [16, 16] }, "belts:block/belt": { size: [16, 16] } });
const output = Deno.makeTempDirSync();
assertThrows(() => import_textures(root, { output }), Error, "--namespace");
const report = import_textures(root, { output, namespace: "belts" });
assertEquals(report.written.map(({ id }) => id), ["belts:block_belt"]);
});
Deno.test("an animation that doesn't fit its png is an error, not a broken texture", () => {
const { problems } = convert_mcmeta({ animation: { frames: [0, 5] } }, { width: 16, height: 32 });
assert(problems.length > 0);
});