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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//! Contains functions for serializing arbitrary objects to the Named Binary
//! Tag format.
//!
//! For working with existing serialization implementations, see `to_writer`.
//! For custom types, implement the `NbtFmt` trait.

use std::collections::{BTreeMap, HashMap};
use std::hash::Hash;
use std::io;

use byteorder::{ReadBytesExt, WriteBytesExt};

use error::{Error, Result};

pub mod raw;

/// A trait indicating that the type has a Named Binary Tag representation.
///
/// Keep in mind that not all Rust types (notably unsigned integers) have an
/// obvious NBT representation, and so structs that implement this trait may
/// have to convert them to one that does.
///
/// ## Usage with Derive
///
/// A compiler plugin is available in the `nbt_macros` package to enable
/// automatic derivation of NBT encoding/decoding for types. This is heavily
/// recommended over implementing this trait by hand. Usage is generally as
/// simple as the following:
///
/// ```ignore
/// #![feature(plugin, custom_derive)]
/// #![plugin(nbt_macros)]
/// 
/// extern crate nbt;
/// 
/// use nbt::serialize::{NbtFmt, to_writer};
/// 
/// #[derive(NbtFmt)]
/// struct MyMob {
///     name: String,
///     health: i8
/// }
///
/// fn main() {
///     let mut bytes = Vec::new();
///     let mob = MyMob { name: "Dr. Evil".to_string(), health: 240 };
///
///     to_writer(&mut bytes, &mob).unwrap();
/// }
/// ```
///
/// The package's documentation provides more detailed usage.
///
/// ## Manual Implementation
///
/// While it is not advisable to implement `NbtFmt` by hand, the code below is
/// similar to what the automated derivation produces:
///
/// ```rust
/// extern crate nbt;
///
/// use std::io::Cursor;
/// use nbt::serialize::*;
///
/// #[derive(Debug, PartialEq)]
/// struct MyMob {
///     name: String,
///     health: i8
/// }
///
/// impl NbtFmt for MyMob {
///     type Into = MyMob;
///
///     fn to_bare_nbt<W>(&self, dst: &mut W) -> nbt::Result<()>
///        where W: std::io::Write
///     {
///         try!(self.name.to_nbt(dst, "name"));
///         try!(self.health.to_nbt(dst, "health"));
///
///         close_nbt(dst)
///     }
///
///     fn read_bare_nbt<R>(src: &mut R) -> nbt::Result<MyMob>
///        where R: std::io::Read
///     {
///         let mut __name: String = Default::default();
///         let mut __health: i8 = Default::default();
///
///         loop {
///             let (t, n) = try!(emit_next_header(src));
///
///             if t == 0x00 { break; } // i.e. Tag_End
///
///             match &n[..] {
///                 "name" => {
///                     __name = try!(read_bare_nbt(src));
///                 },
///                 "health" => {
///                     __health = try!(read_bare_nbt(src));
///                 },
///                 e => {
///                     return Err(nbt::Error::UnexpectedField(e.to_string()));
///                 },
///             };
///         }
///
///         Ok(MyMob { name: __name, health: __health })
///     }
/// }
///
/// fn main() {
///     let mut bytes = Vec::new();
///     let mob = MyMob { name: "Dr. Evil".to_string(), health: 240 };
///
///     to_writer(&mut bytes, &mob).unwrap();
///     let read_mob: MyMob = from_reader(&mut Cursor::new(bytes.clone())).unwrap();
///
///     assert_eq!(&mob, &read_mob);
/// }
/// ```
pub trait NbtFmt {
    type Into: Sized = Self;

    /// Convert this type to NBT format using the specified `io::Write`
    /// destination, but does not serialize its identifying NBT tag or name.
    fn to_bare_nbt<W>(&self, dst: &mut W) -> Result<()>
       where W: io::Write;

    /// Reads from the specified `io::Read` source bytes that can be coverted
    /// into an instance of this type.
    fn read_bare_nbt<R>(src: &mut R) -> Result<Self::Into>
       where R: io::Read;

    /// Convert this type to NBT format using the specified `io::Write`
    /// destination, incuding its tag and a given name.
    #[inline]
    fn to_nbt<W, S>(&self, dst: &mut W, name: S) -> Result<()>
       where W: io::Write,
             S: AsRef<str>
    {
        try!(dst.write_u8(Self::tag()));
        try!(raw::write_bare_string(dst, name.as_ref()));
        self.to_bare_nbt(dst)
    }
    
    /// Indicates the NBT tag that this type corresponds to. Most custom types
    /// (usually structs) will advertise the default, `0x0a`, which is the
    /// default.
    #[inline] fn tag() -> u8 { 0x0a }

