sharing contexts
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
// the mod's data before anything else reads it
|
||||
import { type BlockJson, validate_block } from "./mod_data.ts";
|
||||
import type { ContentContext } from "./mod_api/content.ts";
|
||||
import { ModLoadError, run_phase } from "./mod_loader.ts";
|
||||
import { ModLoadError, registrar, run_phase } from "./mod_loader.ts";
|
||||
import { expose_engine } from "./hooks.ts";
|
||||
import { common_engine } from "./engine_modules.ts";
|
||||
|
||||
@@ -38,20 +38,19 @@ export async function run_content_script(
|
||||
// the whole script loads here, and one with engine access can import cubi/engine. only engine.common is here
|
||||
expose_engine({ common: common_engine() });
|
||||
const blocks: BlockJson[] = [];
|
||||
let setting_up = true;
|
||||
let running = true;
|
||||
const register = registrar(mod, "content", () => running);
|
||||
const ctx: ContentContext = {
|
||||
mod: { id: mod, version },
|
||||
blocks: {
|
||||
register(block) {
|
||||
if (!setting_up) throw new Error("blocks can only be registered during setup");
|
||||
register.running("block");
|
||||
// what a file could hold, so the server, its players and its worldgen workers all get the same thing
|
||||
const json = JSON.parse(JSON.stringify(block ?? null));
|
||||
const id = typeof json?.id === "string" ? json.id : "without an id";
|
||||
const problems = validate_block(json);
|
||||
if (problems.length > 0) throw new Error(`block ${id}: ${problems.join("; ")}`);
|
||||
if (json.id.split(":")[0] !== mod) {
|
||||
throw new Error(`block ${json.id} isn't in this mod's namespace "${mod}"`);
|
||||
}
|
||||
if (problems.length > 0) register.fail(`block ${id}: ${problems.join("; ")}`);
|
||||
register.own("block", json.id);
|
||||
blocks.push(json);
|
||||
},
|
||||
},
|
||||
@@ -63,6 +62,6 @@ export async function run_content_script(
|
||||
const problem = e instanceof ModLoadError ? e.problem : e instanceof Error ? e.message : String(e);
|
||||
return { blocks, problems: [problem] };
|
||||
} finally {
|
||||
setting_up = false;
|
||||
running = false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user