From 725d2f7bb22d68ed43563a08ed3793b665e663f4 Mon Sep 17 00:00:00 2001 From: Jaby Date: Sat, 22 Apr 2023 15:44:21 +0200 Subject: [PATCH] Add colored output to most tools; Make mkoverlay more tolerant for missing overlay files; Make psxcdgen_ex emit a warning when no license file is specified --- src/Tools/cpp_out/Cargo.toml | 2 +- src/Tools/cpp_out/src/main.rs | 6 +++--- src/Tools/jaby_engine_fconv/Cargo.toml | 2 +- src/Tools/jaby_engine_fconv/src/main.rs | 7 +++---- src/Tools/mkoverlay/Cargo.toml | 2 +- src/Tools/mkoverlay/src/main.rs | 23 +++++++++++++++++------ src/Tools/psxcdgen_ex/Cargo.toml | 3 ++- src/Tools/psxcdgen_ex/src/encoder/psx.rs | 4 +++- src/Tools/psxcdgen_ex/src/main.rs | 8 ++++---- src/Tools/tool_helper/Cargo.toml | 3 ++- src/Tools/tool_helper/src/lib.rs | 16 +++++++++++++++- 11 files changed, 52 insertions(+), 24 deletions(-) diff --git a/src/Tools/cpp_out/Cargo.toml b/src/Tools/cpp_out/Cargo.toml index b0964cec..22684519 100644 --- a/src/Tools/cpp_out/Cargo.toml +++ b/src/Tools/cpp_out/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cpp_out" -version = "1.0.1" +version = "1.0.2" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/Tools/cpp_out/src/main.rs b/src/Tools/cpp_out/src/main.rs index e4c8ffc4..78947958 100644 --- a/src/Tools/cpp_out/src/main.rs +++ b/src/Tools/cpp_out/src/main.rs @@ -1,6 +1,7 @@ use clap::{Parser}; -use std::path::PathBuf; use cpp_out::{Configuration, Error, FileType}; +use std::path::PathBuf; +use tool_helper::exit_with_error; #[derive(Parser)] #[clap(about = "Output a file content or stdin to a c++ header/source file", long_about = None)] @@ -48,8 +49,7 @@ fn main() { match run_main() { Ok(_) => (), Err(error) => { - eprintln!("{}", error); - std::process::exit(error.exit_code); + exit_with_error(error) } } } \ No newline at end of file diff --git a/src/Tools/jaby_engine_fconv/Cargo.toml b/src/Tools/jaby_engine_fconv/Cargo.toml index 1f992c77..0877a876 100644 --- a/src/Tools/jaby_engine_fconv/Cargo.toml +++ b/src/Tools/jaby_engine_fconv/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "jaby_engine_fconv" -version = "0.1.2" +version = "0.1.3" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/Tools/jaby_engine_fconv/src/main.rs b/src/Tools/jaby_engine_fconv/src/main.rs index 8db9f729..10b3e819 100644 --- a/src/Tools/jaby_engine_fconv/src/main.rs +++ b/src/Tools/jaby_engine_fconv/src/main.rs @@ -1,7 +1,7 @@ -use jaby_engine_fconv::images::{*}; use clap::{Parser, Subcommand}; +use jaby_engine_fconv::images::*; use std::path::PathBuf; -use tool_helper::Error; +use tool_helper::{Error, exit_with_error}; #[derive(Parser)] #[clap(about = "Converts files to various JabyEngine related file formats", long_about = None)] @@ -69,7 +69,6 @@ fn run_main() -> Result<(), Error> { fn main() { if let Err(error) = run_main() { - eprintln!("{}", error.text); - std::process::exit(error.exit_code); + exit_with_error(error); } } \ No newline at end of file diff --git a/src/Tools/mkoverlay/Cargo.toml b/src/Tools/mkoverlay/Cargo.toml index 5ae6d8a8..ce960207 100644 --- a/src/Tools/mkoverlay/Cargo.toml +++ b/src/Tools/mkoverlay/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mkoverlay" -version = "1.1.0" +version = "1.1.1" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/Tools/mkoverlay/src/main.rs b/src/Tools/mkoverlay/src/main.rs index 315fdfd5..f7fd6224 100644 --- a/src/Tools/mkoverlay/src/main.rs +++ b/src/Tools/mkoverlay/src/main.rs @@ -1,6 +1,7 @@ use clap::Parser; -use tool_helper::{Error, format_if_error, open_input, open_output}; +use mkoverlay::types::OverlayDesc; use std::path::PathBuf; +use tool_helper::{Error, exit_with_error, format_if_error, open_input, open_output}; #[derive(Parser)] #[clap(about = "Creates a linker script and makefile part for the JabyEngine toolchain", long_about = None)] @@ -12,12 +13,22 @@ struct CommandLine { ld_file_output: Option, #[clap(value_parser, help="Input JSON for creating the files")] - input: PathBuf + input: Option +} + +fn parse_input(input: Option) -> Result { + if let Some(input) = input { + let input = format_if_error!(open_input(Some(input)), "Opening input file failed with: {error_text}")?; + format_if_error!(mkoverlay::types::json_reader::read_config(input), "Parsing JSON file failed with: {error_text}") + } + + else { + Ok(OverlayDesc::new()) + } } fn run_main(cmd_line: CommandLine) -> Result<(), Error> { - let input = format_if_error!(open_input(Some(cmd_line.input)), "Opening input file failed with: {error_text}")?; - let input = format_if_error!(mkoverlay::types::json_reader::read_config(input), "Parsing JSON file failed with: {error_text}")?; + 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}")?; @@ -30,11 +41,11 @@ fn main() { Ok(cmd_line) => { match run_main(cmd_line) { Ok(_) => (), - Err(error) => eprintln!("{}", error) + Err(error) => exit_with_error(error) } }, Err(error) => { - eprintln!("{}", error); + exit_with_error(Error::from_error(error)); } } } \ No newline at end of file diff --git a/src/Tools/psxcdgen_ex/Cargo.toml b/src/Tools/psxcdgen_ex/Cargo.toml index 5805d87c..be3566a6 100644 --- a/src/Tools/psxcdgen_ex/Cargo.toml +++ b/src/Tools/psxcdgen_ex/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "psxcdgen_ex" -version = "0.2.0" +version = "0.2.1" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -8,6 +8,7 @@ edition = "2021" [dependencies] cdtypes = {path = "../cdtypes"} clap = {version = "*", features = ["derive"]} +colored = "*" no-comment = "*" paste = "*" roxmltree = "*" diff --git a/src/Tools/psxcdgen_ex/src/encoder/psx.rs b/src/Tools/psxcdgen_ex/src/encoder/psx.rs index bf10aef1..9d492144 100644 --- a/src/Tools/psxcdgen_ex/src/encoder/psx.rs +++ b/src/Tools/psxcdgen_ex/src/encoder/psx.rs @@ -2,6 +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 std::io::{Read, Seek, SeekFrom}; @@ -197,7 +198,8 @@ fn process_system_area(system_area: &SystemArea, sec_writer: &mut dyn SectorWrit } else { - // No license specified - filling it with zeros + // No license specified - filling it with zeros + eprintln!("{}", "WARNING: No license file provided. Some emulators (like No$PSX) will not boot this CD.".yellow()); for _ in 0..SYSTEM_AREA_SECTOR_COUNT { sec_writer.write_cd_xa_data(builder::create_xa_data_zero())?; } diff --git a/src/Tools/psxcdgen_ex/src/main.rs b/src/Tools/psxcdgen_ex/src/main.rs index 0f606515..280d2852 100644 --- a/src/Tools/psxcdgen_ex/src/main.rs +++ b/src/Tools/psxcdgen_ex/src/main.rs @@ -1,7 +1,7 @@ use clap::{Parser, ValueEnum}; use psxcdgen_ex::{encoder::{EncodingFunctions, psx::{calculate_psx_lbas, calculate_psx_length_for, encode_psx_image}}, file_writer::{ImageType, write_image}, config_reader}; -use std::{path::PathBuf, }; -use tool_helper::Error; +use std::{path::PathBuf}; +use tool_helper::{Error, exit_with_error}; #[derive(Parser)] #[clap(about = "Creates an ISO image from a description file", long_about = None)] @@ -57,11 +57,11 @@ fn main() { Ok(cmd_line) => { match run_main(cmd_line) { Ok(_) => (), - Err(error) => eprintln!("{}", error) + Err(error) => exit_with_error(error) } }, Err(error) => { - eprintln!("{}", error); + exit_with_error(Error::from_error(error)) } } } \ No newline at end of file diff --git a/src/Tools/tool_helper/Cargo.toml b/src/Tools/tool_helper/Cargo.toml index 4a0c80a7..eda5d603 100644 --- a/src/Tools/tool_helper/Cargo.toml +++ b/src/Tools/tool_helper/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tool_helper" -version = "0.7.0" +version = "0.8.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -8,6 +8,7 @@ edition = "2021" [dependencies] byteorder = "*" cdtypes = {path = "../cdtypes"} +colored = "*" envmnt = "*" lz4 = "*" paste = "*" \ No newline at end of file diff --git a/src/Tools/tool_helper/src/lib.rs b/src/Tools/tool_helper/src/lib.rs index bc5bc548..59a6075c 100644 --- a/src/Tools/tool_helper/src/lib.rs +++ b/src/Tools/tool_helper/src/lib.rs @@ -1,5 +1,6 @@ -use std::{boxed::Box, io::{BufReader, BufWriter, Read, Write}, path::PathBuf}; +use colored::*; use envmnt::{ExpandOptions, ExpansionType}; +use std::{boxed::Box, io::{BufReader, BufWriter, Read, Write}, path::PathBuf}; pub mod bits; pub mod compress; @@ -79,6 +80,14 @@ impl Error { pub fn ok_or_new(option: Option, error_text: F) -> Result where F: Fn () -> String{ Ok(option.ok_or(Error::from_callback(error_text))?) } + + pub fn print_generic_to_std_err(object: &T) { + eprintln!("{}", format!("ERROR: {}", object).red()); + } + + pub fn print_to_std_err(&self) { + Self::print_generic_to_std_err(self) + } } impl std::fmt::Display for Error { @@ -105,6 +114,11 @@ impl std::convert::From for Error { } } +pub fn exit_with_error(error: Error) { + error.print_to_std_err(); + std::process::exit(error.exit_code); +} + pub fn prefix_if_error(prefix: &str, result: Result) -> Result { match result { Ok(value) => Ok(value),