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
+8 -14
View File
@@ -7,7 +7,7 @@ import type { FeatureChunk, FeatureStep, WorldgenContext } from "@/common/mod_ap
import type { OreJson } from "@/common/mod_data.ts";
import type { SetupFeature, WorldgenSetup } from "./generation.ts";
import { EverythingRegistry } from "./everything_registry.ts";
import { ModLoadError, run_phase } from "./mod_loader.ts";
import { ModLoadError, registrar, run_phase } from "./mod_loader.ts";
import { compile_generator } from "./worldgen/generator.ts";
import type { WorldShape } from "./world_shape.ts";
import {
@@ -54,17 +54,12 @@ export async function load_worldgen(sources: WorldgenSources): Promise<WorldgenS
const data_features = (EverythingRegistry.get_registry<FeatureJson>("features") ?? []).filter((f) => f);
for (const { mod, url, engine_access } of sources.scripts) {
const own = (id: string, what: string) => {
if (!id.startsWith(`${mod}:`)) {
throw new ModLoadError(mod, `${what} ${id} must be in the namespace "${mod}"`);
}
};
let running = true;
const register = registrar(mod, "worldgen", () => running);
const ctx: WorldgenContext = {
register_feature(id, generate, options = {}) {
own(id, "feature");
if (pending.some((p) => p.feature.id === id) || data_features.some((f) => f.id === id)) {
throw new ModLoadError(mod, `feature ${id} is registered twice, as code or data`);
}
const taken = pending.some((p) => p.feature.id === id) || data_features.some((f) => f.id === id);
register.new_id("feature", id, taken);
const step = options.step ?? DEFAULT_STEP;
if (!FEATURE_STEPS.includes(step)) {
throw new ModLoadError(mod, `feature ${id}: step must be one of ${FEATURE_STEPS.join(", ")}`);
@@ -79,13 +74,11 @@ export async function load_worldgen(sources: WorldgenSources): Promise<WorldgenS
});
},
register_feature_type(id, place) {
own(id, "feature type");
if (types.has(id)) throw new ModLoadError(mod, `feature type ${id} is registered twice`);
register.new_id("feature type", id, types.has(id));
types.set(id, place as FeatureType);
},
register_placement_modifier(id, modify) {
own(id, "placement modifier");
if (modifiers.has(id)) throw new ModLoadError(mod, `placement modifier ${id} is registered twice`);
register.new_id("placement modifier", id, modifiers.has(id));
modifiers.set(id, modify as PlacementModifier);
},
register_terrain(id) {
@@ -97,6 +90,7 @@ export async function load_worldgen(sources: WorldgenSources): Promise<WorldgenS
};
await run_phase({ id: mod, engine_access }, url, "worldgen", () => ctx);
running = false;
}
// data features last, so they can use the types and modifiers every script registered