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) {
+2
View File
@@ -28,6 +28,8 @@ const DESKTOP_ARGS = [
"--include",
"server/game/worker.ts",
"--include",
"server/content_worker.ts",
"--include",
"desktop/config.json",
"desktop/main.ts",
];
+2 -2
View File
@@ -14,7 +14,7 @@ import { DEFAULT_REMAPS, type ImportOptions, type ImportReport } from "./importe
import { import_tags } from "./importer/tags.ts";
import { import_textures } from "./importer/textures.ts";
const KINDS: Record<string, (input: string, options: ImportOptions) => ImportReport> = {
const KINDS: Record<string, (input: string, options: ImportOptions) => ImportReport | Promise<ImportReport>> = {
model: import_models,
texture: import_textures,
blockstate: import_blocks,
@@ -113,7 +113,7 @@ if (import.meta.main) {
}
const run = KINDS[kind];
if (!run) throw new Error(`"${kind}" isn't a kind that can be imported yet: ${Object.keys(KINDS).join(", ")}`);
const report = run(input, options);
const report = await run(input, options);
print_report(report);
if (report.errors.length > 0) Deno.exit(1);
} catch (e) {
+2 -2
View File
@@ -81,9 +81,9 @@ function processing(json: Json, context: Context): Converted {
};
}
export function import_recipes(input: string, options: ImportOptions = {}): ImportReport {
export async function import_recipes(input: string, options: ImportOptions = {}): Promise<ImportReport> {
if (options.output === undefined) throw new Error("--output is needed for recipes");
const context = { remaps: options.remaps ?? DEFAULT_REMAPS, known: known_in(options.output) };
const context = { remaps: options.remaps ?? DEFAULT_REMAPS, known: await known_in(options.output) };
const report = empty_report();
if (context.known === undefined) {
report.warnings.push({
+2 -2
View File
@@ -221,10 +221,10 @@ export interface Known {
tags: Record<TagRegistry, Set<string>>;
}
export function known_in(output: string): Known | undefined {
export async function known_in(output: string): Promise<Known | undefined> {
const mod = mod_of(`${output}/x`);
if (!mod) return undefined;
const mods = load_and_check(dirname(mod.dir));
const mods = await load_and_check(dirname(mod.dir));
const tags: Known["tags"] = { blocks: new Set(), items: new Set(), entities: new Set(), biomes: new Set() };
for (const { json } of mods.flatMap((mod) => mod.tags)) tags[json.registry].add(json.id);
return { ids: registered_ids(mods), tags };
+2 -2
View File
@@ -47,7 +47,7 @@ export function tag_id(location: string, remaps: Remaps): string {
return `${namespace}:${path}`;
}
export function import_tags(input: string, options: ImportOptions = {}): ImportReport {
export async function import_tags(input: string, options: ImportOptions = {}): Promise<ImportReport> {
if (options.output === undefined) throw new Error("--output is needed for tags");
const remaps = options.remaps ?? DEFAULT_REMAPS;
const report = empty_report();
@@ -68,7 +68,7 @@ export function import_tags(input: string, options: ImportOptions = {}): ImportR
return [{ location, registry, id, json: JSON.parse(Deno.readTextFileSync(file)) }];
});
const known = known_in(options.output);
const known = await known_in(options.output);
const own = new Set(tags.map(({ registry, id }) => `${registry} ${id}`));
for (const { location, registry, id, json } of tags) {
if (!TAG_ID_PATTERN.test(id)) {
+9 -4
View File
@@ -7,15 +7,17 @@
import { basename, dirname, resolve } from "@std/path";
import { type Mod, MOD_EXTENSION, SCRIPT_SIDES, type ScriptSide, write_mod } from "@/common/mod.ts";
import type { ManifestJson } from "@/common/mod_data.ts";
import { load_and_check, type LoadedMod, mod_deno_config } from "./check_mods.ts";
import { content_script, load_and_check, type LoadedMod, mod_deno_config } from "./check_mods.ts";
import { mod_data } from "@/common/mod_check.ts";
// what each script is bundled for: the server runs in deno, clients in browsers, worldgen and shared in both
// what each script is bundled for: the server runs in deno, clients in browsers, worldgen and shared in both. content
// scripts only run on the server
const PLATFORMS: Record<ScriptSide, "deno" | "browser"> = {
server: "deno",
client: "browser",
worldgen: "browser",
shared: "browser",
content: "deno",
};
// a mod that passed check-mods, as .cubi bytes
@@ -33,9 +35,12 @@ export async function pack_mod(mod: LoadedMod): Promise<Uint8Array<ArrayBuffer>>
}
const texture_json = new Map([...mod.texture_json].sort(([a], [b]) => a.localeCompare(b)));
// without what the content script registered, which the server registers again when it loads the .cubi
const data = mod_data(mod);
data.blocks = mod.blocks.filter(({ file }) => file !== content_script(mod)).map(({ json }) => json);
return write_mod({
manifest,
data: mod_data(mod),
data,
scripts,
textures,
texture_json,
@@ -80,7 +85,7 @@ if (import.meta.main) {
}
// its siblings are checked too, so references to mods it depends on can be checked
const folder = resolve(dir);
const mod = load_and_check(dirname(folder)).find((m) => resolve(m.dir) === folder);
const mod = (await load_and_check(dirname(folder))).find((m) => resolve(m.dir) === folder);
if (!mod) {
console.error(`${dir} isn't a mod folder`);
Deno.exit(1);
+1 -1
View File
@@ -20,7 +20,7 @@ const [center_x, center_z] = [Number(Deno.args[2] ?? 0), Number(Deno.args[3] ??
// the base game, from mods/bworld: its data registered like the game does, and its worldgen script for trees and the
// rest of its features
const bworld = load_and_check().find((mod) => mod.id === "bworld")!;
const bworld = (await load_and_check()).find((mod) => mod.id === "bworld")!;
const jsons = <T>(list: { json: T }[]) => list.map((entry) => entry.json);
register_mod_data([{
id: "bworld",