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
+37 -6
View File
@@ -44,6 +44,7 @@ import type {
import type { FeatureJson } from "./worldgen/features.ts";
import { register_block_item } from "./utils.ts";
import { find_model, type ModelFileJson, type ModelJson, resolve_models } from "./block_models.ts";
import { loading_mod } from "./hooks.ts";
// everything a mod's json files hold, merged into one file by the build (build/mods/<id>/<hash>/data.json)
export interface ModData {
@@ -81,7 +82,7 @@ export interface ModData {
config?: Record<string, unknown>;
}
// a mod as the server lists it to clients, in load order: the .cubi players download, without its server script
// a mod as the server lists it to clients, in load order: the .cubi players download, without its terrain or config
export interface ModListing {
id: string;
name: string;
@@ -100,7 +101,7 @@ export class RecipeBook {
// recipes of mods' own types, by type, and the mod with the first one of each, for the error when nothing registers it
by_type = new Map<string, ModRecipeJson[]>();
first_used_by = new Map<string, string>();
// the types shared scripts registered, and their recipes as parse made them, see SharedContext.recipes.register_type
// the types shared phases registered, and their recipes as parse made them, see SharedContext.recipes.register_type
types = new Map<string, RecipeType<unknown, unknown>>();
parsed = new Map<string, unknown[]>();
// every recipe as its json, in load order, for recipe viewers
@@ -111,12 +112,42 @@ export class RecipeBook {
}
export class ModLoadError extends Error {
// the message without the mod's id
readonly problem: string;
constructor(mod: string, message: string) {
super(`${mod}: ${message}`);
this.name = "ModLoadError";
this.problem = message;
}
}
// what a mod's script can export, each called with its context where and when it runs: content on the server while
// it loads mods, shared on the server and every client, then server or client, and worldgen wherever chunks are
// generated. see docs/modding/concepts/sides.md
export const PHASES = ["content", "shared", "server", "client", "worldgen"] as const;
export type Phase = typeof PHASES[number];
// imports a mod's script and calls its export for phase, if it has one, with the context context makes. a realm
// imports the same url once, so every phase there gets the same module and what it keeps at its top level
export async function run_phase(
mod: { id: string; engine_access?: boolean },
url: string,
phase: Phase,
context: () => unknown,
): Promise<void> {
await loading_mod(mod.id, mod.engine_access ?? false, async () => {
const module = await import(url);
if (!PHASES.some((name) => name in module)) {
throw new ModLoadError(mod.id, `its script exports none of ${PHASES.join(", ")}`);
}
const run = module[phase];
if (run === undefined) return;
if (typeof run !== "function") throw new ModLoadError(mod.id, `its script's ${phase} export isn't a function`);
await run(context());
});
}
// whether a block, item, entity or biome is registered, for tags. air isn't a registered block, but tags can hold it
export function is_registered(registry: TagRegistry, id: string): boolean {
return (registry === "blocks" && id === "cubi:air") || EverythingRegistry.get(registry, id) !== undefined;
@@ -183,7 +214,7 @@ export function register_mod_data(mods: { id: string; data: ModData }[]): Recipe
}
for (const json of data.noise_settings ?? []) EverythingRegistry.register("noise_settings", json.id, json);
for (const json of data.biome_sources ?? []) EverythingRegistry.register("biome_sources", json.id, json);
// compiled with the types worldgen scripts register, see common/worldgen_loader.ts
// compiled with the types worldgen phases register, see common/worldgen_loader.ts
for (const json of data.features ?? []) EverythingRegistry.register("features", json.id, json);
} catch (e) {
throw new ModLoadError(id, (e as Error).message);
@@ -212,7 +243,7 @@ export function register_mod_data(mods: { id: string; data: ModData }[]): Recipe
return recipes;
}
// puts mod's recipe in the book, from its recipes/ or its shared script. throws when its registered type's parse throws
// puts mod's recipe in the book, from its recipes/ or its shared phase. throws when its registered type's parse throws
export function add_recipe(recipes: RecipeBook, recipe: RecipeJson, mod: string) {
recipes.all.push(recipe);
if (is_mod_recipe(recipe)) {
@@ -276,7 +307,7 @@ function checked_item(name: string, problems: string[], id: string): string {
return id;
}
// once every shared script has set up: a recipe of a type no mod registered, which nothing would read, is an error
// once every shared phase has set up: a recipe of a type no mod registered, which nothing would read, is an error
// for the first mod with one, like a misspelled type
export function check_recipe_types(recipes: RecipeBook) {
for (const [type, mod] of recipes.first_used_by) {
@@ -297,7 +328,7 @@ export function recipe_lookup(recipes: () => RecipeBook): RecipeLookup {
};
}
// ctx.recipes for shared scripts, the same on the server and every client: a recipe registered there is in the book
// ctx.recipes for shared phases, the same on the server and every client: a recipe registered there is in the book
// on every side without being sent, like one from the mod's recipes/. checked like a data file's. fail throws, as the
// side's error for the mod. list reads the book as it is, so a script sees the recipes of the ones before it
export function shared_recipes_api(