Single module modding
This commit is contained in:
+18
-10
@@ -1,33 +1,42 @@
|
||||
// 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
|
||||
// runs the content phase of a mod's 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";
|
||||
import { ModLoadError, run_phase } from "./mod_loader.ts";
|
||||
import { expose_engine } from "./hooks.ts";
|
||||
import { common_engine } from "./engine_modules.ts";
|
||||
|
||||
export interface ContentScript {
|
||||
mod: string;
|
||||
version: string;
|
||||
engine_access?: boolean;
|
||||
// importable url of the script
|
||||
url: string;
|
||||
}
|
||||
|
||||
// a .cubi's bundled content script, what server/content_worker.ts is sent
|
||||
// a .cubi's bundled script, what server/content_worker.ts is sent
|
||||
export interface ContentScriptCode {
|
||||
mod: string;
|
||||
version: string;
|
||||
engine_access?: boolean;
|
||||
code: string;
|
||||
}
|
||||
|
||||
// what a content script registered, in order, and what was wrong. with problems the mod doesn't load
|
||||
// what a content phase 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> {
|
||||
export async function run_content_script(
|
||||
{ mod, version, engine_access, url }: ContentScript,
|
||||
): Promise<RegisteredContent> {
|
||||
// the whole script loads here, and one with engine access can import cubi/engine. only engine.common is here
|
||||
expose_engine({ common: common_engine() });
|
||||
const blocks: BlockJson[] = [];
|
||||
let setting_up = true;
|
||||
const ctx: ContentContext = {
|
||||
@@ -48,12 +57,11 @@ export async function run_content_script({ mod, version, url }: ContentScript):
|
||||
},
|
||||
};
|
||||
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);
|
||||
await run_phase({ id: mod, engine_access }, url, "content", () => ctx);
|
||||
return { blocks, problems: [] };
|
||||
} catch (e) {
|
||||
return { blocks, problems: [e instanceof Error ? e.message : String(e)] };
|
||||
const problem = e instanceof ModLoadError ? e.problem : e instanceof Error ? e.message : String(e);
|
||||
return { blocks, problems: [problem] };
|
||||
} finally {
|
||||
setting_up = false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user