From 5575b7f79381c4dac0b4af9effa5dc1050a88607 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Thu, 3 Dec 2026 13:59:29 +0400 Subject: feat(ui): edit the installed sound settings from the menu --- crates/fparkan-assets/src/game_settings.rs | 76 ++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) (limited to 'crates/fparkan-assets/src') diff --git a/crates/fparkan-assets/src/game_settings.rs b/crates/fparkan-assets/src/game_settings.rs index f330bae5..fa50dd4b 100644 --- a/crates/fparkan-assets/src/game_settings.rs +++ b/crates/fparkan-assets/src/game_settings.rs @@ -67,6 +67,66 @@ impl GameSettings { play_cd: self.integer("PLAY_CD_MUSIC").unwrap_or_default() != 0, } } + + /// Replaces selected `[CS]` integers, preserving the other profile bytes. + pub fn with_cs_integers(&self, updates: &[(&str, i32)]) -> Vec { + let mut bytes = self.0.to_vec(); + let newline = if bytes.windows(2).any(|pair| pair == b"\r\n") { + "\r\n" + } else { + "\n" + }; + for &(key, value) in updates { + write_cs_integer(&mut bytes, key, value, newline); + } + bytes + } +} + +fn write_cs_integer(bytes: &mut Vec, key: &str, value: i32, newline: &str) { + let mut offset = 0; + let mut in_cs = false; + let mut insert_at = None; + for line in bytes.split_inclusive(|&byte| byte == b'\n') { + let trimmed = line.trim_ascii(); + if let Some(section) = trimmed.strip_prefix(b"[") { + in_cs = section + .iter() + .position(|&byte| byte == b']') + .is_some_and(|end| section[..end].eq_ignore_ascii_case(b"CS")); + } else if in_cs { + if let Some(equals) = line.iter().position(|&byte| byte == b'=') { + if line[..equals] + .trim_ascii() + .eq_ignore_ascii_case(key.as_bytes()) + { + let field = &line[equals + 1..]; + let content = field.trim_ascii_end(); + let start = + offset + equals + 1 + content.len() - content.trim_ascii_start().len(); + let end = offset + equals + 1 + content.len(); + bytes.splice(start..end, value.to_string().bytes()); + return; + } + } + } + offset += line.len(); + if in_cs { + insert_at = Some(offset); + } + } + + let at = insert_at.unwrap_or(bytes.len()); + let separator = if at > 0 && bytes[at - 1] != b'\n' { + newline + } else { + "" + }; + let entry = match insert_at { + Some(_) => format!("{separator}{key}={value}{newline}"), + None => format!("{separator}[CS]{newline}{key}={value}{newline}"), + }; + bytes.splice(at..at, entry.bytes()); } /// The size, color depth and display mode requested by the legacy profile. @@ -133,3 +193,19 @@ fn reads_separate_effects_and_cd_controls() { assert_eq!(music.audio().cd_gain, Some(0.0)); assert!(music.audio().play_cd); } + +#[cfg(test)] +#[test] +fn updates_only_the_chosen_cs_entries() { + let settings = GameSettings(Arc::from( + &b";\xef\xf0\xe8\xec\xe5\xf0\r\n[cs]\r\n sfx_volume = 20 \r\n[NETWORK]\r\nSFX_VOLUME=5"[..], + )); + assert_eq!( + settings.with_cs_integers(&[("SFX_VOLUME", 48), ("PLAY_CD_MUSIC", 1)]), + b";\xef\xf0\xe8\xec\xe5\xf0\r\n[cs]\r\n sfx_volume = 48 \r\nPLAY_CD_MUSIC=1\r\n[NETWORK]\r\nSFX_VOLUME=5" + ); + assert_eq!( + GameSettings::default().with_cs_integers(&[("CD_VOLUME", 33)]), + b"[CS]\nCD_VOLUME=33\n" + ); +} -- cgit v1.2.3