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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//! .drs is the resource archive file format for the Genie Engine, used by Age of Empires 1/2 and
//! Star Wars: Galactic Battlegrounds. .drs files contain tables, each of which contain resources
//! of a single type. Resources are identified by a numeric identifier.
//!
//! ## Example
//!
//! ```rust
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use std::fs::File;
//! use genie_drs::DRSReader;
//!
//! let mut file = File::open("test.drs")?;
//! let drs = DRSReader::new(&mut file)?;
//!
//! for table in drs.tables() {
//!     for resource in table.resources() {
//!         let content = drs.read_resource(&mut file, table.resource_type, resource.id)?;
//!         println!("{}: {:?}", resource.id, std::str::from_utf8(&content)?);
//!     }
//! }
//! # Ok(())
//! # }
//! ```

#![deny(future_incompatible)]
#![deny(nonstandard_style)]
#![deny(rust_2018_idioms)]
#![deny(unsafe_code)]
#![warn(missing_docs)]
#![warn(unused)]

use byteorder::{ReadBytesExt, WriteBytesExt, LE};
use sorted_vec::SortedVec;
use std::io::{Error, Read, Write};
use std::slice;
use std::str;

mod read;
mod write;

pub use read::DRSReader;
pub use write::{DRSWriter, InMemoryStrategy, ReserveDirectoryStrategy, Strategy as WriteStrategy};

/// A DRS version string.
type DRSVersion = [u8; 4];

/// A resource type name.
///
/// In a .drs archive, type names are represented as 4 bytes. They are laid out in reverse order and
/// padded with ASCII space characters (`' '`). For example, the "slp" resource type is stored as `" pls"`.
///
/// ## Examples
/// ```rust
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use genie_drs::ResourceType;
/// let slp = ResourceType::from(*b" pls");
/// assert_eq!(&slp, "slp", "implements AsRef<str>");
/// assert_eq!(slp.to_string(), "slp", "implements ToString");
/// assert_eq!(slp, ResourceType::from(*b" pls"));
/// let bina = "bina".parse::<ResourceType>()?;
/// assert_ne!(slp, bina);
/// # Ok(()) }
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ResourceType([u8; 4]);
impl ResourceType {
    #[inline]
    fn write_to<W: Write>(self, output: &mut W) -> Result<(), Error> {
        let mut bytes = [0; 4];
        bytes.copy_from_slice(&self.0);
        bytes.reverse();
        output.write_all(&bytes)?;
        Ok(())
    }
}

impl PartialEq<str> for ResourceType {
    fn eq(&self, ext: &str) -> bool {
        let me: &str = self.as_ref();
        me == ext
    }
}

impl AsRef<str> for ResourceType {
    fn as_ref(&self) -> &str {
        str::from_utf8(&self.0[..])
            .expect("resource type must be utf-8")
            .trim()
    }
}

impl ToString for ResourceType {
    fn to_string(&self) -> String {
        let s: &str = self.as_ref();
        s.to_string()
    }
}

/// An error occurred while parsing a resource type.
///
/// This may be caused by:
///   - The input string not being 4 characters long
#[derive(Debug, thiserror::Error)]
#[error("invalid resource type, must be 4 characters")]
pub struct ParseResourceTypeError;

impl core::str::FromStr for ResourceType {
    type Err = ParseResourceTypeError;
    /// Parse a resource type from a string, with error handling.
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let bytes = s.as_bytes();
        if bytes.len() > 4 {
            Err(ParseResourceTypeError)
        } else {
            Ok(bytes.into())
        }
    }
}

impl From<[u8; 4]> for ResourceType {
    fn from(mut u: [u8; 4]) -> Self {
        u.reverse();
        Self(u)
    }
}

/// Parse a resource type from a byte slice, panics if the slice is too long to fit.
impl From<&[u8]> for ResourceType {
    fn from(u: &[u8]) -> Self {
        assert!(u.len() <= 4);
        let mut bytes = [b' '; 4];
        (&mut bytes[0..u.len()]).copy_from_slice(u);
        Self(bytes)
    }
}

