Mod config
This commit is contained in:
@@ -95,7 +95,12 @@ export async function start_game(
|
||||
}
|
||||
|
||||
function script_of(mod: ServerModSource) {
|
||||
return { mod: mod.listing.id, version: mod.listing.version, engine_access: mod.listing.engine_access ?? false };
|
||||
return {
|
||||
mod: mod.listing.id,
|
||||
version: mod.listing.version,
|
||||
engine_access: mod.listing.engine_access ?? false,
|
||||
config: mod.data.config ?? {},
|
||||
};
|
||||
}
|
||||
|
||||
export function data_url(code: string) {
|
||||
|
||||
@@ -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");
|
||||
|
||||
+4
-1
@@ -24,6 +24,9 @@ export interface HostOptions {
|
||||
build_dir: string;
|
||||
// the .bmod files to load
|
||||
server_mods_dir: string;
|
||||
// <mod id>.json files that change mods' settings, see docs/modding/misc/config.md. without it every mod uses its
|
||||
// own config.json
|
||||
config_dir?: string;
|
||||
// where DESKTOP_DOWNLOADS are, served at /dist/. without it there's nothing to download
|
||||
dist_dir?: string;
|
||||
// the world's folder: level.json, and the blocks in region/
|
||||
@@ -44,7 +47,7 @@ export interface Host {
|
||||
|
||||
// throws when the mods have problems, saying what they are
|
||||
export async function start_host(options: HostOptions): Promise<Host> {
|
||||
const mods = await load_bmods(options.server_mods_dir, engine_texture_ids(options.build_dir));
|
||||
const mods = await load_bmods(options.server_mods_dir, engine_texture_ids(options.build_dir), options.config_dir);
|
||||
console.log(`Mods: ${mods.map((mod) => `${mod.listing.id} ${mod.listing.version}`).join(", ") || "none"}`);
|
||||
// what players download, by path
|
||||
const downloads = new Map(mods.map((mod) => [`/${mod.listing.file}`, mod.client_bytes]));
|
||||
|
||||
+74
-5
@@ -1,12 +1,13 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// the server's mods: every .bmod in server_mods/, checked, sorted so dependencies load first, and with the copy
|
||||
// players download (the same mod without its server script) ready to serve
|
||||
// the server's mods: every .bmod in server_mods/, checked, with the server's changes to their config, sorted so
|
||||
// dependencies load first, and with the copy players download (the same mod without its server script) ready to serve
|
||||
import { join } from "@std/path";
|
||||
import { type Bmod, BMOD_EXTENSION, client_copy, read_bmod, write_bmod } from "$/common/bmod.ts";
|
||||
import { check_references, load_order, type LoadedMod } from "$/common/mod_check.ts";
|
||||
import type { ModListing } from "$/common/mod_loader.ts";
|
||||
import { FORMAT_VERSION, is_object } from "$/common/mod_data.ts";
|
||||
import { sha256 } from "$/common/utils.ts";
|
||||
|
||||
export interface ServerMod {
|
||||
@@ -23,9 +24,14 @@ export class ModsError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
// engine_textures are the engine: texture ids, for checking the textures mods use. a mod with problems stops the
|
||||
// whole server from starting, the game never runs with some mods missing
|
||||
export async function load_bmods(dir: string, engine_textures: Iterable<string>): Promise<ServerMod[]> {
|
||||
// engine_textures are the engine: texture ids, for checking the textures mods use. config_dir has the server's
|
||||
// <mod id>.json files that change mods' config. a mod with problems stops the whole server from starting, the game
|
||||
// never runs with some mods missing
|
||||
export async function load_bmods(
|
||||
dir: string,
|
||||
engine_textures: Iterable<string>,
|
||||
config_dir?: string,
|
||||
): Promise<ServerMod[]> {
|
||||
let files: string[];
|
||||
try {
|
||||
files = [...Deno.readDirSync(dir)]
|
||||
@@ -52,6 +58,9 @@ export async function load_bmods(dir: string, engine_textures: Iterable<string>)
|
||||
problems.push((e as Error).message);
|
||||
}
|
||||
}
|
||||
if (config_dir) {
|
||||
for (const { bmod } of by_id.values()) change_config(bmod, config_dir, problems);
|
||||
}
|
||||
|
||||
// the same checks check-mods runs on mod folders
|
||||
const loaded = [...by_id.values()].map(({ file, bmod }) => as_loaded_mod(file, bmod));
|
||||
@@ -92,6 +101,65 @@ export async function load_bmods(dir: string, engine_textures: Iterable<string>)
|
||||
}));
|
||||
}
|
||||
|
||||
// applies config_dir/<mod id>.json to the mod's config, when the server has one
|
||||
function change_config(bmod: Bmod, config_dir: string, problems: string[]) {
|
||||
const file = join(config_dir, `${bmod.manifest.id}.json`);
|
||||
let text: string;
|
||||
try {
|
||||
text = Deno.readTextFileSync(file);
|
||||
} catch (e) {
|
||||
if (e instanceof Deno.errors.NotFound) return;
|
||||
throw e;
|
||||
}
|
||||
let json: unknown;
|
||||
try {
|
||||
json = JSON.parse(text);
|
||||
} catch (e) {
|
||||
problems.push(`${file}: isn't valid json: ${(e as Error).message}`);
|
||||
return;
|
||||
}
|
||||
if (!is_object(json) || json.format_version !== FORMAT_VERSION || !is_object(json.config)) {
|
||||
problems.push(`${file}: must be { "format_version": ${FORMAT_VERSION}, "config": { ... } }`);
|
||||
return;
|
||||
}
|
||||
const changed = apply_config(bmod.data.config ?? {}, json.config);
|
||||
problems.push(...changed.problems.map((problem) => `${file}: ${problem}`));
|
||||
bmod.data.config = changed.config;
|
||||
}
|
||||
|
||||
// a mod's config with changes made to it, like a server's config/<mod id>.json makes: objects are changed setting by
|
||||
// setting, anything else is replaced. a change has to be to a setting the mod has, and the same kind of json value,
|
||||
// so a misspelled one doesn't go unnoticed
|
||||
export function apply_config(
|
||||
config: Record<string, unknown>,
|
||||
changes: Record<string, unknown>,
|
||||
): { config: Record<string, unknown>; problems: string[] } {
|
||||
const problems: string[] = [];
|
||||
const apply = (settings: Record<string, unknown>, changes: Record<string, unknown>, path: string) => {
|
||||
const result = { ...settings };
|
||||
for (const [key, value] of Object.entries(changes)) {
|
||||
const name = `${path}${key}`;
|
||||
if (!Object.hasOwn(settings, key)) {
|
||||
problems.push(`${name} isn't one of the mod's settings`);
|
||||
} else if (json_kind(value) !== json_kind(settings[key])) {
|
||||
problems.push(`${name} must be ${json_kind(settings[key])}`);
|
||||
} else {
|
||||
result[key] = is_object(value)
|
||||
? apply(settings[key] as Record<string, unknown>, value, `${name}.`)
|
||||
: value;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
return { config: apply(config, changes, ""), problems };
|
||||
}
|
||||
|
||||
function json_kind(value: unknown): string {
|
||||
if (value === null) return "null";
|
||||
if (Array.isArray(value)) return "a list";
|
||||
return is_object(value) ? "an object" : `a ${typeof value}`;
|
||||
}
|
||||
|
||||
// in the shape the checks in common/mod_check.ts take
|
||||
function as_loaded_mod(file: string, bmod: Bmod): LoadedMod {
|
||||
const entries = <T>(list: T[]) => list.map((json) => ({ file: `${file} data.json`, json }));
|
||||
@@ -117,6 +185,7 @@ function as_loaded_mod(file: string, bmod: Bmod): LoadedMod {
|
||||
noise_settings: entries(bmod.data.noise_settings ?? []),
|
||||
biome_sources: entries(bmod.data.biome_sources ?? []),
|
||||
features: entries(bmod.data.features ?? []),
|
||||
config: bmod.data.config,
|
||||
textures: [...bmod.textures.keys()],
|
||||
texture_files: new Map(),
|
||||
texture_json: new Map(bmod.texture_json),
|
||||
|
||||
@@ -11,6 +11,7 @@ try {
|
||||
host = await start_host({
|
||||
build_dir: Deno.env.get("BUILD_DIR") ?? "build",
|
||||
server_mods_dir: Deno.env.get("SERVER_MODS_DIR") ?? "server_mods",
|
||||
config_dir: Deno.env.get("CONFIG_DIR") ?? "config",
|
||||
dist_dir: Deno.env.get("DIST_DIR") ?? "dist",
|
||||
world_dir: Deno.env.get("WORLD_DIR") ?? "world",
|
||||
seed: Deno.env.get("SEED"),
|
||||
|
||||
Reference in New Issue
Block a user