// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // the desktop app's data folder, like luanti's: every .cubi in mods/, and the singleplayer worlds in saves/. a world // is a folder the game server saves into, with a world.json of its own saying which mods it's played with import { join } from "@std/path"; import { z } from "zod"; import { read_manifest } from "../common/mod.ts"; import { data_file, FORMAT_VERSION } from "../common/mod_data.ts"; import { mod_files } from "../server/load_mods.ts"; // what the mod lists show export interface ModEntry { id: string; name: string; version: string; description?: string; // the ids of the mods it needs dependencies: string[]; // in mods/ file: string; } export interface WorldEntry { // its folder's name name: string; // the ids of its mods mods: string[]; } const WORLD_FILE = "world.json"; const world_file = data_file("mods", z.array(z.string())); // what works as a folder name on every os const WORLD_NAME = /^[A-Za-z0-9_\- ]{1,32}$/; // the usual place for app data on each os export function data_dir() { const home = Deno.env.get("HOME") ?? Deno.env.get("USERPROFILE") ?? "."; switch (Deno.build.os) { case "windows": return join(Deno.env.get("APPDATA") ?? join(home, "AppData", "Roaming"), "cubi"); case "darwin": return join(home, "Library", "Application Support", "cubi"); default: return join(Deno.env.get("XDG_DATA_HOME") ?? join(home, ".local", "share"), "cubi"); } } export function mods_dir(data: string) { return join(data, "mods"); } function saves_dir(data: string) { return join(data, "saves"); } // puts the .cubi files the app was built with, like the base game, in mods/. they replace the ones with the same file // name, so they're updated along with the app export function install_mods(from: string, data: string) { Deno.mkdirSync(mods_dir(data), { recursive: true }); for (const file of mod_files(from)) { const bytes = Deno.readFileSync(join(from, file)); const to = join(mods_dir(data), file); if (!same_bytes(bytes, read_if_there(to))) Deno.writeFileSync(to, bytes); } } // the mods in mods/ that can be read, by name. ones that can't are left out and logged export function list_mods(data: string): ModEntry[] { const mods = new Map(); for (const file of mod_files(mods_dir(data))) { try { const manifest = read_manifest(Deno.readFileSync(join(mods_dir(data), file)), file); const other = mods.get(manifest.id); if (other) { console.warn(`${file} is left out, ${other.file} is the mod "${manifest.id}" too`); continue; } mods.set(manifest.id, { id: manifest.id, name: manifest.name, version: manifest.version, description: manifest.description, dependencies: (manifest.dependencies ?? []).map(({ id }) => id), file, }); } catch (e) { console.warn((e as Error).message); } } return [...mods.values()].sort((a, b) => a.name.localeCompare(b.name)); } // the folders in saves/ with a world.json, by name export function list_worlds(data: string): WorldEntry[] { const worlds: WorldEntry[] = []; for (const entry of read_dir(saves_dir(data))) { if (!entry.isDirectory) continue; try { worlds.push({ name: entry.name, mods: read_world_mods(data, entry.name) }); } catch (e) { if (!(e instanceof Deno.errors.NotFound)) console.warn(`${entry.name}: ${(e as Error).message}`); } } return worlds.sort((a, b) => a.name.localeCompare(b.name)); } // a new world, which the game server makes once it's played. errors are told to the player export function create_world(data: string, name: unknown, mods: unknown) { if (typeof name !== "string" || !WORLD_NAME.test(name.trim())) { throw new Error("A world's name has to be 1 to 32 letters, numbers, spaces, - and _"); } const trimmed = name.trim(); // any folder, and case insensitive like windows' and macos' folders const taken = [...read_dir(saves_dir(data))].some((entry) => entry.name.toLowerCase() === trimmed.toLowerCase()); if (taken) throw new Error(`There's already a world called ${trimmed}`); const json = world_json(data, mods); const dir = join(saves_dir(data), trimmed); Deno.mkdirSync(dir, { recursive: true }); Deno.writeTextFileSync(join(dir, WORLD_FILE), json); } export function set_world_mods(data: string, name: unknown, mods: unknown) { Deno.writeTextFileSync(join(world_dir(data, name), WORLD_FILE), world_json(data, mods)); } // a world.json, for mods the page sent function world_json(data: string, mods: unknown) { const known = new Set(list_mods(data).map(({ id }) => id)); if (!Array.isArray(mods) || mods.some((id) => !known.has(id))) { throw new Error("Those aren't all mods in the mods folder"); } const json: z.infer = { format_version: FORMAT_VERSION, mods: [...new Set(mods)] }; return JSON.stringify(json, null, "\t") + "\n"; } // what the game server needs to play a world: its folder, and the files in mods/ of its mods export function world_to_play(data: string, name: unknown): { dir: string; mod_files: string[] } { const dir = world_dir(data, name); const mods = new Map(list_mods(data).map((mod) => [mod.id, mod])); const mod_files = read_world_mods(data, name as string).map((id) => { const mod = mods.get(id); if (!mod) throw new Error(`This world plays with the mod "${id}", which isn't in the mods folder`); return mod.file; }); return { dir, mod_files }; } // the folder of a world there is, from a name the page sent function world_dir(data: string, name: unknown) { if (typeof name !== "string" || !list_worlds(data).some((world) => world.name === name)) { throw new Error(`There's no world called ${name}`); } return join(saves_dir(data), name); } function read_world_mods(data: string, name: string): string[] { const json = JSON.parse(Deno.readTextFileSync(join(saves_dir(data), name, WORLD_FILE))); const parsed = world_file.safeParse(json); if (!parsed.success) throw new Error(`${WORLD_FILE} must be { "format_version": 1, "mods": [...] }`); return parsed.data.mods as string[]; } // nothing when it isn't there yet function read_dir(dir: string): Deno.DirEntry[] { try { return [...Deno.readDirSync(dir)]; } catch (e) { if (e instanceof Deno.errors.NotFound) return []; throw e; } } function read_if_there(file: string): Uint8Array | undefined { try { return Deno.readFileSync(file); } catch (e) { if (e instanceof Deno.errors.NotFound) return undefined; throw e; } } function same_bytes(a: Uint8Array, b: Uint8Array | undefined) { return b !== undefined && a.length === b.length && a.every((byte, i) => byte === b[i]); }