Files
bworld/tools/importer.ts
T
2026-10-12 17:39:48 -03:00

101 lines
4.3 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// converts a minecraft java mod's resources into bworld's formats, one kind at a time:
// deno run -A tools/importer.ts model MODEL.json --output PATH
// deno run -A tools/importer.ts texture path/to/resources --output mods/example/textures
// a folder can be a whole mod's resources (every assets/<namespace>/<kind> in it), one assets/<namespace>/<kind>, or a
// folder inside one. each kind is in tools/importer/, see USAGE for the options
import { import_models } from "./importer/models.ts";
import { DEFAULT_REMAPS, type ImportOptions, type ImportReport } from "./importer/resources.ts";
import { import_textures } from "./importer/textures.ts";
const KINDS: Record<string, (input: string, options: ImportOptions) => ImportReport> = {
model: import_models,
texture: import_textures,
};
const USAGE = `deno run -A tools/importer.ts <kind> <file or folder> [options]
kinds:
model java block models to bworld models
texture textures, with their .png.mcmeta animations
options:
--output PATH the file (for one input file) or the folder to write. one model without it is printed
--namespace NS the namespace, when the input isn't inside assets/<namespace>/, or the only one to take from a
folder with several
--parents FOLDER another folder to find parent models in, like another mod's resources. can be repeated
--remap FILE json that renames locations before they become ids, longest match first:
{ "textures": { "minecraft:block/": "bworld:" }, "models": {} }
minecraft's block and item textures become bworld's of the same name unless it says otherwise`;
function parse_args(args: string[]) {
const positional: string[] = [];
const options: ImportOptions & { parents: string[] } = { parents: [] };
for (let i = 0; i < args.length; i++) {
const arg = args[i];
const value = () => {
const next = args[++i];
if (next === undefined) throw new Error(`${arg} needs a value`);
return next;
};
if (arg === "--output") options.output = value();
else if (arg === "--namespace") options.namespace = value();
else if (arg === "--parents") options.parents.push(value());
else if (arg === "--remap") {
const remap = JSON.parse(Deno.readTextFileSync(value()));
options.remaps = {
textures: { ...DEFAULT_REMAPS.textures, ...remap.textures },
models: { ...DEFAULT_REMAPS.models, ...remap.models },
};
} else if (arg.startsWith("--")) throw new Error(`unknown option ${arg}`);
else positional.push(arg);
}
return { positional, options };
}
function print_report(report: ImportReport) {
const [first] = report.written;
if (report.written.length === 1 && first.file === undefined) console.log(first.text?.trimEnd());
for (const { location, reason } of report.skipped) console.error(`skipped ${location}: ${reason}`);
for (const { location, problems } of report.errors) {
for (const problem of problems) console.error(`error in ${location}: ${problem}`);
}
// the same warning about many files is one line
const warnings = new Map<string, string[]>();
for (const { location, warning } of report.warnings) {
warnings.set(warning, [...(warnings.get(warning) ?? []), location]);
}
for (const [warning, locations] of warnings) {
console.error(`warning: ${warning} (${locations.length === 1 ? locations[0] : `${locations.length} files`})`);
}
const converted = new Set(report.written.map(({ location }) => location)).size;
const parents = report.parents.length > 0 ? `${report.parents.length} only parents, ` : "";
console.error(
`${converted} converted, ${parents}${report.skipped.length} skipped, ${report.errors.length} with errors`,
);
}
if (import.meta.main) {
if (Deno.args.includes("--help") || Deno.args.includes("-h")) {
console.log(USAGE);
Deno.exit(0);
}
try {
const { positional: [kind, input], options } = parse_args(Deno.args);
if (!kind || !input) {
console.error(USAGE);
Deno.exit(2);
}
const run = KINDS[kind];
if (!run) throw new Error(`"${kind}" isn't a kind that can be imported yet: ${Object.keys(KINDS).join(", ")}`);
const report = run(input, options);
print_report(report);
if (report.errors.length > 0) Deno.exit(1);
} catch (e) {
console.error((e as Error).message);
Deno.exit(1);
}
}