Block registry api

This commit is contained in:
2026-11-12 20:41:13 -03:00
parent e7fe663746
commit 76802ed430
39 changed files with 499 additions and 100 deletions
+27 -4
View File
@@ -37,7 +37,8 @@ import {
validate_noise_settings,
} from "@/common/worldgen/data.ts";
import { validate_feature } from "@/common/worldgen/features.ts";
import { dirname } from "@std/path";
import { dirname, resolve, toFileUrl } from "@std/path";
import { run_content_script } from "@/common/content_scripts.ts";
export { load_order, type LoadedMod, type ModReport };
@@ -53,19 +54,41 @@ export function engine_texture_ids(): string[] {
}
// loads and checks every mod without typechecking, which is slow. the build uses this
export function load_and_check(mods_dir = "mods"): LoadedMod[] {
export async function load_and_check(mods_dir = "mods"): Promise<LoadedMod[]> {
const mods: LoadedMod[] = [];
for (const entry of safe_read_dir(mods_dir)) {
if (entry.isDirectory && !entry.name.startsWith(".") && !entry.name.startsWith("_")) {
mods.push(load_mod(mods_dir, entry.name));
const mod = load_mod(mods_dir, entry.name);
await add_registered_content(mod);
mods.push(mod);
}
}
check_references(mods, engine_texture_ids());
return mods;
}
// runs the mod's content script and adds the blocks it registers after its files' ones, named after the script, like
// the server does with a .cubi's. packing leaves them out, the server registers them again
async function add_registered_content(mod: LoadedMod) {
const path = content_script(mod);
if (path === undefined || !exists(`${mod.dir}/${path}`)) return;
const { blocks, problems } = await run_content_script({
mod: mod.id,
version: String(mod.manifest?.version),
url: toFileUrl(resolve(mod.dir, path)).href,
});
for (const json of blocks) mod.blocks.push({ file: path, json });
for (const problem of problems) mod.report.errors.push(`${path}: ${problem}`);
}
// the path of the mod's content script in its folder, which is also the file its blocks are listed under
export function content_script(mod: LoadedMod): string | undefined {
const path = (mod.manifest?.scripts as Record<string, unknown> | undefined)?.content;
return typeof path === "string" ? path : undefined;
}
export async function check_mods(mods_dir = "mods", options = { typecheck: true }): Promise<ModReport[]> {
const mods = load_and_check(mods_dir);
const mods = await load_and_check(mods_dir);
try {
load_order(mods);
} catch (e) {