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
use std::io;
use byteorder::{ByteOrder, BigEndian, ReadBytesExt, WriteBytesExt};
use error::{Error, Result};
use serialize::{NbtFmt, close_nbt};
#[inline]
pub fn write_bare_byte<W>(dst: &mut W, value: i8) -> Result<()>
where W: io::Write
{
dst.write_i8(value).map_err(From::from)
}
#[inline]
pub fn write_bare_short<W>(dst: &mut W, value: i16) -> Result<()>
where W: io::Write
{
dst.write_i16::<BigEndian>(value).map_err(From::from)
}
#[inline]
pub fn write_bare_int<W>(dst: &mut W, value: i32) -> Result<()>
where W: io::Write
{
dst.write_i32::<BigEndian>(value).map_err(From::from)
}
#[inline]
pub fn write_bare_long<W>(dst: &mut W, value: i64) -> Result<()>
where W: io::Write
{
dst.write_i64::<BigEndian>(value).map_err(From::from)
}
#[inline]
pub fn write_bare_float<W>(dst: &mut W, value: f32) -> Result<()>
where W: io::Write
{
dst.write_f32::<BigEndian>(value).map_err(From::from)
}
#[inline]
pub fn write_bare_double<W>(dst: &mut W, value: f64) -> Result<()>
where W: io::Write
{
dst.write_f64::<BigEndian>(value).map_err(From::from)
}
#[inline]
pub fn write_bare_byte_array<W>(dst: &mut W, value: &[i8]) -> Result<()>
where W: io::Write
{
try!(dst.write_i32::<BigEndian>(value.len() as i32));
for &v in value {
try!(dst.write_i8(v));
}
Ok(())
}
#[inline]
pub fn write_bare_int_array<W>(dst: &mut W, value: &[i32]) -> Result<()>
where W: io::Write
{
try!(dst.write_i32::<BigEndian>(value.len() as i32));
for &v in value {
try!(dst.write_i32::<BigEndian>(v));
}
Ok(())
}
#[inline]
pub fn write_bare_string<W>(dst: &mut W, value: &str) -> Result<()>
where W: io::Write
{
try!(dst.write_u16::<BigEndian>(value.len() as u16));
dst.write_all(value.as_bytes()).map_err(From::from)
}
#[inline]
pub fn write_bare_list<'a, W, I, T>(dst: &mut W, values: I) -> Result<()>
where W: io::Write,
I: Iterator<Item=&'a T> + ExactSizeIterator,
T: 'a + NbtFmt
{
try!(dst.write_u8(T::tag()));
try!(dst.write_i32::<BigEndian>(values.len() as i32));
for ref value in values {
try!(value.to_bare_nbt(dst));
}
Ok(())
}
#[inline]
pub fn write_bare_compound<'a, W, I, T, S>(dst: &mut W, values: I) -> Result<()>
where W: io::Write,
I: Iterator<Item=(&'a S, &'a T)>,
S: 'a + AsRef<str>,
T: 'a + NbtFmt
{
for (key, ref value) in values {
try!(value.to_nbt(dst, key.as_ref()));
}
close_nbt(dst)
}
#[inline]
pub fn read_bare_byte<R>(src: &mut R) -> Result<i8>
where R: io::Read
{
src.read_i8().map_err(From::from)
}
#[inline]
pub fn read_bare_short<R>(src: &mut R) -> Result<i16>
where R: io::Read
{
src.read_i16::<BigEndian>().map_err(From::from)
}
#[inline]
pub fn read_bare_int<R>(src: &mut R) -> Result<i32>
where R: io::Read
{
src.read_i32::<BigEndian>().map_err(From::from)
}
#[inline]
pub fn read_bare_long<R>(src: &mut R) -> Result<i64>
where R: io::Read
{
src.read_i64::<BigEndian>().map_err(From::from)
}
#[inline]
pub fn read_bare_float<R>(src: &mut R) -> Result<f32>
where R: io::Read
{
src.read_f32::<BigEndian>().map_err(From::from)
}
#[inline]
pub fn read_bare_double<R>(src: &mut R) -> Result<f64>
where R: io::Read
{
src.read_f64::<BigEndian>().map_err(From::from)
}
#[inline]
pub fn read_bare_byte_array<R>(src: &mut R) -> Result<Vec<i8>>
where R: io::Read
{
let len = try!(src.read_i32::<BigEndian>()) as usize;
let mut buf = Vec::with_capacity(len);
for _ in 0..len {
buf.push(try!(src.read_i8()));
}
Ok(buf)
}
#[inline]
pub fn read_bare_int_array<R>(src: &mut R) -> Result<Vec<i32>>
where R: io::Read
{
let len = try!(src.read_i32::<BigEndian>()) as usize;
let mut buf = Vec::with_capacity(len);
for _ in 0..len {
buf.push(try!(src.read_i32::<BigEndian>()));
}
Ok(buf)
}
#[inline]
pub fn read_bare_string<R>(src: &mut R) -> Result<String>
where R: io::Read
{
let len = try!(src.read_u16::<BigEndian>()) as usize;
if len == 0 { return Ok("".to_string()); }
let mut bytes = vec![0; len];
let mut n_read = 0usize;
while n_read < bytes.len() {
match try!(src.read(&mut bytes[n_read..])) {
0 => return Err(Error::IncompleteNbtValue),
n => n_read += n
}
}
String::from_utf8(bytes).map_err(From::from)
}
#[inline]
pub fn read_bare_list<R, T>(src: &mut R) -> Result<Vec<T>>
where R: io::Read,
T: NbtFmt<Into=T>
{
let len = try!(src.read_i32::<BigEndian>()) as usize;
let mut buf = Vec::with_capacity(len);
for _ in 0..len {
buf.push(try!(T::read_bare_nbt(src)));
}
Ok(buf)
}