-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmain.rs
331 lines (276 loc) · 9.94 KB
/
main.rs
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
#![allow(dead_code)]
use std::path::Path;
use std::fs;
use std::io::{Read, Write};
use std::env;
mod dispatch;
mod decompress;
mod mapping;
mod assets;
mod archives;
mod texture;
// mod sound;
use dispatch::{ChunkData, LoaderGlobalData};
#[derive(Debug)]
struct CommandError {
message: String,
}
impl std::error::Error for CommandError {
}
impl std::fmt::Display for CommandError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "Error: {}", self.message)
}
}
impl From<assets::ParserError> for CommandError {
fn from(error: assets::ParserError) -> Self {
let property_error = error.get_properties().into_iter().rev().fold(String::new(), |acc, v| acc + "\n" + v);
CommandError {
message: "Property error occurred: ".to_owned() + &property_error,
}
}
}
type CommandResult = Result<(), CommandError>;
fn cerr(message: &'static str) -> CommandResult {
Err(CommandError {
message: message.to_owned()
})
}
fn serialize(params: &[String]) -> CommandResult {
let path = match params.get(0) {
Some(data) => data,
None => return cerr("No path specified"),
};
let mut dispatch = dispatch::Extractor::new("paks/global", None)?;
let global_data = dispatch.read_global()?;
let package = assets::Package::from_file(path, &global_data)?;
let serial_package = serde_json::to_string(&package).unwrap();
let mut file = fs::File::create(path.to_owned() + ".json").unwrap();
file.write_all(serial_package.as_bytes()).unwrap();
Ok(())
}
fn debug(params: &[String]) -> CommandResult {
let path = match params.get(0) {
Some(data) => data,
None => return cerr("No path specified"),
};
let name_map = LoaderGlobalData::empty();
let package = assets::Package::from_file(path, &name_map)?;
println!("{:#?}", package);
Ok(())
}
fn texture(params: &[String]) -> CommandResult {
let path = match params.get(0) {
Some(data) => data,
None => return cerr("No path specified"),
};
let mut dispatch = dispatch::Extractor::new("paks/global", None)?;
let global_data = dispatch.read_global()?;
let package = assets::Package::from_file(path, &global_data)?;
let package_export = package.get_export_move(0)?.into_any();
let texture = match package_export.downcast::<assets::Texture2D>() {
Ok(data) => data,
Err(_) => return cerr("Package not exporting texture"),
};
let texture_bytes = texture::decode_texture(*texture)?;
let save_path = path.clone() + ".png";
let mut file = fs::File::create(save_path).unwrap();
file.write_all(&texture_bytes).unwrap();
Ok(())
}
/*fn sound(params: &[String]) -> CommandResult {
let path = match params.get(0) {
Some(data) => data,
None => return cerr("No path specified"),
};
let name_map = LoaderGlobalData::empty();
let package = assets::Package::from_file(path, &name_map)?;
let package_export = package.get_export_move(0)?;
let sound = match package_export.downcast::<assets::USoundWave>() {
Ok(data) => data,
Err(_) => return cerr("Package not exporting sound"),
};
let sound_data = sound::decode_sound(*sound)?;
let save_path = path.clone() + ".ogg";
let mut file = fs::File::create(save_path).unwrap();
file.write_all(&sound_data).unwrap();
Ok(())
}*/
fn dispatch(params: &[String]) -> CommandResult {
let path = match params.get(0) {
Some(data) => data,
None => return cerr("No path specified"),
};
let key = match std::fs::read_to_string("key.txt") {
Ok(data) => data,
Err(_) => return cerr("Could not read key"),
};
let pattern = match params.get(1) {
Some(data) => data,
None => return cerr("No pattern specified"),
};
let mut dispatch = dispatch::Extractor::new(&path, Some(&key))?;
let data = dispatch.get_file(pattern)?;
let filename = pattern.rsplit("/").next().unwrap();
let mut file = fs::File::create(filename).unwrap();
file.write_all(&data).unwrap();
Ok(())
}
fn filelist(params: &[String]) -> CommandResult {
let path = match params.get(0) {
Some(data) => data,
None => return cerr("No path specified"),
};
let key = match std::fs::read_to_string("key.txt") {
Ok(data) => data,
Err(_) => return cerr("Could not read key"),
};
let path_dir = Path::new(path);
let paths = match path_dir.is_dir() {
true => {
path_dir.read_dir().unwrap().filter(|v| {
if let Ok(filepath) = v {
return match filepath.path().extension() {
Some(extension) => {
let extension_str = extension.to_str().unwrap();
extension_str == "pak" || extension_str == "utoc"
},
None => false,
};
}
false
}).map(|v| path.clone() + v.unwrap().file_name().to_str().unwrap()).collect()
},
false => {
vec![path.clone()]
},
};
for path in paths {
let file_list: Vec<String> = match &path[(path.len() - 4)..] {
".pak" => {
let archive = match archives::PakExtractor::new(&path, &key) {
Ok(archive) => archive,
Err(_) => continue,
};
let entries = archive.get_entries();
entries.iter().map(|v| v.get_filename().to_owned()).collect()
},
"utoc" => {
let dispatch = dispatch::Extractor::new(&path[..(path.len() - 5)], Some(&key))?;
dispatch.get_file_list().iter().map(|v| v.to_owned()).collect()
}
_ => return cerr("Unrecognised Extension"),
};
let file_str = file_list.iter().fold(String::new(), |acc, v| acc + v + "\n");
let mut file = fs::File::create(path.to_owned() + ".txt").unwrap();
file.write_all(file_str.as_bytes()).unwrap();
}
Ok(())
}
fn idlist(params: &[String]) -> CommandResult {
let path = match params.get(0) {
Some(data) => data,
None => return cerr("No path specified"),
};
let key = match std::fs::read_to_string("key.txt") {
Ok(data) => data,
Err(_) => return cerr("Could not read key"),
};
let dispatch = dispatch::Extractor::new(&path[..(path.len() - 5)], Some(&key))?;
let file_list: Vec<String> = dispatch.get_chunk_ids().iter().map(|v| v.get_id().to_string()).collect();
let file_str = file_list.iter().fold(String::new(), |acc, v| acc + v + "\n");
let mut file = fs::File::create(path.to_owned() + ".txt").unwrap();
file.write_all(file_str.as_bytes()).unwrap();
Ok(())
}
fn read_header(params: &[String]) -> CommandResult {
let path = match params.get(0) {
Some(data) => data,
None => return cerr("No path specified"),
};
let key = match std::fs::read_to_string("key.txt") {
Ok(data) => data,
Err(_) => return cerr("Could not read key"),
};
let mut dispatch = dispatch::Extractor::new(&path, Some(&key))?;
let loader_data = match dispatch.read_chunk(0)? {
ChunkData::ContainerHeader(data) => data,
_ => return cerr("Could not find map"),
};
println!("{:#?}", loader_data);
Ok(())
}
fn extract(params: &[String]) -> CommandResult {
let path = match params.get(0) {
Some(data) => data,
None => return cerr("No path specified"),
};
let key = match std::fs::read_to_string("key.txt") {
Ok(data) => data,
Err(_) => return cerr("Could not read key"),
};
let pattern = match params.get(1) {
Some(data) => data,
None => return cerr("No pattern specified"),
};
let mut archive = archives::PakExtractor::new(path, &key)?;
let entries: Vec<archives::FPakEntry> = archive.get_entries().into_iter().filter(|v| v.get_filename().contains(pattern)).cloned().collect();
for asset in entries {
let file_contents = archive.get_file(&asset);
let path = Path::new(asset.get_filename());
if let Some(basename) = path.parent() {
fs::create_dir_all(basename).expect("Could not create directory");
}
let mut file = fs::File::create(asset.get_filename()).unwrap();
file.write_all(&file_contents).unwrap();
}
Ok(())
}
fn locale(params: &[String]) -> CommandResult {
let path = match params.get(0) {
Some(data) => data,
None => return cerr("No path specified"),
};
let mut locres = match fs::File::open(path) {
Ok(data) => data,
Err(_) => return cerr("Could not read file"),
};
let mut locres_buf = Vec::new();
locres.read_to_end(&mut locres_buf).unwrap();
let package = assets::locale::FTextLocalizationResource::from_buffer(&locres_buf)?;
let serial_package = serde_json::to_string(&package).unwrap();
let mut file = fs::File::create(path.to_owned() + ".json").unwrap();
file.write_all(serial_package.as_bytes()).unwrap();
Ok(())
}
fn main() {
let args: Vec<String> = env::args().collect();
let command = args.get(1);
let command = match command {
Some(data) => data,
None => {
println!("No command specified");
return
}
};
let params = &args[2..];
let err = match (*command).as_ref() {
"serialize" => serialize(params),
"filelist" => filelist(params),
"idlist" => idlist(params),
"extract" => extract(params),
"texture" => texture(params),
"locale" => locale(params),
"debug" => debug(params),
//"sound" => sound(params),
"dispatch" => dispatch(params),
"read_header" => read_header(params),
_ => {
println!("Invalid command");
Ok(())
},
};
if let Err(error) = err {
println!("Error: {}", error);
}
}