Loading schematics

This commit is contained in:
2026-11-15 19:54:08 -03:00
parent 6bb2c87373
commit 0564275f58
15 changed files with 142 additions and 17 deletions
+28 -6
View File
@@ -18,12 +18,23 @@ import { decode_level_chunk, type LevelChunk } from "@/common/chunk_streaming.ts
import { decode_chunk, encode_chunk } from "@/server/game/saves.ts";
import { list_chunks, read_chunk, write_chunks } from "@/server/regions.ts";
import { section_index } from "@/common/section.ts";
import { encode_schematic, type Schematic } from "@/common/schematic.ts";
// mods/bworld and one made from the template, packed into a folder of .cubi files
// a schematic the template mod gets
const POST: Schematic = {
size: [1, 2, 1],
palette: [{ id: "bworld:stone" }],
blocks: [{ i: 0, p: [0, 0, 0] }, { i: 0, p: [0, 1, 0] }],
entities: [],
};
// mods/bworld and one made from the template, with a schematic, packed into a folder of .cubi files
async function packed_mods() {
const sources = Deno.makeTempDirSync({ prefix: "bworld_mod_src_" });
copy_dir("mods/bworld", `${sources}/bworld`);
create_mod("copper_tools", "Copper Tools", sources);
Deno.mkdirSync(`${sources}/copper_tools/schematics`);
Deno.writeFileSync(`${sources}/copper_tools/schematics/post.cbsm`, encode_schematic(POST));
const mods = await load_and_check(sources);
for (const mod of mods) assertEquals(mod.report.errors, [], mod.id);
@@ -32,7 +43,7 @@ async function packed_mods() {
return { sources, folder, mods };
}
Deno.test("packing a mod keeps its data, script, textures and credits, and gives the same bytes every time", async () => {
Deno.test("packing a mod keeps its data, script, textures, schematics and credits, and gives the same bytes every time", async () => {
const { sources, folder, mods } = await packed_mods();
const template = mods.find((mod) => mod.id === "copper_tools")!;
const bytes = Deno.readFileSync(`${folder}/copper_tools.cubi`);
@@ -43,6 +54,8 @@ Deno.test("packing a mod keeps its data, script, textures and credits, and gives
assertEquals(mod.data.blocks.map((b) => b.id), template.blocks.map((b) => b.json.id));
assertEquals(mod.data.recipes, template.recipes.map((r) => r.json));
assertEquals([...mod.textures.keys()].sort(), [...template.textures].sort());
// named by their files
assertEquals(mod.data.schematics, [{ ...POST, id: "copper_tools:post" }]);
assertStringIncludes(mod.script!, "function server");
assert(mod.credits);
@@ -108,7 +121,7 @@ Deno.test("broken .cubi files are turned away, saying why", () => {
);
});
Deno.test("the server loads every .cubi in its folder, dependencies first, and players get them without the terrain", async () => {
Deno.test("the server loads every .cubi in its folder, dependencies first, and players get them without the terrain or schematics", async () => {
const { sources, folder } = await packed_mods();
const mods = await load_mods(folder, ENGINE_TEXTURES);
assertEquals(mods.map((mod) => mod.listing.id), ["bworld", "copper_tools"]);
@@ -123,10 +136,19 @@ Deno.test("the server loads every .cubi in its folder, dependencies first, and p
mod.mod.script,
"players get the whole script, and only run its shared and client phases",
);
// the terrain's data stays on the server too, the rest is the same
const { noises, density_functions, noise_settings, biome_sources, dimensions, ...data } = mod.mod.data;
const none = { noises: [], density_functions: [], noise_settings: [], biome_sources: [], dimensions: [] };
// the terrain's data and schematics stay on the server too, the rest is the same
const { noises, density_functions, noise_settings, biome_sources, dimensions, schematics: _, ...data } =
mod.mod.data;
const none = {
noises: [],
density_functions: [],
noise_settings: [],
biome_sources: [],
dimensions: [],
schematics: [],
};
assertEquals(copy.data, { ...data, ...none });
if (mod.listing.id === "copper_tools") assertEquals(mod.mod.data.schematics?.length, 1);
if (mod.listing.id === "bworld") {
const terrain = [noises, density_functions, noise_settings, biome_sources, dimensions];
assert(terrain.every((list) => list!.length > 0));
+16
View File
@@ -6,6 +6,7 @@ import { check_mods } from "@/tools/check_mods.ts";
import { MAX_TEXTURE_SIZE } from "@/common/constants.ts";
import { create_mod } from "@/tools/new_mod.ts";
import { item_from_json, item_to_json } from "@/common/mod_data.ts";
import { encode_schematic } from "@/common/schematic.ts";
function temp_mods_dir() {
const dir = Deno.makeTempDirSync({ prefix: "bworld_mods_" });
@@ -127,6 +128,18 @@ Deno.test("check-mods finds broken mods", async () => {
new DataView(huge.buffer).setUint32(16, MAX_TEXTURE_SIZE + 1);
new DataView(huge.buffer).setUint32(20, 16);
Deno.writeFileSync(`${mod}/textures/huge.png`, huge);
// a schematic with a block and an entity that don't exist, and a file that isn't a schematic
Deno.mkdirSync(`${mod}/schematics`);
Deno.writeFileSync(
`${mod}/schematics/ghost_hut.cbsm`,
encode_schematic({
size: [1, 1, 1],
palette: [{ id: "broken:no_such_block" }],
blocks: [{ i: 0, p: [0, 0, 0] }],
entities: [{ p: [0.5, 0, 0.5], id: "broken:no_such_entity", data: {} }],
}),
);
Deno.writeTextFileSync(`${mod}/schematics/notes.cbsm`, "not a schematic");
// a script that doesn't typecheck
Deno.writeTextFileSync(`${mod}/scripts/main.ts`, "export function client(ctx: number) { ctx.nope(); }\n");
// a dependency that isn't installed, and no longer depending on bworld
@@ -151,6 +164,9 @@ Deno.test("check-mods finds broken mods", async () => {
has(report.errors, `depends on "missing_mod", which isn't installed`);
has(report.errors, "entity model broken:no_such_worn doesn't exist");
has(report.errors, "worn must be { model, texture }");
has(report.errors, "schematics/ghost_hut.cbsm: block broken:no_such_block doesn't exist");
has(report.errors, "schematics/ghost_hut.cbsm: entity broken:no_such_entity doesn't exist");
has(report.errors, "schematics/notes.cbsm: ");
has(report.warnings, "worn model broken:stiff has no bone named like a player part");
has(report.warnings, "texture broken:nope doesn't exist");
has(report.warnings, `uses bworld:stone but doesn't list "bworld" in dependencies`);
+25 -1
View File
@@ -3,7 +3,7 @@
// exporting a box of the world as a schematic file, with /exportschematic, and placing registered schematics
import { assert, assertAlmostEquals, assertEquals, assertStringIncludes, assertThrows } from "@std/assert";
import { decode_schematic, type Schematic } from "@/common/schematic.ts";
import { decode_schematic, encode_schematic, type Schematic } from "@/common/schematic.ts";
import { create_entity } from "@/server/game/entity/entity_types.ts";
import { new_stack } from "@/server/game/mod_runtime.ts";
import { get_state_value, set_state_value } from "@/common/utils.ts";
@@ -127,6 +127,12 @@ export function server(ctx: ServerContext) {
} catch (e) {
ctx.storage.set("broken", (e as Error).message);
}
// the file's is registered before scripts run
try {
ctx.schematics.register("testmod:post", { size: [1, 1, 1], palette: [], blocks: [], entities: [] });
} catch (e) {
ctx.storage.set("post", (e as Error).message);
}
(globalThis as Record<string, unknown>).placing = ctx.schematics;
}
`;
@@ -147,6 +153,16 @@ async function place_setup() {
}),
);
Deno.writeTextFileSync(`${dir}/testmod/scripts/main.ts`, PLACE_SCRIPT);
Deno.mkdirSync(`${dir}/testmod/schematics`);
Deno.writeFileSync(
`${dir}/testmod/schematics/post.cbsm`,
encode_schematic({
size: [1, 2, 1],
palette: [{ id: "bworld:oak_log" }],
blocks: [{ i: 0, p: [0, 0, 0] }, { i: 0, p: [0, 1, 0] }],
entities: [],
}),
);
const t = await test_game(dir);
Deno.removeSync(dir, { recursive: true });
stone_floor(t.game);
@@ -228,3 +244,11 @@ Deno.test("a schematic is checked when it's registered, and a placement before a
);
assertEquals(t.game.world.get_block_id(0, 100, 0), "cubi:air");
});
Deno.test("a mod's schematics/<name>.cbsm files are registered as <mod id>:<name> before its scripts run", async () => {
const t = await place_setup();
assertStringIncludes(String(t.game.mods.storage.testmod?.post), "registered twice");
t.schematics.place("testmod:post", { position: { x: 3, y: 100, z: 4 } });
assertEquals(t.game.world.get_block_id(3, 100, 4), "bworld:oak_log");
assertEquals(t.game.world.get_block_id(3, 101, 4), "bworld:oak_log");
});