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
+38 -43
View File
@@ -1,7 +1,7 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// runs mods' server scripts: builds each one's ServerContext and keeps what they register.
// runs mods' server phases: builds each one's ServerContext and keeps what they register.
// see docs/modding/. parts not built yet throw when used, saying so
import { BlockRegistry, EverythingRegistry, fluid_height, ItemRegistry } from "@/common/everything_registry.ts";
import { Container as EngineContainer, ItemStack as EngineItemStack, max_stack } from "@/common/inventory.ts";
@@ -23,7 +23,6 @@ import { fluid_exists, FluidRules, same_fluid } from "@/common/fluids.ts";
import { interact, item_handler, tank_handler, transfer, use_container } from "./fluid_handlers.ts";
import type { SharedContext } from "@/common/mod_api/shared.ts";
import { tag_api } from "@/common/tags.ts";
import { loading_mod } from "@/common/hooks.ts";
import type {
BlockComponent,
BlockRef,
@@ -52,7 +51,14 @@ import type {
ServerContext,
SlotOptions,
} from "@/common/mod_api/server.ts";
import { check_recipe_types, ModLoadError, recipe_lookup, shared_recipes_api } from "@/common/mod_loader.ts";
import {
check_recipe_types,
ModLoadError,
type Phase,
recipe_lookup,
run_phase,
shared_recipes_api,
} from "@/common/mod_loader.ts";
import {
argument,
block as block_argument,
@@ -114,7 +120,6 @@ export class Signal<T> {
// the mod whose code is running right now, for apis shared between mods that need to know who called
let running_mod = "";
// a mod's script, imported from url. engine_access lets it import cubi/engine
// a component a block lists, with its params from the block's json
export interface BlockComponentOf {
mod: string;
@@ -123,6 +128,7 @@ export interface BlockComponentOf {
params: unknown;
}
// a mod's script, imported from url. engine_access lets it import cubi/engine
interface ScriptSource {
mod: string;
version: string;
@@ -159,13 +165,13 @@ interface Timer {
export class ModRuntime {
block_components = new Map<string, { mod: string; component: BlockComponent }>();
// what shared scripts registered, see common/block_rules.ts, running on the game's world
// what shared phases registered, see common/block_rules.ts, running on the game's world
rules = new BlockRules(
run_guarded,
(x, y, z) => this.game.world.get_block_id(x, y, z),
(x, y, z, from) => this.#neighbor_changed(x, y, z, from),
);
// the fluids' rules from shared scripts, see common/fluids.ts
// the fluids' rules from shared phases, see common/fluids.ts
fluid_rules = new FluidRules(run_guarded);
item_components = new Map<string, { mod: string; component: ItemComponent }>();
effect_components = new Map<string, { mod: string; component: EffectComponent }>();
@@ -264,30 +270,45 @@ export class ModRuntime {
}
game!: GameServer;
// imports each server script and calls its setup, in load order
// shared scripts, the block rules clients run too. their world is the server's
// runs each script's shared phase in load order: the block rules clients run too. their world is the server's
async load_shared_scripts(game: GameServer, scripts: ScriptSource[]) {
this.game = game;
this.#shared_setup = true;
try {
for (const { mod, version, url, engine_access, config } of scripts) {
await loading_mod(mod, engine_access, () => this.#load_shared_script(mod, version, url, config));
}
await this.#run_phase(scripts, "shared", (script) => this.#shared_context(script));
check_recipe_types(game.recipes);
} finally {
this.#shared_setup = false;
}
}
// while shared scripts set up, the only time they can register recipes, as on clients
// while shared phases run, the only time they can register recipes, as on clients
#shared_setup = false;
async #load_shared_script(mod: string, version: string, url: string, config: Record<string, unknown>) {
const module = await import(url);
if (typeof module.setup !== "function") {
throw new ModLoadError(mod, "the shared script doesn't export a setup function");
// runs each script's server phase in load order
async load_scripts(game: GameServer, scripts: ScriptSource[]) {
this.game = game;
await this.#run_phase(scripts, "server", ({ mod, version, config }) => this.#context(mod, version, config));
}
async #run_phase(scripts: ScriptSource[], phase: Phase, context: (script: ScriptSource) => unknown) {
for (const script of scripts) {
running_mod = script.mod;
try {
await run_phase(
{ id: script.mod, engine_access: script.engine_access },
script.url,
phase,
() => context(script),
);
} finally {
running_mod = "";
}
}
}
#shared_context({ mod, version, config }: ScriptSource): SharedContext {
const { world, blocks } = this.#context(mod, version, config);
const ctx: SharedContext = {
return {
mod: { id: mod, version },
side: "server",
tags: tag_api(),
@@ -322,32 +343,6 @@ export class ModRuntime {
},
},
};
running_mod = mod;
try {
await module.setup(ctx);
} finally {
running_mod = "";
}
}
async load_scripts(game: GameServer, scripts: ScriptSource[]) {
this.game = game;
for (const { mod, version, url, engine_access, config } of scripts) {
await loading_mod(mod, engine_access, () => this.#load_script(mod, version, url, config));
}
}
async #load_script(mod: string, version: string, url: string, config: Record<string, unknown>) {
const module = await import(url);
if (typeof module.setup !== "function") {
throw new ModLoadError(mod, "the server script doesn't export a setup function");
}
running_mod = mod;
try {
await module.setup(this.#context(mod, version, config));
} finally {
running_mod = "";
}
}
// every effect mods define, from their json. they're made before scripts run, since the world loads first and