135 lines
5.6 KiB
TypeScript
135 lines
5.6 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
import { assert, assertEquals, assertStringIncludes } 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";
|
|
|
|
// 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 {
|
|
const root = Deno.makeTempDirSync();
|
|
for (const [path, json] of Object.entries(models)) {
|
|
const file = `${root}/assets/${namespace}/models/${path}.json`;
|
|
Deno.mkdirSync(file.slice(0, file.lastIndexOf("/")), { recursive: true });
|
|
Deno.writeTextFileSync(file, JSON.stringify(json));
|
|
}
|
|
return root;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
const GEAR = {
|
|
credit: "Made with Blockbench",
|
|
texture_size: [32, 32],
|
|
textures: { "0": "gears:block/gear", particle: "gears:block/gear" },
|
|
elements: [{
|
|
name: "hub",
|
|
from: [4, 0, 4],
|
|
to: [12, 4, 12],
|
|
rotation: { angle: 22.5, axis: "y", origin: [8, 8, 8] },
|
|
faces: {
|
|
up: { uv: [0, 0, 8, 8], texture: "#0", cullface: "up" },
|
|
down: { uv: [0, 0, 8, 8], texture: "#0", cullface: "down", tintindex: 0 },
|
|
north: { uv: [0, 8, 8, 12], texture: "#0", rotation: 90 },
|
|
},
|
|
}],
|
|
};
|
|
|
|
Deno.test("a java block model becomes a bworld model that passes its checks", () => {
|
|
const model = convert({ "block/gear": GEAR }, "block/gear");
|
|
assertEquals(validate_model(model), []);
|
|
// folders are joined into the id, the way textures in subfolders are named
|
|
assertEquals(model.id, "gears:block_gear");
|
|
assertEquals(model.textures, { "0": "gears:block_gear" });
|
|
const [hub] = model.elements;
|
|
assertEquals(Object.keys(hub.faces).sort(), ["bottom", "north", "top"]);
|
|
assertEquals(hub.faces.top, { texture: "#0", uv: [0, 0, 8, 8], cullface: "top" });
|
|
assertEquals(hub.faces.north?.rotation, 90);
|
|
assertEquals(hub.rotation, { origin: [8, 8, 8], axis: "y", angle: 22.5 });
|
|
});
|
|
|
|
Deno.test("a model gets its parents' elements and textures, its own textures winning", () => {
|
|
const model = convert({
|
|
"block/gear": GEAR,
|
|
"block/gear_brass": { parent: "gears:block/gear", textures: { "0": "gears:block/brass_gear" } },
|
|
}, "block/gear_brass");
|
|
assertEquals(model.textures, { "0": "gears:block_brass_gear" });
|
|
assertEquals(model.elements.length, GEAR.elements.length);
|
|
});
|
|
|
|
Deno.test("vanilla parents like cube_all and stairs convert without minecraft's files", () => {
|
|
const cube = convert(
|
|
{ "block/casing": { parent: "minecraft:block/cube_all", textures: { all: "block/stone" } } },
|
|
"block/casing",
|
|
);
|
|
assertEquals(validate_model(cube), []);
|
|
assertEquals(Object.keys(cube.elements[0].faces).length, 6);
|
|
// minecraft's block textures are taken to be the base game's of the same name
|
|
assertEquals(cube.textures?.all, "bworld:stone");
|
|
|
|
const stairs = convert({
|
|
"block/stairs": {
|
|
parent: "block/stairs",
|
|
textures: { top: "gears:block/a", bottom: "gears:block/a", side: "gears:block/a" },
|
|
},
|
|
}, "block/stairs");
|
|
assertEquals(validate_model(stairs), []);
|
|
assert(stairs.elements.length > 1);
|
|
});
|
|
|
|
Deno.test("a folder converts into the same folders, with models that are only parents left out", () => {
|
|
const root = resources({
|
|
"block/gear": GEAR,
|
|
"block/large/gear": { parent: "gears:block/gear" },
|
|
"block/template": { parent: "block/block", textures: { "0": "gears:block/gear" } },
|
|
});
|
|
const output = Deno.makeTempDirSync();
|
|
const report = import_models(root, { output });
|
|
assertEquals(report.errors, []);
|
|
assertEquals(report.parents, ["gears:block/template"]);
|
|
for (const path of ["block/gear", "block/large/gear"]) {
|
|
const json = JSON.parse(Deno.readTextFileSync(`${output}/${path}.json`));
|
|
assertEquals(json.format_version, 1);
|
|
assertEquals(validate_model(json.model), []);
|
|
}
|
|
});
|
|
|
|
Deno.test("models that can't be converted are reported, not written", () => {
|
|
const root = resources({
|
|
"block/obj": { loader: "forge:obj", model: "gears:models/block/thing.obj" },
|
|
"item/ingot": { parent: "item/generated", textures: { layer0: "gears:item/ingot" } },
|
|
"block/orphan": { parent: "gears:block/nowhere" },
|
|
"block/tilted": {
|
|
...GEAR,
|
|
elements: [{ ...GEAR.elements[0], rotation: { angle: 30, axis: "x", origin: [8, 8, 8] } }],
|
|
},
|
|
// these two would both be gears:block_a_b
|
|
"block/a/b": GEAR,
|
|
"block/a_b": GEAR,
|
|
});
|
|
const output = Deno.makeTempDirSync();
|
|
const report = import_models(root, { output });
|
|
assertEquals(report.skipped.map(({ location }) => location).sort(), ["gears:block/obj", "gears:item/ingot"]);
|
|
const errors = Object.fromEntries(report.errors.map(({ location, problems }) => [location, problems.join(" ")]));
|
|
assertStringIncludes(errors["gears:block/orphan"], "gears:block/nowhere");
|
|
assertStringIncludes(errors["gears:block/tilted"], "30");
|
|
assertStringIncludes(errors["gears:block/a_b"] ?? errors["gears:block/a/b"], "--remap");
|
|
assertEquals(report.written.length, 1);
|
|
});
|
|
|
|
Deno.test("--remap renames locations before they become ids", () => {
|
|
const root = resources({ "block/gear": GEAR });
|
|
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;
|
|
assertEquals(model.id, "cogs:big_gear");
|
|
assertEquals(model.textures, { "0": "cogs:gear" });
|
|
});
|