forked from SpaceManiac/SpacemanDMM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
497 lines (447 loc) · 17 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
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
//! CLI tools, including a map renderer, using the same backend as the editor.
#![forbid(unsafe_code)]
#![doc(hidden)] // Don't interfere with lib docs.
extern crate rayon;
extern crate structopt;
extern crate serde;
extern crate serde_json;
#[macro_use] extern crate serde_derive;
extern crate dreammaker as dm;
extern crate dmm_tools;
use std::collections::HashMap;
use std::fmt;
use std::path::Path;
use std::sync::atomic::{AtomicIsize, Ordering};
use std::sync::RwLock;
use std::collections::HashSet;
use structopt::StructOpt;
use dm::objtree::ObjectTree;
use dmm_tools::*;
// ----------------------------------------------------------------------------
// Main driver
fn main() {
let opt = Opt::from_clap(&Opt::clap()
.long_version(concat!(
env!("CARGO_PKG_VERSION"), "\n",
include_str!(concat!(env!("OUT_DIR"), "/build-info.txt")),
).trim_end())
.get_matches());
let mut context = Context::default();
context.dm_context.set_print_severity(Some(dm::Severity::Error));
rayon::ThreadPoolBuilder::new()
.num_threads(opt.jobs)
.build_global()
.expect("failed to initialize thread pool");
context.parallel = opt.jobs != 1;
run(&opt, &opt.command, &mut context);
std::process::exit(context.exit_status.into_inner() as i32);
}
#[derive(Default)]
struct Context {
dm_context: dm::Context,
objtree: ObjectTree,
icon_cache: IconCache,
exit_status: AtomicIsize,
parallel: bool,
procs: bool,
}
impl Context {
fn objtree(&mut self, opt: &Opt) {
let environment = match opt.environment {
Some(ref env) => env.into(),
None => match dm::detect_environment_default() {
Ok(Some(found)) => found,
_ => dm::DEFAULT_ENV.into(),
},
};
println!("parsing {}", environment.display());
if let Some(parent) = environment.parent() {
self.icon_cache.set_icons_root(&parent);
}
self.dm_context.autodetect_config(&environment);
let pp = match dm::preprocessor::Preprocessor::new(&self.dm_context, environment) {
Ok(pp) => pp,
Err(e) => {
eprintln!("i/o error opening environment:\n{}", e);
std::process::exit(1);
}
};
let indents = dm::indents::IndentProcessor::new(&self.dm_context, pp);
let mut parser = dm::parser::Parser::new(&self.dm_context, indents);
if self.procs {
parser.enable_procs();
}
self.objtree = parser.parse_object_tree();
}
}
#[derive(StructOpt, Debug)]
#[structopt(name="dmm-tools",
author="Copyright (C) 2017-2020 Tad Hardesty",
about="This program comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under the conditions of the GNU
General Public License version 3.")]
struct Opt {
/// The environment file to operate under.
#[structopt(short="e", long="env")]
environment: Option<String>,
#[structopt(short="v", long="verbose")]
verbose: bool,
/// Set the number of threads to be used for parallel execution when
/// possible. A value of 0 will select automatically, and 1 will be serial.
#[structopt(long="jobs", default_value="1")]
jobs: usize,
#[structopt(subcommand)]
command: Command,
}
// ----------------------------------------------------------------------------
// Subcommands
#[derive(StructOpt, Debug)]
enum Command {
/// Show information about the render-pass list.
#[structopt(name = "list-passes")]
ListPasses {
/// Output as JSON.
#[structopt(short="j", long="json")]
json: bool,
},
/// Check the environment for errors and warnings.
#[structopt(name = "check")]
Check {
/// The minimum severity to print, of "error", "warning", "info", "hint".
#[structopt(long="severity", default_value="info")]
severity: String,
/// Check proc bodies as well as the object tree.
#[structopt(long="procs")]
procs: bool,
},
/// Build minimaps of the specified maps.
#[structopt(name = "minimap")]
Minimap {
/// The output directory.
#[structopt(short="o", default_value="data/minimaps")]
output: String,
/// Set the minimum x,y or x,y,z coordinate to act upon (1-indexed, inclusive).
#[structopt(long="min")]
min: Option<CoordArg>,
/// Set the maximum x,y or x,y,z coordinate to act upon (1-indexed, inclusive).
#[structopt(long="max")]
max: Option<CoordArg>,
/// Enable render-passes, or "all" to only exclude those passed to --disable.
#[structopt(long="enable", default_value="")]
enable: String,
/// Disable render-passes, or "all" to only use those passed to --enable.
#[structopt(long="disable", default_value="")]
disable: String,
/// Run output through pngcrush automatically. Requires pngcrush.
#[structopt(long="pngcrush")]
pngcrush: bool,
/// Run output through optipng automatically. Requires optipng.
#[structopt(long="optipng")]
optipng: bool,
/// The list of maps to process.
files: Vec<String>,
},
/// List the differing coordinates between two maps.
#[structopt(name="diff-maps")]
DiffMaps {
left: String,
right: String,
},
/// Show metadata information about the map.
#[structopt(name="map-info")]
MapInfo {
/// Output as JSON.
#[structopt(short="j", long="json")]
json: bool,
/// The list of maps to show info on.
files: Vec<String>,
},
}
fn run(opt: &Opt, command: &Command, context: &mut Context) {
match *command {
// --------------------------------------------------------------------
Command::ListPasses { json } => {
if json {
#[derive(Serialize)]
struct Pass<'a> {
name: &'a str,
desc: &'a str,
default: bool,
}
let mut report = Vec::new();
for &render_passes::RenderPassInfo {
name, desc, default, new: _,
} in render_passes::RENDER_PASSES {
report.push(Pass { name, desc, default });
}
output_json(&report);
} else {
println!("default passes:");
let mut non_default = Vec::new();
for pass in render_passes::RENDER_PASSES {
if pass.default {
println!("{}: {}", pass.name, pass.desc);
} else {
non_default.push(pass);
}
}
if !non_default.is_empty() {
println!("\nadditional passes:");
for pass in non_default {
println!("{}: {}", pass.name, pass.desc);
}
}
}
},
// --------------------------------------------------------------------
Command::Check { ref severity, procs } => {
let severity = match severity.as_str() {
"error" => dm::Severity::Error,
"warning" => dm::Severity::Warning,
"info" => dm::Severity::Info,
_ => dm::Severity::Hint,
};
context.dm_context.set_print_severity(Some(severity));
context.procs = procs;
context.objtree(opt);
*context.exit_status.get_mut() = context
.dm_context
.errors()
.iter()
.filter(|e| e.severity() <= severity)
.count() as isize;
},
// --------------------------------------------------------------------
Command::Minimap {
ref output, min, max, ref enable, ref disable, ref files,
pngcrush, optipng,
} => {
context.objtree(opt);
if context
.dm_context
.errors()
.iter()
.filter(|e| e.severity() <= dm::Severity::Error)
.next()
.is_some()
{
println!("there were some parsing errors; render may be inaccurate")
}
let Context {
ref objtree,
ref icon_cache,
ref exit_status,
parallel,
..
} = *context;
let render_passes = &dmm_tools::render_passes::configure(enable, disable);
let paths: Vec<&Path> = files.iter().map(|p| p.as_ref()).collect();
let errors: RwLock<HashSet<String>> = Default::default();
let perform_job = move |path: &Path| {
let mut filename;
let prefix = if parallel {
filename = path.file_name().unwrap().to_string_lossy().into_owned();
filename.push_str(": ");
println!("{}{}", filename, path.display());
&filename
} else {
println!("{}", path.display());
" "
};
let map = match dmm::Map::from_file(path) {
Ok(map) => map,
Err(e) => {
eprintln!("Failed to load {}:\n{}", path.display(), e);
exit_status.fetch_add(1, Ordering::Relaxed);
return;
}
};
let (dim_x, dim_y, dim_z) = map.dim_xyz();
let mut min = min.unwrap_or(CoordArg { x: 0, y: 0, z: 0 });
let mut max = max.unwrap_or(CoordArg {
x: dim_x + 1,
y: dim_y + 1,
z: dim_z + 1,
});
min.x = clamp(min.x, 1, dim_x);
min.y = clamp(min.y, 1, dim_y);
min.z = clamp(min.z, 1, dim_z);
max.x = clamp(max.x, min.x, dim_x);
max.y = clamp(max.y, min.y, dim_y);
max.z = clamp(max.z, min.z, dim_z);
println!("{}rendering from {} to {}", prefix, min, max);
let do_z_level = |z| {
println!("{}generating z={}", prefix, 1 + z);
let bump = Default::default();
let minimap_context = minimap::Context {
objtree: &objtree,
map: &map,
level: map.z_level(z),
min: (min.x - 1, min.y - 1),
max: (max.x - 1, max.y - 1),
render_passes: &render_passes,
errors: &errors,
bump: &bump,
};
let image = minimap::generate(minimap_context, icon_cache).unwrap();
if let Err(e) = std::fs::create_dir_all(output) {
eprintln!("Failed to create output directory {}:\n{}", output, e);
exit_status.fetch_add(1, Ordering::Relaxed);
return;
}
let outfile = format!(
"{}/{}-{}.png",
output,
path.file_stem().unwrap().to_string_lossy(),
1 + z
);
println!("{}saving {}", prefix, outfile);
image.to_file(outfile.as_ref()).unwrap();
if pngcrush {
println!(" pngcrush {}", outfile);
let temp = format!("{}.temp", outfile);
assert!(std::process::Command::new("pngcrush")
.arg("-ow")
.arg(&outfile)
.arg(&temp)
.stderr(std::process::Stdio::null())
.status()
.unwrap()
.success(), "pngcrush failed");
}
if optipng {
println!("{}optipng {}", prefix, outfile);
assert!(std::process::Command::new("optipng")
.arg(&outfile)
.stderr(std::process::Stdio::null())
.status()
.unwrap()
.success(), "optipng failed");
}
};
if parallel {
use rayon::iter::{IntoParallelIterator, ParallelIterator};
((min.z - 1)..(max.z)).into_par_iter().for_each(do_z_level);
} else {
((min.z - 1)..(max.z)).into_iter().for_each(do_z_level);
}
};
if parallel {
use rayon::iter::{IntoParallelIterator, ParallelIterator};
// Suboptimal due to mixing I/O in with what's meant for CPU
// tasks, but it should get the job done for now.
paths.into_par_iter().for_each(perform_job);
} else {
paths.into_iter().for_each(perform_job);
}
},
// --------------------------------------------------------------------
Command::DiffMaps {
ref left, ref right,
} => {
use std::cmp::min;
let path: &std::path::Path = left.as_ref();
println!("{}", path.display());
let left_map = dmm::Map::from_file(path).unwrap();
let path: &std::path::Path = right.as_ref();
println!("{}", path.display());
let right_map = dmm::Map::from_file(path).unwrap();
let left_dims = left_map.dim_xyz();
let right_dims = right_map.dim_xyz();
if left_dims != right_dims {
println!(" different size: {:?} {:?}", left_dims, right_dims);
}
for z in 0..min(left_dims.2, right_dims.2) {
for y in 0..min(left_dims.1, right_dims.1) {
for x in 0..min(left_dims.0, right_dims.0) {
let left_tile = &left_map.dictionary[&left_map.grid[(z, left_dims.1 - y - 1, x)]];
let right_tile = &right_map.dictionary[&right_map.grid[(z, right_dims.1 - y - 1, x)]];
if left_tile != right_tile {
println!(" different tile: ({}, {}, {})", x + 1, y + 1, z + 1);
}
}
}
}
},
// --------------------------------------------------------------------
Command::MapInfo {
json, ref files,
} => {
if !json {
eprintln!("non-JSON output is not yet supported");
}
#[derive(Serialize)]
struct Map {
size: (usize, usize, usize),
key_length: u8,
num_keys: usize,
}
let mut report = HashMap::new();
for path in files.iter() {
let path = std::path::Path::new(path);
let map = dmm::Map::from_file(path).unwrap();
report.insert(path, Map {
size: map.dim_xyz(),
key_length: map.key_length(),
num_keys: map.dictionary.len(),
});
}
output_json(&report);
},
// --------------------------------------------------------------------
}
}
// ----------------------------------------------------------------------------
// Argument parsing helpers
#[derive(Debug, Copy, Clone)]
struct CoordArg {
x: usize,
y: usize,
z: usize,
}
impl fmt::Display for CoordArg {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.z != 0 {
write!(f, "{},{},{}", self.x, self.y, self.z)
} else {
write!(f, "{},{}", self.x, self.y)
}
}
}
impl std::str::FromStr for CoordArg {
type Err = String;
fn from_str(s: &str) -> Result<Self, String> {
match s
.split(",")
.map(|x| x.parse())
.collect::<Result<Vec<_>, std::num::ParseIntError>>()
{
Ok(ref vec) if vec.len() == 2 => Ok(CoordArg {
x: vec[0],
y: vec[1],
z: 0,
}),
Ok(ref vec) if vec.len() == 3 => Ok(CoordArg {
x: vec[0],
y: vec[1],
z: vec[2],
}),
Ok(_) => Err("must specify 2 or 3 coordinates".into()),
Err(e) => Err(e.to_string()),
}
}
}
fn clamp(val: usize, min: usize, max: usize) -> usize {
if val < min {
min
} else if val > max {
max
} else {
val
}
}
fn output_json<T: serde::Serialize>(t: &T) {
let stdout = std::io::stdout();
serde_json::to_writer(stdout.lock(), t).unwrap();
println!();
}