Single module modding

This commit is contained in:
2026-11-14 12:18:41 -03:00
parent 605008f584
commit 313098892e
134 changed files with 919 additions and 924 deletions
+19 -35
View File
@@ -59,14 +59,11 @@ 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
const FIXED_TIME = new Date(Date.UTC(2020, 0, 1));
export type ScriptSide = "server" | "client" | "worldgen" | "shared" | "content";
export const SCRIPT_SIDES: ScriptSide[] = ["server", "client", "worldgen", "shared", "content"];
export interface Mod {
manifest: ManifestJson;
data: ModData;
// bundled javascript, one module per side
scripts: Partial<Record<ScriptSide, string>>;
// its script bundled into one javascript module, which every side runs the phases of, see run_phase
script?: string;
// png bytes by texture id, like copper_tools:copper_block
textures: Map<string, Uint8Array>;
// the .json of the textures that have one, like animated ones, by texture id
@@ -82,27 +79,24 @@ export class ModError extends Error {
}
// the zip's layout:
// manifest.json the mod's manifest, with "scripts" and "credits" pointing into the zip
// manifest.json the mod's manifest, with "main" and "credits" pointing into the zip
// data.json every block, model, item, recipe, ore, entity, entity model, effect, fluid, tag, loot
// table and biome, and the mod's config
// scripts/<side>.js
// script.js
// textures/<name>.png the texture <mod id>:<name>
// textures/<name>.json its animation, if it has one
// credits.md
export function write_mod(mod: Mod): Uint8Array<ArrayBuffer> {
const text = (value: string) => new TextEncoder().encode(value);
const manifest: ManifestJson = { ...mod.manifest, scripts: {} };
const manifest: ManifestJson = { ...mod.manifest };
delete manifest.main;
delete manifest.credits;
const files: Record<string, Uint8Array> = {};
for (const side of SCRIPT_SIDES) {
const code = mod.scripts[side];
if (code !== undefined) {
files[`scripts/${side}.js`] = text(code);
manifest.scripts![side] = `scripts/${side}.js`;
}
if (mod.script !== undefined) {
files["script.js"] = text(mod.script);
manifest.main = "script.js";
}
if (Object.keys(manifest.scripts!).length === 0) delete manifest.scripts;
if (mod.credits !== undefined) {
files["credits.md"] = text(mod.credits);
manifest.credits = "credits.md";
@@ -233,18 +227,13 @@ export function read_mod(bytes: Uint8Array, file: string): Mod {
if (typeof id === "string" && id.split(":")[0] !== mod) problems.push(`${id} isn't in this mod's namespace`);
}
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) {
problems.push(`manifest.json: scripts.${side} ${path} isn't in the file`);
} else {
scripts[side as ScriptSide] = code;
}
const script = manifest.main === undefined ? undefined : text(manifest.main);
if (manifest.main !== undefined && script === undefined) {
problems.push(`manifest.json: main ${manifest.main} isn't in the file`);
}
// what the cubi/engine shim reads, so it's in the bundle of every script that imports it
if (!manifest.engine_access && Object.values(scripts).some((code) => code.includes(ENGINE_GLOBAL))) {
problems.push(`a script uses cubi/engine, which needs "engine_access": true in manifest.json`);
// what the cubi/engine shim reads, so it's in the bundle of a script that imports it
if (!manifest.engine_access && script?.includes(ENGINE_GLOBAL)) {
problems.push(`its script uses cubi/engine, which needs "engine_access": true in manifest.json`);
}
const credits = manifest.credits === undefined ? undefined : text(manifest.credits);
if (manifest.credits !== undefined && credits === undefined) {
@@ -293,17 +282,12 @@ export function read_mod(bytes: Uint8Array, file: string): Mod {
}
if (problems.length > 0) throw new ModError(file, problems);
return { manifest, data, scripts, textures, texture_json, credits };
return { manifest, data, script, 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
// the same mod without the terrain's data and its config, what players get. the server sends them the world. its
// script goes with it whole, and players only run its shared and client phases
export function client_copy(mod: Mod): Mod {
const scripts = { ...mod.scripts };
delete scripts.server;
delete scripts.worldgen;
// players get what it registered in data, see common/mod_api/content.ts
delete scripts.content;
const {
noises: _n,
density_functions: _d,
@@ -314,7 +298,7 @@ export function client_copy(mod: Mod): Mod {
config: _c,
...data
} = mod.data;
return { ...mod, scripts, data };
return { ...mod, data };
}
function checked(list: unknown, name: string, validate: (json: unknown) => string[], problems: string[]): unknown[] {