161 lines
7.5 KiB
TypeScript
161 lines
7.5 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// blocks a script's content phase registers in code are the mod's data like its files': checked, registered on the
|
|
// server, sent to players and generated by worldgen workers. see common/mod_api/content.ts
|
|
import { assert, assertEquals, assertRejects, assertStringIncludes, assertThrows } from "@std/assert";
|
|
import { EverythingRegistry } from "@/common/everything_registry.ts";
|
|
import { ID_MASK } from "@/common/constants.ts";
|
|
import { client_copy, read_mod } from "@/common/mod.ts";
|
|
import { to_flat } from "@/common/section.ts";
|
|
import { load_bundle } from "@/common/worldgen_bundle.ts";
|
|
import type { ContentContext } from "@/common/mod_api/content.ts";
|
|
import { load_client_mods } from "@/client/mods.ts";
|
|
import { load_mods } from "@/server/load_mods.ts";
|
|
import { load_and_check } from "@/tools/check_mods.ts";
|
|
import { pack_mod } from "@/tools/pack_mod.ts";
|
|
import { copy_dir, GROUND_BLOCKS, test_game } from "./helpers.ts";
|
|
|
|
// a block for every rock and its bricks, and one more where the script can't read files, like the server's worker
|
|
const CONTENT = `
|
|
import type { ContentContext } from "cubi/content";
|
|
export function content(ctx: ContentContext) {
|
|
for (const rock of ["granite", "basalt"]) {
|
|
ctx.blocks.register({ id: \`rocks:\${rock}\`, textures: "cubi:missing", drops: \`rocks:\${rock}\` });
|
|
ctx.blocks.register({ id: \`rocks:\${rock}_bricks\`, textures: "cubi:missing" });
|
|
}
|
|
try {
|
|
Deno.readTextFileSync("deno.json");
|
|
} catch {
|
|
ctx.blocks.register({ id: "rocks:sandboxed", textures: "cubi:missing" });
|
|
}
|
|
(globalThis as any).rocks_ctx = ctx;
|
|
EXTRA
|
|
}
|
|
`;
|
|
|
|
const CODE_BLOCKS = ["rocks:granite", "rocks:granite_bricks", "rocks:basalt", "rocks:basalt_bricks"];
|
|
|
|
// the base game, and rocks: a block file, the content phase above, and a tag and an ore that name its blocks
|
|
function rocks_mod(extra = "") {
|
|
const dir = Deno.makeTempDirSync({ prefix: "bworld_content_" });
|
|
copy_dir("mods/bworld", `${dir}/bworld`);
|
|
const write = (path: string, content: unknown) => {
|
|
Deno.mkdirSync(`${dir}/rocks/${path.split("/").slice(0, -1).join("/")}`, { recursive: true });
|
|
Deno.writeTextFileSync(`${dir}/rocks/${path}`, typeof content === "string" ? content : JSON.stringify(content));
|
|
};
|
|
write("manifest.json", {
|
|
format_version: 1,
|
|
id: "rocks",
|
|
name: "Rocks",
|
|
version: "1.0.0",
|
|
dependencies: [{ id: "bworld", version: "*" }],
|
|
main: "scripts/content.ts",
|
|
});
|
|
write("blocks/lamp.json", { format_version: 1, block: { id: "rocks:lamp", textures: "cubi:missing" } });
|
|
write("scripts/content.ts", CONTENT.replace("EXTRA", extra));
|
|
write("tags/blocks/rocks/rocks.json", { format_version: 1, tag: { values: ["rocks:granite", "rocks:basalt"] } });
|
|
write("worldgen/ores.json", {
|
|
format_version: 1,
|
|
ores: [{ id: "rocks:basalt", replaces: GROUND_BLOCKS, min_y: 5, max_y: 200, scale: 0.07, threshold: 0.3 }],
|
|
});
|
|
return dir;
|
|
}
|
|
|
|
Deno.test("check-mods runs a mod's content phase, and its files can name the blocks it registers", async () => {
|
|
const dir = rocks_mod();
|
|
const rocks = (await load_and_check(dir)).find((mod) => mod.id === "rocks")!;
|
|
Deno.removeSync(dir, { recursive: true });
|
|
assertEquals(rocks.report.errors, []);
|
|
// after the files' blocks, in the order they were registered, listed under the script
|
|
assertEquals(rocks.blocks.map(({ json }) => json.id), ["rocks:lamp", ...CODE_BLOCKS]);
|
|
assertEquals(rocks.blocks.slice(1).map(({ file }) => file), CODE_BLOCKS.map(() => "scripts/content.ts"));
|
|
|
|
// only during setup
|
|
const ctx = (globalThis as unknown as { rocks_ctx: ContentContext }).rocks_ctx;
|
|
assertThrows(
|
|
() => ctx.blocks.register({ id: "rocks:late", textures: "cubi:missing" }),
|
|
Error,
|
|
"only be registered during setup",
|
|
);
|
|
});
|
|
|
|
Deno.test("the server registers a mod's content phase's blocks, and a worldgen worker numbers and generates them alike", async () => {
|
|
const dir = rocks_mod();
|
|
const { game } = await test_game(dir);
|
|
Deno.removeSync(dir, { recursive: true });
|
|
for (const id of CODE_BLOCKS) {
|
|
assert(EverythingRegistry.get("blocks", id), `${id} is a block`);
|
|
assert(EverythingRegistry.get("items", id), `${id} has an item`);
|
|
}
|
|
|
|
const bundle = structuredClone(game.worldgen_bundle!);
|
|
const game_ids = { ...game.world.block_ids };
|
|
EverythingRegistry.clear();
|
|
const loaded = await load_bundle(bundle);
|
|
assertEquals(loaded.block_ids, game_ids, "blocks are numbered like the game numbers them");
|
|
const basalt = loaded.block_ids["rocks:basalt"];
|
|
const generated = to_flat(loaded.generate(0, 0).column.sections);
|
|
assert(generated.some((value) => (value & ID_MASK) === basalt), "the ore of a block made in code is generated");
|
|
});
|
|
|
|
Deno.test("a packed mod leaves its content phase's blocks out, the server registers them without permissions, and players get them as data", async () => {
|
|
const sources = rocks_mod();
|
|
const folder = Deno.makeTempDirSync({ prefix: "bworld_content_mods_" });
|
|
for (const mod of await load_and_check(sources)) {
|
|
Deno.writeFileSync(`${folder}/${mod.id}.cubi`, await pack_mod(mod));
|
|
}
|
|
Deno.removeSync(sources, { recursive: true });
|
|
const packed = read_mod(Deno.readFileSync(`${folder}/rocks.cubi`), "rocks.cubi");
|
|
assertEquals(packed.data.blocks.map((block) => block.id), ["rocks:lamp"]);
|
|
assert(packed.script);
|
|
|
|
const rocks = (await load_mods(folder, ["cubi:missing"])).find((mod) => mod.listing.id === "rocks")!;
|
|
Deno.removeSync(folder, { recursive: true });
|
|
// its worker can't read files, unlike check-mods
|
|
const ids = ["rocks:lamp", ...CODE_BLOCKS, "rocks:sandboxed"];
|
|
assertEquals(rocks.mod.data.blocks.map((block) => block.id), ids);
|
|
|
|
const copy = read_mod(rocks.client_bytes, rocks.listing.file);
|
|
// players get the script too, but never run its content phase: they get what it registered as data
|
|
assertEquals(copy.data.blocks, client_copy(rocks.mod).data.blocks);
|
|
EverythingRegistry.clear();
|
|
await load_client_mods([rocks.listing], [copy]);
|
|
for (const id of ids) assert(EverythingRegistry.get("blocks", id), `${id} is a block on the client`);
|
|
});
|
|
|
|
Deno.test("a content phase's mistakes stop its mod loading, naming the script", async () => {
|
|
const cases: [string, string][] = [
|
|
[
|
|
`ctx.blocks.register({ id: "bworld:granite", textures: "cubi:missing" });`,
|
|
`scripts/content.ts: block bworld:granite isn't in this mod's namespace "rocks"`,
|
|
],
|
|
[`ctx.blocks.register({ id: "rocks:broken" } as any);`, "scripts/content.ts: block rocks:broken: "],
|
|
[
|
|
`ctx.blocks.register({ id: "rocks:lamp", textures: "cubi:missing" });`,
|
|
"scripts/content.ts: block rocks:lamp is also defined by rocks",
|
|
],
|
|
[`throw new Error("no rocks today");`, "scripts/content.ts: no rocks today"],
|
|
];
|
|
for (const [extra, error] of cases) {
|
|
const dir = rocks_mod(extra);
|
|
const rocks = (await load_and_check(dir)).find((mod) => mod.id === "rocks")!;
|
|
Deno.removeSync(dir, { recursive: true });
|
|
assert(rocks.report.errors.some((e) => e.includes(error)), `${error} in ${rocks.report.errors.join("\n")}`);
|
|
}
|
|
|
|
// on the server too, from the .cubi
|
|
const sources = rocks_mod(`ctx.blocks.register({ id: "bworld:granite", textures: "cubi:missing" });`);
|
|
const folder = Deno.makeTempDirSync({ prefix: "bworld_content_mods_" });
|
|
for (const mod of await load_and_check(sources)) {
|
|
Deno.writeFileSync(`${folder}/${mod.id}.cubi`, await pack_mod(mod));
|
|
}
|
|
Deno.removeSync(sources, { recursive: true });
|
|
const rejected = await assertRejects(() => load_mods(folder, ["cubi:missing"]));
|
|
Deno.removeSync(folder, { recursive: true });
|
|
assertStringIncludes(
|
|
(rejected as Error).message,
|
|
`rocks.cubi: script.js: block bworld:granite isn't in this mod's namespace "rocks"`,
|
|
);
|
|
});
|