1use std::collections::HashMap;
4
5#[derive(Debug, Clone, Default)]
6pub struct MapInfo {
7 pub file: String,
8 pub title: String,
9 pub author: String,
10 pub song: String,
11 pub track: u32,
13 pub ending_a: usize,
15 pub ending_b: usize,
16 pub fog: bool,
17 pub messages: HashMap<u32, String>,
18}
19
20#[derive(Debug, Clone, Default)]
21pub struct Episode {
22 pub title: String,
23 pub maps: Vec<String>,
24 pub bloodbath_only: bool,
25 pub cutscene_a: Option<String>,
29 pub wav_a: Option<String>,
30 pub cutscene_a_level: usize,
31 pub cutscene_b: Option<String>,
32 pub wav_b: Option<String>,
33}
34
35#[derive(Debug, Clone, Default)]
36pub struct BloodIni {
37 pub episodes: Vec<Episode>,
38 pub maps: HashMap<String, MapInfo>,
39}
40
41impl BloodIni {
42 pub fn parse(text: &str) -> BloodIni {
43 let mut ini = BloodIni::default();
44 let mut section = String::new();
45 for raw in text.lines() {
46 let line = raw.split(';').next().unwrap_or("").trim();
47 if line.is_empty() {
48 continue;
49 }
50 if line.starts_with('[') && line.ends_with(']') {
51 section = line[1..line.len() - 1].trim().to_ascii_uppercase();
52 if section.starts_with("EPISODE") {
53 ini.episodes.push(Episode {
54 cutscene_a_level: 1,
55 ..Default::default()
56 });
57 } else if section != "INSTALL" {
58 ini.maps.entry(section.clone()).or_insert_with(|| MapInfo {
59 file: section.clone(),
60 ..Default::default()
61 });
62 }
63 continue;
64 }
65 let Some((k, v)) = line.split_once('=') else { continue };
66 let k = k.trim().to_ascii_lowercase();
67 let v = v.trim().to_string();
68 if section.starts_with("EPISODE") {
69 let Some(ep) = ini.episodes.last_mut() else { continue };
70 match k.as_str() {
71 "title" => ep.title = v,
72 "bloodbathonly" => ep.bloodbath_only = v.trim() != "0",
73 "cutscenea" => ep.cutscene_a = Some(v),
74 "cutwava" => ep.wav_a = Some(v),
75 "cutsceneb" => ep.cutscene_b = Some(v),
76 "cutwavb" => ep.wav_b = Some(v),
77 "cutscenealevel" => ep.cutscene_a_level = v.trim().parse().unwrap_or(1),
78 _ if k.starts_with("map") && k[3..].chars().all(|c| c.is_ascii_digit()) => {
79 let n: usize = k[3..].parse().unwrap_or(0);
80 if n > 0 {
81 if ep.maps.len() < n {
82 ep.maps.resize(n, String::new());
83 }
84 ep.maps[n - 1] = v.to_ascii_uppercase();
85 }
86 }
87 _ => {}
88 }
89 } else if let Some(m) = ini.maps.get_mut(§ion) {
90 match k.as_str() {
91 "title" => m.title = v,
92 "author" => m.author = v,
93 "song" => m.song = v.to_ascii_uppercase(),
94 "track" => m.track = v.parse().unwrap_or(0),
95 "endinga" => m.ending_a = v.parse().unwrap_or(0),
96 "endingb" => m.ending_b = v.parse().unwrap_or(0),
97 "fog" => m.fog = v.trim() == "1",
98 _ if k.starts_with("message") => {
99 if let Ok(n) = k[7..].parse::<u32>() {
100 m.messages.insert(n, v);
101 }
102 }
103 _ => {}
104 }
105 }
106 }
107 for ep in ini.episodes.iter_mut() {
108 ep.maps.retain(|m| !m.is_empty());
109 }
110 ini
111 }
112
113 pub fn map(&self, name: &str) -> Option<&MapInfo> {
114 self.maps.get(&name.to_ascii_uppercase())
115 }
116
117 pub fn locate(&self, name: &str) -> Option<(usize, usize)> {
119 let n = name.to_ascii_uppercase();
120 for (e, ep) in self.episodes.iter().enumerate() {
121 if let Some(m) = ep.maps.iter().position(|m| *m == n) {
122 return Some((e, m));
123 }
124 }
125 None
126 }
127
128 pub fn next_map(&self, name: &str, secret: bool) -> Option<String> {
130 let (e, m) = self.locate(name)?;
131 let ep = &self.episodes[e];
132 let info = self.map(name);
133 let target = info.map(|i| if secret { i.ending_b } else { i.ending_a }).unwrap_or(0);
134 let idx = if target > 0 { target - 1 } else { m + 1 };
135 if idx == m && target > 0 && !secret {
136 return None;
137 }
138 ep.maps.get(idx).cloned()
139 }
140}
141
142#[cfg(test)]
143mod tests {
144 use super::*;
145
146 const SAMPLE: &str = "[Episode1]\nTitle = The Way of All Flesh\nMap1 = e1m1\nMap2 = e1m2\nMap8 = e1m8\n\n[e1m1]\nTitle = Cradle to Grave\nSong = UNHOLY\nTrack = 3\nEndingA = 2\nEndingB = 8\nMessage2 = Hit the switch to end the level.\n";
147
148 #[test]
149 fn parses_episode_and_map() {
150 let ini = BloodIni::parse(SAMPLE);
151 assert_eq!(ini.episodes[0].title, "The Way of All Flesh");
152 assert_eq!(ini.episodes[0].maps, vec!["E1M1", "E1M2", "E1M8"]);
153 let m = ini.map("e1m1").unwrap();
154 assert_eq!((m.track, m.song.as_str()), (3, "UNHOLY"));
155 assert_eq!(m.messages[&2], "Hit the switch to end the level.");
156 assert_eq!(ini.next_map("E1M1", false).as_deref(), Some("E1M2"));
157 }
158}