Import textures
This commit is contained in:
@@ -6,7 +6,25 @@
|
||||
// and what bworld has no use for (blockbench's names, display transforms, particle textures) is left out
|
||||
import type { ModelElementJson, ModelFace, ModelFaceJson, ModelJson } from "$/common/block_models.ts";
|
||||
import { FORMAT_VERSION } from "$/common/mod_data.ts";
|
||||
import { is_valid_id, location_string, parse_location, type Remaps, to_id } from "./resources.ts";
|
||||
import { join } from "@std/path";
|
||||
import {
|
||||
DEFAULT_REMAPS,
|
||||
empty_report,
|
||||
find_roots,
|
||||
format_json,
|
||||
id_claims,
|
||||
type ImportOptions,
|
||||
type ImportReport,
|
||||
input_files,
|
||||
is_file,
|
||||
is_valid_id,
|
||||
location_of,
|
||||
location_string,
|
||||
parse_location,
|
||||
type Remaps,
|
||||
to_id,
|
||||
write_report,
|
||||
} from "./resources.ts";
|
||||
import { NOT_BOXES, VANILLA_MODELS } from "./vanilla_models.ts";
|
||||
|
||||
// reads a java model by location ("create:block/cogwheel"), undefined when there's none
|
||||
@@ -39,6 +57,58 @@ interface Flat {
|
||||
|
||||
class ImportError extends Error {}
|
||||
|
||||
// converts one model, or every model in a folder. parents can be anywhere in the same roots or in options.parents, not
|
||||
// only in what's being converted
|
||||
export function import_models(input: string, options: ImportOptions = {}): ImportReport {
|
||||
const remaps = options.remaps ?? DEFAULT_REMAPS;
|
||||
const single = is_file(input);
|
||||
if (!single && options.output === undefined) throw new Error("--output is needed to convert a folder");
|
||||
const files = input_files(input, "models", ".json", options.namespace);
|
||||
const lookup = [
|
||||
...new Set(files.map(({ root }) => root)),
|
||||
...(options.parents ?? []).flatMap((dir) => find_roots(dir, "models")),
|
||||
];
|
||||
const cache = new Map<string, Record<string, unknown> | undefined>();
|
||||
const read: ModelSource = (location) => {
|
||||
if (cache.has(location)) return cache.get(location);
|
||||
const { namespace, path } = parse_location(location);
|
||||
const file = lookup.filter((root) => root.namespace === namespace).map((root) => `${root.dir}/${path}.json`)
|
||||
.find(is_file);
|
||||
let json: Record<string, unknown> | undefined;
|
||||
if (file) {
|
||||
try {
|
||||
json = JSON.parse(Deno.readTextFileSync(file));
|
||||
} catch (e) {
|
||||
throw new Error(`${file} isn't valid json: ${(e as Error).message}`);
|
||||
}
|
||||
}
|
||||
cache.set(location, json);
|
||||
return json;
|
||||
};
|
||||
|
||||
const several_namespaces = new Set(files.map(({ root }) => root.namespace)).size > 1;
|
||||
const report = empty_report();
|
||||
const claim = id_claims(report);
|
||||
for (const { root, file } of files) {
|
||||
const where = location_of(root, file, ".json");
|
||||
const location = location_string(where);
|
||||
const result = convert_model(location, read, remaps);
|
||||
if (result.kind === "parent") report.parents.push(location);
|
||||
else if (result.kind === "skipped") report.skipped.push({ location, reason: result.reason });
|
||||
else if (result.kind === "error") report.errors.push({ location, problems: result.problems });
|
||||
else if (claim(result.id, location)) {
|
||||
for (const warning of result.warnings) report.warnings.push({ location, warning });
|
||||
// a model's id is inside it, so its file can keep the folders it had
|
||||
const output = single
|
||||
? options.output
|
||||
: join(options.output!, ...(several_namespaces ? [where.namespace] : []), `${where.path}.json`);
|
||||
report.written.push({ location, id: result.id, file: output, text: format_json(result.json) + "\n" });
|
||||
}
|
||||
}
|
||||
write_report(report);
|
||||
return report;
|
||||
}
|
||||
|
||||
export function convert_model(location: string, read: ModelSource, remaps: Remaps): ModelResult {
|
||||
let flat: Flat;
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user