    /// Indicates whether this type is "bare", in that it must be wrapped in an
    /// NBT Compound before serialization. By default this is `false`, since
    /// most imeplementations will be Compound-like objects. Primitive NBT
    /// types (`i8`, `i16`, `String`, etc.) return `true`.
    #[inline] fn is_bare() -> bool { false }
}

/// A convenience function for closing NBT format objects.
///
/// This function writes a single `0x00` byte to the `io::Write` destination,
/// which in the NBT format indicates that an open Compound is now closed.
pub fn close_nbt<W>(dst: &mut W) -> Result<()>
    where W: io::Write {

    dst.write_u8(0x00).map_err(From::from)
}

/// Extracts the next header (tag and name) from an NBT format source.
///
/// This function will also return the `TAG_End` byte and an empty name if it
/// encounters it.
pub fn emit_next_header<R>(src: &mut R) -> Result<(u8, String)>
    where R: io::Read
{
    let tag  = try!(src.read_u8());

    match tag {
        0x00 => { Ok((tag, "".to_string())) },
        _    => {
            let name = try!(raw::read_bare_string(src));
            Ok((tag, name))
        },
    }
}

/// Serializes an object into NBT format at a given destination.
///
/// This function will try to ensure that the output is always a valid NBT
/// file, i.e. that it has a top-level Compound.
pub fn to_writer<W, T>(dst: &mut W, obj: &T) -> Result<()>
    where W: io::Write,
          T: NbtFmt
{
    match T::is_bare() {
        // Refuse to blindly serialize types not wrapped in an NBT Compound.
        true  => { return Err(Error::NoRootCompound); },
        false => obj.to_nbt(dst, ""),
    }
}

/// Deserializes an object in NBT format from a given source.
pub fn from_reader<R, T>(src: &mut R) -> Result<T>
    where R: io::Read,
          T: NbtFmt<Into=T>
{
    if T::is_bare() {
        // Valid NBT files do not contain bare types.
        return Err(Error::NoRootCompound);
    }

    let next_tag  = try!(src.read_u8());
    if next_tag != T::tag() {
        return Err(Error::TagMismatch(next_tag, T::tag()));
    }

    let _ = try!(raw::read_bare_string(src));
    let rval = try!(T::read_bare_nbt(src));

    Ok(rval)
}

/// Deserializes a bare object (i.e. with no name or tag) from a given source.
pub fn read_bare_nbt<R, T>(src: &mut R) -> Result<T>
    where R: io::Read,
          T: NbtFmt<Into=T>
{
    (T::read_bare_nbt(src)).map_err(From::from)
}

macro_rules! nbtfmt_value {
  ($T:ty, $read_method:expr, $write_method:expr, $tag:expr) => (
    impl NbtFmt for $T {
        #[inline]
        fn to_bare_nbt<W>(&self, dst: &mut W) -> Result<()>
           where W: io::Write
        {
            $write_method(dst, *self)
        }

        #[inline]
        fn read_bare_nbt<R>(src: &mut R) -> Result<$T>
           where R: io::Read
        {
            $read_method(src)
        }

        #[inline] fn tag() -> u8 { $tag }
        #[inline] fn is_bare() -> bool { true }
    }
  )
}

macro_rules! nbtfmt_ptr {
  ($T:ty, $Into:ty, $read_method:expr, $write_method:expr, $tag:expr) => (
    impl NbtFmt for $T {
        type Into = $Into;

        #[inline]
        fn to_bare_nbt<W>(&self, dst: &mut W) -> Result<()>
           where W: io::Write
        {
            $write_method(dst, self)
        }

        #[inline]
        fn read_bare_nbt<R>(src: &mut R) -> Result<$Into>
           where R: io::Read
        {
            $read_method(src)
        }

        #[inline] fn tag() -> u8 { $tag }
        #[inline] fn is_bare() -> bool { true }
    }
  )
}

macro_rules! nbtfmt_slice {
  ($T:ty, $read_method:expr, $write_method:expr, $tag:expr) => (
    impl NbtFmt for $T {
        #[inline]
        fn to_bare_nbt<W>(&self, dst: &mut W) -> Result<()>
           where W: io::Write
        {
            $write_method(dst, &self[..])
        }

        #[inline]
        fn read_bare_nbt<R>(src: &mut R) -> Result<$T>
           where R: io::Read
        {
            $read_method(src)
        }

        #[inline] fn tag() -> u8 { $tag }
        #[inline] fn is_bare() -> bool { true }
    }
  )
}

