Add various generic models
This commit is contained in:
+29
-25
@@ -3,7 +3,7 @@
|
||||
|
||||
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 type { ModelFileJson } from "$/common/block_models.ts";
|
||||
import { import_models } from "$/tools/importer/models.ts";
|
||||
import { convert_mcmeta, import_textures } from "$/tools/importer/textures.ts";
|
||||
|
||||
@@ -18,7 +18,7 @@ function resources(models: Record<string, unknown>, namespace = "gears"): string
|
||||
return root;
|
||||
}
|
||||
|
||||
function convert(models: Record<string, unknown>, path: string): ModelJson {
|
||||
function convert(models: Record<string, unknown>, path: string): ModelFileJson {
|
||||
const root = resources(models);
|
||||
const report = import_models(`${root}/assets/gears/models/${path}.json`);
|
||||
assertEquals(report.errors, []);
|
||||
@@ -47,65 +47,66 @@ Deno.test("a java block model becomes a bworld model that passes its checks", ()
|
||||
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;
|
||||
// particle comes along too, since some models draw faces with it
|
||||
assertEquals(model.textures, { "0": "gears:block_gear", particle: "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", () => {
|
||||
Deno.test("a model keeps its parent, which bworld merges in when it loads", () => {
|
||||
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);
|
||||
assertEquals(model, {
|
||||
id: "gears:block_gear_brass",
|
||||
parent: "gears:block_gear",
|
||||
textures: { "0": "gears:block_brass_gear" },
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test("vanilla parents like cube_all and stairs convert without minecraft's files", () => {
|
||||
Deno.test("minecraft's template parents become the base game's, and others need a remap", () => {
|
||||
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);
|
||||
assertEquals(cube.parent, "bworld:cube_all");
|
||||
// minecraft's block textures are taken to be the base game's of the same name
|
||||
assertEquals(cube.textures?.all, "bworld:stone");
|
||||
assertEquals(convert({ "block/steps": { parent: "block/stairs" } }, "block/steps").parent, "bworld:stairs");
|
||||
|
||||
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);
|
||||
const root = resources({ "block/stand": { parent: "block/lectern" } });
|
||||
const report = import_models(`${root}/assets/gears/models/block/stand.json`);
|
||||
assertStringIncludes(report.errors[0].problems[0], "--remap");
|
||||
});
|
||||
|
||||
Deno.test("a folder converts into the same folders, with models that are only parents left out", () => {
|
||||
Deno.test("a folder converts into the same folders, models only there to be parents included", () => {
|
||||
const root = resources({
|
||||
"block/gear": GEAR,
|
||||
"block/large/gear": { parent: "gears:block/gear" },
|
||||
// block/block only holds display transforms, so this keeps its textures and nothing else
|
||||
"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"]) {
|
||||
for (const path of ["block/gear", "block/large/gear", "block/template"]) {
|
||||
const json = JSON.parse(Deno.readTextFileSync(`${output}/${path}.json`));
|
||||
assertEquals(json.format_version, 1);
|
||||
assertEquals(validate_model(json.model), []);
|
||||
}
|
||||
assertEquals(JSON.parse(Deno.readTextFileSync(`${output}/block/template.json`)).model.parent, undefined);
|
||||
});
|
||||
|
||||
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" },
|
||||
// a sprite's child is a sprite too
|
||||
"item/shiny_ingot": { parent: "gears:item/ingot" },
|
||||
"block/tilted": {
|
||||
...GEAR,
|
||||
elements: [{ ...GEAR.elements[0], rotation: { angle: 30, axis: "x", origin: [8, 8, 8] } }],
|
||||
@@ -116,9 +117,12 @@ Deno.test("models that can't be converted are reported, not written", () => {
|
||||
});
|
||||
const output = Deno.makeTempDirSync();
|
||||
const report = import_models(root, { output });
|
||||
assertEquals(report.skipped.map(({ location }) => location).sort(), ["gears:block/obj", "gears:item/ingot"]);
|
||||
assertEquals(report.skipped.map(({ location }) => location).sort(), [
|
||||
"gears:block/obj",
|
||||
"gears:item/ingot",
|
||||
"gears:item/shiny_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);
|
||||
@@ -131,7 +135,7 @@ Deno.test("--remap renames locations before they become ids", () => {
|
||||
});
|
||||
const model = JSON.parse(report.written[0].text!).model;
|
||||
assertEquals(model.id, "cogs:big_gear");
|
||||
assertEquals(model.textures, { "0": "cogs:gear" });
|
||||
assertEquals(model.textures, { "0": "cogs:gear", particle: "cogs:gear" });
|
||||
});
|
||||
|
||||
// a png with only the header, which is all that's read to size it
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import { assert, assertEquals } from "@std/assert";
|
||||
import { BUILTIN_MODELS, type ModelJson, resolve_models, resolve_texture } from "$/common/block_models.ts";
|
||||
import { EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import { load_and_check } from "$/tools/check_mods.ts";
|
||||
import { create_mod } from "$/tools/new_mod.ts";
|
||||
import { copy_dir, test_game } from "./helpers.ts";
|
||||
|
||||
const BOX = { from: [0, 0, 0], to: [16, 8, 16], faces: { top: { texture: "#top" } } } as ModelJson["elements"][0];
|
||||
|
||||
Deno.test("a model gets its parent's elements and textures, with its own textures and elements winning", () => {
|
||||
const { models, problems } = resolve_models([
|
||||
{ id: "a:base", textures: { top: "a:stone", side: "a:stone" }, elements: [BOX] },
|
||||
{ id: "a:brass", parent: "a:base", textures: { top: "a:brass" } },
|
||||
{ id: "a:tall", parent: "a:brass", elements: [BOX, BOX] },
|
||||
], () => undefined);
|
||||
assertEquals(problems, []);
|
||||
const [, brass, tall] = models;
|
||||
assertEquals(brass, { id: "a:brass", textures: { top: "a:brass", side: "a:stone" }, elements: [BOX] });
|
||||
// its parent's parent counts too
|
||||
assertEquals(tall.textures, { top: "a:brass", side: "a:stone" });
|
||||
assertEquals(tall.elements.length, 2);
|
||||
});
|
||||
|
||||
Deno.test("a parent can be another mod's model or a built-in one", () => {
|
||||
const earlier: Record<string, ModelJson> = { "base:slab": { id: "base:slab", elements: [BOX] } };
|
||||
const { models, problems } = resolve_models([
|
||||
{ id: "a:slab", parent: "base:slab", textures: { top: "a:top" } },
|
||||
{ id: "a:cross", parent: "engine:cross" },
|
||||
], (id) => BUILTIN_MODELS[id] ?? earlier[id]);
|
||||
assertEquals(problems, []);
|
||||
assertEquals(models[0].elements, [BOX]);
|
||||
assertEquals(models[1].elements, BUILTIN_MODELS["engine:cross"].elements);
|
||||
});
|
||||
|
||||
Deno.test("a missing parent and parents that loop are problems, and the models still resolve", () => {
|
||||
const { models, problems } = resolve_models([
|
||||
{ id: "a:orphan", parent: "a:nowhere", elements: [BOX] },
|
||||
{ id: "a:one", parent: "a:two" },
|
||||
{ id: "a:two", parent: "a:one" },
|
||||
], () => undefined);
|
||||
assertEquals(models.length, 3);
|
||||
assertEquals(models[0].elements, [BOX]);
|
||||
assertEquals(problems.map(({ id }) => id).sort(), ["a:one", "a:orphan", "a:two"]);
|
||||
assert(problems.find(({ id }) => id === "a:one")!.problem.includes("loop"));
|
||||
});
|
||||
|
||||
// mods/bworld and a mod made from the template, with models added to it
|
||||
function mods_with(models: Record<string, unknown>) {
|
||||
const dir = Deno.makeTempDirSync({ prefix: "bworld_parents_" });
|
||||
copy_dir("mods/bworld", `${dir}/bworld`);
|
||||
create_mod("copper_tools", "Copper Tools", dir);
|
||||
Deno.mkdirSync(`${dir}/copper_tools/models`, { recursive: true });
|
||||
for (const [name, model] of Object.entries(models)) {
|
||||
Deno.writeTextFileSync(`${dir}/copper_tools/models/${name}.json`, JSON.stringify({ format_version: 1, model }));
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
|
||||
Deno.test("a mod's model can use the base game's templates as its parent", async () => {
|
||||
const dir = mods_with({
|
||||
pillar: { id: "copper_tools:pillar", parent: "bworld:cube_column", textures: { end: "bworld:oak_log_top" } },
|
||||
});
|
||||
await test_game(dir);
|
||||
const model = EverythingRegistry.get<ModelJson>("models", "copper_tools:pillar")!;
|
||||
assertEquals(Object.keys(model.elements[0].faces).length, 6);
|
||||
// the cube's six variables lead through cube_column's to the textures a block gives
|
||||
const faces = model.elements[0].faces;
|
||||
assertEquals(resolve_texture(faces.top!.texture, { side: "a:bark" }, model), "bworld:oak_log_top");
|
||||
assertEquals(resolve_texture(faces.north!.texture, { side: "a:bark" }, model), "a:bark");
|
||||
Deno.removeSync(dir, { recursive: true });
|
||||
});
|
||||
|
||||
Deno.test("check-mods reports a parent that doesn't exist and parents that loop", () => {
|
||||
const dir = mods_with({
|
||||
orphan: { id: "copper_tools:orphan", parent: "copper_tools:nowhere" },
|
||||
one: { id: "copper_tools:one", parent: "copper_tools:two" },
|
||||
two: { id: "copper_tools:two", parent: "copper_tools:one" },
|
||||
});
|
||||
const errors = load_and_check(dir).find((mod) => mod.id === "copper_tools")!.report.errors.join("\n");
|
||||
assert(errors.includes("model copper_tools:nowhere doesn't exist"), errors);
|
||||
assert(errors.includes("copper_tools:one's parents loop"), errors);
|
||||
Deno.removeSync(dir, { recursive: true });
|
||||
});
|
||||
Reference in New Issue
Block a user