sharing contexts

This commit is contained in:
2026-11-14 13:49:21 -03:00
parent 313098892e
commit a69db0cd2a
13 changed files with 192 additions and 208 deletions
+4 -4
View File
@@ -71,12 +71,12 @@ Deno.test("check-mods runs a mod's content phase, and its files can name the blo
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
// only while the content phase runs
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",
"block can only be registered while the content phase runs",
);
});
@@ -128,7 +128,7 @@ Deno.test("a content phase's mistakes stop its mod loading, naming the script",
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"`,
`scripts/content.ts: block bworld:granite must be in the namespace "rocks"`,
],
[`ctx.blocks.register({ id: "rocks:broken" } as any);`, "scripts/content.ts: block rocks:broken: "],
[
@@ -155,6 +155,6 @@ Deno.test("a content phase's mistakes stop its mod loading, naming the script",
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"`,
`rocks.cubi: script.js: block bworld:granite must be in the namespace "rocks"`,
);
});
+1 -1
View File
@@ -154,7 +154,7 @@ Deno.test("a feature can't be both code and data", async () => {
await assertRejects(
() => test_game(clash),
Error,
"feature testmod:glass_pillar is registered twice, as code or data",
"feature testmod:glass_pillar is registered twice",
);
} finally {
Deno.removeSync(clash, { recursive: true });
+12 -17
View File
@@ -27,11 +27,12 @@ import type { ModelJson } from "@/common/block_models.ts";
import type { SavedChunk, SavedChunks, WorldSave } from "@/server/game/saves.ts";
import { FluidRules } from "@/common/fluids.ts";
import { BlockRules } from "@/common/block_rules.ts";
import { collision_shape_at, covers_face, is_solid } from "@/common/block_shapes.ts";
import { tag_api } from "@/common/tags.ts";
import { get_state_value, set_state_value, shared_items_api } from "@/common/utils.ts";
import { collision_shape_at, covers_face } from "@/common/block_shapes.ts";
import { get_state_value, set_state_value } from "@/common/utils.ts";
import type { Face, SharedWorld } from "@/common/mod_api/shared.ts";
import { shared as setup_shared } from "@/mods/bworld/scripts/shared.ts";
import { shared_context } from "@/common/shared_context.ts";
import { RecipeBook } from "@/common/mod_loader.ts";
// the repo's own mods/, read once: reading and checking every mod's files is most of starting a game in a test. only
// mods/ is kept, since tests change the temporary folders they make between games
@@ -377,6 +378,7 @@ export function client_of(game: GameServer, area: Area) {
const info = (id: string) => EverythingRegistry.get<BlockRegistry>("blocks", id);
const rules = new BlockRules((_mod, _what, fn) => fn(), (x, y, z) => at(x, y, z)?.[0]);
const recipes = new RecipeBook();
const world: SharedWorld = {
get_block: (x, y, z) => at(x, y, z)?.[0],
get_state(x, y, z, name) {
@@ -403,22 +405,15 @@ export function client_of(game: GameServer, area: Area) {
biome_at: () => undefined,
season_at: () => undefined,
};
setup_shared({
mod: { id: "bworld", version: "test" },
setup_shared(shared_context({ id: "bworld", version: "test" }, {
side: "client",
tags: tag_api(),
blocks: {
exists: (id) => info(id) !== undefined,
components: (id) => structuredClone(info(id)!.components ?? {}),
solid: (id) => id !== AIR_ID && is_solid(info(id)!),
},
items: shared_items_api("bworld", new FluidRules((_mod, _what, fn) => fn())),
// only its blocks are looked at here
fluids: { exists: () => false, same: () => false, register() {} },
recipes: { register() {}, list: () => [], register_type() {}, find: () => undefined, all: () => [] },
world,
components: { register_block: (id, block_rules) => rules.register("bworld", id, block_rules) },
});
block_rules: rules,
fluid_rules: new FluidRules((_mod, _what, fn) => fn()),
// its recipes aren't looked at here
recipes: () => recipes,
running: () => true,
}));
return {
// what game_mode does when this client places held against face of x y z, at hit
+1 -1
View File
@@ -86,6 +86,6 @@ Deno.test("hud layers are in the mod's namespace, one to an id", async () => {
ctx.hud.register("testmod:bar", ${layer});
}`),
Error,
"hud layer testmod:bar already exists",
"hud layer testmod:bar is registered twice",
);
});
+1 -1
View File
@@ -217,7 +217,7 @@ Deno.test("recipes can only be registered while shared phases set up", async ()
`(globalThis as any).testrecipes_late = () => ctx.recipes.register("testrecipes:sifting", {});`,
);
try {
await assertRejects(() => test_game(dir), Error, "recipes can only be registered during setup");
await assertRejects(() => test_game(dir), Error, "recipe can only be registered while the shared phase runs");
} finally {
delete (globalThis as Record<string, unknown>).testrecipes_late;
Deno.removeSync(dir, { recursive: true });