1use std::path::{Path, PathBuf};
4
5pub const RESOLUTIONS: &[(u32, u32)] = &[
7 (640, 480),
8 (800, 600),
9 (1024, 768),
10 (1280, 720),
11 (1280, 1024),
12 (1600, 900),
13 (1920, 1080),
14 (1920, 1200),
15 (2560, 1080),
16 (2560, 1440),
17 (3440, 1440),
18 (3840, 2160),
19];
20
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub enum Upscale {
24 Nearest,
26 Smooth,
28 Sharp,
30}
31
32impl Upscale {
33 pub const ALL: [Upscale; 3] = [Upscale::Nearest, Upscale::Smooth, Upscale::Sharp];
34 pub fn name(self) -> &'static str {
35 match self {
36 Upscale::Nearest => "Nearest",
37 Upscale::Smooth => "Smooth",
38 Upscale::Sharp => "Sharp",
39 }
40 }
41 fn key(self) -> &'static str {
42 match self {
43 Upscale::Nearest => "nearest",
44 Upscale::Smooth => "smooth",
45 Upscale::Sharp => "sharp",
46 }
47 }
48}
49
50pub const MAX_FPS: &[u32] = &[0, 60, 120, 144, 240];
52
53pub const ANTIALIAS: &[(&str, f64)] = &[("Off", 1.0), ("2x", std::f64::consts::SQRT_2), ("4x", 2.0)];
55
56#[derive(Debug, Clone)]
57pub struct Config {
58 pub data_dir: PathBuf,
60 pub fullscreen: bool,
61 pub window_width: u32,
62 pub window_height: u32,
63 pub render_scale: f64,
65 pub mouse_sensitivity: f64,
66 pub invert_mouse: bool,
67 pub always_run: bool,
68 pub sound: bool,
69 pub music: bool,
70 pub volume: f64,
71 pub music_volume: f64,
72 pub start_map: Option<String>,
73 pub skill: i32,
74 pub resolution: Option<(u32, u32)>,
76 pub upscale: Upscale,
77 pub antialias: usize,
79 pub max_fps: u32,
81 pub ini_path: Option<PathBuf>,
83}
84
85impl Default for Config {
86 fn default() -> Self {
87 Config {
88 data_dir: PathBuf::from("."),
89 fullscreen: true,
90 window_width: 1600,
91 window_height: 900,
92 render_scale: 1.0,
93 mouse_sensitivity: 0.35,
94 invert_mouse: false,
95 always_run: true,
96 sound: true,
97 music: true,
98 volume: 0.8,
99 music_volume: 0.6,
100 start_map: None,
101 skill: 2,
102 resolution: None,
103 upscale: Upscale::Sharp,
104 antialias: 0,
105 max_fps: 0,
106 ini_path: None,
107 }
108 }
109}
110
111pub const DEFAULT_INI: &str = "; OpenBlood configuration
112; Path to your ORIGINAL Blood installation (folder with BLOOD.RFF, SOUNDS.RFF, TILES*.ART)
113data_dir = .
114fullscreen = 1
115window_width = 1600
116window_height = 900
117; native, or a size like 1920x1080 (the image is fitted to the window)
118resolution = native
119; 1.0 renders at the full resolution; lower values trade sharpness for speed
120render_scale = 1.0
121; nearest, smooth or sharp: how the image is scaled to the window
122upscale = sharp
123; anti-aliasing (supersampling): 0 = off, 1 = 2x, 2 = 4x
124antialias = 0
125; frame-rate cap: 0 = unlimited, or e.g. 60 / 120 / 144
126max_fps = 0
127mouse_sensitivity = 0.35
128invert_mouse = 0
129always_run = 1
130sound = 1
131music = 1
132volume = 0.8
133music_volume = 0.6
134skill = 2
135; start_map = E1M1
136";
137
138fn truthy(v: &str) -> bool {
139 matches!(v.trim().to_ascii_lowercase().as_str(), "1" | "true" | "yes" | "on")
140}
141
142impl Config {
143 pub fn load(path: &Path) -> Config {
144 let mut c = Config {
145 ini_path: Some(path.to_path_buf()),
146 ..Config::default()
147 };
148 let Ok(text) = std::fs::read_to_string(path) else {
149 let _ = std::fs::write(path, DEFAULT_INI);
150 return c;
151 };
152 let base = path.parent().unwrap_or(Path::new("."));
153 for line in text.lines() {
154 let line = line.split(';').next().unwrap_or("").trim();
155 let Some((k, v)) = line.split_once('=') else { continue };
156 let (k, v) = (k.trim().to_ascii_lowercase(), v.trim());
157 match k.as_str() {
158 "data_dir" => {
159 let p = PathBuf::from(v);
160 c.data_dir = if p.is_absolute() { p } else { base.join(p) };
161 }
162 "fullscreen" => c.fullscreen = truthy(v),
163 "window_width" => c.window_width = v.parse().unwrap_or(c.window_width),
164 "window_height" => c.window_height = v.parse().unwrap_or(c.window_height),
165 "render_scale" => c.render_scale = v.parse().unwrap_or(c.render_scale),
166 "mouse_sensitivity" => c.mouse_sensitivity = v.parse().unwrap_or(c.mouse_sensitivity),
167 "invert_mouse" => c.invert_mouse = truthy(v),
168 "always_run" => c.always_run = truthy(v),
169 "sound" => c.sound = truthy(v),
170 "music" => c.music = truthy(v),
171 "volume" => c.volume = v.parse().unwrap_or(c.volume),
172 "music_volume" => c.music_volume = v.parse().unwrap_or(c.music_volume),
173 "start_map" => c.start_map = Some(v.to_string()),
174 "skill" => c.skill = v.parse().unwrap_or(c.skill),
175 "resolution" => c.resolution = parse_resolution(v),
176 "upscale" => {
177 if let Some(u) = Upscale::ALL.iter().find(|u| u.key() == v.to_ascii_lowercase()) {
178 c.upscale = *u;
179 }
180 }
181 "antialias" => c.antialias = v.parse::<usize>().unwrap_or(0).min(ANTIALIAS.len() - 1),
182 "max_fps" => c.max_fps = v.parse().unwrap_or(0),
183 _ => {}
184 }
185 }
186 c
187 }
188}
189
190fn parse_resolution(v: &str) -> Option<(u32, u32)> {
191 let (w, h) = v
192 .to_ascii_lowercase()
193 .split_once('x')
194 .map(|(a, b)| (a.trim().to_string(), b.trim().to_string()))?;
195 let (w, h) = (w.parse().ok()?, h.parse().ok()?);
196 (w >= 320 && h >= 200).then_some((w, h))
197}
198
199impl Config {
200 pub fn save(&self) -> std::io::Result<()> {
202 let Some(path) = &self.ini_path else { return Ok(()) };
203 let b = |v: bool| if v { 1 } else { 0 };
204 let res = match self.resolution {
205 Some((w, h)) => format!("{w}x{h}"),
206 None => "native".into(),
207 };
208 let data_dir = self.data_dir.to_string_lossy();
209 let mut out = format!(
210 "; OpenBlood configuration
211; Path to your ORIGINAL Blood installation (folder with BLOOD.RFF, SOUNDS.RFF, TILES*.ART)
212data_dir = {data_dir}
213fullscreen = {}
214window_width = {}
215window_height = {}
216; native, or a size like 1920x1080 (the image is fitted to the window)
217resolution = {res}
218; 1.0 renders at the full resolution; lower values trade sharpness for speed
219render_scale = {}
220; nearest, smooth or sharp: how the image is scaled to the window
221upscale = {}
222; anti-aliasing (supersampling): 0 = off, 1 = 2x, 2 = 4x
223antialias = {}
224; frame-rate cap: 0 = unlimited, or e.g. 60 / 120 / 144
225max_fps = {}
226mouse_sensitivity = {}
227invert_mouse = {}
228always_run = {}
229sound = {}
230music = {}
231volume = {}
232music_volume = {}
233skill = {}
234",
235 b(self.fullscreen),
236 self.window_width,
237 self.window_height,
238 self.render_scale,
239 self.upscale.key(),
240 self.antialias,
241 self.max_fps,
242 self.mouse_sensitivity,
243 b(self.invert_mouse),
244 b(self.always_run),
245 b(self.sound),
246 b(self.music),
247 self.volume,
248 self.music_volume,
249 self.skill,
250 );
251 match &self.start_map {
252 Some(m) => out.push_str(&format!(
253 "start_map = {m}
254"
255 )),
256 None => out.push_str(
257 "; start_map = E1M1
258",
259 ),
260 }
261 std::fs::write(path, out)
262 }
263}
264
265#[cfg(test)]
266mod tests {
267 use super::*;
268
269 #[test]
270 fn parses_values() {
271 let dir = std::env::temp_dir().join(format!("ob_cfg_{}", std::process::id()));
272 std::fs::create_dir_all(&dir).unwrap();
273 let p = dir.join("openblood.ini");
274 std::fs::write(&p, "fullscreen = 0\nrender_scale = 0.5 ; half\nstart_map=E2M1\n").unwrap();
275 let c = Config::load(&p);
276 assert!(!c.fullscreen);
277 assert_eq!(c.render_scale, 0.5);
278 assert_eq!(c.start_map.as_deref(), Some("E2M1"));
279 assert_eq!(c.resolution, None);
280 }
281
282 #[test]
283 fn video_settings_round_trip() {
284 let dir = std::env::temp_dir().join(format!("ob_cfg_rt_{}", std::process::id()));
285 std::fs::create_dir_all(&dir).unwrap();
286 let p = dir.join("openblood.ini");
287 std::fs::write(
288 &p,
289 "resolution = 1920x1080
290upscale = smooth
291antialias = 2
292",
293 )
294 .unwrap();
295 let mut c = Config::load(&p);
296 assert_eq!(c.resolution, Some((1920, 1080)));
297 assert_eq!(c.upscale, Upscale::Smooth);
298 assert_eq!(c.antialias, 2);
299 c.resolution = None;
300 c.upscale = Upscale::Nearest;
301 c.save().unwrap();
302 let c2 = Config::load(&p);
303 assert_eq!(c2.resolution, None);
304 assert_eq!(c2.upscale, Upscale::Nearest);
305 assert_eq!(c2.antialias, 2);
306 }
307}