Make fconv delete faulty files

This commit is contained in:
Jaby Blubb 2023-10-12 14:03:44 +02:00
parent 7ceb13863e
commit e37321de89
11 changed files with 1014 additions and 995 deletions

View File

@ -35,7 +35,7 @@ fn configurate(cmd: &mut CommandLine) -> Result<Configuration, Error> {
fn run_main(mut cmd: CommandLine) -> Result<(), Error> {
let cfg = configurate(&mut cmd)?;
let input = tool_helper::open_input(cmd.input_file)?;
let output = tool_helper::open_output(Some(cmd.output_file))?;
let output = tool_helper::open_output(&Some(cmd.output_file))?;
return cpp_out::convert(cfg, input, output);
}

View File

@ -1,6 +1,6 @@
[package]
name = "jaby_engine_fconv"
version = "0.1.4"
version = "0.1.5"
edition = "2021"
[profile.release]

View File

@ -64,9 +64,7 @@ fn modify_palette(mut image: IndexedImage, clut_align: ClutAlignment, semi_trans
fn encode<T: PSXImageConverter>(image: T, color_depth: ColorType, clut_align: ClutAlignment, output: &mut dyn Write) -> Result<(), Error> {
let (width, height) = {
fn return_error(clut_type: u32, div: u32, width: u16, height: u16) -> Result<(u16, u16), Error> {
return Err(Error::from_callback(|| {format!("CLUT {} images require a width divideable by {} (found width: {}/{}={}, height: {}/{}={})", clut_type, div,
width, div, (width as f32/div as f32),
height, div, (height as f32/div as f32))}));
return Err(Error::from_callback(|| {format!("CLUT {} images require a width divideable by {} (found width: {}/{}={}, height: {})", clut_type, div, width, div, (width as f32/div as f32), height)}));
}
let width = image.width();

View File

@ -1 +1,2 @@
pub mod images;
pub mod nothing;

View File

@ -1,5 +1,5 @@
use clap::{Parser, Subcommand};
use jaby_engine_fconv::images::*;
use jaby_engine_fconv::{images::*, nothing};
use std::path::PathBuf;
use tool_helper::{Error, exit_with_error};
@ -28,7 +28,7 @@ enum SubCommands {
fn run_main(cmd: CommandLine) -> Result<(), Error> {
let mut 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 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
@ -39,13 +39,23 @@ fn run_main(cmd: CommandLine) -> Result<(), Error> {
}
};
let cmd_result: Result<(), Error> = {
match cmd.sub_command {
SubCommands::Nothing => {
std::io::copy(&mut input, dst_buffer)?;
},
SubCommands::SimpleTIM(args) => {
reduced_tim::convert(args, input, dst_buffer)?;
SubCommands::Nothing => nothing::copy(&mut input, dst_buffer),
SubCommands::SimpleTIM(args) => reduced_tim::convert(args, input, dst_buffer)
}
};
if let Err(cmd_error) = cmd_result {
if let Some(file_path) = cmd.output_file {
let _result = std::fs::remove_file(file_path);
}
else {
tool_helper::print_warning("Open stream detected! Incomplete file can not be deleted".to_owned());
}
return Err(cmd_error);
}
// We encoded the file to a temporary buffer and now need to write it

View File

@ -0,0 +1,7 @@
use std::io::Write;
use tool_helper::{Error, Input};
pub fn copy(input: &mut Input, output: &mut dyn Write) -> Result<(), Error> {
std::io::copy(input, output)?;
Ok(())
}

View File

@ -29,8 +29,8 @@ fn parse_input(input: Option<PathBuf>) -> Result<OverlayDesc, Error> {
fn run_main(cmd_line: CommandLine) -> Result<(), Error> {
let input = parse_input(cmd_line.input)?;
let mut mk_output = format_if_error!(open_output(cmd_line.mk_file_output), "Opening file for writing makefile failed with: {error_text}")?;
let mut ld_output = format_if_error!(open_output(cmd_line.ld_file_output), "Opening file for writing linkerfile failed with: {error_text}")?;
let mut mk_output = format_if_error!(open_output(&cmd_line.mk_file_output), "Opening file for writing makefile failed with: {error_text}")?;
let mut ld_output = format_if_error!(open_output(&cmd_line.ld_file_output), "Opening file for writing linkerfile failed with: {error_text}")?;
format_if_error!(mkoverlay::creator::makefile::write(&mut mk_output, &input), "Writing makefile failed with: {error_text}")?;
format_if_error!(mkoverlay::creator::ldscript::write(&mut ld_output, &input), "Writing ld script failed with: {error_text}")

View File

@ -2,8 +2,7 @@ use super::{*, SectorWriter, {CDDesc, Error}};
use super::super::types::{helper::{DirectoryRecordMember, PathTableMember}, layout::Layout, *};
use builder::SubModeBuilder;
use cdtypes::types::{cdstring::{AString, DString}, date::*, dir_record::*, helper::{round_bytes_mode2_form1, sector_count_mode2_form1}, path_table::*, pvd as cd_pvd, lsb_msb::*, sector::Mode2Form1};
use colored::*;
use tool_helper::{BufferedInputFile, format_if_error, open_input_file_buffered};
use tool_helper::{BufferedInputFile, format_if_error, open_input_file_buffered, print_warning};
use std::io::{Read, Seek, SeekFrom};
const ROOT_DIR_NAME:&'static str = "\x00";
@ -199,7 +198,7 @@ fn process_system_area(system_area: &SystemArea, sec_writer: &mut dyn SectorWrit
else {
// No license specified - filling it with zeros
eprintln!("{}", "WARNING: No license file provided. Some emulators (like No$PSX) will not boot this CD.".yellow());
print_warning("WARNING: No license file provided. Some emulators (like No$PSX) will not boot this CD.".to_owned());
for _ in 0..SYSTEM_AREA_SECTOR_COUNT {
sec_writer.write_cd_xa_data(builder::create_xa_data_zero())?;
}

View File

@ -44,7 +44,7 @@ fn run_main(cmd_line: CommandLine) -> Result<(), Error> {
write_image(&desc, encoding_functions.encoder, cmd_line.output_type, cmd_line.output_file)?;
if let Some(list_content_option) = cmd_line.list_content {
psxcdgen_ex::dump_content(&desc, tool_helper::open_output(list_content_option)?)
psxcdgen_ex::dump_content(&desc, tool_helper::open_output(&list_content_option)?)
}
else {

View File

@ -36,7 +36,7 @@ pub fn main() {
fn run_main(cmd: CommandLine) -> Result<(), Error> {
if cmd.raw_dump {
psxreadmap::get_tool_output(cmd.use_wsl, cmd.input, open_output(cmd.output)?)?;
psxreadmap::get_tool_output(cmd.use_wsl, cmd.input, open_output(&cmd.output)?)?;
return Ok(());
}
@ -59,7 +59,7 @@ fn run_main(cmd: CommandLine) -> Result<(), Error> {
fn dump_memory_map(output: Option<PathBuf>, memory_map: &readmap::types::MemoryMap) -> Result<(), Error> {
if let Some(output) = output {
let output = tool_helper::open_output(Some(output))?;
let output = tool_helper::open_output(&Some(output))?;
readmap::dump::write(output, &memory_map)?;
}

View File

@ -125,6 +125,10 @@ pub fn exit_with_error(error: Error) {
std::process::exit(error.exit_code);
}
pub fn print_warning(str: String) {
eprintln!("{}", str.yellow());
}
pub fn prefix_if_error<T>(prefix: &str, result: Result<T, Error>) -> Result<T, Error> {
match result {
Ok(value) => Ok(value),
@ -166,7 +170,7 @@ pub fn open_output_file(output_path: &PathBuf) -> Result<BufWriter<std::fs::File
Ok(std::io::BufWriter::new(std::fs::File::create(output_path)?))
}
pub fn open_output(output_file: Option<PathBuf>) -> Result<Output, Error> {
pub fn open_output(output_file: &Option<PathBuf>) -> Result<Output, Error> {
match output_file {
Some(output_path) => Ok(Box::new(open_output_file(&output_path)?)),
None => Ok(Box::new(BufWriter::new(std::io::stdout()))),