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
+31 -1
View File
@@ -13,10 +13,13 @@ import {
type EntityJson,
type EntityModelJson,
type FluidJson,
is_ogg,
type ItemJson,
type ManifestJson,
type OreJson,
type RecipeJson,
SOUND_PATH_PATTERN,
type SoundJson,
type SpawnJson,
type TagJson,
validate_block,
@@ -30,6 +33,7 @@ import {
validate_model,
validate_ore,
validate_recipe,
validate_sound,
validate_spawn,
validate_tag,
} from "./mod_data.ts";
@@ -71,6 +75,8 @@ export interface Mod {
textures: Map<string, Uint8Array>;
// the .json of the textures that have one, like animated ones, by texture id
texture_json?: Map<string, TextureJson>;
// ogg bytes by path in its sounds/ without .ogg, like jester/stomp_1, what its sounds.json's files name
sounds?: Map<string, Uint8Array>;
credits?: string;
}
@@ -88,6 +94,7 @@ export class ModError extends Error {
// script.js
// textures/<name>.png the texture <mod id>:<name>
// textures/<name>.json its animation, if it has one
// sounds/<path>.ogg a sound file, see SoundJson
// credits.md
export function write_mod(mod: Mod): Uint8Array<ArrayBuffer> {
const text = (value: string) => new TextEncoder().encode(value);
@@ -110,6 +117,9 @@ export function write_mod(mod: Mod): Uint8Array<ArrayBuffer> {
for (const [id, json] of mod.texture_json ?? []) {
files[`textures/${id.split(":")[1]}.json`] = text(JSON.stringify(json));
}
for (const [path, ogg] of mod.sounds ?? []) {
files[`sounds/${path}.ogg`] = ogg;
}
files["manifest.json"] = text(JSON.stringify(manifest, null, "\t"));
files["data.json"] = text(JSON.stringify(mod.data));
@@ -192,6 +202,7 @@ export function read_mod(bytes: Uint8Array, file: string): Mod {
problems,
) as EntityModelJson[],
animations: checked(raw_data.animations, "animations", validate_clip, problems) as ClipJson[],
sounds: checked(raw_data.sounds, "sounds", validate_sound, problems) as SoundJson[],
effects: checked(raw_data.effects, "effects", validate_effect, problems) as EffectJson[],
fluids: checked(raw_data.fluids, "fluids", validate_fluid, problems) as FluidJson[],
tags: checked(raw_data.tags, "tags", validate_tag, problems) as TagJson[],
@@ -232,6 +243,7 @@ export function read_mod(bytes: Uint8Array, file: string): Mod {
...data.entities,
...data.entity_models,
...data.animations!,
...data.sounds!,
...data.effects,
...data.fluids,
];
@@ -302,8 +314,26 @@ export function read_mod(bytes: Uint8Array, file: string): Mod {
}
}
const sounds = new Map<string, Uint8Array>();
for (const [name, content] of Object.entries(files)) {
const match = name.match(/^sounds\/(.+)\.ogg$/);
if (!match) continue;
if (!SOUND_PATH_PATTERN.test(match[1])) {
problems.push(`${name}: sound paths must be a-z, 0-9 and _ in folders`);
} else if (!is_ogg(content)) {
problems.push(`${name}: isn't an ogg file`);
} else {
sounds.set(match[1], content);
}
}
for (const sound of data.sounds!) {
for (const path of Array.isArray(sound.files) ? sound.files : []) {
if (!sounds.has(path)) problems.push(`sound ${sound.id}: there's no sounds/${path}.ogg`);
}
}
if (problems.length > 0) throw new ModError(file, problems);
return { manifest, data, script, textures, texture_json, credits };
return { manifest, data, script, textures, texture_json, sounds, credits };
}
// the same mod without the terrain's data, its schematics and its config, what players get. the server sends them the world. its