fable_format/wld/
decode.rs

1use std::io::{Read,Seek};
2
3use crate::nom::{
4    IResult,
5    many1,
6    many_till,
7    all_consuming,
8};
9
10use crate::{
11    Decode,
12    Error,
13    ScriptField
14};
15
16use super::{WldMap, WldRegion, Wld};
17
18impl Decode for Wld {
19    fn decode<Source>(source: &mut Source) -> Result<Wld, Error> where
20        Source: Read + Seek
21    {
22        let mut data = Vec::new();
23        source.read_to_end(&mut data)?;
24        let (_, wld) = all_consuming(Wld::decode_wld)(&data)?;
25        Ok(wld)
26    }
27}
28
29impl Wld {
30    pub fn decode_wld(input: &[u8]) -> IResult<&[u8], Wld, Error> {
31        let (input, start_initial_quests) = Self::decode_wld_initial_quests(input)?;
32        let (input, map_uid_count) = ScriptField::decode_field_named("MapUIDCount")(input)?;
33        let (input, thing_manager_uid_count) = ScriptField::decode_field_named("ThingManagerUIDCount")(input)?;
34        let (input, maps) = many1(Self::decode_wld_map)(input)?;
35        let (input, regions) = many1(Self::decode_wld_region)(input)?;
36
37        Ok(
38            (
39                input,
40                Wld {
41                    start_initial_quests: start_initial_quests,
42                    map_uid_count: map_uid_count,
43                    thing_manager_uid_count: thing_manager_uid_count,
44                    maps: maps,
45                    regions: regions,
46                }
47            )
48        )
49    }
50
51    pub fn decode_wld_initial_quests(input: &[u8]) -> IResult<&[u8], Vec<ScriptField>, Error> {
52        let (input, _start) = ScriptField::decode_field_named("START_INITIAL_QUESTS")(input)?;
53        let (input, (instrs, _end)) = many_till(ScriptField::decode_field, ScriptField::decode_field_named("END_INITIAL_QUESTS"))(input)?;
54
55        Ok(
56            (
57                input,
58                instrs,
59            )
60        )
61    }
62
63    pub fn decode_wld_map(input: &[u8]) -> IResult<&[u8], WldMap, Error> {
64        let (input, new_map) = ScriptField::decode_field_named("NewMap")(input)?;
65        let (input, (instrs, _end_instr)) = many_till(ScriptField::decode_field, ScriptField::decode_field_named("EndMap"))(input)?;
66
67        Ok(
68            (
69                input,
70                WldMap {
71                    new_map: new_map,
72                    instrs: instrs,
73                }
74            )
75        )
76    }
77
78    pub fn decode_wld_region(input: &[u8]) -> IResult<&[u8], WldRegion, Error> {
79        let (input, new_region) = ScriptField::decode_field_named("NewRegion")(input)?;
80        let (input, (instrs, _end)) = many_till(ScriptField::decode_field, ScriptField::decode_field_named("EndRegion"))(input)?;
81
82        Ok(
83            (
84                input,
85                WldRegion {
86                    new_region: new_region,
87                    instrs: instrs,
88                }
89            )
90        )
91    }
92}