/// Parse a resource type from a string, panics if the string is too long to fit (>4 bytes).
impl From<&str> for ResourceType {
    fn from(s: &str) -> Self {
        s.as_bytes().into()
    }
}

/// The DRS archive header.
pub struct DRSHeader {
    /// A copyright message.
    banner_msg: [u8; 40],
    /// File version. (always "1.00")
    version: DRSVersion,
    /// File password / identifier.
    password: [u8; 12],
    /// The amount of resource types (tables).
    num_resource_types: u32,
    /// Size in bytes of the metadata and tables. Resource contents start at this offset.
    directory_size: u32,
}

impl Default for DRSHeader {
    fn default() -> Self {
        Self {
            banner_msg: *b"Copyright (c) 1997 Ensemble Studios.\x1a\x00\x00\x00",
            version: *b"1.00",
            password: *b"tribe\x00\x00\x00\x00\x00\x00\x00",
            num_resource_types: 0,
            directory_size: 0,
        }
    }
}

impl DRSHeader {
    #[inline]
    /// Read a DRS archive header from a `Read`able handle.
    fn from<R: Read>(source: &mut R) -> Result<DRSHeader, Error> {
        let mut banner_msg = [0 as u8; 40];
        let mut version = [0 as u8; 4];
        let mut password = [0 as u8; 12];
        source.read_exact(&mut banner_msg)?;
        source.read_exact(&mut version)?;
        source.read_exact(&mut password)?;
        let num_resource_types = source.read_u32::<LE>()?;
        let directory_size = source.read_u32::<LE>()?;
        Ok(DRSHeader {
            banner_msg,
            version,
            password,
            num_resource_types,
            directory_size,
        })
    }

    #[inline]
    fn write_to<W: Write>(&self, output: &mut W) -> Result<(), Error> {
        output.write_all(&self.banner_msg)?;
        output.write_all(&self.version)?;
        output.write_all(&self.password)?;
        output.write_u32::<LE>(self.num_resource_types)?;
        output.write_u32::<LE>(self.directory_size)?;
        Ok(())
    }
}

impl std::fmt::Debug for DRSHeader {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f,
           "DRSHeader {{ banner_msg: '{}', version: '{}', password: '{}', num_resource_types: {}, directory_size: {} }}",
           str::from_utf8(&self.banner_msg).expect("banner must be utf8"),
           str::from_utf8(&self.version).expect("version must be utf8"),
           str::from_utf8(&self.password).expect("password must be utf8"),
           self.num_resource_types,
           self.directory_size
        )
    }
}

/// A table containing resource entries.
pub struct DRSTable {
    /// Type of the resource as a little-endian char array.
    pub resource_type: ResourceType,
    /// Offset in the DRS archive where this table's resource entries can be found.
    offset: u32,
    /// Number of resource entries in this table.
    num_resources: u32,
    /// Resources.
    resources: Vec<DRSResource>,
    /// Resource IDs.
    resource_ids: SortedVec<u32>,
}

impl DRSTable {
    fn new(resource_type: ResourceType, offset: u32, num_resources: u32) -> Self {
        Self {
            resource_type,
            offset,
            num_resources,
            resources: Default::default(),
            resource_ids: Default::default(),
        }
    }

    /// Read a DRS table header from a `Read`able handle.
    #[inline]
    fn from<R: Read>(source: &mut R) -> Result<DRSTable, Error> {
        let mut resource_type = [0 as u8; 4];
        source.read_exact(&mut resource_type)?;
        let offset = source.read_u32::<LE>()?;
        let num_resources = source.read_u32::<LE>()?;
        Ok(DRSTable::new(resource_type.into(), offset, num_resources))
    }

