Sounds on the client
This commit is contained in:
@@ -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, "{ ... }");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user