65 lines
2.7 KiB
TypeScript
65 lines
2.7 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// deno task pack-mod mods/copper_tools [copper_tools.cubi]
|
|
// builds a mod folder into a .cubi: its data merged into data.json, its script bundled into one module, and its
|
|
// textures, sounds and credits. deno task build does this for every mod in mods/, see docs/modding/gettingstarted/modfiles.md
|
|
import { basename, dirname, resolve } from "@std/path";
|
|
import { MOD_EXTENSION, write_mod } from "@/common/mod.ts";
|
|
import type { ManifestJson } from "@/common/mod_data.ts";
|
|
import { bundle_script, load_and_check, type LoadedMod, main_script } from "./check_mods.ts";
|
|
import { mod_data } from "@/common/mod_check.ts";
|
|
|
|
// a mod that passed check-mods, as .cubi bytes
|
|
export async function pack_mod(mod: LoadedMod): Promise<Uint8Array<ArrayBuffer>> {
|
|
const manifest = mod.manifest as unknown as ManifestJson;
|
|
const script = manifest.main ? await bundle_script(mod.dir, `${mod.dir}/${manifest.main}`) : undefined;
|
|
|
|
const textures = new Map<string, Uint8Array>();
|
|
for (const [id, path] of [...mod.texture_files].sort(([a], [b]) => a.localeCompare(b))) {
|
|
textures.set(id, Deno.readFileSync(path));
|
|
}
|
|
const texture_json = new Map([...mod.texture_json].sort(([a], [b]) => a.localeCompare(b)));
|
|
const sounds = new Map<string, Uint8Array>();
|
|
for (const [path, file] of [...mod.sound_files].sort(([a], [b]) => a.localeCompare(b))) {
|
|
sounds.set(path, Deno.readFileSync(file));
|
|
}
|
|
|
|
// without what the content phase registered, which the server registers again when it loads the .cubi
|
|
const data = mod_data(mod);
|
|
data.blocks = mod.blocks.filter(({ file }) => file !== main_script(mod)).map(({ json }) => json);
|
|
return write_mod({
|
|
manifest,
|
|
data,
|
|
script,
|
|
textures,
|
|
texture_json,
|
|
sounds,
|
|
credits: manifest.credits ? Deno.readTextFileSync(`${mod.dir}/${manifest.credits}`) : undefined,
|
|
});
|
|
}
|
|
|
|
if (import.meta.main) {
|
|
const [dir, out] = Deno.args;
|
|
if (!dir) {
|
|
console.error("usage: deno task pack-mod <mod folder> [output.cubi]");
|
|
Deno.exit(1);
|
|
}
|
|
// its siblings are checked too, so references to mods it depends on can be checked
|
|
const folder = resolve(dir);
|
|
const mod = (await load_and_check(dirname(folder))).find((m) => resolve(m.dir) === folder);
|
|
if (!mod) {
|
|
console.error(`${dir} isn't a mod folder`);
|
|
Deno.exit(1);
|
|
}
|
|
if (mod.report.errors.length > 0) {
|
|
console.error(`${mod.id} has errors (deno task check-mods for details):`);
|
|
for (const error of mod.report.errors) console.error(` ${error}`);
|
|
Deno.exit(1);
|
|
}
|
|
const file = out ?? `${basename(folder)}${MOD_EXTENSION}`;
|
|
const bytes = await pack_mod(mod);
|
|
Deno.writeFileSync(file, bytes);
|
|
console.log(`Packed ${mod.id} into ${file} (${(bytes.length / 1024).toFixed(1)} KB)`);
|
|
}
|