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
+104 -10
View File
@@ -5,7 +5,7 @@
// "create:block/cogwheel" (or "block/stone", which is minecraft's) becomes the id "create:block_cogwheel": bworld ids
// have no folders, so the path's folders are joined with _, the same way check_mods names textures in subfolders
import { ID_PATTERN } from "$/common/mod_data.ts";
import { relative, resolve, SEPARATOR } from "@std/path";
import { dirname, relative, resolve, SEPARATOR } from "@std/path";
export interface Location {
namespace: string;
@@ -38,17 +38,19 @@ export const DEFAULT_REMAPS: Remaps = {
models: {},
};
export function to_id(location: string, remap: Remap = {}): string {
export function remap_location(location: string, remap: Remap = {}): Location {
const full = location_string(parse_location(location));
let mapped = full;
for (const from of Object.keys(remap).sort((a, b) => b.length - a.length)) {
const prefix = from.endsWith("/") || from.endsWith(":");
if (prefix ? full.startsWith(from) : full === from) {
mapped = remap[from] + full.slice(from.length);
break;
return parse_location(remap[from] + full.slice(from.length));
}
}
const { namespace, path } = parse_location(mapped);
return parse_location(full);
}
export function to_id(location: string, remap: Remap = {}): string {
const { namespace, path } = remap_location(location, remap);
return `${namespace}:${path.replaceAll("/", "_")}`;
}
@@ -62,8 +64,8 @@ export interface ResourceRoot {
dir: string;
}
// the roots of a kind inside a folder: every assets/<namespace>/<kind> under it, or the folder itself when it's one, or
// is inside one. a folder that's none of those is a root of the given namespace, if there is one
// the roots of a kind inside a folder: every assets/<namespace>/<kind> under it (only namespace's, when it's given), or
// the folder itself when it's one or is inside one. a folder that's none of those is a root of the given namespace
export function find_roots(input: string, kind: string, namespace?: string): ResourceRoot[] {
const dir = resolve(input);
const enclosing = enclosing_root(dir, kind);
@@ -76,8 +78,32 @@ export function find_roots(input: string, kind: string, namespace?: string): Res
}
}
}
if (roots.length === 0 && namespace) roots.push({ namespace, dir });
return roots;
if (roots.length === 0) return namespace ? [{ namespace, dir }] : [];
return namespace ? roots.filter((root) => root.namespace === namespace) : roots;
}
// a file to convert and the root it's in
export interface InputFile {
root: ResourceRoot;
file: string;
}
// what to convert: one file, or every file with the extension in a folder's roots of a kind
export function input_files(input: string, kind: string, extension: string, namespace?: string): InputFile[] {
if (is_file(input)) {
const root = enclosing_root(input, kind) ?? (namespace && { namespace, dir: dirname(resolve(input)) });
if (!root) throw new Error(`can't tell ${input}'s namespace: pass --namespace`);
return [{ root, file: resolve(input) }];
}
const roots = find_roots(input, kind, namespace);
if (roots.length === 0) {
throw new Error(`no ${kind} in ${input}: pass --namespace when it isn't inside assets/<namespace>/${kind}`);
}
const full = resolve(input);
return roots.flatMap((root) => {
const inside = full === root.dir || full.startsWith(root.dir + SEPARATOR);
return walk_files(inside ? full : root.dir, extension).map((file) => ({ root, file }));
});
}
// the assets/<namespace>/<kind> folder a path is in, if it's in one
@@ -128,6 +154,14 @@ function read_dir(dir: string): Deno.DirEntry[] {
}
}
export function is_file(path: string): boolean {
try {
return Deno.statSync(path).isFile;
} catch {
return false;
}
}
function is_dir(path: string): boolean {
try {
return Deno.statSync(path).isDirectory;
@@ -136,6 +170,66 @@ function is_dir(path: string): boolean {
}
}
export interface ImportOptions {
// the file (for one input file) or folder to write
output?: string;
// the namespace of an input outside assets/<namespace>/, or the only one to take from a folder with several
namespace?: string;
// more folders to look things up in, like parent models from another mod
parents?: string[];
remaps?: Remaps;
}
// one file an import makes: text to write, or a file to copy
export interface Written {
location: string;
id: string;
// where it goes, undefined to print it
file?: string;
text?: string;
copy?: string;
}
export interface ImportReport {
written: Written[];
// models with no boxes of their own, only there for others to use as a parent
parents: string[];
skipped: { location: string; reason: string }[];
errors: { location: string; problems: string[] }[];
warnings: { location: string; warning: string }[];
}
export function empty_report(): ImportReport {
return { written: [], parents: [], skipped: [], errors: [], warnings: [] };
}
// two locations that would become the same id are an error, since one would overwrite the other. claim returns false
// for the second, after reporting it
export function id_claims(report: ImportReport) {
const owners = new Map<string, string>();
return (id: string, location: string) => {
const owner = owners.get(id);
if (owner === undefined) {
owners.set(id, location);
return true;
}
report.errors.push({
location,
problems: [`its id ${id} is the same as ${owner}'s, so give one of them another with --remap`],
});
return false;
};
}
export function write_report(report: ImportReport) {
for (const { file, text, copy } of report.written) {
if (file === undefined) continue;
Deno.mkdirSync(dirname(file), { recursive: true });
if (copy !== undefined) Deno.copyFileSync(copy, file);
else Deno.writeTextFileSync(file, text ?? "");
}
}
// json in the style of the repo's data files: tabs, and short lists and objects of plain values on one line
export function format_json(value: unknown, indent = ""): string {
if (value === null || typeof value !== "object") return JSON.stringify(value);