wolfrpg_map_parser/db_parser/parsers/
game_data_parser.rs

1use crate::db_parser::game_data::GameData;
2use crate::db_parser::DATA_MAGIC;
3use std::fs;
4use std::io::Result;
5use std::path::Path;
6
7/// Parse a .dat file containing information on a WolfRPG Editor game.
8///
9/// Returns miscellaneous data regarding the game.
10/// If you have already read the bytes, consider using [`parse_bytes`].
11///
12/// # Panics
13/// This function will panic if the given file does not represent a valid game data structure.
14pub fn parse(data: &Path) -> Result<GameData> {
15    match fs::read(data) {
16        Ok(contents) => {
17            Ok(parse_bytes(&contents))
18        }
19        Err(e) => {
20            Err(e)
21        }
22    }
23}
24
25/// Parse bytes containing information on a WolfRPG Editor game.
26///
27/// Returns miscellaneous data regarding the game.
28/// If you need to read the file to call this function, consider using [`parse`].
29///
30/// # Panics
31/// This function will panic if the given bytes do not represent a valid game data structure.
32pub fn parse_bytes(bytes: &[u8]) -> GameData {
33    let mut offset: usize = 0;
34
35    let header: &[u8] = &bytes[0..11];
36    offset += 11;
37
38    if &header[..10] != DATA_MAGIC {
39        panic!("Invalid data header.");
40    }
41
42    offset += 3; // Padding
43
44    GameData::parse(&bytes[offset..])
45}