Importer
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// where things are in a java mod's resources, and what they're called in bworld. a resource location like
|
||||
// "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";
|
||||
|
||||
export interface Location {
|
||||
namespace: string;
|
||||
path: string;
|
||||
}
|
||||
|
||||
export function parse_location(value: string): Location {
|
||||
const colon = value.indexOf(":");
|
||||
return colon < 0
|
||||
? { namespace: "minecraft", path: value }
|
||||
: { namespace: value.slice(0, colon), path: value.slice(colon + 1) };
|
||||
}
|
||||
|
||||
export function location_string({ namespace, path }: Location): string {
|
||||
return `${namespace}:${path}`;
|
||||
}
|
||||
|
||||
// rewrites applied to a location before it becomes an id, longest match first: { "minecraft:block/": "bworld:" } turns
|
||||
// minecraft's block textures into the base game's, which are named the same. a key without a trailing / only matches
|
||||
// that location exactly
|
||||
export type Remap = Record<string, string>;
|
||||
|
||||
export interface Remaps {
|
||||
textures: Remap;
|
||||
models: Remap;
|
||||
}
|
||||
|
||||
export const DEFAULT_REMAPS: Remaps = {
|
||||
textures: { "minecraft:block/": "bworld:", "minecraft:item/": "bworld:" },
|
||||
models: {},
|
||||
};
|
||||
|
||||
export function to_id(location: string, remap: Remap = {}): string {
|
||||
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;
|
||||
}
|
||||
}
|
||||
const { namespace, path } = parse_location(mapped);
|
||||
return `${namespace}:${path.replaceAll("/", "_")}`;
|
||||
}
|
||||
|
||||
export function is_valid_id(id: string): boolean {
|
||||
return ID_PATTERN.test(id);
|
||||
}
|
||||
|
||||
// a folder of one kind of resource (models, textures, ...) for one namespace: assets/<namespace>/<kind>
|
||||
export interface ResourceRoot {
|
||||
namespace: string;
|
||||
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
|
||||
export function find_roots(input: string, kind: string, namespace?: string): ResourceRoot[] {
|
||||
const dir = resolve(input);
|
||||
const enclosing = enclosing_root(dir, kind);
|
||||
if (enclosing) return [enclosing];
|
||||
const roots: ResourceRoot[] = [];
|
||||
for (const assets of find_dirs(dir, "assets")) {
|
||||
for (const entry of read_dir(assets)) {
|
||||
if (entry.isDirectory && is_dir(`${assets}/${entry.name}/${kind}`)) {
|
||||
roots.push({ namespace: entry.name, dir: `${assets}/${entry.name}/${kind}` });
|
||||
}
|
||||
}
|
||||
}
|
||||
if (roots.length === 0 && namespace) roots.push({ namespace, dir });
|
||||
return roots;
|
||||
}
|
||||
|
||||
// the assets/<namespace>/<kind> folder a path is in, if it's in one
|
||||
export function enclosing_root(path: string, kind: string): ResourceRoot | undefined {
|
||||
const parts = resolve(path).split(SEPARATOR);
|
||||
for (let i = parts.length - 1; i >= 2; i--) {
|
||||
if (parts[i] === kind && parts[i - 2] === "assets") {
|
||||
return { namespace: parts[i - 1], dir: parts.slice(0, i + 1).join(SEPARATOR) };
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// a file's location inside its root: assets/create/models/block/cogwheel.json is create:block/cogwheel
|
||||
export function location_of(root: ResourceRoot, file: string, extension: string): Location {
|
||||
const path = relative(root.dir, resolve(file)).split(SEPARATOR).join("/");
|
||||
return { namespace: root.namespace, path: path.slice(0, -extension.length) };
|
||||
}
|
||||
|
||||
// every file with the extension under a folder, as full paths
|
||||
export function walk_files(dir: string, extension: string): string[] {
|
||||
const files: string[] = [];
|
||||
for (const entry of read_dir(dir)) {
|
||||
const path = `${dir}/${entry.name}`;
|
||||
if (entry.isDirectory) files.push(...walk_files(path, extension));
|
||||
else if (entry.name.endsWith(extension)) files.push(path);
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
function find_dirs(dir: string, name: string): string[] {
|
||||
const found: string[] = [];
|
||||
for (const entry of read_dir(dir)) {
|
||||
if (!entry.isDirectory) continue;
|
||||
const path = `${dir}/${entry.name}`;
|
||||
if (entry.name === name) found.push(path);
|
||||
else found.push(...find_dirs(path, name));
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
function read_dir(dir: string): Deno.DirEntry[] {
|
||||
try {
|
||||
return [...Deno.readDirSync(dir)].sort((a, b) => a.name.localeCompare(b.name));
|
||||
} catch (e) {
|
||||
if (e instanceof Deno.errors.NotFound || e instanceof Deno.errors.NotADirectory) return [];
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
function is_dir(path: string): boolean {
|
||||
try {
|
||||
return Deno.statSync(path).isDirectory;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
const entries = Array.isArray(value)
|
||||
? value.map((v) => format_json(v, indent + "\t"))
|
||||
: Object.entries(value).filter(([, v]) => v !== undefined).map(([k, v]) =>
|
||||
`${JSON.stringify(k)}: ${format_json(v, indent + "\t")}`
|
||||
);
|
||||
const [open, close] = Array.isArray(value) ? ["[", "]"] : ["{", "}"];
|
||||
if (entries.length === 0) return open + close;
|
||||
const flat = Object.values(value).every((v) =>
|
||||
v === null || typeof v !== "object" || (Array.isArray(v) && v.every((x) => x === null || typeof x !== "object"))
|
||||
);
|
||||
const one_line = Array.isArray(value) ? `[${entries.join(", ")}]` : `{ ${entries.join(", ")} }`;
|
||||
if (flat && !one_line.includes("\n") && indent.length * 4 + one_line.length <= 100) return one_line;
|
||||
return `${open}\n${entries.map((e) => indent + "\t" + e).join(",\n")}\n${indent}${close}`;
|
||||
}
|
||||
Reference in New Issue
Block a user