nbtfmt_value!(i8, raw::read_bare_byte, raw::write_bare_byte, 0x01);
nbtfmt_value!(i16, raw::read_bare_short, raw::write_bare_short, 0x02);
nbtfmt_value!(i32, raw::read_bare_int, raw::write_bare_int, 0x03);
nbtfmt_value!(i64, raw::read_bare_long, raw::write_bare_long, 0x04);
nbtfmt_value!(f32, raw::read_bare_float, raw::write_bare_float, 0x05);
nbtfmt_value!(f64, raw::read_bare_double, raw::write_bare_double, 0x06);
nbtfmt_ptr!(str, String, raw::read_bare_string, raw::write_bare_string, 0x08);
nbtfmt_slice!(String, raw::read_bare_string, raw::write_bare_string, 0x08);

// For now, to handle conflicting implementations, use slices to indicate when
// byte and int arrays should be preferred to lists.

nbtfmt_ptr!([i8], Vec<i8>, raw::read_bare_byte_array, raw::write_bare_byte_array, 0x07);
nbtfmt_ptr!([i32], Vec<i32>, raw::read_bare_int_array, raw::write_bare_int_array, 0x0b);

// FIXME: Remove this workaround and enable some way of uncommenting the lines
// that follow.

// nbtfmt_slice!(Vec<i8>, write_bare_byte_array, 0x07);
// nbtfmt_slice!(Vec<i32>, write_bare_int_array, 0x0b);

// impl<T> NbtFmt for [T] where T: NbtFmt {
//  fn to_bare_nbt<W>(&self, dst: &mut W) -> Result<()>
//        where W: io::Write {
        
//          write_bare_list(dst, self.iter())
//  }
//     #[inline] fn tag() -> u8 { 0x09 }
//     #[inline] fn is_bare() -> bool { true }
// }

// This leaves Vec<T> alone for lists (which kind of makes sense).

impl<T> NbtFmt for Vec<T> where T: NbtFmt<Into=T> {
    type Into = Vec<T>;

    #[inline]
    fn to_bare_nbt<W>(&self, dst: &mut W) -> Result<()>
       where W: io::Write
    {
        raw::write_bare_list(dst, self.iter())
    }

    #[inline]
    fn read_bare_nbt<R>(src: &mut R) -> Result<Vec<T>>
       where R: io::Read
    {
        let tag = try!(src.read_u8());
        if tag != T::tag() {
            // FIXME: New error needed for this.
            return Err(Error::IncompleteNbtValue);
        }

        // Err(Error::IncompleteNbtValue)

        raw::read_bare_list(src)
    }

    #[inline] fn tag() -> u8 { 0x09 }
    #[inline] fn is_bare() -> bool { true }
}

impl<S, T> NbtFmt for HashMap<S, T> where S: AsRef<str> + Hash + Eq, T: NbtFmt {
    type Into = HashMap<String, T::Into>;

    #[inline]
    fn to_bare_nbt<W>(&self, dst: &mut W) -> Result<()>
       where W: io::Write
    {
        raw::write_bare_compound(dst, self.iter())
    }

    #[inline]
    fn read_bare_nbt<R>(src: &mut R) -> Result<Self::Into>
       where R: io::Read
    {
        let mut rval = HashMap::new();

        loop {
            let (tag, key) = try!(emit_next_header(src));

            if tag == 0x00 { break; } // i.e. Tag_End
            if tag != T::tag() {
                return Err(Error::TagMismatch(T::tag(), tag));
            }

            let value = try!(T::read_bare_nbt(src));

            // Check for key collisions.
            match rval.insert(key.clone(), value) {
                None    => (),
                Some(_) => return Err(Error::UnexpectedField(key)),
            };
        }

        Ok(rval)
    }

    #[inline] fn tag() -> u8 { 0x0a }
    #[inline] fn is_bare() -> bool { false }
}

impl<S, T> NbtFmt for BTreeMap<S, T> where S: AsRef<str>, T: NbtFmt {
    type Into = BTreeMap<String, T::Into>;

    #[inline]
    fn to_bare_nbt<W>(&self, dst: &mut W) -> Result<()>
       where W: io::Write
    {
        raw::write_bare_compound(dst, self.iter())
    }

    #[inline]
    fn read_bare_nbt<R>(src: &mut R) -> Result<Self::Into>
       where R: io::Read
    {
        let mut rval = BTreeMap::new();

        loop {
            let (tag, key) = try!(emit_next_header(src));

            if tag == 0x00 { break; } // i.e. Tag_End
            if tag != T::tag() {
                return Err(Error::TagMismatch(T::tag(), tag));
            }

            let value = try!(T::read_bare_nbt(src));

            // Check for key collisions.
            match rval.insert(key.clone(), value) {
                None    => (),
                Some(_) => return Err(Error::UnexpectedField(key)),
            };
        }

        Ok(rval)
    }

    #[inline] fn tag() -> u8 { 0x0a }
    #[inline] fn is_bare() -> bool { false }
}