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 |
|
15880
|
138 |
let messages = vec![];
|
15547
|
139 |
|
|
140 |
while let Some(cmd) = read_command(&mut reader, &mut buffer)? {
|
|
141 |
if let Some(index) = cmd.find(' ') {
|
|
142 |
match cmd.chars().next().unwrap_or_default() {
|
|
143 |
'T' => {
|
|
144 |
if cmd != "TD" {
|
|
145 |
let () = error("Not a demo file")?;
|
|
146 |
}
|
|
147 |
}
|
|
148 |
'e' => {
|
|
149 |
if let Some(index) = cmd.find(' ') {
|
|
150 |
let (name, arg) = cmd.split_at(index);
|
|
151 |
match name {
|
|
152 |
"script" => config.push(GameCfg::Script(get_script_name(arg)?)),
|
|
153 |
"map" => config.push(GameCfg::MapType(arg.to_string())),
|
|
154 |
"theme" => config.push(GameCfg::Theme(arg.to_string())),
|
|
155 |
"seed" => config.push(GameCfg::Seed(arg.to_string())),
|
|
156 |
"$gmflags" => game_flags = get_game_flags(arg)?,
|
|
157 |
"$scriptparam" => {
|
|
158 |
*scheme_properties.last_mut().unwrap() = arg.to_string()
|
|
159 |
}
|
|
160 |
"$template_filter" => config.push(GameCfg::Template(
|
|
161 |
u32::from_str(arg).unwrap_or_default(),
|
|
162 |
)),
|
|
163 |
"$feature_size" => config.push(GameCfg::FeatureSize(
|
|
164 |
u32::from_str(arg).unwrap_or_default(),
|
|
165 |
)),
|
|
166 |
"$map_gen" => config.push(GameCfg::MapGenerator(
|
|
167 |
u32::from_str(arg).unwrap_or_default(),
|
|
168 |
)),
|
|
169 |
"$maze_size" => config.push(GameCfg::MazeSize(
|
|
170 |
u32::from_str(arg).unwrap_or_default(),
|
|
171 |
)),
|
|
172 |
"addteam" => {
|
15831
|
173 |
let parts = arg.splitn(3, ' ').collect::<Vec<_>>();
|
|
174 |
let color = parts.get(1).unwrap_or(&"1");
|
|
175 |
let name = parts.get(2).unwrap_or(&"Unnamed");
|
|
176 |
teams.push(TeamInfo {
|
|
177 |
color: (u32::from_str(color).unwrap_or(2113696) / 2113696
|
|
178 |
- 1)
|
|
179 |
as u8,
|
|
180 |
name: name.to_string(),
|
|
181 |
..TeamInfo::default()
|
|
182 |
});
|
15547
|
183 |
}
|
|
184 |
"fort" => teams
|
|
185 |
.last_mut()
|
|
186 |
.iter_mut()
|
|
187 |
.for_each(|t| t.fort = arg.to_string()),
|
|
188 |
"grave" => teams
|
|
189 |
.last_mut()
|
|
190 |
.iter_mut()
|
|
191 |
.for_each(|t| t.grave = arg.to_string()),
|
|
192 |
"addhh" => {
|
|
193 |
hog_index = (hog_index + 1) % 8;
|
15831
|
194 |
let parts = arg.splitn(3, ' ').collect::<Vec<_>>();
|
|
195 |
let health = parts.get(1).unwrap_or(&"100");
|
|
196 |
teams.last_mut().iter_mut().for_each(|t| {
|
|
197 |
if let Some(difficulty) = parts.get(0) {
|
|
198 |
t.difficulty = u8::from_str(difficulty).unwrap_or(0);
|
|
199 |
}
|
|
200 |
if let Some(init_health) = parts.get(1) {
|
|
201 |
scheme_properties[2] = init_health.to_string();
|
|
202 |
}
|
|
203 |
t.hedgehogs_number = (hog_index + 1) as u8;
|
|
204 |
t.hedgehogs[hog_index].name =
|
|
205 |
parts.get(2).unwrap_or(&"Unnamed").to_string();
|
|
206 |
});
|
15547
|
207 |
}
|
|
208 |
"hat" => {
|
|
209 |
teams
|
|
210 |
.last_mut()
|
|
211 |
.iter_mut()
|
|
212 |
.for_each(|t| t.hedgehogs[hog_index].hat = arg.to_string());
|
|
213 |
}
|
|
214 |
name => {
|
|
215 |
if let Some(index) =
|
|
216 |
SCHEME_PROPERTY_NAMES.iter().position(|n| *n == name)
|
|
217 |
{
|
|
218 |
scheme_properties[index] = arg.to_string();
|
|
219 |
} else if let Some(index) =
|
|
220 |
AMMO_PROPERTY_NAMES.iter().position(|n| *n == name)
|
|
221 |
{
|
|
222 |
ammo_settings[index] = arg.to_string();
|
|
223 |
}
|
|
224 |
}
|
|
225 |
}
|
|
226 |
}
|
|
227 |
}
|
|
228 |
'+' => {}
|
|
229 |
_ => (),
|
|
230 |
}
|
|
231 |
}
|
|
232 |
}
|
|
233 |
|
|
234 |
game_flags.append(&mut scheme_properties);
|
|
235 |
config.push(GameCfg::Scheme("ADHOG_SCHEME".to_string(), game_flags));
|
|
236 |
config.push(GameCfg::Ammo(
|
|
237 |
"ADHOG_AMMO".to_string(),
|
|
238 |
Some(ammo_settings.concat()),
|
|
239 |
));
|
|
240 |
|
|
241 |
Ok(Demo {
|
|
242 |
teams,
|
|
243 |
config,
|
|
244 |
messages,
|
|
245 |
})
|
|
246 |
}
|
|
247 |
}
|
15576
|
248 |
|
15796
|
249 |
fn replay_to_haskell(mut replay: Replay) -> HaskellValue {
|
15581
|
250 |
use HaskellValue as Hs;
|
|
251 |
|
15796
|
252 |
let mut teams = Vec::with_capacity(replay.teams.len());
|
|
253 |
for team in replay.teams {
|
15581
|
254 |
let mut fields = HashMap::<String, HaskellValue>::new();
|
|
255 |
|
|
256 |
fields.insert("teamowner".to_string(), Hs::String(team.owner));
|
|
257 |
fields.insert("teamname".to_string(), Hs::String(team.name));
|
|
258 |
fields.insert("teamcolor".to_string(), Hs::Number(team.color));
|
|
259 |
fields.insert("teamgrave".to_string(), Hs::String(team.grave));
|
|
260 |
fields.insert("teamvoicepack".to_string(), Hs::String(team.voice_pack));
|
|
261 |
fields.insert("teamflag".to_string(), Hs::String(team.flag));
|
|
262 |
fields.insert("difficulty".to_string(), Hs::Number(team.difficulty));
|
|
263 |
fields.insert("hhnum".to_string(), Hs::Number(team.hedgehogs_number));
|
|
264 |
|
|
265 |
let hogs = team
|
|
266 |
.hedgehogs
|
|
267 |
.iter()
|
|
268 |
.map(|hog| Hs::AnonStruct {
|
|
269 |
name: "HedgehogInfo".to_string(),
|
|
270 |
fields: vec![Hs::String(hog.name.clone()), Hs::String(hog.hat.clone())],
|
|
271 |
})
|
|
272 |
.collect();
|
|
273 |
|
|
274 |
fields.insert("hedgehogs".to_string(), Hs::List(hogs));
|
|
275 |
|
|
276 |
teams.push(Hs::Struct {
|
|
277 |
name: "TeamInfo".to_string(),
|
|
278 |
fields,
|
|
279 |
})
|
|
280 |
}
|
|
281 |
|
|
282 |
let mut map_config = vec![];
|
|
283 |
let mut game_config = vec![];
|
|
284 |
|
|
285 |
let mut save_map_config = |name: &str, value: String| {
|
|
286 |
map_config.push(Hs::Tuple(vec![
|
|
287 |
Hs::String(name.to_string()),
|
|
288 |
Hs::String(value),
|
|
289 |
]));
|
|
290 |
};
|
|
291 |
|
15796
|
292 |
let config = replay.config;
|
|
293 |
|
|
294 |
save_map_config("FEATURE_SIZE", config.feature_size.to_string());
|
|
295 |
save_map_config("MAP", config.map_type);
|
|
296 |
save_map_config("MAPGEN", config.map_generator.to_string());
|
|
297 |
save_map_config("MAZE_SIZE", config.maze_size.to_string());
|
|
298 |
save_map_config("SEED", config.seed);
|
|
299 |
save_map_config("TEMPLATE", config.template.to_string());
|
|
300 |
if let Some(drawn_map) = config.drawn_map {
|
|
301 |
save_map_config("DRAWNMAP", drawn_map);
|
15581
|
302 |
}
|
|
303 |
|
|
304 |
let mut save_game_config = |name: &str, mut value: Vec<String>| {
|
15880
|
305 |
game_config.push(Hs::Tuple(vec![
|
15581
|
306 |
Hs::String(name.to_string()),
|
|
307 |
Hs::List(value.drain(..).map(Hs::String).collect()),
|
|
308 |
]));
|
|
309 |
};
|
|
310 |
|
15796
|
311 |
match config.ammo {
|
|
312 |
Ammo {
|
|
313 |
name,
|
|
314 |
settings: Some(settings),
|
|
315 |
} => save_game_config("AMMO", vec![name, settings.clone()]),
|
|
316 |
Ammo { name, .. } => save_game_config("AMMO", vec![name.clone()]),
|
|
317 |
}
|
|
318 |
|
|
319 |
match config.scheme {
|
|
320 |
Scheme { name, settings } => {
|
|
321 |
let mut values = vec![name];
|
|
322 |
values.extend_from_slice(&settings);
|
|
323 |
save_game_config("SCHEME", values);
|
15581
|
324 |
}
|
|
325 |
}
|
|
326 |
|
15796
|
327 |
save_game_config("SCRIPT", vec![config.script]);
|
|
328 |
save_game_config("THEME", vec![config.theme]);
|
|
329 |
|
15581
|
330 |
Hs::Tuple(vec![
|
|
331 |
Hs::List(teams),
|
|
332 |
Hs::List(map_config),
|
|
333 |
Hs::List(game_config),
|
15796
|
334 |
Hs::List(replay.message_log.drain(..).map(Hs::String).collect()),
|
15581
|
335 |
])
|
|
336 |
}
|
|
337 |
|
15796
|
338 |
fn haskell_to_replay(value: HaskellValue) -> Option<Replay> {
|
15576
|
339 |
use HaskellValue::*;
|
15796
|
340 |
let mut config = RoomConfig::new();
|
15576
|
341 |
let mut lists = value.into_tuple()?;
|
|
342 |
let mut lists_iter = lists.drain(..);
|
|
343 |
|
|
344 |
let teams_list = lists_iter.next()?.into_list()?;
|
|
345 |
let map_config = lists_iter.next()?.into_list()?;
|
|
346 |
let game_config = lists_iter.next()?.into_list()?;
|
|
347 |
let engine_messages = lists_iter.next()?.into_list()?;
|
|
348 |
|
|
349 |
let mut teams = Vec::with_capacity(teams_list.len());
|
|
350 |
|
|
351 |
for team in teams_list {
|
|
352 |
let (_, mut fields) = team.into_struct()?;
|
|
353 |
|
|
354 |
let mut team_info = TeamInfo::default();
|
|
355 |
for (name, value) in fields.drain() {
|
|
356 |
match &name[..] {
|
|
357 |
"teamowner" => team_info.owner = value.into_string()?,
|
|
358 |
"teamname" => team_info.name = value.into_string()?,
|
|
359 |
"teamcolor" => team_info.color = u8::from_str(&value.into_string()?).ok()?,
|
|
360 |
"teamgrave" => team_info.grave = value.into_string()?,
|
|
361 |
"teamfort" => team_info.fort = value.into_string()?,
|
|
362 |
"teamvoicepack" => team_info.voice_pack = value.into_string()?,
|
|
363 |
"teamflag" => team_info.flag = value.into_string()?,
|
|
364 |
"difficulty" => team_info.difficulty = value.into_number()?,
|
|
365 |
"hhnum" => team_info.hedgehogs_number = value.into_number()?,
|
|
366 |
"hedgehogs" => {
|
|
367 |
for (index, hog) in value
|
|
368 |
.into_list()?
|
|
369 |
.drain(..)
|
|
370 |
.enumerate()
|
|
371 |
.take(team_info.hedgehogs.len())
|
|
372 |
{
|
|
373 |
let (_, mut fields) = hog.into_anon_struct()?;
|
|
374 |
let mut fields_iter = fields.drain(..);
|
|
375 |
team_info.hedgehogs[index] = HedgehogInfo {
|
|
376 |
name: fields_iter.next()?.into_string()?,
|
|
377 |
hat: fields_iter.next()?.into_string()?,
|
|
378 |
}
|
|
379 |
}
|
|
380 |
}
|
|
381 |
_ => (),
|
|
382 |
}
|
|
383 |
}
|
|
384 |
teams.push(team_info)
|
|
385 |
}
|
|
386 |
|
15578
|
387 |
for item in map_config {
|
|
388 |
let mut tuple = item.into_tuple()?;
|
|
389 |
let mut tuple_iter = tuple.drain(..);
|
|
390 |
let name = tuple_iter.next()?.into_string()?;
|
|
391 |
let value = tuple_iter.next()?.into_string()?;
|
|
392 |
|
15796
|
393 |
match &name[..] {
|
|
394 |
"FEATURE_SIZE" => config.feature_size = u32::from_str(&value).ok()?,
|
|
395 |
"MAP" => config.map_type = value,
|
|
396 |
"MAPGEN" => config.map_generator = u32::from_str(&value).ok()?,
|
|
397 |
"MAZE_SIZE" => config.maze_size = u32::from_str(&value).ok()?,
|
|
398 |
"SEED" => config.seed = value,
|
|
399 |
"TEMPLATE" => config.template = u32::from_str(&value).ok()?,
|
|
400 |
"DRAWNMAP" => config.drawn_map = Some(value),
|
|
401 |
_ => {}
|
15578
|
402 |
};
|
|
403 |
}
|
|
404 |
|
|
405 |
for item in game_config {
|
|
406 |
let mut tuple = item.into_tuple()?;
|
|
407 |
let mut tuple_iter = tuple.drain(..);
|
|
408 |
let name = tuple_iter.next()?.into_string()?;
|
15580
|
409 |
let mut value = tuple_iter.next()?.into_list()?;
|
|
410 |
let mut value_iter = value.drain(..);
|
15576
|
411 |
|
15578
|
412 |
let config_item = match &name[..] {
|
15796
|
413 |
"AMMO" => {
|
|
414 |
config.ammo = Ammo {
|
|
415 |
name: value_iter.next()?.into_string()?,
|
|
416 |
settings: value_iter.next().and_then(|v| v.into_string()),
|
|
417 |
}
|
|
418 |
}
|
|
419 |
"SCHEME" => {
|
|
420 |
config.scheme = Scheme {
|
|
421 |
name: value_iter.next()?.into_string()?,
|
|
422 |
settings: value_iter.filter_map(|v| v.into_string()).collect(),
|
|
423 |
}
|
|
424 |
}
|
|
425 |
"SCRIPT" => config.script = value_iter.next()?.into_string()?,
|
|
426 |
"THEME" => config.theme = value_iter.next()?.into_string()?,
|
15578
|
427 |
_ => None?,
|
|
428 |
};
|
|
429 |
}
|
15576
|
430 |
|
|
431 |
let mut messages = Vec::with_capacity(engine_messages.len());
|
|
432 |
|
|
433 |
for message in engine_messages {
|
|
434 |
messages.push(message.into_string()?);
|
|
435 |
}
|
|
436 |
|
15796
|
437 |
Some(Replay {
|
|
438 |
config,
|
15576
|
439 |
teams,
|
15796
|
440 |
message_log: messages,
|
15576
|
441 |
})
|
|
442 |
}
|
15796
|
443 |
|
|
444 |
impl Replay {
|
|
445 |
pub fn save(self, filename: String) -> io::Result<()> {
|
|
446 |
let text = format!("{}", replay_to_haskell(self));
|
|
447 |
let mut file = fs::File::open(filename)?;
|
|
448 |
file.write(text.as_bytes())?;
|
|
449 |
Ok(())
|
|
450 |
}
|
|
451 |
|
|
452 |
pub fn load(filename: &str) -> io::Result<Self> {
|
|
453 |
let mut file = fs::File::open(filename)?;
|
|
454 |
let mut bytes = vec![];
|
|
455 |
file.read_to_end(&mut bytes)?;
|
|
456 |
match super::haskell::parse(&bytes[..]) {
|
|
457 |
Ok((_, value)) => haskell_to_replay(value).ok_or(io::Error::new(
|
|
458 |
io::ErrorKind::InvalidData,
|
|
459 |
"Invalid replay structure",
|
|
460 |
)),
|
|
461 |
Err(_) => Err(io::Error::new(
|
|
462 |
io::ErrorKind::InvalidData,
|
|
463 |
"Unable to parse file",
|
|
464 |
)),
|
|
465 |
}
|
|
466 |
}
|
|
467 |
}
|