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
+60
View File
@@ -0,0 +1,60 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// runs a mod's content script, see common/mod_api/content.ts. check-mods runs it on the mod's folder, the server on
// its .cubi in a worker without permissions (server/content_worker.ts), and both add what it registers to the mod's
// data before anything else reads it
import { type BlockJson, validate_block } from "./mod_data.ts";
import type { ContentContext } from "./mod_api/content.ts";
export interface ContentScript {
mod: string;
version: string;
// importable url of the script
url: string;
}
// a .cubi's bundled content script, what server/content_worker.ts is sent
export interface ContentScriptCode {
mod: string;
version: string;
code: string;
}
// what a content script registered, in order, and what was wrong. with problems the mod doesn't load
export interface RegisteredContent {
blocks: BlockJson[];
problems: string[];
}
export async function run_content_script({ mod, version, url }: ContentScript): Promise<RegisteredContent> {
const blocks: BlockJson[] = [];
let setting_up = true;
const ctx: ContentContext = {
mod: { id: mod, version },
blocks: {
register(block) {
if (!setting_up) throw new Error("blocks can only be registered during setup");
// what a file could hold, so the server, its players and its worldgen workers all get the same thing
const json = JSON.parse(JSON.stringify(block ?? null));
const id = typeof json?.id === "string" ? json.id : "without an id";
const problems = validate_block(json);
if (problems.length > 0) throw new Error(`block ${id}: ${problems.join("; ")}`);
if (json.id.split(":")[0] !== mod) {
throw new Error(`block ${json.id} isn't in this mod's namespace "${mod}"`);
}
blocks.push(json);
},
},
};
try {
const module = await import(url);
if (typeof module.setup !== "function") throw new Error("the content script doesn't export a setup function");
await module.setup(ctx);
return { blocks, problems: [] };
} catch (e) {
return { blocks, problems: [e instanceof Error ? e.message : String(e)] };
} finally {
setting_up = false;
}
}
+14 -2
View File
@@ -59,8 +59,8 @@ const MAX_UNZIPPED_BYTES = 64 * 1024 * 1024;
// written into every entry, so packing the same mod twice gives the same bytes and the same hash
const FIXED_TIME = new Date(Date.UTC(2020, 0, 1));
export type ScriptSide = "server" | "client" | "worldgen" | "shared";
export const SCRIPT_SIDES: ScriptSide[] = ["server", "client", "worldgen", "shared"];
export type ScriptSide = "server" | "client" | "worldgen" | "shared" | "content";
export const SCRIPT_SIDES: ScriptSide[] = ["server", "client", "worldgen", "shared", "content"];
export interface Mod {
manifest: ManifestJson;
@@ -302,6 +302,8 @@ export function client_copy(mod: Mod): Mod {
const scripts = { ...mod.scripts };
delete scripts.server;
delete scripts.worldgen;
// players get what it registered in data, see common/mod_api/content.ts
delete scripts.content;
const {
noises: _n,
density_functions: _d,
@@ -354,3 +356,13 @@ export function png_size(bytes: Uint8Array): { width: number; height: number } |
const view = new DataView(bytes.buffer, bytes.byteOffset);
return { width: view.getUint32(16), height: view.getUint32(20) };
}
// a mod's bundled script as a url import() takes, for realms without file access
export function data_url(code: string) {
const bytes = new TextEncoder().encode(code);
let binary = "";
for (let i = 0; i < bytes.length; i += 0x8000) {
binary += String.fromCharCode(...bytes.subarray(i, i + 0x8000));
}
return `data:application/javascript;base64,${btoa(binary)}`;
}
+20
View File
@@ -0,0 +1,20 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// content scripts: blocks made in code, for content that's easier to generate than to write out file by file, like
// every block of every kind of rock. the server runs them while it loads mods, before anything else reads their data,
// and what they register becomes part of the mod's data just like its files: checked the same way, sent to players
// with the rest, and generated by worldgen workers. see docs/modding/gettingstarted/modfiles.md
import type { BlockJson } from "../mod_data.ts";
import type { ModInfo } from "./shared.ts";
export type { BlockJson };
export interface ContentContext {
readonly mod: ModInfo;
readonly blocks: {
// adds a block, what a blocks/ file holds under "block", checked the same way. only during setup, and only in
// the mod's own namespace. it's registered right after the mod's blocks/ files, in the order of the calls
register(block: BlockJson): void;
};
}
+1
View File
@@ -87,6 +87,7 @@ const manifest_schema = (folder_name: string) =>
client: z.string().optional(),
worldgen: z.string().optional(),
shared: z.string().optional(),
content: z.string().optional(),
}).optional(),
credits: z.string().optional(),
}).superRefine((manifest, ctx) => {