diff options
Diffstat (limited to 'crates/fparkan-assets/src/game_settings.rs')
| -rw-r--r-- | crates/fparkan-assets/src/game_settings.rs | 83 |
1 files changed, 83 insertions, 0 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()); +} |
