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