jabyengine/src/Tools/jaby_engine_fconv/src/main.rs

74 lines
2.1 KiB
Rust

use jaby_engine_fconv::images::{*};
use clap::{Parser, Subcommand};
use std::path::PathBuf;
use tool_helper::Error;
#[derive(Parser)]
#[clap(about = "Converts files to various JabyEngine related file formats", long_about = None)]
struct CommandLine {
#[clap(long="lz4", default_value_t=false)]
compress_lz4: bool,
#[clap(short='o')]
output_file: Option<PathBuf>,
#[clap(value_parser)]
input_file: Option<PathBuf>,
#[clap(subcommand)]
sub_command: SubCommands,
}
#[derive(Subcommand)]
enum SubCommands {
SimpleTIM(reduced_tim::Arguments)
}
fn run_main() -> Result<(), Error> {
match CommandLine::try_parse() {
Ok(cmd) => {
let input = tool_helper::open_input(cmd.input_file)?;
let mut buffer = Vec::<u8>::new();
let mut output_file = tool_helper::open_output(cmd.output_file)?;
let dst_buffer = {
if cmd.compress_lz4 {
&mut buffer as &mut dyn std::io::Write
}
else {
&mut output_file as &mut dyn std::io::Write
}
};
match cmd.sub_command {
SubCommands::SimpleTIM(args) => {
reduced_tim::convert(args, input, dst_buffer)?;
}
}
// We encoded the file to a temporary buffer and now need to write it
if cmd.compress_lz4 {
println!("Buffer-Size: {} ({} Sectors)", buffer.len(), (buffer.len() + 2047)/2048);
let buffer = tool_helper::compress::lz4(&buffer, 16)?;
println!("New buffer-Size: {} ({} Sectors)", buffer.len(), (buffer.len() + 2047)/2048);
output_file.write(&buffer)?;
}
Ok(())
},
Err(error) => Err({
let mut error = Error::from_error(error);
error.exit_code = 0;
error
})
}
}
fn main() {
if let Err(error) = run_main() {
println!("{}", error.text);
std::process::exit(error.exit_code);
}
}