Mod config
This commit is contained in:
+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),
|
||||
|
||||
Reference in New Issue
Block a user