-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
119 lines (106 loc) · 3.91 KB
/
mod.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
mod color;
mod convert;
mod diff;
mod dump;
mod tree;
mod verify;
#[cfg(feature = "color")]
use self::color::get_styles;
use crate::logger::LogLevel;
use clap::CommandFactory as _;
use color_print::cprintln;
use serde_hkx_features::{
convert::{convert, OutFormat},
diff::exec,
dump as hexdump,
error::{Error, Result},
tree::output as show_tree,
verify::verify as reproduce,
};
use std::{io, path::PathBuf};
pub(crate) async fn run(args: Args) -> Result<()> {
crate::logger::init(args.log_file, args.log_level, args.stdout)?;
// For drag & drop
if let Some(input) = args.input {
let out_fmt = OutFormat::from_input(&input)?;
let output: Option<PathBuf> = None;
return convert(input, output, out_fmt).await;
}
if let Some(command) = args.subcommand {
match command {
SubCommands::Convert(args) => convert(&args.input, args.output, args.format).await,
SubCommands::Tree(args) => show_tree(args.input, args.output).await,
SubCommands::Dump(args) => match args.reverse {
true => hexdump::to_bytes(args.input, args.output).await,
false => hexdump::to_string(args.input, args.output).await,
},
SubCommands::Diff(args) => exec(args.old, args.new, args.output, args.color).await,
SubCommands::Verify(args) => {
println!("Verifying...");
reproduce(&args.path, args.color).map(|_| {
cprintln!(
"<green>Complete hkx reproduction: {}</green>",
args.path.display()
);
})
}
SubCommands::Completions { shell } => {
shell.generate(&mut Args::command(), &mut io::stdout());
Ok(())
}
}
} else {
Args::command().print_long_help()?;
pub const ERR_MSG: &str = color_print::cstr!(
r#"
<red>error:</red> '<red>hkxc</red>' requires a subcommand or hkx/xml path.
For more information, try '<cyan!>--help</cyan!>'.
"#
);
Err(Error::IoError {
source: io::Error::new(io::ErrorKind::InvalidInput, ERR_MSG),
})
}
}
/// CLI command arguments
#[derive(Debug, clap::Parser)]
#[clap(version, about, author)]
#[cfg_attr(feature = "color", clap(styles=get_styles()))]
#[clap(arg_required_else_help = true, args_conflicts_with_subcommands = true, after_long_help = convert::EXAMPLES)]
pub(crate) struct Args {
/// The path of the target for drag and drop hkx(64bit) <-> xml conversion (The converted file will be created in the same location)
pub input: Option<PathBuf>,
#[clap(subcommand)]
subcommand: Option<SubCommands>,
// --logger (Global options)
#[clap(global = true, long, display_order = 100)]
/// Enable standard output of the log
pub stdout: bool,
#[clap(global = true, long, display_order = 101)]
#[clap(ignore_case = true, default_value = "error")]
/// Log level to be recorded in logger
pub log_level: LogLevel,
#[clap(global = true, long, display_order = 102)]
/// Output path of log file
pub log_file: Option<PathBuf>,
}
#[derive(Debug, clap::Parser)]
pub(crate) enum SubCommands {
/// Convert hkx <-> xml
Convert(convert::Args),
/// Show dependency tree from havok behavior state machine (hkx/xml file)
Tree(tree::Args),
/// Dump binary data in hexadecimal
Dump(dump::Args),
/// Show diff between two files.(In the case of `.hkx`, it is automatically converted to hexdump)
Diff(diff::Args),
/// Parallel hkx reproduction checks. If an error occurs, return a diff showing the location of each error.
Verify(verify::Args),
/// Generate shell completions
#[clap(arg_required_else_help = true)]
Completions {
/// The shell to generate the completions for
#[arg(value_enum)]
shell: clap_complete_command::Shell,
},
}