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
// Copyright © 2014, Peter Atashian

use std::cmp::Ordering::{Equal, Greater, Less};
use std::mem::transmute;
use std::num::FromPrimitive;

/// The basic functionality for all nodes. 
pub trait GenericNode<'a> {
    /// Gets the child node of the specified name.
    fn get(&self, name: &str) -> Option<Node<'a>>;
    /// Gets the type of this node.
    fn dtype(&self) -> Type;
    /// Gets the string value of this node. This will be `None` if the node is not a string node.
    fn string(&self) -> Option<&'a str>;
    /// Gets the integer value of this node. This will be `None` if the node is not an integer 
    /// node.
    fn integer(&self) -> Option<i64>;
    /// Gets the float value of this node. This will be `None` if the node is not a float node.
    fn float(&self) -> Option<f64>;
    /// Gets the vector value of this node. This will be `None` if the node is not a vector node.
    fn vector(&self) -> Option<(i32, i32)>;
}

/// A node in an NX file.
#[derive(Copy)]
pub struct Node<'a> {
    data: &'a Data,
    file: &'a super::File,
}

impl<'a> Node<'a> {
    /// Creates a Node from the data representing it and the file the data is from.
    pub unsafe fn construct(data: &'a Data, file: &'a super::File) -> Node<'a> {
        Node { data: data, file: file }
    }
    /// Gets whether or not the node is empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.data.count == 0
    }
    /// Gets the name of this node from the string table.
    #[inline]
    pub fn name(&self) -> &'a str {
        self.file.get_str(self.data.name)
    }
    /// Gets an iterator over this node's children.
    #[inline]
    pub fn iter(&self) -> Nodes<'a> {
        let data = unsafe { self.file.nodetable.offset(self.data.children as isize) };
        Nodes {
            data: data,
            count: self.data.count,
            file: self.file,
        }
    }
}

impl<'a> GenericNode<'a> for Node<'a> {
    #[inline]
    fn get(&self, name: &str) -> Option<Node<'a>> {
        let mut data = unsafe { self.file.nodetable.offset(self.data.children as isize) };
        let mut count = self.data.count as isize;
        while count > 0 {
            let half = count / 2;
            let temp = unsafe { data.offset(half) };
            let other = self.file.get_str(unsafe { (*temp).name });
            match other.cmp(name) {
                Less => {
                    data = unsafe { temp.offset(1) };
                    count -= half + 1;
                },
                Equal => return Some(Node {
                    data: unsafe { &*temp },
                    file: self.file,
                }),
                Greater => count = half,
            }
        }
        None
    }
    #[inline]
    fn dtype(&self) -> Type {
        match FromPrimitive::from_u16(self.data.dtype) {
            Some(dtype) => dtype,
            None => Type::Empty,
        }
    }
    #[inline]
    fn string(&self) -> Option<&'a str> {
        match self.dtype() {
            Type::String => Some(self.file.get_str(unsafe {
                transmute::<_, String>(self.data.data).index
            })),
            _ => None,
        }
    }
    #[inline]
    fn integer(&self) -> Option<i64> {
        match self.dtype() {
            Type::Integer => Some(unsafe { transmute::<_, Integer>(self.data.data).value }),
            _ => None,
        }
    }
    #[inline]
    fn float(&self) -> Option<f64> {
        match self.dtype() {
            Type::Float => Some(unsafe { transmute::<_, Float>(self.data.data).value }),
            _ => None,
        }
    }
    #[inline]
    fn vector(&self) -> Option<(i32, i32)> {
        match self.dtype() {
            Type::Vector => Some(unsafe {
                let vec = transmute::<_, Vector>(self.data.data);
                (vec.x, vec.y)
            }),
            _ => None,
        }
    }
}
impl<'a> GenericNode<'a> for Option<Node<'a>> {
    #[inline]
    fn get(&self, name: &str) -> Option<Node<'a>> {
        match self {
            &Some(n) => n.get(name),
            &None => None,
        }
    }
    #[inline]
    fn dtype(&self) -> Type {
        match self {
            &Some(n) => n.dtype(),
            &None => Type::Empty,
        }
    }
    #[inline]
    fn string(&self) -> Option<&'a str> {
        match self {
            &Some(n) => n.string(),
            &None => None,
        }
    }
    #[inline]
    fn integer(&self) -> Option<i64> {
        match self {
            &Some(n) => n.integer(),
            &None => None,
        }
    }
    #[inline]
    fn float(&self) -> Option<f64> {
        match self {
            &Some(n) => n.float(),
            &None => None,
        }
    }
    #[inline]
    fn vector(&self) -> Option<(i32, i32)> {
        match self {
            &Some(n) => n.vector(),
            &None => None,
        }
    }
}

impl<'a> PartialEq for Node<'a> {
    #[inline]
    fn eq(&self, other: &Node) -> bool {
        self.data as *const Data == other.data as *const Data
    }
}

impl<'a> Eq for Node<'a> {}

/// An iterator over nodes.
pub struct Nodes<'a> {
    data: *const Data,
    count: u16,
    file: &'a super::File,
}

impl<'a> Iterator for Nodes<'a> {
    type Item = Node<'a>;
    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.count as usize, Some(self.count as usize))
    }
    #[inline]
    fn next(&mut self) -> Option<Node<'a>> {
        match self.count {
            0 => None,
            _ => {
                self.count -= 1;
                let node = Node {
                    data: unsafe { &*self.data },
                    file: self.file
                };
                self.data = unsafe { self.data.offset(1) };
                Some(node)
            }
        }
    }
}

/// The data contained by an NX node.
#[repr(packed)]
pub struct Data {
    name: u32,
    children: u32,
    count: u16,
    dtype: u16,
    data: u64,
}

/// The types of NX nodes.
#[derive(FromPrimitive, PartialEq, Eq, Copy)]
pub enum Type {
    /// A node containing no data.
    Empty = 0,
    /// A node containing integer data.
    Integer = 1,
    /// A node containing floating-point data.
    Float = 2,
    /// A node containing string data.
    String = 3,
    /// A node containing vector (or point) data.
    Vector = 4,
    /// A node containing bitmap data.
    Bitmap = 5,
    /// A node containing audio data.
    Audio = 6,
}

#[repr(packed)]
struct Integer {
    value: i64,
}

#[repr(packed)]
struct Float {
    value: f64,
}

#[repr(packed)]
struct String {
    index: u32,
    _unused: u32,
}

#[repr(packed)]
struct Vector {
    x: i32,
    y: i32,
}

#[repr(packed)]
#[allow(dead_code)]
struct Bitmap {
    index: u32,
    width: u16,
    height: u16,
}

#[repr(packed)]
#[allow(dead_code)]
struct Audio {
    index: u32,
    length: u32,
}