134 lines
5.8 KiB
TypeScript
134 lines
5.8 KiB
TypeScript
import { resolve, toFileUrl } from "@std/path";
|
|
import { EverythingRegistry } from "$/common/everything_registry.ts";
|
|
import { load_and_check, load_order } from "$/tools/check_mods.ts";
|
|
import type { ServerModSource } from "$/server/game/load_mods.ts";
|
|
import { GameServer } from "$/server/game/game_server.ts";
|
|
import { start_game } from "$/server/game/load_mods.ts";
|
|
import { PROTOCOL_VERSION } from "$/common/protocol.ts";
|
|
import { decode_level_chunk } from "$/common/chunk_streaming.ts";
|
|
import { CHUNK_AREA, CHUNK_SIZE, ID_MASK } from "$/common/constants.ts";
|
|
import type { WorldSave } from "$/server/game/saves.ts";
|
|
|
|
// mods straight from their source folders, skipping the build: scripts are imported as typescript
|
|
export function mod_sources(mods_dir: string): ServerModSource[] {
|
|
const mods = load_and_check(mods_dir);
|
|
const errors = mods.flatMap((mod) => mod.report.errors.map((e) => `${mod.id}: ${e}`));
|
|
if (errors.length) throw new Error(errors.join("\n"));
|
|
return load_order(mods).map((mod) => {
|
|
const scripts = (mod.manifest?.scripts ?? {}) as Record<string, string>;
|
|
const url = (path?: string) => path ? toFileUrl(resolve(mod.dir, path)).href : undefined;
|
|
return {
|
|
listing: {
|
|
id: mod.id,
|
|
name: String(mod.manifest?.name),
|
|
version: String(mod.manifest?.version),
|
|
sha256: "source",
|
|
file: `mods/${mod.id}.bmod`,
|
|
size: 0,
|
|
...(mod.manifest?.engine_access === true ? { engine_access: true } : {}),
|
|
},
|
|
data: {
|
|
blocks: mod.blocks.map((b) => b.json),
|
|
models: mod.models.map((m) => m.json),
|
|
items: mod.items.map((i) => i.json),
|
|
recipes: mod.recipes.map((r) => r.json),
|
|
ores: mod.ores.map((o) => o.json),
|
|
spawns: mod.spawns.map((s) => s.json),
|
|
entities: mod.entities.map((e) => e.json),
|
|
entity_models: mod.entity_models.map((m) => m.json),
|
|
effects: mod.effects.map((e) => e.json),
|
|
tags: mod.tags.map((t) => t.json),
|
|
loot_tables: mod.loot_tables.map((t) => t.json),
|
|
biomes: mod.biomes.map((b) => b.json),
|
|
noises: mod.noises.map((n) => n.json),
|
|
density_functions: mod.density_functions.map((d) => d.json),
|
|
noise_settings: mod.noise_settings.map((n) => n.json),
|
|
biome_sources: mod.biome_sources.map((b) => b.json),
|
|
features: mod.features.map((f) => f.json),
|
|
},
|
|
server_url: url(scripts.server),
|
|
worldgen_url: url(scripts.worldgen),
|
|
shared_url: url(scripts.shared),
|
|
};
|
|
});
|
|
}
|
|
|
|
// a game with an in memory host that remembers what it sent to each connection. the seed has low, dry land around
|
|
// 0, 0, where players spawn and where tests build things
|
|
export async function test_game(mods_dir: string, save?: WorldSave, seed = "test-seed-90") {
|
|
EverythingRegistry.clear();
|
|
const outbox = new Map<number, unknown[]>();
|
|
const closed = new Set<number>();
|
|
// what host.download gives back, by url. other urls fail
|
|
const downloads = new Map<string, Uint8Array>();
|
|
const host = {
|
|
// chunks come out decoded, as { type: "level_chunk", x, z, blocks, biomes }
|
|
send: (conn: number, data: string | Uint8Array) => {
|
|
if (!outbox.has(conn)) outbox.set(conn, []);
|
|
outbox.get(conn)!.push(
|
|
typeof data === "string" ? JSON.parse(data) : { type: "level_chunk", ...decode_level_chunk(data) },
|
|
);
|
|
},
|
|
close(conn: number) {
|
|
closed.add(conn);
|
|
},
|
|
download(url: string) {
|
|
const bytes = downloads.get(url);
|
|
return bytes ? Promise.resolve(bytes) : Promise.reject(new Error("Couldn't download it"));
|
|
},
|
|
};
|
|
const game: GameServer = await start_game(host, save, seed, mod_sources(mods_dir));
|
|
game.chunk_send_budget_ms = Infinity;
|
|
// tests that want natural spawning turn it back on
|
|
game.do_mob_spawning = false;
|
|
// deno-lint-ignore no-explicit-any
|
|
const take = (conn: number): any[] => {
|
|
const messages = outbox.get(conn) ?? [];
|
|
outbox.set(conn, []);
|
|
return messages;
|
|
};
|
|
const send = (conn: number, message: unknown) => game.on_message(conn, JSON.stringify(message));
|
|
// the whole handshake: hello, welcome, ready, join. returns join
|
|
const join = (conn: number, name: string) => {
|
|
game.on_connect(conn);
|
|
send(conn, { type: "hello", name, protocol: PROTOCOL_VERSION });
|
|
send(conn, { type: "ready" });
|
|
return take(conn).find((m) => m.type === "join");
|
|
};
|
|
return { game, take, send, join, closed, downloads };
|
|
}
|
|
|
|
// the block id and state bits at x, y, z in the chunks among messages, read the way a client does with the table
|
|
// join sent. undefined when no chunk there was sent
|
|
// deno-lint-ignore no-explicit-any
|
|
export function sent_block(messages: any[], join: { blocks: string[] }, x: number, y: number, z: number) {
|
|
const chunk_x = Math.floor(x / CHUNK_SIZE);
|
|
const chunk_z = Math.floor(z / CHUNK_SIZE);
|
|
const chunk = messages.findLast((m) => m.type === "level_chunk" && m.x === chunk_x && m.z === chunk_z);
|
|
if (!chunk) return undefined;
|
|
const value = chunk.blocks[y * CHUNK_AREA + (z - chunk_z * CHUNK_SIZE) * CHUNK_SIZE + (x - chunk_x * CHUNK_SIZE)];
|
|
return [join.blocks[value & ID_MASK], value >>> 16];
|
|
}
|
|
|
|
export function copy_dir(from: string, to: string) {
|
|
if (Deno.statSync(from).isDirectory) {
|
|
Deno.mkdirSync(to, { recursive: true });
|
|
for (const entry of Deno.readDirSync(from)) copy_dir(`${from}/${entry.name}`, `${to}/${entry.name}`);
|
|
} else {
|
|
Deno.copyFileSync(from, to);
|
|
}
|
|
}
|
|
|
|
// menus, see server/game/menu.ts. menu 0 is the player's inventory (slots 0-35) and crafting grid (36-45)
|
|
// deno-lint-ignore no-explicit-any
|
|
type Msg = any;
|
|
|
|
// the last menu opened among messages
|
|
export const opened_menu = (messages: Msg[]) => messages.findLast((m) => m.type === "open_menu");
|
|
|
|
// what the open menu's slots last held among messages, by menu slot index
|
|
export const menu_items = (messages: Msg[]) => messages.findLast((m) => m.type === "menu_content")?.items;
|
|
|
|
// what the open menu's data last was among messages
|
|
export const menu_data = (messages: Msg[]) => messages.findLast((m) => m.type === "menu_data")?.data;
|