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
+36
View File
@@ -10,6 +10,7 @@ import type { ModListing } from "@/common/mod_loader.ts";
import { data_file, FORMAT_VERSION } from "@/common/mod_data.ts";
import { is_object, json_object } from "@/common/schema.ts";
import { sha256 } from "@/common/utils.ts";
import type { ContentScriptCode, RegisteredContent } from "@/common/content_scripts.ts";
export interface ServerMod {
mod: Mod;
@@ -62,6 +63,7 @@ export async function load_mods(
if (config_dir) {
for (const { mod } of by_id.values()) change_config(mod, config_dir, problems);
}
await add_registered_content([...by_id.values()], problems);
// the same checks check-mods runs on mod folders
const loaded = [...by_id.values()].map(({ file, mod }) => as_loaded_mod(file, mod));
@@ -102,6 +104,40 @@ export async function load_mods(
}));
}
// runs the mods' content scripts in a worker without permissions and adds the blocks they register to their data,
// after their files' ones. from then on they're the mod's data like the rest: checked, in the copy players download,
// and in the worldgen bundle
async function add_registered_content(mods: { file: string; mod: Mod }[], problems: string[]) {
const with_scripts = mods.filter(({ mod }) => mod.scripts.content !== undefined);
if (with_scripts.length === 0) return;
const scripts: ContentScriptCode[] = with_scripts.map(({ mod }) => ({
mod: mod.manifest.id,
version: mod.manifest.version,
code: mod.scripts.content!,
}));
const worker = new Worker(new URL("./content_worker.ts", import.meta.url), {
type: "module",
deno: { permissions: "none" },
} as WorkerOptions);
let results: RegisteredContent[];
try {
results = await new Promise((resolve, reject) => {
worker.onmessage = (event) => resolve(event.data);
worker.onerror = (event) => {
event.preventDefault();
reject(new Error(`content scripts: ${event.message}`));
};
worker.postMessage(scripts);
});
} finally {
worker.terminate();
}
with_scripts.forEach(({ file, mod }, i) => {
mod.data.blocks.push(...results[i].blocks);
problems.push(...results[i].problems.map((problem) => `${file}: ${mod.manifest.scripts!.content}: ${problem}`));
});
}
// applies config_dir/<mod id>.json to the mod's config, when the server has one
function change_config(mod: Mod, config_dir: string, problems: string[]) {
const file = join(config_dir, `${mod.manifest.id}.json`);