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
+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,