summaryrefslogtreecommitdiff
path: root/src/fmt.rs
blob: 2129b55d2d7c1f1d7668544beb9a9977c4b9c23d (plain)
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
extern crate byteorder;
extern crate simple_error;

use std::collections::HashMap;
use std::env;
use std::error::Error;
use std::fs::File;
use std::io::Read;
use std::path::Path;

use self::byteorder::{ByteOrder, LittleEndian};

// The ".grp" file format is just a collection of a lot of files stored into 1 big
// one. I tried to make the format as simple as possible: The first 12 bytes
// contains my name, "KenSilverman". The next 4 bytes is the number of files that
// were compacted into the group file. Then for each file, there is a 16 byte
// structure, where the first 12 bytes are the filename, and the last 4 bytes are
// the file's size. The rest of the group file is just the raw data packed one
// after the other in the same order as the list of files.

/// Implementation of a group file "cache", into which the contents of several
/// group files can be loaded. This is somewhat similar to the way that
/// Silverman's original code goes about loading game data. Additionally, this
/// struct manages a list of "search paths" on the filesystem to resolve the
/// absolute path of data files that have several possible locations.
/// See the documentation of 'load_file' for more information.
#[derive(Debug)]
pub struct GroupManager {
    files: HashMap<String, Vec<u8>>,
    search: Vec<String>,
}

impl GroupManager {
    pub fn new() -> GroupManager {
        let mut result = GroupManager {
            files: HashMap::new(),
            search: Vec::new()
        };

        result.init_search_paths();

        result
    }

    /// Loads the contents of an in-memory group file into the cache.
    pub fn load_data(&mut self, data: &[u8]) -> Result<(), Box<Error>> {
        let len = data.len();

        if len < 16 {
            bail!("'data' is too small to contain the GRP header.");
        }

        let header = String::from_utf8(data[..12].to_vec())?;

        if header.as_str() != "KenSilverman" {
            bail!("Invalid GRP header.");
        }

        let file_count = LittleEndian::read_u32(&data[12..16]) as usize;

        // 16 bytes for the header, and 16 bytes for each table entry. The raw
        // data will follow.
        let data_start = 16 * (file_count + 1) as usize;

        if data_start >= len {
            bail!("Invalid number of files.");
        }

        let mut data_off = data_start;

        for i in 0..file_count {
            // Similar to how 'data_start' was calculated - 16 bytes for the
            // header, and 16 bytes for each table entry.
            let table_off = 16 * (i + 1);

            let name = &data[table_off..table_off+12];
            let name = String::from_utf8(name.to_vec())?;
            let name = if let Some(j) = name.find('\x00') {
                 String::from(&name[..j])
            } else {
                name
            };

            let size = &data[table_off+12..table_off+16];
            let size = LittleEndian::read_u32(size) as usize;

            let data = data[data_off..data_off+size].to_vec();
            data_off += size;

            self.files.insert(name, data);
        }

        Ok(())
    }

    /// Obtains binary data associated with the given filename from the cache.
    pub fn get(&self, filename: &str) -> Option<&[u8]> {
        Some(&self.files.get(filename)?)
    }

    /// Go through the search path list in order and load the contents of the
    /// first group archive with the given name. Specifically, the default
    /// search path order is:
    ///
    /// - ".",
    /// - "/usr/local/share/games/rebuild",
    /// - "/usr/share/games/rebuild",
    /// - "/usr/local/share/games/eduke32",
    /// - "/usr/share/games/eduke32",
    /// - "/usr/local/share/games/jfduke3d",
    /// - "/usr/share/games/jfduke3d",
    /// - "$HOME/.rebuild",
    ///
    /// This is followed by any additional paths in the search path list that
    /// came about from a call to 'add_search_path'.
    pub fn load_file(&mut self, filename: &str) -> Result<(), Box<Error>> {
        for directory in self.search.clone().iter() {
            let path = format!("{}/{}", directory, filename);

            if Path::new(&path).exists() {
                let mut file = File::open(path)?;
                let mut bytes: Vec<u8> = Vec::new();

                file.read_to_end(&mut bytes)?;
                self.load_data(&bytes)?;

                return Ok(());
            }
        }

        bail!("File not found in any search paths.")
    }

    /// Adds an additional path for 'load_file' to search.
    pub fn add_search_path(&mut self, path: &str) -> Result<(), Box<Error>> {
        if Path::new(&path).exists() {
            self.search.push(String::from(path));
        } else {
            bail!("Path does not exist");
        }

        Ok(())
    }

    // TODO: Conventional paths for OSX and Windows from EDuke32's
    // G_AddSearchPaths.
    fn init_search_paths(&mut self) {
        // Initial base paths that don't need a $HOME expansion.
        let directories = vec![
            ".",
            "/usr/share/games/jfduke3d",
            "/usr/local/share/games/jfduke3d",
            "/usr/share/games/eduke32",
            "/usr/local/share/games/eduke32",
            "/usr/share/games/rebuild",
            "/usr/local/share/games/rebuild",
        ];

        for directory in directories.iter() {
            self.add_search_path(directory).ok();
        }

        // TODO: Steam paths.
        let directories = vec![
            "$HOME/.rebuild",
        ];

        if let Some(home) = env::home_dir() {
            if let Some(home) = home.to_str() {
                for path in directories.iter() {
                    let path = String::from(*path).replace("$HOME", home);
                    self.add_search_path(&path).ok();
                }
            }
        }
    }
}


