Rename bmod to just mod

This commit is contained in:
2026-11-04 22:07:45 -03:00
parent fe0d857021
commit 5baaf0239a
33 changed files with 253 additions and 253 deletions
+25 -25
View File
@@ -1,7 +1,7 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// .bmod files: a built mod in one zip, what servers load from server_mods/ and send to players. see
// .cubi files: a built mod in one zip, what servers load from server_mods/ and send to players. see
// docs/modding/gettingstarted/modfiles.md. the same code reads them on the server and in the browser, so it only works on bytes
import { unzipSync, zipSync } from "fflate";
import type { ModelFileJson } from "./block_models.ts";
@@ -49,9 +49,9 @@ import { type FeatureJson, validate_feature } from "./worldgen/features.ts";
import { texture_animation, type TextureJson, validate_texture_json } from "./texture_animation.ts";
import { ENGINE_GLOBAL } from "./hooks.ts";
export const BMOD_EXTENSION = ".bmod";
export const MOD_EXTENSION = ".cubi";
// what a .bmod can hold at most unzipped, so a small file can't unzip into gigabytes
// what a .cubi can hold at most unzipped, so a small file can't unzip into gigabytes
const MAX_FILES = 4096;
const MAX_UNZIPPED_BYTES = 64 * 1024 * 1024;
// written into every entry, so packing the same mod twice gives the same bytes and the same hash
@@ -60,7 +60,7 @@ const FIXED_TIME = new Date(Date.UTC(2020, 0, 1));
export type ScriptSide = "server" | "client" | "worldgen" | "shared";
export const SCRIPT_SIDES: ScriptSide[] = ["server", "client", "worldgen", "shared"];
export interface Bmod {
export interface Mod {
manifest: ManifestJson;
data: ModData;
// bundled javascript, one module per side
@@ -72,10 +72,10 @@ export interface Bmod {
credits?: string;
}
export class BmodError extends Error {
export class ModError extends Error {
constructor(file: string, problems: string[]) {
super(`${file}: ${problems.join("; ")}`);
this.name = "BmodError";
this.name = "ModError";
}
}
@@ -87,32 +87,32 @@ export class BmodError extends Error {
// textures/<name>.png the texture <mod id>:<name>
// textures/<name>.json its animation, if it has one
// credits.md
export function write_bmod(bmod: Bmod): Uint8Array<ArrayBuffer> {
export function write_mod(mod: Mod): Uint8Array<ArrayBuffer> {
const text = (value: string) => new TextEncoder().encode(value);
const manifest: ManifestJson = { ...bmod.manifest, scripts: {} };
const manifest: ManifestJson = { ...mod.manifest, scripts: {} };
delete manifest.credits;
const files: Record<string, Uint8Array> = {};
for (const side of SCRIPT_SIDES) {
const code = bmod.scripts[side];
const code = mod.scripts[side];
if (code !== undefined) {
files[`scripts/${side}.js`] = text(code);
manifest.scripts![side] = `scripts/${side}.js`;
}
}
if (Object.keys(manifest.scripts!).length === 0) delete manifest.scripts;
if (bmod.credits !== undefined) {
files["credits.md"] = text(bmod.credits);
if (mod.credits !== undefined) {
files["credits.md"] = text(mod.credits);
manifest.credits = "credits.md";
}
for (const [id, png] of bmod.textures) {
for (const [id, png] of mod.textures) {
files[`textures/${id.split(":")[1]}.png`] = png;
}
for (const [id, json] of bmod.texture_json ?? []) {
for (const [id, json] of mod.texture_json ?? []) {
files[`textures/${id.split(":")[1]}.json`] = text(JSON.stringify(json));
}
files["manifest.json"] = text(JSON.stringify(manifest, null, "\t"));
files["data.json"] = text(JSON.stringify(bmod.data));
files["data.json"] = text(JSON.stringify(mod.data));
// sorted, so the order doesn't depend on how the files were collected
const sorted: Record<string, Uint8Array> = {};
@@ -120,8 +120,8 @@ export function write_bmod(bmod: Bmod): Uint8Array<ArrayBuffer> {
return zipSync(sorted, { mtime: FIXED_TIME, level: 9 }) as Uint8Array<ArrayBuffer>;
}
// opens and checks a .bmod. file is only for error messages. throws BmodError listing what's wrong
export function read_bmod(bytes: Uint8Array, file: string): Bmod {
// opens and checks a .cubi. file is only for error messages. throws ModError listing what's wrong
export function read_mod(bytes: Uint8Array, file: string): Mod {
let files: Record<string, Uint8Array>;
let count = 0;
let total = 0;
@@ -137,7 +137,7 @@ export function read_bmod(bytes: Uint8Array, file: string): Bmod {
},
});
} catch (e) {
throw new BmodError(file, [`isn't a readable .bmod: ${(e as Error).message}`]);
throw new ModError(file, [`isn't a readable .cubi: ${(e as Error).message}`]);
}
const problems: string[] = [];
@@ -158,10 +158,10 @@ export function read_bmod(bytes: Uint8Array, file: string): Bmod {
const manifest = parse("manifest.json") as ManifestJson | undefined;
const raw_data = parse("data.json") as Partial<ModData> | undefined;
if (!manifest || !raw_data) throw new BmodError(file, problems);
if (!manifest || !raw_data) throw new ModError(file, problems);
for (const problem of validate_manifest(manifest, manifest.id)) problems.push(`manifest.json: ${problem}`);
if (problems.length > 0) throw new BmodError(file, problems);
if (problems.length > 0) throw new ModError(file, problems);
const mod = manifest.id;
const data: ModData = {
@@ -229,7 +229,7 @@ export function read_bmod(bytes: Uint8Array, file: string): Bmod {
if (typeof id === "string" && id.split(":")[0] !== mod) problems.push(`${id} isn't in this mod's namespace`);
}
const scripts: Bmod["scripts"] = {};
const scripts: Mod["scripts"] = {};
for (const [side, path] of Object.entries(manifest.scripts ?? {})) {
const code = text(path);
if (!SCRIPT_SIDES.includes(side as ScriptSide) || code === undefined) {
@@ -288,14 +288,14 @@ export function read_bmod(bytes: Uint8Array, file: string): Bmod {
}
}
if (problems.length > 0) throw new BmodError(file, problems);
if (problems.length > 0) throw new ModError(file, problems);
return { manifest, data, scripts, textures, texture_json, credits };
}
// the same mod without its server and worldgen scripts, the terrain's data and its config, what players get. the
// server sends them the world
export function client_copy(bmod: Bmod): Bmod {
const scripts = { ...bmod.scripts };
export function client_copy(mod: Mod): Mod {
const scripts = { ...mod.scripts };
delete scripts.server;
delete scripts.worldgen;
const {
@@ -306,8 +306,8 @@ export function client_copy(bmod: Bmod): Bmod {
features: _f,
config: _c,
...data
} = bmod.data;
return { ...bmod, scripts, data };
} = mod.data;
return { ...mod, scripts, data };
}
function checked(list: unknown, name: string, validate: (json: unknown) => string[], problems: string[]): unknown[] {
+2 -2
View File
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// checks between mods: dependencies, load order, and ids one mod uses from another. pure, so the build, check-mods
// and the server (on .bmod files) all use it
// and the server (on .cubi files) all use it
import {
type BlockJson,
choice_list,
@@ -78,7 +78,7 @@ export interface LoadedMod {
texture_json: Map<string, TextureJson>;
}
// what a mod's data files hold, as packed into its .bmod
// what a mod's data files hold, as packed into its .cubi
export function mod_data(mod: LoadedMod): ModData {
return {
blocks: mod.blocks.map((b) => b.json),
+2 -2
View File
@@ -73,12 +73,12 @@ export interface ModData {
config?: Record<string, unknown>;
}
// a mod as the server lists it to clients, in load order: the .bmod players download, without its server script
// a mod as the server lists it to clients, in load order: the .cubi players download, without its server script
export interface ModListing {
id: string;
name: string;
version: string;
// sha-256 in hex of the .bmod, clients check it before using anything in it
// sha-256 in hex of the .cubi, clients check it before using anything in it
sha256: string;
// where it's served from, relative to the server's root. named by its hash, so it never changes
file: string;