Engine api

This commit is contained in:
2026-10-06 20:27:17 -03:00
parent d13938550d
commit 69c5c187db
27 changed files with 1026 additions and 90 deletions
+31 -12
View File
@@ -14,6 +14,9 @@ import { AIR_ID } from "$/common/protocol.ts";
import type { Client } from "./client.ts";
import { code_url, fetch_verified } from "./handshake.ts";
import { GuiModScreen } from "./gui/gui_mod_screens.ts";
import { clear_hooks, expose_engine, loading_mod } from "$/common/hooks.ts";
import { common_engine } from "$/common/engine_modules.ts";
import { client_engine } from "./engine_modules.ts";
// each loaded mod's credits file, for the credits screen
export const mod_credits: { name: string; version: string; text: string }[] = [];
@@ -110,6 +113,10 @@ export async function download_mods(listings: ModListing[], base: URL): Promise<
const bytes = await fetch_verified(new URL(listing.file, base), listing.sha256, `${listing.name}`);
const bmod = read_bmod(bytes, `${listing.id}.bmod`);
if (bmod.manifest.id !== listing.id) throw new Error(`the server listed it as ${listing.id}`);
// the confirm screen said whether it has engine access, going by the listing
if ((bmod.manifest.engine_access ?? false) !== (listing.engine_access ?? false)) {
throw new Error("its engine access isn't what the server listed");
}
return bmod;
} catch (e) {
throw new ModLoadError(listing.id, (e as Error).message);
@@ -133,26 +140,38 @@ export async function load_client_mods(listings: ModListing[], bmods: Bmod[]) {
mod_names.clear();
for (const listing of listings) mod_names.set(listing.id, listing.name);
// for engine access mods. the last server's hooks go first
clear_hooks();
expose_engine({ common: common_engine(), client: client_engine() });
const as_mod = (bmod: Bmod, fn: () => Promise<void>) =>
loading_mod(bmod.manifest.id, bmod.manifest.engine_access ?? false, fn);
// every mod's shared rules first, so client scripts can count on any mod's being there
block_rules = new BlockRules(run_mod);
rules_setup = true;
for (const [i, bmod] of bmods.entries()) {
if (!bmod.scripts.shared) continue;
const module = await import(script_url(bmod.scripts.shared));
if (typeof module.setup !== "function") {
throw new ModLoadError(bmod.manifest.id, "the shared script doesn't export a setup function");
}
await module.setup(shared_context(listings[i]));
const code = bmod.scripts.shared;
if (!code) continue;
await as_mod(bmod, async () => {
const module = await import(script_url(code));
if (typeof module.setup !== "function") {
throw new ModLoadError(bmod.manifest.id, "the shared script doesn't export a setup function");
}
await module.setup(shared_context(listings[i]));
});
}
rules_setup = false;
for (const [i, bmod] of bmods.entries()) {
if (!bmod.scripts.client) continue;
const module = await import(script_url(bmod.scripts.client));
if (typeof module.setup !== "function") {
throw new ModLoadError(bmod.manifest.id, "the client script doesn't export a setup function");
}
await module.setup(client_context(listings[i]));
const code = bmod.scripts.client;
if (!code) continue;
await as_mod(bmod, async () => {
const module = await import(script_url(code));
if (typeof module.setup !== "function") {
throw new ModLoadError(bmod.manifest.id, "the client script doesn't export a setup function");
}
await module.setup(client_context(listings[i]));
});
}
}