1 use crate::core::types::{GameCfg, TeamInfo}; |
1 use crate::{ |
2 |
2 core::types::{GameCfg, HedgehogInfo, TeamInfo}, |
|
3 server::haskell::HaskellValue, |
|
4 }; |
3 use std::{ |
5 use std::{ |
4 fs, |
6 fs, |
5 io::{self, BufReader, Read, Write}, |
7 io::{self, BufReader, Read, Write}, |
6 str::FromStr, |
8 str::FromStr, |
7 }; |
9 }; |
15 impl Demo { |
17 impl Demo { |
16 fn save(&self, file: String) -> io::Result<()> { |
18 fn save(&self, file: String) -> io::Result<()> { |
17 Ok(unimplemented!()) |
19 Ok(unimplemented!()) |
18 } |
20 } |
19 |
21 |
20 fn load(file: String) -> io::Result<Self> { |
22 fn load(filename: String) -> io::Result<Self> { |
21 let value = super::haskell::parse(&[]); |
23 let mut file = fs::File::open(filename)?; |
22 Ok(unimplemented!()) |
24 let mut bytes = vec![]; |
23 } |
25 file.read_to_end(&mut bytes)?; |
24 |
26 match super::haskell::parse(&bytes[..]) { |
25 fn load_hwd(file: String) -> io::Result<Self> { |
27 Ok((_, value)) => haskell_to_demo(value).ok_or(io::Error::new( |
26 let datum = fs::File::open(file)?; |
28 io::ErrorKind::InvalidData, |
27 let mut reader = io::BufReader::new(datum); |
29 "Invalid demo structure", |
|
30 )), |
|
31 Err(_) => Err(io::Error::new( |
|
32 io::ErrorKind::InvalidData, |
|
33 "Unable to parse file", |
|
34 )), |
|
35 } |
|
36 } |
|
37 |
|
38 fn load_hwd(filename: String) -> io::Result<Self> { |
|
39 let file = fs::File::open(filename)?; |
|
40 let mut reader = io::BufReader::new(file); |
28 |
41 |
29 #[inline] |
42 #[inline] |
30 fn error<T>(cause: &str) -> io::Result<T> { |
43 fn error<T>(cause: &str) -> io::Result<T> { |
31 Err(io::Error::new(io::ErrorKind::InvalidData, cause)) |
44 Err(io::Error::new(io::ErrorKind::InvalidData, cause)) |
32 } |
45 } |
252 config, |
265 config, |
253 messages, |
266 messages, |
254 }) |
267 }) |
255 } |
268 } |
256 } |
269 } |
|
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 |
|
321 for item in map_config {} |
|
322 |
|
323 for item in game_config {} |
|
324 |
|
325 let mut messages = Vec::with_capacity(engine_messages.len()); |
|
326 |
|
327 for message in engine_messages { |
|
328 messages.push(message.into_string()?); |
|
329 } |
|
330 |
|
331 Some(Demo { |
|
332 teams, |
|
333 config, |
|
334 messages, |
|
335 }) |
|
336 } |