diff options
| author | Valentin Popov <valentin@popov.link> | 2026-12-03 12:27:37 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-12-03 12:27:37 +0300 |
| commit | e59c55289c494a5bd0126b6c40bc4e07a52b8a28 (patch) | |
| tree | 9b968144498a0339b98f1d583f23bb574b8e3e03 /crates/fparkan-assets/src | |
| parent | e9ba4a3e0a039715fdf1c169edd7f56faa8570b1 (diff) | |
| download | fparkan-e59c55289c494a5bd0126b6c40bc4e07a52b8a28.tar.xz fparkan-e59c55289c494a5bd0126b6c40bc4e07a52b8a28.zip | |
feat(ui): use the installed display mode at game startup
Diffstat (limited to 'crates/fparkan-assets/src')
| -rw-r--r-- | crates/fparkan-assets/src/game_settings.rs | 83 | ||||
| -rw-r--r-- | crates/fparkan-assets/src/lib.rs | 1 |
2 files changed, 84 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()); +} 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; |
