124 lines
5.9 KiB
TypeScript
124 lines
5.9 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_blocks } from "./importer/blocks.ts";
|
|
import { import_items } from "./importer/items.ts";
|
|
import { import_models } from "./importer/models.ts";
|
|
import { import_recipes } from "./importer/recipes.ts";
|
|
import { DEFAULT_REMAPS, type ImportOptions, type ImportReport } from "./importer/resources.ts";
|
|
import { import_tags } from "./importer/tags.ts";
|
|
import { import_textures } from "./importer/textures.ts";
|
|
|
|
const KINDS: Record<string, (input: string, options: ImportOptions) => ImportReport | Promise<ImportReport>> = {
|
|
model: import_models,
|
|
texture: import_textures,
|
|
blockstate: import_blocks,
|
|
item: import_items,
|
|
tag: import_tags,
|
|
recipe: import_recipes,
|
|
};
|
|
|
|
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
|
|
blockstate blockstates to blocks, with their states, models and items, and placeholder mining
|
|
item item models to items, for the items that aren't a block's
|
|
tag tags (data/<namespace>/tags) to tags/, entries that don't exist in the mods next to --output
|
|
made optional
|
|
recipe recipes to recipes/, for the types bworld has: stonecutting, and create's pressing in its own
|
|
shape with bworld's item choices. ones naming items or tags that don't exist in the mods next
|
|
to --output are left out
|
|
|
|
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. a file outside assets/ is otherwise the mod's that --output is in, and its
|
|
bare names (a texture "gear") are that mod's too, the way blockbench exports them
|
|
--parents FOLDER another folder to follow parent models through, like another mod's resources. can be repeated
|
|
--remap FILE json that renames locations before they become ids, longest match first, and leaves out
|
|
locations that match a "skip" pattern:
|
|
{ "textures": { "minecraft:block/": "bworld:" }, "models": {}, "ids": {}, "tags": {},
|
|
"skip": ["crimson"] }
|
|
minecraft's block and item textures, blocks, items and tags become bworld's of the same name,
|
|
and forge's shared tags c's, 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 },
|
|
ids: { ...DEFAULT_REMAPS.ids, ...remap.ids },
|
|
tags: { ...DEFAULT_REMAPS.tags, ...remap.tags },
|
|
...(remap.skip ? { skip: remap.skip } : {}),
|
|
};
|
|
} 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());
|
|
// the same reason for many files is one line
|
|
const by_reason = (entries: { location: string; reason: string }[]) => {
|
|
const grouped = new Map<string, string[]>();
|
|
for (const { location, reason } of entries) grouped.set(reason, [...(grouped.get(reason) ?? []), location]);
|
|
return [...grouped].map(([reason, locations]) =>
|
|
`${reason} (${locations.length === 1 ? locations[0] : `${locations.length} files`})`
|
|
);
|
|
};
|
|
for (const line of by_reason(report.skipped)) console.error(`skipped: ${line}`);
|
|
for (const { location, problems } of report.errors) {
|
|
for (const problem of problems) console.error(`error in ${location}: ${problem}`);
|
|
}
|
|
const warnings = report.warnings.map(({ location, warning }) => ({ location, reason: warning }));
|
|
for (const line of by_reason(warnings)) console.error(`warning: ${line}`);
|
|
const converted = new Set(report.written.map(({ location }) => location)).size;
|
|
console.error(
|
|
`${converted} converted, ${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 = await 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);
|
|
}
|
|
}
|