    #[inline]
    fn write_to<W: Write>(&self, output: &mut W) -> Result<(), Error> {
        self.resource_type.write_to(output)?;
        output.write_u32::<LE>(self.offset)?;
        output.write_u32::<LE>(self.num_resources)?;
        Ok(())
    }

    /// Read the table itself.
    #[inline]
    fn read_resources<R: Read>(&mut self, source: &mut R) -> Result<(), Error> {
        for _ in 0..self.num_resources {
            let resource = DRSResource::from(source)?;
            let _discard = self.resource_ids.insert(resource.id);
            self.resources.push(resource);
        }
        Ok(())
    }

    /// Get the number of resources in this table.
    #[inline]
    pub fn len(&self) -> usize {
        self.num_resources as usize
    }

    /// Check if the table contains no resources.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.num_resources == 0
    }

    /// Iterate over the resources in this table.
    #[inline]
    pub fn resources(&self) -> DRSResourceIterator<'_> {
        self.resources.iter()
    }

    /// Find a resource by ID.
    #[inline]
    pub fn get_resource(&self, id: u32) -> Option<&DRSResource> {
        self.resource_ids
            .binary_search(&id)
            .ok()
            .map(|index| &self.resources[index])
    }

    /// Get the resource type this table contains.
    #[inline]
    pub fn resource_type(&self) -> ResourceType {
        self.resource_type
    }

    /// Deprecated, use `resource_type()` instead.
    #[inline]
    pub fn resource_ext(&self) -> String {
        self.resource_type.to_string()
    }

    /// Add a resource to this table.
    #[inline]
    pub(crate) fn add(&mut self, res: DRSResource) -> &mut DRSResource {
        self.resources.push(res);
        self.num_resources += 1;
        self.resources.last_mut().expect("last_mut returned None?")
    }
}

impl std::fmt::Debug for DRSTable {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "DRSTable {{ resource_type: '{}', offset: {}, num_resources: {} }}",
            self.resource_type.to_string(),
            self.offset,
            self.num_resources
        )
    }
}

/// A single resource in a DRS archive.
#[derive(Debug)]
pub struct DRSResource {
    /// The resource ID.
    pub id: u32,
    /// The offset into the DRS archive where the resource can be found.
    offset: u32,
    /// The size in bytes of the resource.
    pub size: u32,
}

impl DRSResource {
    /// Read DRS resource metadata from a `Read`able handle.
    #[inline]
    fn from<R: Read>(source: &mut R) -> Result<DRSResource, Error> {
        let id = source.read_u32::<LE>()?;
        let offset = source.read_u32::<LE>()?;
        let size = source.read_u32::<LE>()?;
        Ok(DRSResource { id, offset, size })
    }

    #[inline]
    fn write_to<W: Write>(&self, output: &mut W) -> Result<(), Error> {
        output.write_u32::<LE>(self.id)?;
        output.write_u32::<LE>(self.offset)?;
        output.write_u32::<LE>(self.size)?;
        Ok(())
    }
}

/// An iterator over DRS table metadata structs.
pub type DRSTableIterator<'a> = slice::Iter<'a, DRSTable>;
/// An iterator over DRS resource metadata structs.
pub type DRSResourceIterator<'a> = slice::Iter<'a, DRSResource>;

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

    #[test]
    fn it_works() -> anyhow::Result<()> {
        let mut file = File::open("test.drs")?;
        let drs = DRSReader::new(&mut file)?;
        let mut expected = vec![
            // (reversed_type, id, size)
            ("js".parse()?, 1, 632),
            ("js".parse()?, 2, 452),
            ("js".parse()?, 3, 38),
            ("json".parse()?, 4, 710),
        ];

        for table in drs.tables() {
            for resource in table.resources() {
                let content = drs.read_resource(&mut file, table.resource_type, resource.id)?;
                assert_eq!(
                    expected.remove(0),
                    (table.resource_type, resource.id, content.len())
                );
            }
        }

        Ok(())
    }
}