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