Worm bin model

This commit is contained in:
2026-10-12 18:58:38 -03:00
parent 99bdcaeffc
commit 038b6d2caa
16 changed files with 447 additions and 39 deletions
+35 -12
View File
@@ -12,10 +12,11 @@ export interface Location {
path: string;
}
export function parse_location(value: string): Location {
// bare is the namespace of a location written without one: minecraft's, in a mod's resources
export function parse_location(value: string, bare = "minecraft"): Location {
const colon = value.indexOf(":");
return colon < 0
? { namespace: "minecraft", path: value }
? { namespace: bare, path: value }
: { namespace: value.slice(0, colon), path: value.slice(colon + 1) };
}
@@ -53,8 +54,8 @@ function template_remaps(): Remap {
);
}
export function remap_location(location: string, remap: Remap = {}): Location {
const full = location_string(parse_location(location));
export function remap_location(location: string, remap: Remap = {}, bare = "minecraft"): Location {
const full = location_string(parse_location(location, bare));
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) {
@@ -64,8 +65,8 @@ export function remap_location(location: string, remap: Remap = {}): Location {
return parse_location(full);
}
export function to_id(location: string, remap: Remap = {}): string {
const { namespace, path } = remap_location(location, remap);
export function to_id(location: string, remap: Remap = {}, bare = "minecraft"): string {
const { namespace, path } = remap_location(location, remap, bare);
return `${namespace}:${path.replaceAll("/", "_")}`;
}
@@ -77,6 +78,9 @@ export function is_valid_id(id: string): boolean {
export interface ResourceRoot {
namespace: string;
dir: string;
// not in a mod's assets/ but a loose folder, like blockbench's exports. what it names without a namespace is its
// own, not minecraft's: a texture "gear" next to a model is that model's texture
loose?: boolean;
}
// the roots of a kind inside a folder: every assets/<namespace>/<kind> under it (only namespace's, when it's given), or
@@ -93,7 +97,7 @@ export function find_roots(input: string, kind: string, namespace?: string): Res
}
}
}
if (roots.length === 0) return namespace ? [{ namespace, dir }] : [];
if (roots.length === 0) return namespace ? [{ namespace, dir, loose: true }] : [];
return namespace ? roots.filter((root) => root.namespace === namespace) : roots;
}
@@ -103,12 +107,21 @@ export interface InputFile {
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[] {
// what to convert: one file, or every file with the extension in a folder's roots of a kind. a file outside any
// assets/ folder is the namespace's, or the mod's that output is in
export function input_files(
input: string,
kind: string,
extension: string,
namespace?: string,
output?: 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 enclosing = enclosing_root(input, kind);
if (enclosing) return [{ root: enclosing, file: resolve(input) }];
const loose = namespace ?? (output && mod_namespace(output));
if (!loose) throw new Error(`can't tell ${input}'s namespace: pass --namespace, or an --output in a mod`);
return [{ root: { namespace: loose, dir: dirname(resolve(input)), loose: true }, file: resolve(input) }];
}
const roots = find_roots(input, kind, namespace);
if (roots.length === 0) {
@@ -169,6 +182,16 @@ function read_dir(dir: string): Deno.DirEntry[] {
}
}
// the id of the mod a path is in, from the manifest.json of the nearest folder up that has one
export function mod_namespace(path: string): string | undefined {
for (let dir = dirname(resolve(path)); dir !== dirname(dir); dir = dirname(dir)) {
if (!is_file(`${dir}/manifest.json`)) continue;
const id = JSON.parse(Deno.readTextFileSync(`${dir}/manifest.json`)).id;
return typeof id === "string" ? id : undefined;
}
return undefined;
}
export function is_file(path: string): boolean {
try {
return Deno.statSync(path).isFile;