Sounds on the client
This commit is contained in:
@@ -605,6 +605,43 @@ const clip_schema = z.object({
|
||||
|
||||
export type ClipJson = z.infer<typeof clip_schema>;
|
||||
|
||||
// which volume slider a sound follows, each under the player's master volume. see docs/modding/resources/sounds.md
|
||||
export const SOUND_CATEGORIES = ["music", "ambient", "blocks", "entities", "players", "ui"] as const;
|
||||
export type SoundCategory = typeof SOUND_CATEGORIES[number];
|
||||
|
||||
// a path in a mod's sounds/ without .ogg, like jester/stomp_1, and a sound's name in sounds.json
|
||||
export const SOUND_PATH_PATTERN = /^[a-z0-9_]+(\/[a-z0-9_]+)*$/;
|
||||
|
||||
// one of the sounds in a mod's sounds.json, with its id: the key "jester/step" in dangerous_business's is
|
||||
// dangerous_business:jester/step
|
||||
const sound_schema = z.object({
|
||||
id: z.string('must be a sound id like "my_mod:thing/step"').regex(TAG_ID_PATTERN),
|
||||
// paths in the mod's own sounds/, one picked at random each time it plays
|
||||
files: z.array(
|
||||
z.string('must be a path in sounds/ without .ogg, like "thing/step_1"').regex(SOUND_PATH_PATTERN),
|
||||
).min(1),
|
||||
// at most 1, plays multiply it. 1 when not given
|
||||
volume: z.number().min(0).max(1).optional(),
|
||||
// 1 when not given
|
||||
pitch: z.number().gt(0).optional(),
|
||||
// how far away it's heard at volume 1, in blocks. 16 when not given
|
||||
range: z.number().gt(0).optional(),
|
||||
// only the master volume when not given
|
||||
category: z.enum(SOUND_CATEGORIES).optional(),
|
||||
});
|
||||
|
||||
export type SoundJson = z.infer<typeof sound_schema>;
|
||||
|
||||
export function validate_sound(json: unknown): Problems {
|
||||
return find_problems(sound_schema, json);
|
||||
}
|
||||
|
||||
// whether a file starts the way every ogg does, so a wav or mp3 renamed to .ogg is caught when the mod is checked
|
||||
// instead of failing to play
|
||||
export function is_ogg(bytes: Uint8Array): boolean {
|
||||
return bytes.length >= 4 && bytes[0] === 0x4f && bytes[1] === 0x67 && bytes[2] === 0x67 && bytes[3] === 0x53;
|
||||
}
|
||||
|
||||
// something in a tag file's values or remove: an id, a "#tag", or either with whether it must exist. a plain one must
|
||||
const tag_entry = z.union(
|
||||
[id_or_tag, z.object({ id: id_or_tag, required: z.boolean().optional() })],
|
||||
|
||||
Reference in New Issue
Block a user