Mod config
This commit is contained in:
@@ -110,6 +110,8 @@ interface ScriptSource {
|
||||
version: string;
|
||||
url: string;
|
||||
engine_access: boolean;
|
||||
// its settings, see ServerContext.config
|
||||
config: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export function run_guarded<T>(mod: string, what: string, fn: () => T): T | undefined {
|
||||
@@ -242,17 +244,17 @@ export class ModRuntime {
|
||||
// shared scripts, the block rules clients run too. their world is the server's
|
||||
async load_shared_scripts(game: GameServer, scripts: ScriptSource[]) {
|
||||
this.game = game;
|
||||
for (const { mod, version, url, engine_access } of scripts) {
|
||||
await loading_mod(mod, engine_access, () => this.#load_shared_script(mod, version, url));
|
||||
for (const { mod, version, url, engine_access, config } of scripts) {
|
||||
await loading_mod(mod, engine_access, () => this.#load_shared_script(mod, version, url, config));
|
||||
}
|
||||
}
|
||||
|
||||
async #load_shared_script(mod: string, version: string, url: string) {
|
||||
async #load_shared_script(mod: string, version: string, url: string, config: Record<string, unknown>) {
|
||||
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 { world } = this.#context(mod, version, config);
|
||||
const ctx: SharedContext = {
|
||||
mod: { id: mod, version },
|
||||
side: "server",
|
||||
@@ -285,19 +287,19 @@ export class ModRuntime {
|
||||
|
||||
async load_scripts(game: GameServer, scripts: ScriptSource[]) {
|
||||
this.game = game;
|
||||
for (const { mod, version, url, engine_access } of scripts) {
|
||||
await loading_mod(mod, engine_access, () => this.#load_script(mod, version, url));
|
||||
for (const { mod, version, url, engine_access, config } of scripts) {
|
||||
await loading_mod(mod, engine_access, () => this.#load_script(mod, version, url, config));
|
||||
}
|
||||
}
|
||||
|
||||
async #load_script(mod: string, version: string, url: string) {
|
||||
async #load_script(mod: string, version: string, url: string, config: Record<string, unknown>) {
|
||||
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));
|
||||
await module.setup(this.#context(mod, version, config));
|
||||
} finally {
|
||||
running_mod = "";
|
||||
}
|
||||
@@ -573,7 +575,7 @@ export class ModRuntime {
|
||||
};
|
||||
}
|
||||
|
||||
#context(mod: string, version: string): ServerContext {
|
||||
#context(mod: string, version: string, config: Record<string, unknown>): ServerContext {
|
||||
const setup_only = (what: string) => {
|
||||
if (!this.#setting_up) {
|
||||
throw new Error(`[${mod}] ${what} can only be registered during setup`);
|
||||
@@ -588,6 +590,7 @@ export class ModRuntime {
|
||||
|
||||
const ctx: ServerContext = {
|
||||
mod: { id: mod, version },
|
||||
config,
|
||||
components: {
|
||||
register_block: (id, component) => {
|
||||
setup_only("components");
|
||||
|
||||
Reference in New Issue
Block a user