// What's the .MAP / .ART file format?
//
// Go to my Build Source Code Page and download BUILDSRC.ZIP. I have a text file
// in there (BUILDINF.TXT) which describes both formats.

// TODO: Write documentation
// pub struct Art {

// }


// impl Art {
//     pub fn new(data: &[u8]) -> Result<Art, Box<Error>> {
//         let len = data.len();
//         let version = LittleEndian::read_u32(&data[0..4]);

//         let _tile_count = LittleEndian::read_u32(&data[4..8]);
//         let first_tile = LittleEndian::read_u32(&data[8..12]);
//         let last_tile = LittleEndian::read_u32(&data[12..16]);
//         let tile_count = last_tile - first_tile + 1; // + 1?

//         let tiles_x: Vec<u16> = Vec::new();
//         let tiles_y: Vec<u16> = Vec::new();
//         let tiles_animation: Vec<u32> = Vec::new();

//         // short tilesizx[localtileend-localtilestart+1];
//         // short tilesizy[localtileend-localtilestart+1];

//         if version != 1 {
//             bail!("Invalid ART version");
//         }
//     }
// }


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_load_slice() {
        // Binary blob containing a GRP test vector, made by me. Contains the
        // "KenSilverman" header, and a table consisting of 3 files:
        //
        // - 'TESTFILEA': A single byte, 0x01.
        // - 'TESTFILEB': 0x02, repeated twice.
        // - 'TESTFILEC': 0x03, repeated three times.
        //
        // The sizes listed in the table accurately represent this.

        let data = vec![
            b'K', b'e', b'n', b'S', b'i', b'l', b'v', b'e',
            b'r', b'm', b'a', b'n', 0x03, 0x00, 0x00, 0x00,
            b'T', b'E', b'S', b'T', b'F', b'I', b'L', b'E',
            b'A', 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
            b'T', b'E', b'S', b'T', b'F', b'I', b'L', b'E',
            b'B', 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
            b'T', b'E', b'S', b'T', b'F', b'I', b'L', b'E',
            b'C', 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
            0x01, 0x02, 0x02, 0x03, 0x03, 0x03,
        ];

        let mut group_manager = GroupManager::new();

        match group_manager.load_data(&data) {
            Err(e) => panic!("{}", e),
            Ok(_) => (),
        }

        let data = match group_manager.get("TESTFILEA") {
            Some(data) => data,
            None => panic!("TESTFILEA wasn't found in the archive"),
        };

        assert_eq!(data.len(), 1);
        assert_eq!(data[0], 0x01);

        let data = match group_manager.get("TESTFILEB") {
            Some(data) => data,
            None => panic!("TESTFILEB wasn't found in the archive"),
        };

        assert_eq!(data.len(), 2);
        assert_eq!(data[0], 0x02);
        assert_eq!(data[1], 0x02);

        let data = match group_manager.get("TESTFILEC") {
            Some(data) => data,
            None => panic!("TESTFILEC wasn't found in the archive"),
        };

        assert_eq!(data.len(), 3);
        assert_eq!(data[0], 0x03);
        assert_eq!(data[1], 0x03);
        assert_eq!(data[2], 0x03);
    }

    #[test]
    fn test_incomplete_header() {
        // Binary blob similar to the GRP test vector above, but with a header
        // that would be too small to be valid.

        let data = vec![
            b'J', b'a', b'k', b'o', b'b',
        ];

        let mut group_manager = GroupManager::new();

        match group_manager.load_data(&data) {
            Ok(_) => panic!("Accepted incomplete header."),
            Err(_) => (),
        }
    }

    #[test]
    fn test_invalid_header() {
        // Binary blob similar to the GRP test vector above, but with an invalid
        // "magic" header.

        let data = vec![
            b'J', b'a', b'k', b'o', b'b', b'L', b'K', b'r',
            b'e', b'u', b'z', b'e', 0x01, 0x00, 0x00, 0x00,
            b'T', b'E', b'S', b'T', b'F', b'I', b'L', b'E',
            b'A', 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
            0x01,
        ];

        let mut group_manager = GroupManager::new();

        match group_manager.load_data(&data) {
            Ok(_) => panic!("Accepted invalid header."),
            Err(_) => (),
        }
    }

    #[test]
    fn test_invalid_file_count() {
        // Binary blob similar to the GRP test vector above, but with a header
        // indicating that there are more files than could possibly be contained
        // in the table.

        let data = vec![
            b'K', b'e', b'n', b'S', b'i', b'l', b'v', b'e',
            b'r', b'm', b'a', b'n', 0x69, 0x00, 0x00, 0x00,
            b'T', b'E', b'S', b'T', b'F', b'I', b'L', b'E',
            b'A', 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
            0x01,
        ];

        let mut group_manager = GroupManager::new();

        match group_manager.load_data(&data) {
            Ok(_) => panic!("Accepted invalid header."),
            Err(_) => (),
        }
    }

    // TODO: Add checks for data_off and table_off going out of bounds.
}