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
+1 -1
View File
@@ -1,7 +1,7 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// what engine access mods get as engine.client, in client scripts. see docs/modding/misc/engine.md
// what engine access mods get as engine.client, in client phases. see docs/modding/misc/engine.md
import { Client } from "./client.ts";
import { Entity } from "./entity/entity.ts";
import { LocalPlayer } from "./entity/local_player.ts";
+1 -1
View File
@@ -1,7 +1,7 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// the fluids' rules from the mods' shared scripts on this client, the same as the server's (see common/fluids.ts), for
// the fluids' rules from the mods' shared phases on this client, the same as the server's (see common/fluids.ts), for
// drawing fluids and naming them in tooltips. its own module so those don't import client/mods.ts, which loads the mods
// and makes it again for each server's
import { FluidRules } from "@/common/fluids.ts";
+2 -2
View File
@@ -1,7 +1,7 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// screens mods' client scripts make, see docs/modding/gui/index.md: the screens of their menus, and screens with no
// screens mods' client phases make, see docs/modding/gui/index.md: the screens of their menus, and screens with no
// menu. a mod's screen that throws is closed, with the error in the console
import { TICK_DELTA } from "@/common/constants.ts";
import type { ClientMenu as ModClientMenu, MenuScreen, Screen } from "@/common/mod_api/client.ts";
@@ -98,7 +98,7 @@ export class GuiModMenuScreen extends GuiMenuScreen {
}
}
// a screen with no menu, opened by a client script
// a screen with no menu, opened by a mod's client phase
export class GuiModScreen extends GuiScreen {
#mod: string;
#screen: Screen;
+1 -1
View File
@@ -1,7 +1,7 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// the immediate mode drawing mods' client scripts get (Graphics in common/mod_api/client.ts), on top of the renderer.
// the immediate mode drawing mods' client phases get (Graphics in common/mod_api/client.ts), on top of the renderer.
// one per screen, so text inputs keep their text and focus while it's open. mods work in gui pixels, GUI_SCALE canvas
// pixels each
import { EverythingRegistry, type ItemRegistry } from "@/common/everything_registry.ts";
+1 -1
View File
@@ -31,7 +31,7 @@ const ICON_GAP = 4;
// from the mouse, so the cursor doesn't cover it
const OFFSET = 16;
// what client scripts add to every item's tooltip, in load order, see ctx.ui.add_item_tooltip. client/mods.ts fills it,
// what client phases add to every item's tooltip, in load order, see ctx.ui.add_item_tooltip. client/mods.ts fills it,
// each wrapped so one throwing is logged under its mod
export const item_tooltips: ((item: ApiItemStack, lines: TooltipLine[]) => void)[] = [];
+24 -33
View File
@@ -1,8 +1,8 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// loads the mods the server lists: downloads their .cubi files, registers their data, then runs their client scripts.
// see docs/modding/networking/index.md
// loads the mods the server lists: downloads their .cubi files, registers their data, then runs their scripts' shared
// and client phases. see docs/modding/networking/index.md
import { AIR } from "@/common/constants.ts";
import { BlockRegistry, EverythingRegistry, type ItemRegistry } from "@/common/everything_registry.ts";
import type {
@@ -19,9 +19,11 @@ import {
check_recipe_types,
ModListing,
ModLoadError,
type Phase,
recipe_lookup,
RecipeBook,
register_mod_data,
run_phase,
shared_recipes_api,
} from "@/common/mod_loader.ts";
import { type Mod, read_mod } from "@/common/mod.ts";
@@ -37,7 +39,7 @@ import type { Client } from "./client.ts";
import { code_url, fetch_verified } from "./handshake.ts";
import { GuiModScreen } from "./gui/gui_mod_screens.ts";
import { item_tooltips } from "./gui/tooltip.ts";
import { clear_hooks, expose_engine, loading_mod } from "@/common/hooks.ts";
import { clear_hooks, expose_engine } from "@/common/hooks.ts";
import { common_engine } from "@/common/engine_modules.ts";
import { client_engine } from "./engine_modules.ts";
import { block_visuals } from "./rendering/visuals.ts";
@@ -130,11 +132,11 @@ const rules_world: SharedWorld = {
},
};
// what the mods' shared scripts registered, run to guess ahead of the server, see common/block_rules.ts
// what the mods' shared phases registered, run to guess ahead of the server, see common/block_rules.ts
export let block_rules = new BlockRules(run_mod, rules_world.get_block);
// whether shared scripts are being set up, the only time they can register rules
// whether shared phases are being set up, the only time they can register rules
let rules_setup = false;
// whether client scripts are being set up, the only time they can register visuals, since chunk workers are told
// whether client phases are being set up, the only time they can register visuals, since chunk workers are told
// which blocks have them when the level starts
let client_setup = false;
@@ -186,7 +188,7 @@ export async function download_mods(listings: ModListing[], base: URL): Promise<
}));
}
// registers the mods' data and runs their client scripts, in the order the server listed them
// registers the mods' data and runs their scripts' shared and client phases, in the order the server listed them
export async function load_client_mods(listings: ModListing[], mods: Mod[]) {
for (const [i, mod] of mods.entries()) {
if (mod.credits) {
@@ -209,39 +211,28 @@ export async function load_client_mods(listings: ModListing[], mods: Mod[]) {
// for engine access mods. the last server's hooks go first
clear_hooks();
expose_engine({ common: common_engine(), client: client_engine() });
const as_mod = (mod: Mod, fn: () => Promise<void>) =>
loading_mod(mod.manifest.id, mod.manifest.engine_access ?? false, fn);
// every mod's shared rules first, so client scripts can count on any mod's being there
// one url for each mod's script, so its phases share one module
const urls = mods.map((mod) => mod.script === undefined ? undefined : script_url(mod.script));
const run = async (phase: Phase, context: (i: number) => unknown) => {
for (const [i, mod] of mods.entries()) {
const url = urls[i];
if (url === undefined) continue;
const as = { id: mod.manifest.id, engine_access: mod.manifest.engine_access };
await run_phase(as, url, phase, () => context(i));
}
};
// every mod's shared rules first, so client phases can count on any mod's being there
block_rules = new BlockRules(run_mod, rules_world.get_block);
set_fluid_rules(new FluidRules(run_mod));
rules_setup = true;
for (const [i, mod] of mods.entries()) {
const code = mod.scripts.shared;
if (!code) continue;
await as_mod(mod, async () => {
const module = await import(script_url(code));
if (typeof module.setup !== "function") {
throw new ModLoadError(mod.manifest.id, "the shared script doesn't export a setup function");
}
await module.setup(shared_context(listings[i]));
});
}
await run("shared", (i) => shared_context(listings[i]));
rules_setup = false;
check_recipe_types(recipe_book);
client_setup = true;
for (const [i, mod] of mods.entries()) {
const code = mod.scripts.client;
if (!code) continue;
await as_mod(mod, async () => {
const module = await import(script_url(code));
if (typeof module.setup !== "function") {
throw new ModLoadError(mod.manifest.id, "the client script doesn't export a setup function");
}
await module.setup(client_context(listings[i]));
});
}
await run("client", (i) => client_context(listings[i]));
client_setup = false;
}
@@ -249,7 +240,7 @@ function script_url(code: string) {
return code_url(new TextEncoder().encode(code));
}
// ctx.blocks, the same in shared and client scripts
// ctx.blocks, the same in shared and client phases
function blocks_api(mod: string): SharedContext["blocks"] {
return {
exists: (id) => EverythingRegistry.get("blocks", id) !== undefined,