From 90e6f4070561b1e3e0b7550e146a244da041cb73 Mon Sep 17 00:00:00 2001 From: jaby Date: Sat, 28 Sep 2024 15:00:18 +0200 Subject: [PATCH] Support LZ4 compression for external CMDs --- src/Tools/jaby_engine_fconv/src/main.rs | 22 ++++++++++++++++------ src/Tools/tool_helper/src/lib.rs | 9 ++++++++- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/Tools/jaby_engine_fconv/src/main.rs b/src/Tools/jaby_engine_fconv/src/main.rs index 23e43e01..e0487ff6 100644 --- a/src/Tools/jaby_engine_fconv/src/main.rs +++ b/src/Tools/jaby_engine_fconv/src/main.rs @@ -88,16 +88,26 @@ fn run_internal_conversion(cmd: CommandLine) -> Result<(), Error> { fn run_external_conversion(cmd: CommandLine) -> Result<(), Error> { let input_file = cmd.input_file.ok_or(Error::from_str("Input has to be a file"))?; let output_file = cmd.output_file.ok_or(Error::from_str("Output has to be a file"))?; - - if cmd.compress_lz4 { - print_warning("LZ4 compression not supported for external commands".to_owned()); - } + let is_xa = matches!(cmd.sub_command, SubCommands::XA(_)); match cmd.sub_command { - SubCommands::VAG(args) => vag::convert(args, input_file, output_file), - SubCommands::XA(args) => xa::convert(args, input_file, output_file), + SubCommands::VAG(args) => vag::convert(args, input_file, output_file.clone()), + SubCommands::XA(args) => xa::convert(args, input_file, output_file.clone()), _ => Err(Error::from_str("Internal functions can not be called for external conversion")) + }?; + + if cmd.compress_lz4 { + if is_xa { + print_warning(format!("File {} is of type XA and can not be compressed", output_file.to_string_lossy())); + } + + else { + let compressed_file = tool_helper::compress::psx_default::lz4(&tool_helper::read_file(&output_file)?)?; + return tool_helper::write_file(&output_file, compressed_file); + } } + + Ok(()) } fn run_main(cmd: CommandLine) -> Result<(), Error> { diff --git a/src/Tools/tool_helper/src/lib.rs b/src/Tools/tool_helper/src/lib.rs index 44a97326..1e515631 100644 --- a/src/Tools/tool_helper/src/lib.rs +++ b/src/Tools/tool_helper/src/lib.rs @@ -1,6 +1,6 @@ use colored::*; use envmnt::{ExpandOptions, ExpansionType}; -use std::{boxed::Box, io::{BufRead, BufReader, BufWriter, Read, Write}, path::PathBuf, str::FromStr}; +use std::{boxed::Box, fs::OpenOptions, io::{BufRead, BufReader, BufWriter, Read, Write}, path::PathBuf, str::FromStr}; pub mod bits; pub mod compress; @@ -241,6 +241,13 @@ pub fn read_file_to_string(file_path: &PathBuf) -> Result { } } +pub fn write_file(file_path: &PathBuf, data: Vec) -> Result<(), Error> { + let mut file = OpenOptions::new().read(true).write(true).truncate(true).create(true).open(file_path)?; + + file.write_all(data.as_slice())?; + Ok(()) +} + fn create_file_read_error(file_path: &PathBuf, error: std::io::Error) -> Result { Err(Error::from_text(format!("Failed reading file {} with error: \"{}\"", file_path.display(), error))) }