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
#![cfg_attr(feature = "dev", feature(plugin))]
#![cfg_attr(feature = "dev", plugin(clippy))]
#[cfg(feature = "ffi")]
extern crate libc;
#[macro_use]
extern crate log;
extern crate rustc_serialize;
extern crate zip;
#[cfg(feature = "ffi")]
use std::ffi::{CStr, CString};
use std::fs;
use std::fs::File;
use std::io::Read;
use std::ops::Deref;
use std::path::Path;
use std::result;
use std::thread;
#[cfg(feature = "ffi")]
use libc::c_char;
use rustc_serialize::json;
use zip::ZipArchive;
pub use self::chat_line::ChatLine;
pub use self::command::{CmdType, Command};
pub use self::error::Error;
pub use self::item::Item;
pub use self::map::Map;
pub use self::player::Player;
pub use self::replay::Replay;
mod chat_line;
mod command;
mod error;
mod item;
mod map;
mod player;
mod replay;
mod stream;
pub type Result<T> = result::Result<T, Error>;
#[derive(Debug, RustcEncodable)]
pub struct Vault {
pub replays: Vec<Replay>
}
impl Vault {
pub fn parse(path: &Path) -> Result<Vault> {
let meta = try!(fs::metadata(path));
let replays = if meta.is_dir() {
try!(Vault::parse_dir(path))
}
else if meta.is_file() {
match path.extension() {
Some(ext) => match ext.to_string_lossy().deref() {
"rec" => try!(Vault::parse_rec(path)),
"zip" => try!(Vault::parse_zip(path)),
_ => return Err(Error::InvalidFileExtension),
},
None => return Err(Error::InvalidFileExtension),
}
}
else {
return Err(Error::InvalidFileExtension);
};
Ok(Vault {
replays: replays
})
}
fn parse_rec(path: &Path) -> Result<Vec<Replay>> {
let mut replay = try!(Replay::new(&path));
replay.parse();
let replay = replay;
let mut replays: Vec<Replay> = Vec::with_capacity(1);
replays.push(replay);
let replays = replays;
Ok(replays)
}
fn parse_zip(path: &Path) -> Result<Vec<Replay>> {
let archive_file = try!(File::open(path));
let mut archive = try!(ZipArchive::new(archive_file));
let mut handles: Vec<_> = Vec::new();
let mut replays: Vec<Replay> = Vec::new();
let size = archive.len();
for idx in 0..size {
let mut replay_file = match archive.by_index(idx) {
Ok(file) => file,
Err(_) => {
error!("cannot read file at index {}", idx);
continue;
}
};
let name = replay_file.name().to_owned();
let inner_path = Path::new(&name);
match inner_path.extension() {
Some(ext) => match ext.to_string_lossy().deref() {
"rec" => {
let base_name = path.to_string_lossy();
let base_name = base_name.deref();
let mut combo_name = String::from(base_name);
combo_name.push_str(":");
combo_name.push_str(&name);
let mut buff: Vec<u8> = Vec::with_capacity(replay_file.size() as usize);
match replay_file.read_to_end(&mut buff) {
Ok(_) => (),
Err(err) => {
replays.push(Replay::new_with_error(&combo_name, Error::IoError(err)));
continue;
}
}
let handle = thread::spawn(move || {
match Replay::from_bytes(&combo_name, buff) {
Ok(replay) => {
let mut replay = replay;
replay.parse();
replay
},
Err(err) => Replay::new_with_error(&combo_name, err),
}
});
handles.push(handle);
},
_ => info!("skipping {}, not a replay", path.display()),
},
None => info!("skipping {}, not a replay", path.display())
}
}
for handle in handles {
match handle.join() {
Ok(replay) => replays.push(replay),
Err(_) => error!("parse failed"),
}
}
let replays = replays;
Ok(replays)
}
fn parse_dir(path: &Path) -> Result<Vec<Replay>> {
let dir = try!(fs::read_dir(path));
let mut replays: Vec<Replay> = Vec::new();
let mut handles: Vec<_> = Vec::new();
for item in dir {
let item = match item {
Ok(val) => val,
Err(_) => {
error!("error reading file in directory");
continue;
}
};
let path = item.path();
let meta = match fs::metadata(&path) {
Ok(val) => val,
Err(_) => {
error!("error reading file in directory");
continue;
}
};
if meta.is_file() {
match path.extension() {
Some(ext) => match ext.to_string_lossy().deref() {
"rec" => {
let path = path.to_owned();
let handle = thread::spawn(move || {
match Vault::parse_rec(&path) {
Ok(results) => results,
Err(err) => {
let mut result = Vec::with_capacity(1);
let long_name = path.to_string_lossy();
let long_name = long_name.deref();
result.push(Replay::new_with_error(long_name, err));
result
}
}
});
handles.push(handle);
},
"zip" => {
let path = path.to_owned();
let handle = thread::spawn(move || {
match Vault::parse_zip(&path) {
Ok(results) => results,
Err(err) => {
let mut result = Vec::with_capacity(1);
let long_name = path.to_string_lossy();
let long_name = long_name.deref();
result.push(Replay::new_with_error(long_name, err));
result
}
}
});
handles.push(handle);
},
_ => info!("skipping {}, not a replay or archive", path.display()),
},
None => info!("skipping {}, not a replay or archive", path.display())
}
}
}
for handle in handles {
match handle.join() {
Ok(result) => replays.extend(result.into_iter()),
Err(_) => error!("parse failed"),
}
}
Ok(replays)
}
pub fn to_json(&self) -> Result<String> {
Ok(try!(json::encode(&self)))
}
}
pub fn print_version() {
println!("vault v{}", env!("CARGO_PKG_VERSION"));
}
#[cfg(feature = "ffi")]
#[no_mangle]
pub extern fn parse_to_cstring(path: *const c_char) -> *mut c_char {
let cstr = unsafe { CStr::from_ptr(path) };
let cow = cstr.to_string_lossy();
let path_str = cow.deref();
let path = Path::new(&path_str);
let vault = Vault::parse(&path).unwrap();
let result = vault.to_json().unwrap();
let val = CString::new(result.into_bytes()).unwrap();
val.into_raw()
}
#[cfg(feature = "ffi")]
#[no_mangle]
pub extern fn free_cstring(ptr: *mut c_char) {
let _ = unsafe { CString::from_raw(ptr) };
}