Single module modding

This commit is contained in:
2026-11-14 12:18:41 -03:00
parent 605008f584
commit 313098892e
134 changed files with 919 additions and 924 deletions
+12 -12
View File
@@ -1,8 +1,8 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// blocks a content script 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
// 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";
@@ -19,7 +19,7 @@ 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 setup(ctx: ContentContext) {
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" });
@@ -36,7 +36,7 @@ const CONTENT = `
const CODE_BLOCKS = ["rocks:granite", "rocks:granite_bricks", "rocks:basalt", "rocks:basalt_bricks"];
// the base game, and rocks: a block file, the content script above, and a tag and an ore that name its blocks
// 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`);
@@ -50,7 +50,7 @@ function rocks_mod(extra = "") {
name: "Rocks",
version: "1.0.0",
dependencies: [{ id: "bworld", version: "*" }],
scripts: { content: "scripts/content.ts" },
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));
@@ -62,7 +62,7 @@ function rocks_mod(extra = "") {
return dir;
}
Deno.test("check-mods runs a mod's content script, and its files can name the blocks it registers", async () => {
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 });
@@ -80,7 +80,7 @@ Deno.test("check-mods runs a mod's content script, and its files can name the bl
);
});
Deno.test("the server registers a content script's blocks, and a worldgen worker numbers and generates them alike", async () => {
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 });
@@ -99,7 +99,7 @@ Deno.test("the server registers a content script's blocks, and a worldgen worker
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 script's blocks out, the server registers them without permissions, and players get them as data", async () => {
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)) {
@@ -108,7 +108,7 @@ Deno.test("a packed mod leaves its content script's blocks out, the server regis
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.scripts.content);
assert(packed.script);
const rocks = (await load_mods(folder, ["cubi:missing"])).find((mod) => mod.listing.id === "rocks")!;
Deno.removeSync(folder, { recursive: true });
@@ -117,14 +117,14 @@ Deno.test("a packed mod leaves its content script's blocks out, the server regis
assertEquals(rocks.mod.data.blocks.map((block) => block.id), ids);
const copy = read_mod(rocks.client_bytes, rocks.listing.file);
assertEquals(copy.scripts.content, undefined, "players don't run it, they get what it registered");
// 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 script's mistakes stop its mod loading, naming the script", async () => {
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" });`,
@@ -155,6 +155,6 @@ Deno.test("a content script's mistakes stop its mod loading, naming the script",
Deno.removeSync(folder, { recursive: true });
assertStringIncludes(
(rejected as Error).message,
`rocks.cubi: scripts/content.js: block bworld:granite isn't in this mod's namespace "rocks"`,
`rocks.cubi: script.js: block bworld:granite isn't in this mod's namespace "rocks"`,
);
});