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
+17
View File
@@ -0,0 +1,17 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
/// <reference lib="deno.worker" />
// runs the mods' content scripts for server/load_mods.ts, without permissions like the game's worker, since the host
// that loads mods can read and write files. gets each one's script and answers with what each registered, in order
import { type ContentScriptCode, run_content_script } from "@/common/content_scripts.ts";
import { data_url } from "@/common/mod.ts";
self.onmessage = async (event: MessageEvent<ContentScriptCode[]>) => {
const results = [];
for (const { mod, version, code } of event.data) {
results.push(await run_content_script({ mod, version, url: data_url(code) }));
}
self.postMessage(results);
};
-9
View File
@@ -125,12 +125,3 @@ function script_of(mod: ServerModSource) {
config: mod.data.config ?? {},
};
}
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)}`;
}
+2 -1
View File
@@ -7,7 +7,8 @@
import type { GameServer } from "./game_server.ts";
import { GameLoop } from "./game_loop.ts";
import { GameToHost, HostToGame, READ_DONE, READ_HEADER, READ_TOO_SMALL, READ_WAITING } from "./host_protocol.ts";
import { data_url, start_game } from "./load_mods.ts";
import { start_game } from "./load_mods.ts";
import { data_url } from "@/common/mod.ts";
import { type WorkerPort, WorldgenPool } from "./worldgen_pool.ts";
const SAVE_INTERVAL_MS = 30_000;
+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`);