1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//! # General structs and data
use assembly_core::{
    ldf::{LDFError, LDF},
    types::{ObjectID, ObjectTemplate, Quaternion, Vector3f},
};

#[cfg(feature = "serde-derives")]
use serde::Serialize;

#[derive(Debug)]
#[cfg_attr(feature = "serde-derives", derive(Serialize))]
pub struct Level {
    pub env: Option<Environment>,
    pub objects: Vec<Object<LDF>>,
}

#[derive(Debug)]
#[cfg_attr(feature = "serde-derives", derive(Serialize))]
pub struct Environment {
    pub sec1: Section1,
    pub sky: SkySection,
}

/// The version of a chunk
#[derive(Debug, Copy, Clone)]
pub struct ChunkVersion {
    /// The version of the chunk header format
    pub header: u16,
    /// The version of the chunk data format
    pub data: u16,
}

/// The header for a single chunk
#[derive(Debug)]
pub struct ChunkHeader {
    /// The ID of this chunk
    pub id: u32,
    /// The version of this chunk
    pub version: ChunkVersion,
    /// The chunk size
    pub size: u32,
    /// The chunk data offset
    pub offset: u32,
}

/// A chunk (header + data)
#[derive(Debug)]
pub struct Chunk<T> {
    /// The chunk header
    pub header: ChunkHeader,
    /// The chunk data
    pub data: T,
}

/// The chunk containing the offsets of the other chunks
#[derive(Debug)]
pub struct FileMetaChunkData {
    /// The version of this file
    pub version: u32,
    /// The revision of this file
    pub revision: u32,
    /// The pointer to the chunk #2000
    pub chunk_2000_offset: u32,
    /// The pointer to the chunk #2001
    pub chunk_2001_offset: u32,
    /// The pointer to the chunk #2002
    pub chunk_2002_offset: u32,
}

/// The file meta chunk
pub type FileMetaChunk = Chunk<FileMetaChunkData>;

#[derive(Debug)]
pub struct Chunk2000Data {}

#[derive(Debug)]
#[cfg_attr(feature = "serde-derives", derive(Serialize))]
pub struct ObjectExtra {
    pub field_1a: [u8; 32],
    pub field_1b: [u8; 32],
    pub field_2: u32,
    pub field_3: bool,
    pub field_4: [u32; 16],
    pub field_5: [u8; 3],
}

#[derive(Debug)]
#[cfg_attr(feature = "serde-derives", derive(Serialize))]
pub struct Object<S> {
    pub obj_id: ObjectID,
    pub lot: ObjectTemplate,
    pub asset_type: Option<u32>,
    pub value_1: Option<u32>,
    pub position: Vector3f,
    pub rotation: Quaternion,
    pub scale: f32,
    pub settings: S,
    pub extra: Vec<ObjectExtra>,
}

impl Object<String> {
    pub fn parse_settings(self) -> Result<Object<LDF>, LDFError> {
        let settings = self.settings.parse()?;
        Ok(Object {
            obj_id: self.obj_id,
            lot: self.lot,
            asset_type: self.asset_type,
            value_1: self.value_1,
            position: self.position,
            rotation: self.rotation,
            scale: self.scale,
            settings,
            extra: self.extra,
        })
    }
}

#[derive(Debug)]
pub struct ObjectsChunkData<S> {
    pub objects: Vec<Object<S>>,
}

impl ObjectsChunkData<String> {
    pub fn parse_settings(mut self) -> Result<ObjectsChunkData<LDF>, LDFError> {
        let objects = self
            .objects
            .drain(..)
            .map(Object::parse_settings)
            .collect::<Result<Vec<_>, _>>()?;
        Ok(ObjectsChunkData { objects })
    }
}

#[derive(Debug)]
pub struct EnvironmentChunkData {
    pub section1_address: u32,
    pub sky_address: u32,
    pub section3_address: u32,
}

#[derive(Debug)]
#[cfg_attr(feature = "serde-derives", derive(Serialize))]
pub struct Color {
    pub red: f32,
    pub green: f32,
    pub blue: f32,
}

impl From<(f32, f32, f32)> for Color {
    fn from((red, green, blue): (f32, f32, f32)) -> Self {
        Self { red, green, blue }
    }
}

#[derive(Debug)]
#[cfg_attr(feature = "serde-derives", derive(Serialize))]
pub struct Section1 {
    pub value1: Option<f32>,
    pub value2: Color,
    pub value3: Color,
    pub value4: Color,
    pub value5: Vector3f,
    pub value6: Option<Section1_31>,
    pub value7: Option<Color>,
    pub value8: Option<Section1_43>,
}

#[derive(Debug)]
#[cfg_attr(feature = "serde-derives", derive(Serialize))]
pub struct Section1_31 {
    pub value1: Section1_39,
    pub value2: Color,
}

#[derive(Debug)]
#[cfg_attr(feature = "serde-derives", derive(Serialize))]
pub enum Section1_39 {
    Before {
        value1: f32,
        value2: f32,
    },
    After {
        values: Box<[f32; 12]>,
        array: Vec<Section1_40>,
    },
}

#[derive(Debug)]
#[cfg_attr(feature = "serde-derives", derive(Serialize))]
pub struct Section1_40 {
    pub id: u32,
    pub float1: f32,
    pub float2: f32,
}

#[derive(Debug)]
#[cfg_attr(feature = "serde-derives", derive(Serialize))]
pub struct Section1_43 {
    pub pos: Vector3f,
    pub rot: Option<Quaternion>,
}

#[derive(Debug)]
#[cfg_attr(feature = "serde-derives", derive(Serialize))]
#[cfg_attr(feature = "serde-derives", serde(transparent))]
pub struct SkySection {
    pub files: [String; 6],
}