Sounds on the client

This commit is contained in:
2026-11-22 07:21:29 -03:00
parent f308ab52f1
commit 47934b272c
21 changed files with 1002 additions and 25 deletions
+44
View File
@@ -9,7 +9,10 @@ import {
data_file,
entity_model_from_geo,
FORMAT_VERSION,
is_ogg,
type OreJson,
SOUND_PATH_PATTERN,
type SoundJson,
type SpawnJson,
type TagJson,
validate_block,
@@ -21,6 +24,7 @@ import {
validate_model,
validate_ore,
validate_recipe,
validate_sound,
validate_spawn,
validate_tag,
} from "@/common/mod_data.ts";
@@ -133,6 +137,7 @@ function load_mod(mods_dir: string, id: string): LoadedMod {
entities: [],
entity_models: [],
animations: [],
sounds: [],
effects: [],
fluids: [],
tags: [],
@@ -148,6 +153,7 @@ function load_mod(mods_dir: string, id: string): LoadedMod {
textures: [],
texture_files: new Map(),
texture_json: new Map(),
sound_files: new Map(),
};
const error = (message: string) => mod.report.errors.push(message);
@@ -306,6 +312,44 @@ function load_mod(mods_dir: string, id: string): LoadedMod {
if (problems.length === 0) mod.schematics.push({ file: path, json });
}
// sounds/<path>.ogg, the files sounds.json's sounds play
for (const file of walk(`${dir}/sounds`, ".ogg")) {
const path = file.replace(/\.ogg$/, "");
if (!SOUND_PATH_PATTERN.test(path)) {
error(`sounds/${file}: sound paths must be a-z, 0-9 and _ in folders`);
} else if (!is_ogg(Deno.readFileSync(`${dir}/sounds/${file}`))) {
error(`sounds/${file}: isn't an ogg file`);
} else {
mod.sound_files.set(path, `${dir}/sounds/${file}`);
}
}
// sounds.json: each key is the sound <mod id>:<key>
const used_sound_files = new Set<string>();
if (exists(`${dir}/sounds.json`)) {
const sounds = read_data("sounds.json", "sounds", z.record(z.string(), json_object), '{ "name": { ... } }') ??
{};
for (const [key, sound] of Object.entries(sounds)) {
const json = { ...sound, id: `${id}:${key}` };
const problems = SOUND_PATH_PATTERN.test(key)
? validate_sound(json)
: [`sound names must be a-z, 0-9 and _ in folders, like "thing/step"`];
for (const file of Array.isArray(sound.files) ? sound.files : []) {
if (typeof file !== "string") continue;
used_sound_files.add(file);
if (!mod.sound_files.has(file) && SOUND_PATH_PATTERN.test(file)) {
problems.push(`there's no sounds/${file}.ogg`);
}
}
for (const problem of problems) error(`sounds.json: ${key}: ${problem}`);
if (problems.length === 0) mod.sounds.push({ file: "sounds.json", json: json as SoundJson });
}
}
for (const path of mod.sound_files.keys()) {
if (!used_sound_files.has(path)) {
mod.report.warnings.push(`sounds/${path}.ogg: no sound in sounds.json plays it`);
}
}
if (exists(`${dir}/config.json`)) {
mod.config = read_data("config.json", "config", json_object, "{ ... }");
}
+6 -1
View File
@@ -3,7 +3,7 @@
// 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 and credits. deno task build does this for every mod in mods/, see docs/modding/gettingstarted/modfiles.md
// 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";
@@ -20,6 +20,10 @@ export async function pack_mod(mod: LoadedMod): Promise<Uint8Array<ArrayBuffer>>
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);
@@ -30,6 +34,7 @@ export async function pack_mod(mod: LoadedMod): Promise<Uint8Array<ArrayBuffer>>
script,
textures,
texture_json,
sounds,
credits: manifest.credits ? Deno.readTextFileSync(`${mod.dir}/${manifest.credits}`) : undefined,
});
}