Single module modding
This commit is contained in:
@@ -3,15 +3,16 @@
|
||||
|
||||
/// <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
|
||||
// runs the content phase of the mods' 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) }));
|
||||
for (const { code, ...script } of event.data) {
|
||||
results.push(await run_content_script({ ...script, url: data_url(code) }));
|
||||
}
|
||||
self.postMessage(results);
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// what engine access mods get as engine.server, in server scripts. see docs/modding/misc/engine.md
|
||||
// what engine access mods get as engine.server, in server phases. see docs/modding/misc/engine.md
|
||||
import { GameServer } from "./game_server.ts";
|
||||
import { ServerWorld } from "./world.ts";
|
||||
import { ServerPlayer } from "./player.ts";
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// the fluid handlers the engine makes for server scripts, see ServerFluidApi in mod_api/server.ts: fluid containers',
|
||||
// the fluid handlers the engine makes for server phases, see ServerFluidApi in mod_api/server.ts: fluid containers',
|
||||
// tanks scripts keep, and pouring from one handler into another
|
||||
import { EverythingRegistry, type ItemRegistry } from "@/common/everything_registry.ts";
|
||||
import { contained_fluid } from "@/common/inventory.ts";
|
||||
|
||||
@@ -587,7 +587,7 @@ export class GameServer {
|
||||
this.#send(player, overlay ? { type: "chat", text, overlay } : { type: "chat", text });
|
||||
}
|
||||
|
||||
// a mod's server script to its client script, see ctx.net
|
||||
// a mod's server phase to its client phase, see ctx.net
|
||||
send_mod_message(player: ServerPlayer, channel: string, data: unknown) {
|
||||
this.#send(player, { type: "mod", channel, data });
|
||||
}
|
||||
@@ -1372,7 +1372,7 @@ export class GameServer {
|
||||
return;
|
||||
}
|
||||
|
||||
// the shared rules first, the ones the client guessed, then the server script's. sneaking with an item skips the
|
||||
// the shared rules first, the ones the client guessed, then the server phase's. sneaking with an item skips the
|
||||
// block, like minecraft's secondary use, so a block can be placed against a chest
|
||||
const sneaking = player.sneaking;
|
||||
let handled = false;
|
||||
|
||||
@@ -19,14 +19,8 @@ export type HostToGame =
|
||||
// level.json, and where the saved chunks are. the game reads them with read_chunk when it needs them
|
||||
save: { level?: string; chunks: [number, number][] };
|
||||
default_seed: string;
|
||||
// in load order, with the scripts' code since the worker can't read files
|
||||
mods: {
|
||||
listing: ModListing;
|
||||
data: ModData;
|
||||
server_code?: string;
|
||||
worldgen_code?: string;
|
||||
shared_code?: string;
|
||||
}[];
|
||||
// in load order, with their script's code since the worker can't read files
|
||||
mods: { listing: ModListing; data: ModData; code?: string }[];
|
||||
// the game's ends of channels to the worldgen workers the host started, see worldgen_pool.ts. none means
|
||||
// generating on the game's thread
|
||||
worldgen_ports: MessagePort[];
|
||||
|
||||
+10
-19
@@ -1,8 +1,8 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// starts a game server with its mods: registers their data, then imports worldgen and server scripts.
|
||||
// the worker loads built mods, tests can load mods straight from their source folders
|
||||
// starts a game server with its mods: registers their data, then runs the worldgen, shared and server phases of their
|
||||
// scripts. the worker loads built mods, tests can load mods straight from their source folders
|
||||
import { register_mod_data } from "@/common/mod_loader.ts";
|
||||
import type { ModData, ModListing } from "@/common/mod_loader.ts";
|
||||
import { load_worldgen } from "@/common/worldgen_loader.ts";
|
||||
@@ -22,10 +22,8 @@ import { server_engine } from "./engine_modules.ts";
|
||||
export interface ServerModSource {
|
||||
listing: ModListing;
|
||||
data: ModData;
|
||||
// importable urls of the scripts: data: urls in the worker, file urls in tests
|
||||
server_url?: string;
|
||||
worldgen_url?: string;
|
||||
shared_url?: string;
|
||||
// importable url of its script: a data: url in the worker, a file url in tests
|
||||
url?: string;
|
||||
}
|
||||
|
||||
export async function start_game(
|
||||
@@ -47,9 +45,7 @@ export async function start_game(
|
||||
const generator = dimension.generator;
|
||||
const shape = dimension_shape(dimension);
|
||||
const worldgen_scripts = mods.flatMap((mod) =>
|
||||
mod.worldgen_url
|
||||
? [{ mod: mod.listing.id, url: mod.worldgen_url, engine_access: mod.listing.engine_access }]
|
||||
: []
|
||||
mod.url ? [{ mod: mod.listing.id, url: mod.url, engine_access: mod.listing.engine_access }] : []
|
||||
);
|
||||
const worldgen = await load_worldgen({
|
||||
mods: mods.map((mod) => mod.listing.id),
|
||||
@@ -72,15 +68,10 @@ export async function start_game(
|
||||
runtime,
|
||||
});
|
||||
|
||||
// every mod's shared rules first, so server scripts can count on any mod's being there
|
||||
await runtime.load_shared_scripts(
|
||||
game,
|
||||
mods.flatMap((mod) => mod.shared_url ? [{ ...script_of(mod), url: mod.shared_url }] : []),
|
||||
);
|
||||
await runtime.load_scripts(
|
||||
game,
|
||||
mods.flatMap((mod) => mod.server_url ? [{ ...script_of(mod), url: mod.server_url }] : []),
|
||||
);
|
||||
const scripts = mods.flatMap((mod) => mod.url ? [{ ...script_of(mod), url: mod.url }] : []);
|
||||
// every mod's shared rules first, so server phases can count on any mod's being there
|
||||
await runtime.load_shared_scripts(game, scripts);
|
||||
await runtime.load_scripts(game, scripts);
|
||||
runtime.finish_setup();
|
||||
game.find_ticking_tiles();
|
||||
// what a worldgen worker needs to generate the same chunks, see worldgen_pool.ts
|
||||
@@ -91,7 +82,7 @@ export async function start_game(
|
||||
mods.map((mod) => ({
|
||||
id: mod.listing.id,
|
||||
data: mod.data,
|
||||
worldgen_url: mod.worldgen_url,
|
||||
url: mod.url,
|
||||
engine_access: mod.listing.engine_access,
|
||||
})),
|
||||
);
|
||||
|
||||
+38
-43
@@ -1,7 +1,7 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// runs mods' server scripts: builds each one's ServerContext and keeps what they register.
|
||||
// runs mods' server phases: builds each one's ServerContext and keeps what they register.
|
||||
// see docs/modding/. parts not built yet throw when used, saying so
|
||||
import { BlockRegistry, EverythingRegistry, fluid_height, ItemRegistry } from "@/common/everything_registry.ts";
|
||||
import { Container as EngineContainer, ItemStack as EngineItemStack, max_stack } from "@/common/inventory.ts";
|
||||
@@ -23,7 +23,6 @@ import { fluid_exists, FluidRules, same_fluid } from "@/common/fluids.ts";
|
||||
import { interact, item_handler, tank_handler, transfer, use_container } from "./fluid_handlers.ts";
|
||||
import type { SharedContext } from "@/common/mod_api/shared.ts";
|
||||
import { tag_api } from "@/common/tags.ts";
|
||||
import { loading_mod } from "@/common/hooks.ts";
|
||||
import type {
|
||||
BlockComponent,
|
||||
BlockRef,
|
||||
@@ -52,7 +51,14 @@ import type {
|
||||
ServerContext,
|
||||
SlotOptions,
|
||||
} from "@/common/mod_api/server.ts";
|
||||
import { check_recipe_types, ModLoadError, recipe_lookup, shared_recipes_api } from "@/common/mod_loader.ts";
|
||||
import {
|
||||
check_recipe_types,
|
||||
ModLoadError,
|
||||
type Phase,
|
||||
recipe_lookup,
|
||||
run_phase,
|
||||
shared_recipes_api,
|
||||
} from "@/common/mod_loader.ts";
|
||||
import {
|
||||
argument,
|
||||
block as block_argument,
|
||||
@@ -114,7 +120,6 @@ export class Signal<T> {
|
||||
// the mod whose code is running right now, for apis shared between mods that need to know who called
|
||||
let running_mod = "";
|
||||
|
||||
// a mod's script, imported from url. engine_access lets it import cubi/engine
|
||||
// a component a block lists, with its params from the block's json
|
||||
export interface BlockComponentOf {
|
||||
mod: string;
|
||||
@@ -123,6 +128,7 @@ export interface BlockComponentOf {
|
||||
params: unknown;
|
||||
}
|
||||
|
||||
// a mod's script, imported from url. engine_access lets it import cubi/engine
|
||||
interface ScriptSource {
|
||||
mod: string;
|
||||
version: string;
|
||||
@@ -159,13 +165,13 @@ interface Timer {
|
||||
|
||||
export class ModRuntime {
|
||||
block_components = new Map<string, { mod: string; component: BlockComponent }>();
|
||||
// what shared scripts registered, see common/block_rules.ts, running on the game's world
|
||||
// what shared phases registered, see common/block_rules.ts, running on the game's world
|
||||
rules = new BlockRules(
|
||||
run_guarded,
|
||||
(x, y, z) => this.game.world.get_block_id(x, y, z),
|
||||
(x, y, z, from) => this.#neighbor_changed(x, y, z, from),
|
||||
);
|
||||
// the fluids' rules from shared scripts, see common/fluids.ts
|
||||
// the fluids' rules from shared phases, see common/fluids.ts
|
||||
fluid_rules = new FluidRules(run_guarded);
|
||||
item_components = new Map<string, { mod: string; component: ItemComponent }>();
|
||||
effect_components = new Map<string, { mod: string; component: EffectComponent }>();
|
||||
@@ -264,30 +270,45 @@ export class ModRuntime {
|
||||
}
|
||||
game!: GameServer;
|
||||
|
||||
// imports each server script and calls its setup, in load order
|
||||
// shared scripts, the block rules clients run too. their world is the server's
|
||||
// runs each script's shared phase in load order: the block rules clients run too. their world is the server's
|
||||
async load_shared_scripts(game: GameServer, scripts: ScriptSource[]) {
|
||||
this.game = game;
|
||||
this.#shared_setup = true;
|
||||
try {
|
||||
for (const { mod, version, url, engine_access, config } of scripts) {
|
||||
await loading_mod(mod, engine_access, () => this.#load_shared_script(mod, version, url, config));
|
||||
}
|
||||
await this.#run_phase(scripts, "shared", (script) => this.#shared_context(script));
|
||||
check_recipe_types(game.recipes);
|
||||
} finally {
|
||||
this.#shared_setup = false;
|
||||
}
|
||||
}
|
||||
// while shared scripts set up, the only time they can register recipes, as on clients
|
||||
// while shared phases run, the only time they can register recipes, as on clients
|
||||
#shared_setup = false;
|
||||
|
||||
async #load_shared_script(mod: string, version: string, url: string, config: Record<string, unknown>) {
|
||||
const module = await import(url);
|
||||
if (typeof module.setup !== "function") {
|
||||
throw new ModLoadError(mod, "the shared script doesn't export a setup function");
|
||||
// runs each script's server phase in load order
|
||||
async load_scripts(game: GameServer, scripts: ScriptSource[]) {
|
||||
this.game = game;
|
||||
await this.#run_phase(scripts, "server", ({ mod, version, config }) => this.#context(mod, version, config));
|
||||
}
|
||||
|
||||
async #run_phase(scripts: ScriptSource[], phase: Phase, context: (script: ScriptSource) => unknown) {
|
||||
for (const script of scripts) {
|
||||
running_mod = script.mod;
|
||||
try {
|
||||
await run_phase(
|
||||
{ id: script.mod, engine_access: script.engine_access },
|
||||
script.url,
|
||||
phase,
|
||||
() => context(script),
|
||||
);
|
||||
} finally {
|
||||
running_mod = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#shared_context({ mod, version, config }: ScriptSource): SharedContext {
|
||||
const { world, blocks } = this.#context(mod, version, config);
|
||||
const ctx: SharedContext = {
|
||||
return {
|
||||
mod: { id: mod, version },
|
||||
side: "server",
|
||||
tags: tag_api(),
|
||||
@@ -322,32 +343,6 @@ export class ModRuntime {
|
||||
},
|
||||
},
|
||||
};
|
||||
running_mod = mod;
|
||||
try {
|
||||
await module.setup(ctx);
|
||||
} finally {
|
||||
running_mod = "";
|
||||
}
|
||||
}
|
||||
|
||||
async load_scripts(game: GameServer, scripts: ScriptSource[]) {
|
||||
this.game = game;
|
||||
for (const { mod, version, url, engine_access, config } of scripts) {
|
||||
await loading_mod(mod, engine_access, () => this.#load_script(mod, version, url, config));
|
||||
}
|
||||
}
|
||||
|
||||
async #load_script(mod: string, version: string, url: string, config: Record<string, unknown>) {
|
||||
const module = await import(url);
|
||||
if (typeof module.setup !== "function") {
|
||||
throw new ModLoadError(mod, "the server script doesn't export a setup function");
|
||||
}
|
||||
running_mod = mod;
|
||||
try {
|
||||
await module.setup(this.#context(mod, version, config));
|
||||
} finally {
|
||||
running_mod = "";
|
||||
}
|
||||
}
|
||||
|
||||
// every effect mods define, from their json. they're made before scripts run, since the world loads first and
|
||||
|
||||
@@ -120,9 +120,7 @@ async function init(message: Extract<HostToGame, { type: "init" }>) {
|
||||
listing: mod.listing,
|
||||
data: mod.data,
|
||||
// the worker has no file access, so the host sends the code and it's imported from memory
|
||||
server_url: mod.server_code ? data_url(mod.server_code) : undefined,
|
||||
worldgen_url: mod.worldgen_code ? data_url(mod.worldgen_code) : undefined,
|
||||
shared_url: mod.shared_code ? data_url(mod.shared_code) : undefined,
|
||||
url: mod.code ? data_url(mod.code) : undefined,
|
||||
})),
|
||||
);
|
||||
} catch (e) {
|
||||
|
||||
@@ -19,7 +19,7 @@ self.onmessage = (event: MessageEvent<{ port: MessagePort }>) => {
|
||||
port.onmessage = async (event: MessageEvent<PoolToWorker>) => {
|
||||
const message = event.data;
|
||||
if (message.type === "init") {
|
||||
// worldgen scripts with engine access hook engine.common here, in this worker's own copy of the engine
|
||||
// worldgen phases with engine access hook engine.common here, in this worker's own copy of the engine
|
||||
expose_engine({ common: common_engine() });
|
||||
loaded = load_bundle(message.bundle);
|
||||
try {
|
||||
|
||||
+1
-3
@@ -151,9 +151,7 @@ export async function start_host(options: HostOptions): Promise<Host> {
|
||||
mods: mods.map(({ listing, mod }) => ({
|
||||
listing,
|
||||
data: mod.data,
|
||||
server_code: mod.scripts.server,
|
||||
worldgen_code: mod.scripts.worldgen,
|
||||
shared_code: mod.scripts.shared,
|
||||
code: mod.script,
|
||||
})),
|
||||
worldgen_ports: worldgen.ports,
|
||||
} satisfies HostToGame,
|
||||
|
||||
+9
-8
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// the server's mods: every .cubi in server_mods/, checked, with the server's changes to their config, sorted so
|
||||
// dependencies load first, and with the copy players download (the same mod without its server script) ready to serve
|
||||
// dependencies load first, and with the copy players download (the same mod without its server phase) ready to serve
|
||||
import { join } from "@std/path";
|
||||
import { client_copy, type Mod, MOD_EXTENSION, read_mod, write_mod } from "@/common/mod.ts";
|
||||
import { check_references, load_order, type LoadedMod } from "@/common/mod_check.ts";
|
||||
@@ -104,16 +104,17 @@ 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
|
||||
// runs the content phase of the mods' 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);
|
||||
const with_scripts = mods.filter(({ mod }) => mod.script !== 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!,
|
||||
engine_access: mod.manifest.engine_access,
|
||||
code: mod.script!,
|
||||
}));
|
||||
const worker = new Worker(new URL("./content_worker.ts", import.meta.url), {
|
||||
type: "module",
|
||||
@@ -125,7 +126,7 @@ async function add_registered_content(mods: { file: string; mod: Mod }[], proble
|
||||
worker.onmessage = (event) => resolve(event.data);
|
||||
worker.onerror = (event) => {
|
||||
event.preventDefault();
|
||||
reject(new Error(`content scripts: ${event.message}`));
|
||||
reject(new Error(`content phases: ${event.message}`));
|
||||
};
|
||||
worker.postMessage(scripts);
|
||||
});
|
||||
@@ -134,7 +135,7 @@ async function add_registered_content(mods: { file: string; mod: Mod }[], proble
|
||||
}
|
||||
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}`));
|
||||
problems.push(...results[i].problems.map((problem) => `${file}: ${mod.manifest.main}: ${problem}`));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ export interface WorldgenWorkers {
|
||||
export function start_worldgen_workers(
|
||||
count: number,
|
||||
options: {
|
||||
// "none" like the game, for the host. tests load worldgen scripts from files, which needs "inherit"
|
||||
// "none" like the game, for the host. tests load worldgen phases from files, which needs "inherit"
|
||||
permissions: "none" | "inherit";
|
||||
// a worker crashed and was started again: the game's end of its new channel
|
||||
on_restart(index: number, port: MessagePort): void;
|
||||
|
||||
Reference in New Issue
Block a user