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
+35 -8
View File
@@ -128,6 +128,35 @@ export class ModLoadError extends Error {
export const PHASES = ["content", "shared", "server", "client", "worldgen"] as const;
export type Phase = typeof PHASES[number];
// the checks everything a mod registers from code goes through, the same on every side: only while the phase that
// registers it runs, in the mod's own namespace, and once. running says whether that phase is running. what names the
// kind of thing, like "component"
export function registrar(mod: string, phase: Phase, running: () => boolean) {
const fail = (message: string): never => {
throw new ModLoadError(mod, message);
};
const checks = {
fail,
running(what: string) {
if (!running()) fail(`${what} can only be registered while the ${phase} phase runs`);
},
own(what: string, id: string) {
if (typeof id !== "string" || !id.startsWith(`${mod}:`)) {
fail(`${what} ${id} must be in the namespace "${mod}"`);
}
},
// all three, taken saying whether id is registered already
new_id(what: string, id: string, taken: boolean) {
checks.running(what);
checks.own(what, id);
if (taken) fail(`${what} ${id} is registered twice`);
},
};
return checks;
}
export type Registrar = ReturnType<typeof registrar>;
// 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(
@@ -329,18 +358,18 @@ export function recipe_lookup(recipes: () => RecipeBook): RecipeLookup {
}
// 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
// on every side without being sent, like one from the mod's recipes/. checked like a data file's. list reads the book
// as it is, so a script sees the recipes of the ones before it
export function shared_recipes_api(
mod: string,
recipes: () => RecipeBook,
setting_up: () => boolean,
fail: (message: string) => never,
register: Registrar,
): SharedContext["recipes"] {
const fail = register.fail;
return {
...recipe_lookup(recipes),
register(type, json) {
if (!setting_up()) fail("recipes can only be registered during setup");
register.running("recipe");
const recipe = { ...structuredClone(json), type };
const problems = validate_recipe(recipe);
if (problems.length > 0) fail(`recipe of type ${type}: ${problems.join(", ")}`);
@@ -353,10 +382,8 @@ export function shared_recipes_api(
list: (type) => structuredClone(type ? recipes().all.filter((recipe) => recipe.type === type) : recipes().all),
// the recipes of the type there are so far are parsed now, and the ones registered after as they come
register_type(type, rules) {
if (!setting_up()) fail("recipe types can only be registered during setup");
if (!type.startsWith(`${mod}:`)) fail(`recipe type ${type} must be in the namespace "${mod}"`);
const book = recipes();
if (book.types.has(type)) fail(`recipe type ${type} is already registered`);
register.new_id("recipe type", type, book.types.has(type));
const type_rules = rules as RecipeType<unknown, unknown>;
try {
book.parsed.set(