Import textures

This commit is contained in:
2026-10-12 17:39:48 -03:00
parent 355e7a21ca
commit e5baeab193
7 changed files with 419 additions and 152 deletions
+129
View File
@@ -0,0 +1,129 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// java textures (assets/<namespace>/textures/**.png) to a bworld mod's textures/. bworld names a texture in a subfolder
// by joining the folders with _, like resources.ts does for every id, so the folders stay as they are and models
// converted from the same mod find their textures. a .png.mcmeta becomes the .json next to the png: bworld reads the
// same animation format
import { join } from "@std/path";
import { png_size, texture_size_problem } from "$/common/bmod.ts";
import { type TextureJson, validate_texture_json } from "$/common/texture_animation.ts";
import {
DEFAULT_REMAPS,
empty_report,
format_json,
id_claims,
type ImportOptions,
type ImportReport,
input_files,
is_file,
is_valid_id,
location_of,
location_string,
remap_location,
to_id,
write_report,
} from "./resources.ts";
const ANIMATION_FIELDS = ["frametime", "interpolate", "frames", "width", "height"];
export type MetaResult = { json?: TextureJson; warnings: string[]; problems: string[] };
// a .png.mcmeta's animation, for a png of the size, or no json for one with no animation. the other sections (blur and
// clamping, a gui's nine-slice scaling) have nothing in bworld to go to
export function convert_mcmeta(mcmeta: unknown, size: { width: number; height: number }): MetaResult {
if (mcmeta === null || typeof mcmeta !== "object" || Array.isArray(mcmeta)) {
return { warnings: [], problems: ["the .mcmeta must be an object"] };
}
const warnings = Object.keys(mcmeta).filter((key) => key !== "animation")
.map((key) => `the .mcmeta's "${key}" section was left out`);
const animation = (mcmeta as Record<string, unknown>).animation as Record<string, unknown> | undefined;
if (animation === undefined) return { warnings, problems: [] };
for (const key of Object.keys(animation ?? {})) {
if (!ANIMATION_FIELDS.includes(key)) warnings.push(`the animation's "${key}" was left out`);
}
const json = {
animation: Object.fromEntries(
Object.entries(animation ?? {}).filter(([key]) => ANIMATION_FIELDS.includes(key)),
),
};
const problems = validate_texture_json(json, size);
return problems.length > 0 ? { warnings, problems } : { json: json as TextureJson, warnings, problems };
}
// copies one texture, or every texture in a folder, with their animations. minecraft's own textures aren't copied:
// models refer to them through the remap, which by default makes them the base game's
export function import_textures(input: string, options: ImportOptions = {}): ImportReport {
if (options.output === undefined) throw new Error("--output is needed for textures");
const remaps = options.remaps ?? DEFAULT_REMAPS;
const single = is_file(input);
const report = empty_report();
const claim = id_claims(report);
const files = input_files(input, "textures", ".png", options.namespace);
// a texture gets its namespace from the mod it's in, so two namespaces' textures can't share one folder
const namespaces = [...new Set(files.map(({ root }) => root.namespace))].filter((ns) => ns !== "minecraft");
if (namespaces.length > 1) {
throw new Error(`${input} has textures of ${namespaces.join(", ")}: pick one with --namespace`);
}
for (const { root, file } of files) {
const location = location_string(location_of(root, file, ".png"));
if (root.namespace === "minecraft") {
report.skipped.push({
location,
reason: "minecraft's own textures aren't copied, the remap points at bworld's",
});
continue;
}
const id = to_id(location, remaps.textures);
if (!is_valid_id(id)) {
report.errors.push({ location, problems: [`it doesn't make a valid texture id (${id})`] });
continue;
}
const size = png_size(Deno.readFileSync(file));
if (!size) {
report.errors.push({ location, problems: ["it isn't a png"] });
continue;
}
let json: TextureJson | undefined;
if (is_file(`${file}.mcmeta`)) {
let mcmeta: unknown;
try {
mcmeta = JSON.parse(Deno.readTextFileSync(`${file}.mcmeta`));
} catch (e) {
report.errors.push({ location, problems: [`its .mcmeta isn't valid json: ${(e as Error).message}`] });
continue;
}
const result = convert_mcmeta(mcmeta, size);
if (result.problems.length > 0) {
report.errors.push({ location, problems: result.problems });
continue;
}
for (const warning of result.warnings) report.warnings.push({ location, warning });
json = result.json;
}
// the limits a .bmod checks, like a title screen's 512×512 panorama being too big
const too_big = texture_size_problem(size, json);
if (too_big) {
report.skipped.push({ location, reason: `it ${too_big}` });
continue;
}
if (!claim(id, location)) continue;
// where it goes is what names it, so a remapped texture goes where its new name says
const png = single
? options.output
: join(options.output, `${remap_location(location, remaps.textures).path}.png`);
report.written.push({ location, id, file: png, copy: file });
if (json) {
report.written.push({
location,
id,
file: png.replace(/\.png$/, "") + ".json",
text: format_json(json) + "\n",
});
}
}
write_report(report);
return report;
}