diff options
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/fparkan-assets/src/game_settings.rs | 83 | ||||
| -rw-r--r-- | crates/fparkan-assets/src/lib.rs | 1 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/hero_look.rs | 29 |
3 files changed, 89 insertions, 24 deletions
diff --git a/crates/fparkan-assets/src/game_settings.rs b/crates/fparkan-assets/src/game_settings.rs new file mode 100644 index 00000000..a57d3f62 --- /dev/null +++ b/crates/fparkan-assets/src/game_settings.rs @@ -0,0 +1,83 @@ +//! Settings shared with the installed game's `Iron_3D.ini` profile. + +use fparkan_path::{normalize_relative, PathPolicy}; +use fparkan_vfs::{Vfs, VfsError}; +use std::sync::Arc; + +/// The original game's profile. A missing INI supplies no overrides. +#[derive(Default)] +pub struct GameSettings(Arc<[u8]>); + +impl GameSettings { + /// Reads the installed profile through the same VFS as the game data. + pub fn load(vfs: &dyn Vfs) -> Result<Self, VfsError> { + let path = normalize_relative(b"Iron_3D.ini", PathPolicy::StrictLegacy) + .map_err(|_| VfsError::Path)?; + match vfs.read(&path) { + Ok(bytes) => Ok(Self(bytes)), + Err(VfsError::NotFound(_)) => Ok(Self::default()), + Err(error) => Err(error), + } + } + + /// Reads an integer from the case-insensitive `[CS]` section. + pub fn integer(&self, key: &str) -> Option<i32> { + let text = String::from_utf8_lossy(&self.0); + let mut controls = false; + for line in text.lines().map(str::trim) { + if let Some(section) = line.strip_prefix('[').and_then(|line| line.split_once(']')) { + controls = section.0.eq_ignore_ascii_case("CS"); + continue; + } + if controls { + if let Some((name, value)) = line.split_once('=') { + if name.trim().eq_ignore_ascii_case(key) { + return value.trim().parse().ok(); + } + } + } + } + None + } + + /// Returns the requested display when both client dimensions are present. + pub fn display(&self) -> Option<DisplaySettings> { + let positive = |key| u32::try_from(self.integer(key)?).ok().filter(|&n| n != 0); + Some(DisplaySettings { + extent: [positive("DISPLAY_WIDTH")?, positive("DISPLAY_HEIGHT")?], + bit_depth: positive("BITDEPTH").unwrap_or(32), + windowed: self.integer("WINDOW_MODE").unwrap_or(1) != 0, + }) + } +} + +/// The size, color depth and display mode requested by the legacy profile. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct DisplaySettings { + pub extent: [u32; 2], + pub bit_depth: u32, + pub windowed: bool, +} + +impl DisplaySettings { + /// The original mode selector gives each pixel of difference a weight of 100. + pub fn mode_distance(self, extent: [u32; 2], bit_depth: u32) -> u64 { + let [width, height] = self.extent; + (u64::from(width.abs_diff(extent[0])) + u64::from(height.abs_diff(extent[1]))) * 100 + + u64::from(self.bit_depth.abs_diff(bit_depth)) + } +} + +#[cfg(test)] +#[test] +fn reads_the_original_display_profile_and_scores_video_modes() { + let settings = GameSettings(Arc::from( + &b"[OTHER]\nDISPLAY_WIDTH=1\n[cs]\n display_width=800\n DISPLAY_HEIGHT = 600\nBITDEPTH=16\nWINDOW_MODE=0"[..], + )); + let display = settings.display().unwrap(); + assert_eq!(display.extent, [800, 600]); + assert!(!display.windowed); + assert_eq!(display.mode_distance([800, 600], 32), 16); + assert_eq!(display.mode_distance([799, 601], 16), 200); + assert!(GameSettings::default().display().is_none()); +} diff --git a/crates/fparkan-assets/src/lib.rs b/crates/fparkan-assets/src/lib.rs index a0b207d9..df9bbce5 100644 --- a/crates/fparkan-assets/src/lib.rs +++ b/crates/fparkan-assets/src/lib.rs @@ -42,6 +42,7 @@ mod effect_material_phases; mod explosion_profile; pub mod factory_projects; pub mod game_font; +pub mod game_settings; pub mod help_resources; pub mod help_screen; pub mod mission_catalog; diff --git a/crates/fparkan-runtime/src/hero_look.rs b/crates/fparkan-runtime/src/hero_look.rs index 2e923d20..a31512d1 100644 --- a/crates/fparkan-runtime/src/hero_look.rs +++ b/crates/fparkan-runtime/src/hero_look.rs @@ -1,3 +1,4 @@ +use fparkan_assets::game_settings::GameSettings; use fparkan_path::{normalize_relative, PathPolicy}; use fparkan_vfs::Vfs; use std::f32::consts::{PI, TAU}; @@ -29,9 +30,8 @@ impl HeroLook { vfs.read(&path).ok() }; let table_bytes = read(table_name); - let settings_bytes = read(b"Iron_3D.ini"); let table = String::from_utf8_lossy(table_bytes.as_deref().unwrap_or_default()); - let settings = String::from_utf8_lossy(settings_bytes.as_deref().unwrap_or_default()); + let settings = GameSettings::load(vfs).unwrap_or_default(); Self { yaw_turns: mouse_binding( &table, @@ -71,8 +71,9 @@ impl HeroLook { ) .unwrap_or(0.15), ], - reverse_y: control_setting(&settings, "MOUSE_REV_Y").unwrap_or(0) != 0, - sensitivity: control_setting(&settings, "MOUSE_SENS") + reverse_y: settings.integer("MOUSE_REV_Y").unwrap_or(0) != 0, + sensitivity: settings + .integer("MOUSE_SENS") .map_or(1.0, |value| value as f32 * 0.01), filtered: [0.0; 2], phase: 0.5, @@ -198,23 +199,6 @@ fn mouse_binding( }) } -fn control_setting(settings: &str, key: &str) -> Option<i32> { - let mut controls = false; - for line in settings.lines().map(str::trim) { - if let Some(section) = line.strip_prefix('[').and_then(|line| line.split_once(']')) { - controls = section.0.eq_ignore_ascii_case("CS"); - } - if controls { - if let Some((name, value)) = line.split_once('=') { - if name.trim().eq_ignore_ascii_case(key) { - return value.trim().parse().ok(); - } - } - } - } - None -} - #[cfg(test)] mod tests { use super::*; @@ -262,9 +246,6 @@ MOUSE SCAN_NULL SCAN_MOUSE_Y 1 CICLS_TURRET MCMD_ANGLE_Y 0.30 1 MAN_NOTWRAP 0.0 ), Some(0.30) ); - let settings = "[VIDEO]\nMOUSE_SENS=100\n[cs]\n mouse_sens = 50\n mouse_rev_y=1"; - assert_eq!(control_setting(settings, "MOUSE_SENS"), Some(50)); - assert_eq!(control_setting(settings, "MOUSE_REV_Y"), Some(1)); } #[test] |
