Engine api
This commit is contained in:
+62
-45
@@ -7,6 +7,7 @@ import { covers_face } from "$/common/block_shapes.ts";
|
||||
import { BlockRules } from "$/common/block_rules.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,
|
||||
@@ -87,6 +88,14 @@ 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 bworld/engine
|
||||
interface ScriptSource {
|
||||
mod: string;
|
||||
version: string;
|
||||
url: string;
|
||||
engine_access: boolean;
|
||||
}
|
||||
|
||||
export function run_guarded<T>(mod: string, what: string, fn: () => T): T | undefined {
|
||||
const previous = running_mod;
|
||||
running_mod = mod;
|
||||
@@ -213,56 +222,64 @@ export class ModRuntime {
|
||||
|
||||
// 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
|
||||
async load_shared_scripts(game: GameServer, scripts: { mod: string; version: string; url: string }[]) {
|
||||
async load_shared_scripts(game: GameServer, scripts: ScriptSource[]) {
|
||||
this.game = game;
|
||||
for (const { mod, version, url } of scripts) {
|
||||
const module = await import(url);
|
||||
if (typeof module.setup !== "function") {
|
||||
throw new ModLoadError(mod, "the shared script doesn't export a setup function");
|
||||
}
|
||||
const { world } = this.#context(mod, version);
|
||||
const ctx: SharedContext = {
|
||||
mod: { id: mod, version },
|
||||
side: "server",
|
||||
tags: tag_api(),
|
||||
world: {
|
||||
get_block: world.get_block,
|
||||
get_state: world.get_state,
|
||||
set_state: world.set_state,
|
||||
is_face_sturdy: world.is_face_sturdy,
|
||||
},
|
||||
components: {
|
||||
register_block: (id, rules) => {
|
||||
if (!this.#setting_up) throw new ModLoadError(mod, "rules can only be registered during setup");
|
||||
if (!id.startsWith(`${mod}:`)) {
|
||||
throw new ModLoadError(mod, `component ${id} must be in the namespace "${mod}"`);
|
||||
}
|
||||
this.rules.register(mod, id, rules);
|
||||
},
|
||||
},
|
||||
};
|
||||
running_mod = mod;
|
||||
try {
|
||||
await module.setup(ctx);
|
||||
} finally {
|
||||
running_mod = "";
|
||||
}
|
||||
for (const { mod, version, url, engine_access } of scripts) {
|
||||
await loading_mod(mod, engine_access, () => this.#load_shared_script(mod, version, url));
|
||||
}
|
||||
}
|
||||
|
||||
async load_scripts(game: GameServer, scripts: { mod: string; version: string; url: string }[]) {
|
||||
async #load_shared_script(mod: string, version: string, url: string) {
|
||||
const module = await import(url);
|
||||
if (typeof module.setup !== "function") {
|
||||
throw new ModLoadError(mod, "the shared script doesn't export a setup function");
|
||||
}
|
||||
const { world } = this.#context(mod, version);
|
||||
const ctx: SharedContext = {
|
||||
mod: { id: mod, version },
|
||||
side: "server",
|
||||
tags: tag_api(),
|
||||
world: {
|
||||
get_block: world.get_block,
|
||||
get_state: world.get_state,
|
||||
set_state: world.set_state,
|
||||
is_face_sturdy: world.is_face_sturdy,
|
||||
},
|
||||
components: {
|
||||
register_block: (id, rules) => {
|
||||
if (!this.#setting_up) throw new ModLoadError(mod, "rules can only be registered during setup");
|
||||
if (!id.startsWith(`${mod}:`)) {
|
||||
throw new ModLoadError(mod, `component ${id} must be in the namespace "${mod}"`);
|
||||
}
|
||||
this.rules.register(mod, id, rules);
|
||||
},
|
||||
},
|
||||
};
|
||||
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 } of scripts) {
|
||||
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));
|
||||
} finally {
|
||||
running_mod = "";
|
||||
}
|
||||
for (const { mod, version, url, engine_access } of scripts) {
|
||||
await loading_mod(mod, engine_access, () => this.#load_script(mod, version, url));
|
||||
}
|
||||
}
|
||||
|
||||
async #load_script(mod: string, version: string, url: string) {
|
||||
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));
|
||||
} finally {
|
||||
running_mod = "";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user