Mod config

This commit is contained in:
2026-10-14 14:14:52 -03:00
parent d7f1b91c0a
commit cedc7725b5
20 changed files with 371 additions and 32 deletions
+17 -3
View File
@@ -8,6 +8,7 @@ import { mod_data } from "$/common/mod_check.ts";
import type { ServerModSource } from "$/server/game/load_mods.ts";
import { GameServer } from "$/server/game/game_server.ts";
import { start_game } from "$/server/game/load_mods.ts";
import { apply_config } from "$/server/load_bmods.ts";
import { PROTOCOL_VERSION } from "$/common/protocol.ts";
import { decode_level_chunk } from "$/common/chunk_streaming.ts";
import { CHUNK_AREA, CHUNK_SIZE, ID_MASK } from "$/common/constants.ts";
@@ -68,9 +69,22 @@ class MemoryChunks implements SavedChunks {
}
// a game with an in memory host that remembers what it sent to each connection. the seed has low, dry land around
// 0, 0, where players spawn and where tests build things
export async function test_game(mods_dir: string, save?: WorldSave, seed = "test-seed-90") {
// 0, 0, where players spawn and where tests build things. config changes mods' settings, by mod id, like a server's
// config/<mod id>.json files
export async function test_game(
mods_dir: string,
save?: WorldSave,
seed = "test-seed-90",
config: Record<string, Record<string, unknown>> = {},
) {
EverythingRegistry.clear();
const mods = mod_sources(mods_dir);
for (const mod of mods) {
if (!config[mod.listing.id]) continue;
const changed = apply_config(mod.data.config ?? {}, config[mod.listing.id]);
if (changed.problems.length) throw new Error(`${mod.listing.id}: ${changed.problems.join("; ")}`);
mod.data.config = changed.config;
}
const outbox = new Map<number, unknown[]>();
const closed = new Set<number>();
// what host.download gives back, by url. other urls fail
@@ -97,7 +111,7 @@ export async function test_game(mods_dir: string, save?: WorldSave, seed = "test
host,
{ level: save?.level, chunks },
seed,
mod_sources(mods_dir),
mods,
);
// what it saves is read back from memory, like a host's region files
const save_game = game.save.bind(game);
+115
View File
@@ -0,0 +1,115 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// mods' config.json: their settings, which servers and tests can change, see docs/modding/misc/config.md
import { assert, assertEquals, assertRejects } from "@std/assert";
import { read_bmod, write_bmod } from "$/common/bmod.ts";
import type { ModData } from "$/common/mod_loader.ts";
import { apply_config, load_bmods } from "$/server/load_bmods.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 config_file = (config: unknown) => JSON.stringify({ format_version: 1, config });
Deno.test("changing a mod's config only changes settings it has, to the same kind of value", () => {
const config = { max_speed: 256, stress: { enabled: true, impact: { press: 8, mixer: 4 } }, fuels: ["coal"] };
const changed = apply_config(config, { max_speed: 64, stress: { impact: { press: 2 } }, fuels: [] });
assertEquals(changed.problems, []);
assertEquals(changed.config, {
max_speed: 64,
stress: { enabled: true, impact: { press: 2, mixer: 4 } },
fuels: [],
});
assertEquals(config.stress.impact.press, 8, "the mod's own settings stay as they were");
const wrong = apply_config(config, { max_sped: 64, stress: { enabled: "no", impact: 8 }, fuels: "coal" });
assertEquals(wrong.problems.map((problem) => problem.split(" ")[0]), [
"max_sped",
"stress.enabled",
"stress.impact",
"fuels",
]);
});
Deno.test("a server script reads its mod's config, with the changes a test makes", async () => {
const dir = Deno.makeTempDirSync({ prefix: "bworld_config_" });
copy_dir("mods/bworld", `${dir}/bworld`);
create_mod("copper_tools", "Copper Tools", dir);
Deno.writeTextFileSync(
`${dir}/copper_tools/scripts/server.ts`,
`import type { ServerContext } from "bworld/server";
export function setup(ctx: ServerContext) {
ctx.components.register_block("copper_tools:announce", {});
ctx.commands.register("settings", {
description: "Shows the settings",
usage: "/settings",
run: (_args, player) => player.send_message(JSON.stringify(ctx.config)),
});
}
`,
);
// what the script sees
const config_of = async (changes?: Record<string, Record<string, unknown>>) => {
const { join, send, take } = await test_game(dir, undefined, undefined, changes);
join(1, "bob");
send(1, { type: "chat", text: "/settings" });
return JSON.parse(take(1).find((m) => m.type === "chat" && m.text.startsWith("{")).text);
};
assertEquals(await config_of(), {}, "a mod without config.json has no settings");
Deno.writeTextFileSync(`${dir}/copper_tools/config.json`, config_file({ speed: 16, ore: { size: 4, depth: 30 } }));
assertEquals(await config_of(), { speed: 16, ore: { size: 4, depth: 30 } });
assertEquals(await config_of({ copper_tools: { ore: { size: 8 } } }), { speed: 16, ore: { size: 8, depth: 30 } });
await assertRejects(() => config_of({ copper_tools: { sped: 1 } }), Error, "sped");
Deno.writeTextFileSync(`${dir}/copper_tools/config.json`, config_file([16]));
const errors = load_and_check(dir).find((mod) => mod.id === "copper_tools")!.report.errors;
assert(errors.some((error) => error.startsWith("config.json")), errors.join("\n"));
Deno.removeSync(dir, { recursive: true });
});
Deno.test("a server changes a mod's settings with config/<mod id>.json, and players never get them", async () => {
const dir = Deno.makeTempDirSync({ prefix: "bworld_config_server_" });
const data: ModData = {
blocks: [],
models: [],
items: [],
recipes: [],
ores: [],
spawns: [],
entities: [],
entity_models: [],
effects: [],
tags: [],
config: { speed: 16, ore: { size: 4, depth: 30 } },
};
Deno.mkdirSync(`${dir}/server_mods`);
Deno.mkdirSync(`${dir}/config`);
Deno.writeFileSync(
`${dir}/server_mods/tiny.bmod`,
write_bmod({
manifest: { format_version: 1, id: "tiny", name: "Tiny", version: "1.0.0" },
data,
scripts: {},
textures: new Map(),
}),
);
const config_of = async () => (await load_bmods(`${dir}/server_mods`, [], `${dir}/config`))[0].bmod.data.config;
assertEquals(await config_of(), data.config, "without a file, the mod's own");
Deno.writeTextFileSync(`${dir}/config/tiny.json`, config_file({ ore: { depth: 50 } }));
assertEquals(await config_of(), { speed: 16, ore: { size: 4, depth: 50 } });
const [mod] = await load_bmods(`${dir}/server_mods`, [], `${dir}/config`);
assertEquals(read_bmod(mod.client_bytes, mod.listing.file).data.config, undefined);
Deno.writeTextFileSync(`${dir}/config/tiny.json`, config_file({ ore: { depht: 50 } }));
await assertRejects(config_of, Error, "ore.depht");
Deno.writeTextFileSync(`${dir}/config/tiny.json`, JSON.stringify({ ore: { depth: 50 } }));
await assertRejects(config_of, Error, "tiny.json");
Deno.removeSync(dir, { recursive: true });
});