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
+23 -72
View File
@@ -23,16 +23,17 @@ import {
recipe_lookup,
RecipeBook,
register_mod_data,
registrar,
run_phase,
shared_recipes_api,
} from "@/common/mod_loader.ts";
import { blocks_api, shared_context } from "@/common/shared_context.ts";
import { type Mod, read_mod } from "@/common/mod.ts";
import { get_state_value, set_state_value, shared_items_api } from "@/common/utils.ts";
import { BlockRules } from "@/common/block_rules.ts";
import { fluid_exists, FluidRules, same_fluid } from "@/common/fluids.ts";
import { FluidRules } from "@/common/fluids.ts";
import { fluid_rules, set_fluid_rules } from "./fluid_rules.ts";
import { covers_face, EMPTY_SHAPE, FULL_SHAPE, is_solid } from "@/common/block_shapes.ts";
import type { SharedContext, SharedWorld } from "@/common/mod_api/shared.ts";
import { covers_face, EMPTY_SHAPE, FULL_SHAPE } from "@/common/block_shapes.ts";
import type { SharedWorld } from "@/common/mod_api/shared.ts";
import { tag_api } from "@/common/tags.ts";
import { AIR_ID } from "@/common/protocol.ts";
import type { Client } from "./client.ts";
@@ -227,7 +228,15 @@ export async function load_client_mods(listings: ModListing[], mods: Mod[]) {
block_rules = new BlockRules(run_mod, rules_world.get_block);
set_fluid_rules(new FluidRules(run_mod));
rules_setup = true;
await run("shared", (i) => shared_context(listings[i]));
await run("shared", (i) =>
shared_context({ id: listings[i].id, version: listings[i].version }, {
side: "client",
world: rules_world,
block_rules,
fluid_rules,
recipes: () => recipe_book,
running: () => rules_setup,
}));
rules_setup = false;
check_recipe_types(recipe_book);
@@ -240,56 +249,6 @@ function script_url(code: string) {
return code_url(new TextEncoder().encode(code));
}
// ctx.blocks, the same in shared and client phases
function blocks_api(mod: string): SharedContext["blocks"] {
return {
exists: (id) => EverythingRegistry.get("blocks", id) !== undefined,
components(id) {
const block = EverythingRegistry.get<BlockRegistry>("blocks", id);
if (!block) throw new Error(`[${mod}] unknown block ${id}`);
return structuredClone(block.components ?? {});
},
solid(id) {
if (id === AIR_ID) return false;
const block = EverythingRegistry.get<BlockRegistry>("blocks", id);
if (!block) throw new Error(`[${mod}] unknown block ${id}`);
return is_solid(block);
},
};
}
function shared_context(listing: ModListing): SharedContext {
const mod = listing.id;
return {
mod: { id: mod, version: listing.version },
side: "client",
tags: tag_api(),
blocks: blocks_api(mod),
items: shared_items_api(mod, fluid_rules),
fluids: {
exists: fluid_exists,
same: same_fluid,
register(id, rules) {
if (!rules_setup) throw new Error(`[${mod}] rules can only be registered during setup`);
fluid_rules.register(mod, id, rules);
},
},
recipes: shared_recipes_api(mod, () => recipe_book, () => rules_setup, (message) => {
throw new Error(`[${mod}] ${message}`);
}),
world: rules_world,
components: {
register_block(id, rules) {
if (!id.startsWith(`${mod}:`)) {
throw new Error(`[${mod}] component ${id} must be in the namespace "${mod}"`);
}
if (!rules_setup) throw new Error(`[${mod}] rules can only be registered during setup`);
block_rules.register(mod, id, rules);
},
},
};
}
function client_context(listing: ModListing): ClientContext {
const mod = listing.id;
const not_yet = (name: string, where: string) =>
@@ -305,11 +264,7 @@ function client_context(listing: ModListing): ClientContext {
return client;
};
const own = (id: string, what = "channel") => {
if (!id.startsWith(`${mod}:`)) {
throw new Error(`[${mod}] ${what} ${id} must be in the namespace "${mod}"`);
}
};
const register = registrar(mod, "client", () => client_setup);
const known_item = (id: string) => {
const item = EverythingRegistry.get<ItemRegistry>("items", id);
if (!item) throw new Error(`[${mod}] unknown item ${id}`);
@@ -320,8 +275,8 @@ function client_context(listing: ModListing): ClientContext {
mod: { id: mod, version: listing.version },
ui: {
register_menu_screen(type, create) {
own(type, "menu type");
if (menu_screens.has(type)) throw new Error(`[${mod}] ${type} already has a screen`);
register.own("menu type", type);
if (menu_screens.has(type)) register.fail(`the screen of ${type} is registered twice`);
menu_screens.set(type, { mod, create });
},
open(screen) {
@@ -342,12 +297,8 @@ function client_context(listing: ModListing): ClientContext {
},
hud: {
register(id, layer) {
if (!client_setup) throw new Error(`[${mod}] hud layers can only be registered during setup`);
own(id, "hud layer");
register.new_id("hud layer", id, hud_layers.some((other) => other.id === id));
if (typeof layer?.on_render !== "function") throw new Error(`[${mod}] a hud layer needs an on_render`);
if (hud_layers.some((other) => other.id === id)) {
throw new Error(`[${mod}] hud layer ${id} already exists`);
}
hud_layers.push({ mod, id, layer });
},
on_overlay_message(fn) {
@@ -359,13 +310,13 @@ function client_context(listing: ModListing): ClientContext {
options: key_mappings(need_client),
net: {
on(channel, handler) {
own(channel);
register.own("channel", channel);
const handlers = net_handlers.get(channel) ?? [];
handlers.push({ mod, handler: handler as (data: unknown) => void });
net_handlers.set(channel, handlers);
},
send(channel, data) {
own(channel);
register.own("channel", channel);
need_client().connection.send({ type: "mod", channel, data });
},
},
@@ -451,7 +402,7 @@ function client_context(listing: ModListing): ClientContext {
return mod_name(id);
},
},
blocks: blocks_api(mod),
blocks: blocks_api(),
fluids: {
...fluid_rules.api(),
mod(id) {
@@ -470,12 +421,12 @@ function client_context(listing: ModListing): ClientContext {
tags: tag_api(),
render: {
register_visual(block, create, options) {
if (!client_setup) throw new Error(`[${mod}] visuals can only be registered during setup`);
register.running("visual");
const nid = EverythingRegistry.get_id("blocks", block);
if (nid === undefined) throw new Error(`[${mod}] unknown block ${block}`);
if (typeof create !== "function") throw new Error(`[${mod}] a visual needs a function that makes it`);
const other = block_visuals.get(nid);
if (other) throw new Error(`[${mod}] ${block} already has a visual, from ${other.mod}`);
if (other) register.fail(`${block} already has a visual, from ${other.mod}`);
block_visuals.set(nid, { mod, create, replaces_model: options?.replaces_model ?? false });
},
},