15576
|
1 |
use crate::{
|
|
2 |
core::types::{GameCfg, HedgehogInfo, TeamInfo},
|
|
3 |
server::haskell::HaskellValue,
|
|
4 |
};
|
15547
|
5 |
use std::{
|
15581
|
6 |
collections::HashMap,
|
15547
|
7 |
fs,
|
|
8 |
io::{self, BufReader, Read, Write},
|
|
9 |
str::FromStr,
|
|
10 |
};
|
|
11 |
|
15580
|
12 |
#[derive(PartialEq, Debug)]
|
15581
|
13 |
pub struct Demo {
|
15547
|
14 |
teams: Vec<TeamInfo>,
|
|
15 |
config: Vec<GameCfg>,
|
|
16 |
messages: Vec<String>,
|
|
17 |
}
|
|
18 |
|
|
19 |
impl Demo {
|
15581
|
20 |
fn save(self, filename: String) -> io::Result<()> {
|
|
21 |
let text = format!("{}", demo_to_haskell(self));
|
|
22 |
let mut file = fs::File::open(filename)?;
|
|
23 |
file.write(text.as_bytes())?;
|
|
24 |
Ok(())
|
15547
|
25 |
}
|
|
26 |
|
15576
|
27 |
fn load(filename: String) -> io::Result<Self> {
|
|
28 |
let mut file = fs::File::open(filename)?;
|
|
29 |
let mut bytes = vec![];
|
|
30 |
file.read_to_end(&mut bytes)?;
|
|
31 |
match super::haskell::parse(&bytes[..]) {
|
|
32 |
Ok((_, value)) => haskell_to_demo(value).ok_or(io::Error::new(
|
|
33 |
io::ErrorKind::InvalidData,
|
|
34 |
"Invalid demo structure",
|
|
35 |
)),
|
|
36 |
Err(_) => Err(io::Error::new(
|
|
37 |
io::ErrorKind::InvalidData,
|
|
38 |
"Unable to parse file",
|
|
39 |
)),
|
|
40 |
}
|
15547
|
41 |
}
|
|
42 |
|
15576
|
43 |
fn load_hwd(filename: String) -> io::Result<Self> {
|
|
44 |
let file = fs::File::open(filename)?;
|
|
45 |
let mut reader = io::BufReader::new(file);
|
15547
|
46 |
|
|
47 |
#[inline]
|
|
48 |
fn error<T>(cause: &str) -> io::Result<T> {
|
|
49 |
Err(io::Error::new(io::ErrorKind::InvalidData, cause))
|
|
50 |
}
|
|
51 |
|
|
52 |
fn read_command<'a>(
|
|
53 |
reader: &mut BufReader<fs::File>,
|
|
54 |
buffer: &'a mut [u8],
|
|
55 |
) -> io::Result<Option<&'a str>> {
|
|
56 |
use io::BufRead;
|
|
57 |
|
|
58 |
let mut size = [0u8; 1];
|
|
59 |
if reader.read(&mut size)? == 0 {
|
|
60 |
Ok(None)
|
|
61 |
} else {
|
|
62 |
let text = &mut buffer[0..size[0] as _];
|
|
63 |
|
|
64 |
if reader.read(text)? < text.len() {
|
|
65 |
Err(io::Error::new(
|
|
66 |
io::ErrorKind::UnexpectedEof,
|
|
67 |
"Incomplete command",
|
|
68 |
))
|
|
69 |
} else {
|
|
70 |
std::str::from_utf8(text).map(Some).map_err(|e| {
|
|
71 |
io::Error::new(io::ErrorKind::InvalidInput, "The string is not UTF8")
|
|
72 |
})
|
|
73 |
}
|
|
74 |
}
|
|
75 |
}
|
|
76 |
|
|
77 |
fn get_script_name(arg: &str) -> io::Result<String> {
|
|
78 |
const PREFIX: &str = "Scripts/Multiplayer/";
|
|
79 |
const SUFFIX: &str = ".lua";
|
|
80 |
if arg.starts_with(PREFIX) && arg.ends_with(SUFFIX) {
|
|
81 |
let script = arg[PREFIX.len()..arg.len() - SUFFIX.len()].to_string();
|
15570
|
82 |
Ok(script.replace('_', " "))
|
15547
|
83 |
} else {
|
|
84 |
error("Script is not multiplayer")
|
|
85 |
}
|
|
86 |
}
|
|
87 |
|
|
88 |
fn get_game_flags(arg: &str) -> io::Result<Vec<String>> {
|
|
89 |
const FLAGS: &[u32] = &[
|
|
90 |
0x0000_1000,
|
|
91 |
0x0000_0010,
|
|
92 |
0x0000_0004,
|
|
93 |
0x0000_0008,
|
|
94 |
0x0000_0020,
|
|
95 |
0x0000_0040,
|
|
96 |
0x0000_0080,
|
|
97 |
0x0000_0100,
|
|
98 |
0x0000_0200,
|
|
99 |
0x0000_0400,
|
|
100 |
0x0000_0800,
|
|
101 |
0x0000_2000,
|
|
102 |
0x0000_4000,
|
|
103 |
0x0000_8000,
|
|
104 |
0x0001_0000,
|
|
105 |
0x0002_0000,
|
|
106 |
0x0004_0000,
|
|
107 |
0x0008_0000,
|
|
108 |
0x0010_0000,
|
|
109 |
0x0020_0000,
|
|
110 |
0x0040_0000,
|
|
111 |
0x0080_0000,
|
|
112 |
0x0100_0000,
|
|
113 |
0x0200_0000,
|
|
114 |
0x0400_0000,
|
|
115 |
];
|
|
116 |
|
|
117 |
let flags = u32::from_str(arg).unwrap_or_default();
|
|
118 |
let game_flags = FLAGS
|
|
119 |
.iter()
|
|
120 |
.map(|flag| (flag & flags != 0).to_string())
|
|
121 |
.collect();
|
|
122 |
|
|
123 |
Ok(game_flags)
|
|
124 |
}
|
|
125 |
|
|
126 |
let mut config = Vec::new();
|
|
127 |
let mut buffer = [0u8; u8::max_value() as _];
|
|
128 |
|
|
129 |
let mut game_flags = vec![];
|
|
130 |
let mut scheme_properties: Vec<_> = [
|
|
131 |
"1", "1000", "100", "1", "1", "1000", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
|
|
132 |
"1", "",
|
|
133 |
]
|
|
134 |
.iter()
|
|
135 |
.map(|p| p.to_string())
|
|
136 |
.collect();
|
|
137 |
const SCHEME_PROPERTY_NAMES: &[&str] = &[
|
|
138 |
"$damagepct",
|
|
139 |
"$turntime",
|
|
140 |
"",
|
|
141 |
"$sd_turns",
|
|
142 |
"$casefreq",
|
|
143 |
"$minestime",
|
|
144 |
"$minesnum",
|
|
145 |
"$minedudpct",
|
|
146 |
"$explosives",
|
|
147 |
"$airmines",
|
|
148 |
"$healthprob",
|
|
149 |
"$hcaseamount",
|
|
150 |
"$waterrise",
|
|
151 |
"$healthdec",
|
|
152 |
"$ropepct",
|
|
153 |
"$getawaytime",
|
|
154 |
"$worldedge",
|
|
155 |
];
|
|
156 |
const AMMO_PROPERTY_NAMES: &[&str] = &["eammloadt", "eammprob", "eammdelay", "eammreinf"];
|
|
157 |
let mut ammo_settings = vec![String::new(); AMMO_PROPERTY_NAMES.len()];
|
|
158 |
let mut teams = vec![];
|
|
159 |
let mut hog_index = 7usize;
|
|
160 |
|
|
161 |
let mut messages = vec![];
|
|
162 |
|
|
163 |
while let Some(cmd) = read_command(&mut reader, &mut buffer)? {
|
|
164 |
if let Some(index) = cmd.find(' ') {
|
|
165 |
match cmd.chars().next().unwrap_or_default() {
|
|
166 |
'T' => {
|
|
167 |
if cmd != "TD" {
|
|
168 |
let () = error("Not a demo file")?;
|
|
169 |
}
|
|
170 |
}
|
|
171 |
'e' => {
|
|
172 |
if let Some(index) = cmd.find(' ') {
|
|
173 |
let (name, arg) = cmd.split_at(index);
|
|
174 |
match name {
|
|
175 |
"script" => config.push(GameCfg::Script(get_script_name(arg)?)),
|
|
176 |
"map" => config.push(GameCfg::MapType(arg.to_string())),
|
|
177 |
"theme" => config.push(GameCfg::Theme(arg.to_string())),
|
|
178 |
"seed" => config.push(GameCfg::Seed(arg.to_string())),
|
|
179 |
"$gmflags" => game_flags = get_game_flags(arg)?,
|
|
180 |
"$scriptparam" => {
|
|
181 |
*scheme_properties.last_mut().unwrap() = arg.to_string()
|
|
182 |
}
|
|
183 |
"$template_filter" => config.push(GameCfg::Template(
|
|
184 |
u32::from_str(arg).unwrap_or_default(),
|
|
185 |
)),
|
|
186 |
"$feature_size" => config.push(GameCfg::FeatureSize(
|
|
187 |
u32::from_str(arg).unwrap_or_default(),
|
|
188 |
)),
|
|
189 |
"$map_gen" => config.push(GameCfg::MapGenerator(
|
|
190 |
u32::from_str(arg).unwrap_or_default(),
|
|
191 |
)),
|
|
192 |
"$maze_size" => config.push(GameCfg::MazeSize(
|
|
193 |
u32::from_str(arg).unwrap_or_default(),
|
|
194 |
)),
|
|
195 |
"addteam" => {
|
|
196 |
if let parts = arg.splitn(3, ' ').collect::<Vec<_>>() {
|
|
197 |
let color = parts.get(1).unwrap_or(&"1");
|
|
198 |
let name = parts.get(2).unwrap_or(&"Unnamed");
|
|
199 |
teams.push(TeamInfo {
|
|
200 |
color: (u32::from_str(color).unwrap_or(2113696)
|
|
201 |
/ 2113696
|
|
202 |
- 1)
|
|
203 |
as u8,
|
|
204 |
name: name.to_string(),
|
|
205 |
..TeamInfo::default()
|
|
206 |
})
|
|
207 |
};
|
|
208 |
}
|
|
209 |
"fort" => teams
|
|
210 |
.last_mut()
|
|
211 |
.iter_mut()
|
|
212 |
.for_each(|t| t.fort = arg.to_string()),
|
|
213 |
"grave" => teams
|
|
214 |
.last_mut()
|
|
215 |
.iter_mut()
|
|
216 |
.for_each(|t| t.grave = arg.to_string()),
|
|
217 |
"addhh" => {
|
|
218 |
hog_index = (hog_index + 1) % 8;
|
|
219 |
if let parts = arg.splitn(3, ' ').collect::<Vec<_>>() {
|
|
220 |
let health = parts.get(1).unwrap_or(&"100");
|
|
221 |
teams.last_mut().iter_mut().for_each(|t| {
|
|
222 |
if let Some(difficulty) = parts.get(0) {
|
|
223 |
t.difficulty =
|
|
224 |
u8::from_str(difficulty).unwrap_or(0);
|
|
225 |
}
|
|
226 |
if let Some(init_health) = parts.get(1) {
|
|
227 |
scheme_properties[2] = init_health.to_string();
|
|
228 |
}
|
|
229 |
t.hedgehogs_number = (hog_index + 1) as u8;
|
|
230 |
t.hedgehogs[hog_index].name =
|
|
231 |
parts.get(2).unwrap_or(&"Unnamed").to_string()
|
|
232 |
});
|
|
233 |
}
|
|
234 |
}
|
|
235 |
"hat" => {
|
|
236 |
teams
|
|
237 |
.last_mut()
|
|
238 |
.iter_mut()
|
|
239 |
.for_each(|t| t.hedgehogs[hog_index].hat = arg.to_string());
|
|
240 |
}
|
|
241 |
name => {
|
|
242 |
if let Some(index) =
|
|
243 |
SCHEME_PROPERTY_NAMES.iter().position(|n| *n == name)
|
|
244 |
{
|
|
245 |
scheme_properties[index] = arg.to_string();
|
|
246 |
} else if let Some(index) =
|
|
247 |
AMMO_PROPERTY_NAMES.iter().position(|n| *n == name)
|
|
248 |
{
|
|
249 |
ammo_settings[index] = arg.to_string();
|
|
250 |
}
|
|
251 |
}
|
|
252 |
}
|
|
253 |
}
|
|
254 |
}
|
|
255 |
'+' => {}
|
|
256 |
_ => (),
|
|
257 |
}
|
|
258 |
}
|
|
259 |
}
|
|
260 |
|
|
261 |
game_flags.append(&mut scheme_properties);
|
|
262 |
config.push(GameCfg::Scheme("ADHOG_SCHEME".to_string(), game_flags));
|
|
263 |
config.push(GameCfg::Ammo(
|
|
264 |
"ADHOG_AMMO".to_string(),
|
|
265 |
Some(ammo_settings.concat()),
|
|
266 |
));
|
|
267 |
|
|
268 |
Ok(Demo {
|
|
269 |
teams,
|
|
270 |
config,
|
|
271 |
messages,
|
|
272 |
})
|
|
273 |
}
|
|
274 |
}
|
15576
|
275 |
|
15581
|
276 |
fn demo_to_haskell(mut demo: Demo) -> HaskellValue {
|
|
277 |
use HaskellValue as Hs;
|
|
278 |
|
|
279 |
let mut teams = Vec::with_capacity(demo.teams.len());
|
|
280 |
for team in demo.teams {
|
|
281 |
let mut fields = HashMap::<String, HaskellValue>::new();
|
|
282 |
|
|
283 |
fields.insert("teamowner".to_string(), Hs::String(team.owner));
|
|
284 |
fields.insert("teamname".to_string(), Hs::String(team.name));
|
|
285 |
fields.insert("teamcolor".to_string(), Hs::Number(team.color));
|
|
286 |
fields.insert("teamgrave".to_string(), Hs::String(team.grave));
|
|
287 |
fields.insert("teamvoicepack".to_string(), Hs::String(team.voice_pack));
|
|
288 |
fields.insert("teamflag".to_string(), Hs::String(team.flag));
|
|
289 |
fields.insert("difficulty".to_string(), Hs::Number(team.difficulty));
|
|
290 |
fields.insert("hhnum".to_string(), Hs::Number(team.hedgehogs_number));
|
|
291 |
|
|
292 |
let hogs = team
|
|
293 |
.hedgehogs
|
|
294 |
.iter()
|
|
295 |
.map(|hog| Hs::AnonStruct {
|
|
296 |
name: "HedgehogInfo".to_string(),
|
|
297 |
fields: vec![Hs::String(hog.name.clone()), Hs::String(hog.hat.clone())],
|
|
298 |
})
|
|
299 |
.collect();
|
|
300 |
|
|
301 |
fields.insert("hedgehogs".to_string(), Hs::List(hogs));
|
|
302 |
|
|
303 |
teams.push(Hs::Struct {
|
|
304 |
name: "TeamInfo".to_string(),
|
|
305 |
fields,
|
|
306 |
})
|
|
307 |
}
|
|
308 |
|
|
309 |
let mut map_config = vec![];
|
|
310 |
let mut game_config = vec![];
|
|
311 |
|
|
312 |
let mut save_map_config = |name: &str, value: String| {
|
|
313 |
map_config.push(Hs::Tuple(vec![
|
|
314 |
Hs::String(name.to_string()),
|
|
315 |
Hs::String(value),
|
|
316 |
]));
|
|
317 |
};
|
|
318 |
|
|
319 |
for config_item in &demo.config {
|
|
320 |
match config_item {
|
|
321 |
GameCfg::FeatureSize(size) => save_map_config("FEATURE_SIZE", size.to_string()),
|
|
322 |
GameCfg::MapType(map_type) => save_map_config("MAP", map_type.clone()),
|
|
323 |
GameCfg::MapGenerator(generator) => save_map_config("MAPGEN", generator.to_string()),
|
|
324 |
GameCfg::MazeSize(size) => save_map_config("MAZE_SIZE", size.to_string()),
|
|
325 |
GameCfg::Seed(seed) => save_map_config("SEED", seed.clone()),
|
|
326 |
GameCfg::Template(template) => save_map_config("TEMPLATE", template.to_string()),
|
|
327 |
GameCfg::DrawnMap(map) => save_map_config("DRAWNMAP", map.clone()),
|
|
328 |
_ => (),
|
|
329 |
}
|
|
330 |
}
|
|
331 |
|
|
332 |
let mut save_game_config = |name: &str, mut value: Vec<String>| {
|
|
333 |
map_config.push(Hs::Tuple(vec![
|
|
334 |
Hs::String(name.to_string()),
|
|
335 |
Hs::List(value.drain(..).map(Hs::String).collect()),
|
|
336 |
]));
|
|
337 |
};
|
|
338 |
|
|
339 |
for config_item in &demo.config {
|
|
340 |
match config_item {
|
|
341 |
GameCfg::Ammo(name, Some(ammo)) => {
|
|
342 |
save_game_config("AMMO", vec![name.clone(), ammo.clone()])
|
|
343 |
}
|
|
344 |
GameCfg::Ammo(name, None) => save_game_config("AMMO", vec![name.clone()]),
|
|
345 |
GameCfg::Scheme(name, scheme) => {
|
|
346 |
let mut values = vec![name.clone()];
|
|
347 |
values.extend_from_slice(&scheme);
|
|
348 |
save_game_config("SCHEME", values);
|
|
349 |
}
|
|
350 |
GameCfg::Script(script) => save_game_config("SCRIPT", vec![script.clone()]),
|
|
351 |
GameCfg::Theme(theme) => save_game_config("THEME", vec![theme.clone()]),
|
|
352 |
_ => (),
|
|
353 |
}
|
|
354 |
}
|
|
355 |
|
|
356 |
Hs::Tuple(vec![
|
|
357 |
Hs::List(teams),
|
|
358 |
Hs::List(map_config),
|
|
359 |
Hs::List(game_config),
|
|
360 |
Hs::List(demo.messages.drain(..).map(Hs::String).collect()),
|
|
361 |
])
|
|
362 |
}
|
|
363 |
|
15576
|
364 |
fn haskell_to_demo(value: HaskellValue) -> Option<Demo> {
|
|
365 |
use HaskellValue::*;
|
|
366 |
let mut lists = value.into_tuple()?;
|
|
367 |
let mut lists_iter = lists.drain(..);
|
|
368 |
|
|
369 |
let teams_list = lists_iter.next()?.into_list()?;
|
|
370 |
let map_config = lists_iter.next()?.into_list()?;
|
|
371 |
let game_config = lists_iter.next()?.into_list()?;
|
|
372 |
let engine_messages = lists_iter.next()?.into_list()?;
|
|
373 |
|
|
374 |
let mut teams = Vec::with_capacity(teams_list.len());
|
|
375 |
|
|
376 |
for team in teams_list {
|
|
377 |
let (_, mut fields) = team.into_struct()?;
|
|
378 |
|
|
379 |
let mut team_info = TeamInfo::default();
|
|
380 |
for (name, value) in fields.drain() {
|
|
381 |
match &name[..] {
|
|
382 |
"teamowner" => team_info.owner = value.into_string()?,
|
|
383 |
"teamname" => team_info.name = value.into_string()?,
|
|
384 |
"teamcolor" => team_info.color = u8::from_str(&value.into_string()?).ok()?,
|
|
385 |
"teamgrave" => team_info.grave = value.into_string()?,
|
|
386 |
"teamfort" => team_info.fort = value.into_string()?,
|
|
387 |
"teamvoicepack" => team_info.voice_pack = value.into_string()?,
|
|
388 |
"teamflag" => team_info.flag = value.into_string()?,
|
|
389 |
"difficulty" => team_info.difficulty = value.into_number()?,
|
|
390 |
"hhnum" => team_info.hedgehogs_number = value.into_number()?,
|
|
391 |
"hedgehogs" => {
|
|
392 |
for (index, hog) in value
|
|
393 |
.into_list()?
|
|
394 |
.drain(..)
|
|
395 |
.enumerate()
|
|
396 |
.take(team_info.hedgehogs.len())
|
|
397 |
{
|
|
398 |
let (_, mut fields) = hog.into_anon_struct()?;
|
|
399 |
let mut fields_iter = fields.drain(..);
|
|
400 |
team_info.hedgehogs[index] = HedgehogInfo {
|
|
401 |
name: fields_iter.next()?.into_string()?,
|
|
402 |
hat: fields_iter.next()?.into_string()?,
|
|
403 |
}
|
|
404 |
}
|
|
405 |
}
|
|
406 |
_ => (),
|
|
407 |
}
|
|
408 |
}
|
|
409 |
teams.push(team_info)
|
|
410 |
}
|
|
411 |
|
|
412 |
let mut config = Vec::with_capacity(map_config.len() + game_config.len());
|
|
413 |
|
15578
|
414 |
for item in map_config {
|
|
415 |
let mut tuple = item.into_tuple()?;
|
|
416 |
let mut tuple_iter = tuple.drain(..);
|
|
417 |
let name = tuple_iter.next()?.into_string()?;
|
|
418 |
let value = tuple_iter.next()?.into_string()?;
|
|
419 |
|
|
420 |
let config_item = match &name[..] {
|
|
421 |
"FEATURE_SIZE" => GameCfg::FeatureSize(u32::from_str(&value).ok()?),
|
|
422 |
"MAP" => GameCfg::MapType(value),
|
|
423 |
"MAPGEN" => GameCfg::MapGenerator(u32::from_str(&value).ok()?),
|
|
424 |
"MAZE_SIZE" => GameCfg::MazeSize(u32::from_str(&value).ok()?),
|
|
425 |
"SEED" => GameCfg::Seed(value),
|
|
426 |
"TEMPLATE" => GameCfg::Template(u32::from_str(&value).ok()?),
|
|
427 |
"DRAWNMAP" => GameCfg::DrawnMap(value),
|
|
428 |
_ => None?,
|
|
429 |
};
|
|
430 |
config.push(config_item);
|
|
431 |
}
|
|
432 |
|
|
433 |
for item in game_config {
|
|
434 |
let mut tuple = item.into_tuple()?;
|
|
435 |
let mut tuple_iter = tuple.drain(..);
|
|
436 |
let name = tuple_iter.next()?.into_string()?;
|
15580
|
437 |
let mut value = tuple_iter.next()?.into_list()?;
|
|
438 |
let mut value_iter = value.drain(..);
|
15576
|
439 |
|
15578
|
440 |
let config_item = match &name[..] {
|
15580
|
441 |
"AMMO" => GameCfg::Ammo(
|
|
442 |
value_iter.next()?.into_string()?,
|
|
443 |
value_iter.next().and_then(|v| v.into_string()),
|
|
444 |
),
|
|
445 |
"SCHEME" => GameCfg::Scheme(
|
|
446 |
value_iter.next()?.into_string()?,
|
|
447 |
value_iter.filter_map(|v| v.into_string()).collect(),
|
|
448 |
),
|
|
449 |
"SCRIPT" => GameCfg::Script(value_iter.next()?.into_string()?),
|
|
450 |
"THEME" => GameCfg::Theme(value_iter.next()?.into_string()?),
|
15578
|
451 |
_ => None?,
|
|
452 |
};
|
|
453 |
config.push(config_item);
|
|
454 |
}
|
15576
|
455 |
|
|
456 |
let mut messages = Vec::with_capacity(engine_messages.len());
|
|
457 |
|
|
458 |
for message in engine_messages {
|
|
459 |
messages.push(message.into_string()?);
|
|
460 |
}
|
|
461 |
|
|
462 |
Some(Demo {
|
|
463 |
teams,
|
|
464 |
config,
|
|
465 |
messages,
|
|
466 |
})
|
|
467 |
}
|