// 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 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"; import { GameHost, GameServer } from "./game_server.ts"; import type { WorldSave } from "./saves.ts"; import { ModRuntime } from "./mod_runtime.ts"; import { register_entity_type } from "./entity/entity_types.ts"; import { mod_entity_factory } from "./entity/mod_entity.ts"; import { set_effect_types } from "./entity/effects.ts"; import { clear_hooks, expose_engine } from "$/common/hooks.ts"; import { common_engine } from "$/common/engine_modules.ts"; 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; } export async function start_game( host: GameHost, save: WorldSave | undefined, default_seed: string, mods: ServerModSource[], ): Promise { // for engine access mods. the last game's hooks go first, a realm only runs one game at a time clear_hooks(); expose_engine({ common: common_engine(), server: server_engine() }); const recipes = register_mod_data(mods.map((mod) => ({ id: mod.listing.id, data: mod.data }))); for (const mod of mods) { for (const json of mod.data.entities) register_entity_type(json.id, mod_entity_factory(json)); } const worldgen_scripts = mods.flatMap((mod) => mod.worldgen_url ? [{ mod: mod.listing.id, url: mod.worldgen_url, engine_access: mod.listing.engine_access }] : [] ); const worldgen = await load_worldgen(worldgen_scripts, recipes.ores); const runtime = new ModRuntime(); // before the world loads, its mobs can have effects set_effect_types(runtime.effect_types()); const game = new GameServer(host, save, default_seed, { listings: mods.map((mod) => mod.listing), recipes, worldgen, 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 }] : []), ); runtime.finish_setup(); runtime.after.server_start.emit({}); return game; } function script_of(mod: ServerModSource) { return { mod: mod.listing.id, version: mod.listing.version, engine_access: mod.listing.engine_access ?? false }; } 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)}`; }