Animated textures !

This commit is contained in:
2026-10-07 19:11:44 -03:00
parent b6b10e35e2
commit fbc9266936
20 changed files with 555 additions and 25 deletions
+54 -11
View File
@@ -2,7 +2,7 @@
// 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 { ModelJson } from "./block_models.ts";
import { MAX_TEXTURE_SIZE } from "./constants.ts";
import { MAX_ANIMATED_TEXTURE_SIZE, MAX_TEXTURE_SIZE } from "./constants.ts";
import {
type BlockJson,
type EffectJson,
@@ -28,6 +28,7 @@ import {
validate_tag,
} from "./mod_data.ts";
import type { ModData } from "./mod_loader.ts";
import { texture_animation, type TextureJson, validate_texture_json } from "./texture_animation.ts";
import { ENGINE_GLOBAL } from "./hooks.ts";
export const BMOD_EXTENSION = ".bmod";
@@ -48,6 +49,8 @@ export interface Bmod {
scripts: Partial<Record<ScriptSide, 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
texture_json?: Map<string, TextureJson>;
credits?: string;
}
@@ -63,6 +66,7 @@ export class BmodError extends Error {
// data.json every block, model, item, recipe, ore, entity, entity model, effect and tag
// scripts/<side>.js
// 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> {
const text = (value: string) => new TextEncoder().encode(value);
@@ -85,6 +89,9 @@ export function write_bmod(bmod: Bmod): Uint8Array<ArrayBuffer> {
for (const [id, png] of bmod.textures) {
files[`textures/${id.split(":")[1]}.png`] = png;
}
for (const [id, json] of bmod.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));
@@ -115,7 +122,7 @@ export function read_bmod(bytes: Uint8Array, file: string): Bmod {
}
const problems: string[] = [];
const json = (name: string): unknown => {
const parse = (name: string): unknown => {
const content = files[name];
if (!content) {
problems.push(`${name} is missing`);
@@ -130,8 +137,8 @@ export function read_bmod(bytes: Uint8Array, file: string): Bmod {
};
const text = (name: string) => files[name] && new TextDecoder().decode(files[name]);
const manifest = json("manifest.json") as ManifestJson | undefined;
const raw_data = json("data.json") as Partial<ModData> | undefined;
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);
for (const problem of validate_manifest(manifest, manifest.id)) problems.push(`manifest.json: ${problem}`);
@@ -179,7 +186,8 @@ export function read_bmod(bytes: Uint8Array, file: string): Bmod {
problems.push(`manifest.json: credits ${manifest.credits} isn't in the file`);
}
const textures = new Map<string, Uint8Array>();
// sizes are checked once it's known which textures are animated
const pngs = new Map<string, Uint8Array>();
for (const [name, content] of Object.entries(files)) {
const match = name.match(/^textures\/(.+)\.png$/);
if (!match) continue;
@@ -189,15 +197,38 @@ export function read_bmod(bytes: Uint8Array, file: string): Bmod {
problems.push(`${name}: texture names must be a-z, 0-9 and _`);
} else if (!size) {
problems.push(`${name}: isn't a png`);
} else if (texture_size_problem(size)) {
problems.push(`${name}: ${texture_size_problem(size)}`);
} else {
textures.set(id, content);
pngs.set(id, content);
}
}
const texture_json = new Map<string, TextureJson>();
for (const name of Object.keys(files)) {
const match = name.match(/^textures\/(.+)\.json$/);
if (!match) continue;
const id = `${mod}:${match[1]}`;
const png = pngs.get(id);
if (!png) {
problems.push(`${name}: there's no textures/${match[1]}.png for it to go with`);
continue;
}
const json = parse(name);
if (json === undefined) continue;
const json_problems = validate_texture_json(json, png_size(png)!);
for (const problem of json_problems) problems.push(`${name}: ${problem}`);
if (json_problems.length === 0) texture_json.set(id, json as TextureJson);
}
const textures = new Map<string, Uint8Array>();
for (const [id, png] of pngs) {
const problem = texture_size_problem(png_size(png)!, texture_json.get(id));
if (problem) {
problems.push(`textures/${id.split(":")[1]}.png: ${problem}`);
} else {
textures.set(id, png);
}
}
if (problems.length > 0) throw new BmodError(file, problems);
return { manifest, data, scripts, textures, credits };
return { manifest, data, scripts, textures, texture_json, credits };
}
// the same mod without its server and worldgen scripts, what players get. the server sends them the world
@@ -220,9 +251,21 @@ function checked(list: unknown, name: string, validate: (json: unknown) => strin
return list;
}
// textures can be any size up to MAX_TEXTURE_SIZE, like 64×32 for an entity. blocks and items stretch theirs to fit
export function texture_size_problem(size: { width: number; height: number }): string | undefined {
// textures can be any size up to MAX_TEXTURE_SIZE, like 64×32 for an entity. blocks and items stretch theirs to fit.
// for an animated one (json is its .json) that's each frame's size
export function texture_size_problem(size: { width: number; height: number }, json?: TextureJson): string | undefined {
const { width, height } = size;
const animation = json && texture_animation(json, size);
if (animation) {
const { frame_width, frame_height } = animation;
if (width > MAX_ANIMATED_TEXTURE_SIZE || height > MAX_ANIMATED_TEXTURE_SIZE) {
return `is ${width}×${height}, animated textures can be at most ${MAX_ANIMATED_TEXTURE_SIZE}×${MAX_ANIMATED_TEXTURE_SIZE}`;
}
if (frame_width > MAX_TEXTURE_SIZE || frame_height > MAX_TEXTURE_SIZE) {
return `has ${frame_width}×${frame_height} frames, textures can be at most ${MAX_TEXTURE_SIZE}×${MAX_TEXTURE_SIZE}`;
}
return undefined;
}
if (width < 1 || height < 1 || width > MAX_TEXTURE_SIZE || height > MAX_TEXTURE_SIZE) {
return `is ${width}×${height}, textures can be at most ${MAX_TEXTURE_SIZE}×${MAX_TEXTURE_SIZE}`;
}