From d2a671e04c1aede50b4e8859bbca6236c745a7d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B3zsef=20Sallai?= Date: Mon, 7 Mar 2022 15:47:37 +0200 Subject: [PATCH] refactor CS+ soundtrack loading (#79) --- src/engine_constants/mod.rs | 20 ++++++++++++++------ src/menu/settings_menu.rs | 3 ++- src/scene/jukebox_scene.rs | 3 ++- src/shared_game_state.rs | 7 +++++++ src/sound/mod.rs | 6 ++++-- 5 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/engine_constants/mod.rs b/src/engine_constants/mod.rs index 3999580..a19b7d7 100644 --- a/src/engine_constants/mod.rs +++ b/src/engine_constants/mod.rs @@ -203,6 +203,13 @@ pub struct AnimatedFace { pub anim_frames: Vec<(u16, u16)>, } +#[derive(Debug, Clone)] +pub struct ExtraSoundtrack { + pub name: String, + pub path: String, + pub available: bool, +} + #[derive(Debug, Copy, Clone)] pub struct TextScriptConsts { pub encoding: TextScriptEncoding, @@ -298,7 +305,7 @@ pub struct EngineConstants { pub font_path: String, pub font_scale: f32, pub font_space_offset: f32, - pub soundtracks: HashMap, + pub soundtracks: Vec, pub music_table: Vec, pub organya_paths: Vec, pub credit_illustration_paths: Vec, @@ -1552,7 +1559,12 @@ impl EngineConstants { font_path: "csfont.fnt".to_owned(), font_scale: 1.0, font_space_offset: 0.0, - soundtracks: HashMap::new(), + soundtracks: vec![ + ExtraSoundtrack { name: "Remastered".to_owned(), path: "/base/Ogg11/".to_owned(), available: false }, + ExtraSoundtrack { name: "New".to_owned(), path: "/base/Ogg/".to_owned(), available: false }, + ExtraSoundtrack { name: "Famitracks".to_owned(), path: "/base/ogg17/".to_owned(), available: false }, + ExtraSoundtrack { name: "Ridiculon".to_owned(), path: "/base/ogg_ridic/".to_owned(), available: false }, + ], music_table: vec![ "xxxx".to_owned(), "wanpaku".to_owned(), @@ -1639,8 +1651,6 @@ impl EngineConstants { self.font_path = "csfont.fnt".to_owned(); self.font_scale = 0.5; - self.soundtracks.insert("Remastered".to_owned(), "/base/Ogg11/".to_owned()); - self.soundtracks.insert("New".to_owned(), "/base/Ogg/".to_owned()); let typewriter_sample = PixToneParameters { // fx2 (CS+) @@ -1688,8 +1698,6 @@ impl EngineConstants { self.textscript.text_speed_normal = 1; self.textscript.text_speed_fast = 0; self.textscript.fade_ticks = 21; - self.soundtracks.insert("Famitracks".to_owned(), "/base/ogg17/".to_owned()); - self.soundtracks.insert("Ridiculon".to_owned(), "/base/ogg_ridic/".to_owned()); self.game.tile_offset_x = 3; self.game.new_game_player_pos = (13, 8); } diff --git a/src/menu/settings_menu.rs b/src/menu/settings_menu.rs index fa4b5d4..4b313e1 100644 --- a/src/menu/settings_menu.rs +++ b/src/menu/settings_menu.rs @@ -109,7 +109,8 @@ impl SettingsMenu { self.sound.push_entry(MenuEntry::Active(format!("Soundtrack: {}", state.settings.soundtrack))); self.sound.push_entry(MenuEntry::Active("< Back".to_owned())); - let mut soundtrack_entries = state.constants.soundtracks.keys().map(|s| s.to_owned()).collect_vec(); + let mut soundtrack_entries = + state.constants.soundtracks.iter().filter(|s| s.available).map(|s| s.name.to_owned()).collect_vec(); soundtrack_entries.push("Organya".to_owned()); if let Ok(dir) = filesystem::read_dir(ctx, "/Soundtracks/") { diff --git a/src/scene/jukebox_scene.rs b/src/scene/jukebox_scene.rs index 253ff72..f2b0ec2 100644 --- a/src/scene/jukebox_scene.rs +++ b/src/scene/jukebox_scene.rs @@ -68,7 +68,8 @@ impl Scene for JukeboxScene { self.song_list = state.constants.music_table.iter().filter(|song| !song.contains("fanfale")).cloned().collect(); - let mut soundtrack_entries = state.constants.soundtracks.keys().map(|s| s.to_owned()).collect_vec(); + let mut soundtrack_entries = + state.constants.soundtracks.iter().filter(|s| s.available).map(|s| s.name.to_owned()).collect_vec(); soundtrack_entries.push("Organya".to_owned()); if let Ok(dir) = filesystem::read_dir(ctx, "/Soundtracks/") { diff --git a/src/shared_game_state.rs b/src/shared_game_state.rs index acd5b09..2e1c557 100644 --- a/src/shared_game_state.rs +++ b/src/shared_game_state.rs @@ -233,6 +233,13 @@ impl SharedGameState { info!("NXEngine-evo data files detected."); } + for soundtrack in constants.soundtracks.iter_mut() { + if filesystem::exists(ctx, &soundtrack.path) { + info!("Enabling soundtrack {} from {}.", soundtrack.name, soundtrack.path); + soundtrack.available = true; + } + } + let season = Season::current(); constants.rebuild_path_list(None, season, &settings); diff --git a/src/sound/mod.rs b/src/sound/mod.rs index 9d7986c..4b08f0a 100644 --- a/src/sound/mod.rs +++ b/src/sound/mod.rs @@ -200,8 +200,10 @@ impl SoundManager { paths.insert(0, "/Soundtracks/".to_owned() + &settings.soundtrack + "/"); - if let Some(soundtrack) = constants.soundtracks.get(&settings.soundtrack) { - paths.insert(0, soundtrack.clone()); + if let Some(soundtrack) = + constants.soundtracks.iter().find(|s| s.available && s.name == settings.soundtrack) + { + paths.insert(0, soundtrack.path.clone()); } let songs_paths = paths.iter().map(|prefix| {