From 38a9a4d11821fe5b9948d4a94935e5ccb103fdf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Mon, 4 Jan 2021 21:45:32 +0100 Subject: cows aren't optional --- mumd/src/audio.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'mumd/src/audio.rs') diff --git a/mumd/src/audio.rs b/mumd/src/audio.rs index b5b5c7f..569cdc5 100644 --- a/mumd/src/audio.rs +++ b/mumd/src/audio.rs @@ -248,10 +248,11 @@ impl Audio { self.sounds = NotificationEvents::iter() .map(|event| { - let bytes = overrides.get(&event) - .map(|file| get_resource(file)) - .flatten() - .unwrap_or(Cow::from(include_bytes!("fallback_sfx.wav").as_ref())); + + let bytes = match overrides.get(&event) { + Some(file) => get_sfx(file), + None => get_default_sfx(), + }; let reader = hound::WavReader::new(bytes.as_ref()).unwrap(); let spec = reader.spec(); let samples = match spec.sample_format { @@ -382,14 +383,19 @@ impl Audio { } // moo -fn get_resource(file: &str) -> Option> { +fn get_sfx(file: &str) -> Cow<'static, [u8]> { let mut buf: Vec = Vec::new(); if let Ok(mut file) = File::open(file) .or_else(|_| File::open(format!("/home/gustav/dev/mum/mumd/src/resources/{}", file))) { file.read_to_end(&mut buf).unwrap(); - Some(Cow::from(buf)) + Cow::from(buf) } else { - None + warn!("File not found: '{}'", file); + get_default_sfx() } } + +fn get_default_sfx() -> Cow<'static, [u8]> { + Cow::from(include_bytes!("fallback_sfx.wav").as_ref()) +} -- cgit v1.2.1