From 10a38ee28331988683cda52f153a227b9b6a893a Mon Sep 17 00:00:00 2001 From: Jaby Date: Fri, 11 Nov 2022 17:00:10 +0100 Subject: [PATCH] Support command line parameters --- src/Tools/Tools.code-workspace | 2 +- src/Tools/psxcdgen_ex/src/encoder/mod.rs | 8 +++++- .../psxcdgen_ex/src/file_writer/bin_cue.rs | 8 +++--- src/Tools/psxcdgen_ex/src/file_writer/mod.rs | 10 ++++--- src/Tools/psxcdgen_ex/src/main.rs | 26 +++++++++++-------- src/Tools/tool_helper/src/lib.rs | 4 +-- 6 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src/Tools/Tools.code-workspace b/src/Tools/Tools.code-workspace index 2ddd7452..332cbbeb 100644 --- a/src/Tools/Tools.code-workspace +++ b/src/Tools/Tools.code-workspace @@ -78,7 +78,7 @@ { "id": "cargo run args", "type": "pickString", - "options": ["", "--help", "psx bin-cue -o wuff.bin ../Tests/ISO_Planschbecken.xml"], + "options": ["", "--help", "psx bin-cue -o ../Tests/Test_Planschbecken ../Tests/ISO_Planschbecken.xml"], "default": "", "description": "Argument options to pass to cargo run" } diff --git a/src/Tools/psxcdgen_ex/src/encoder/mod.rs b/src/Tools/psxcdgen_ex/src/encoder/mod.rs index c54476c4..b93c953e 100644 --- a/src/Tools/psxcdgen_ex/src/encoder/mod.rs +++ b/src/Tools/psxcdgen_ex/src/encoder/mod.rs @@ -3,4 +3,10 @@ use super::{file_writer::{SectorWriter}, types::{CDDesc, Error}}; pub mod psx; pub mod builder; -pub type EncoderFunction = fn(CDDesc, &mut dyn SectorWriter) -> Result<(), Error>; \ No newline at end of file +pub type LbaCalculatorFunction = fn(&mut CDDesc); +pub type ImageEncoderFunction = fn(CDDesc, &mut dyn SectorWriter) -> Result<(), Error>; + +pub struct EncodingFunctions { + pub lba_calculator: LbaCalculatorFunction, + pub encoder: ImageEncoderFunction +} \ No newline at end of file diff --git a/src/Tools/psxcdgen_ex/src/file_writer/bin_cue.rs b/src/Tools/psxcdgen_ex/src/file_writer/bin_cue.rs index 9e9b6d7e..1046b8cd 100644 --- a/src/Tools/psxcdgen_ex/src/file_writer/bin_cue.rs +++ b/src/Tools/psxcdgen_ex/src/file_writer/bin_cue.rs @@ -10,8 +10,8 @@ pub struct BinCueWriter { } impl BinCueWriter { - pub fn new(writer: T, cue_path: &PathBuf) -> Result { - Ok(BinCueWriter{bin_out: std::boxed::Box::new(writer), cd_time: Time::cd_start(), cue: Self::default_cue(cue_path)?}) + pub fn new(writer: T, bin_path: &PathBuf) -> Result { + Ok(BinCueWriter{bin_out: std::boxed::Box::new(writer), cd_time: Time::cd_start(), cue: Self::default_cue(bin_path)?}) } pub fn write_cue(self, dst: T) -> Result<(), Error> { @@ -29,10 +29,10 @@ impl BinCueWriter { Ok(()) } - fn default_cue(cue_path: &PathBuf) -> Result, Error> { + fn default_cue(bin_path: &PathBuf) -> Result, Error> { let mut cue = Vec::new(); - cue.push(CueSpecifier::File{path: get_file_name_from_path_buf(cue_path, "cue output path")?, format: CueFormat::Binary}); + cue.push(CueSpecifier::File{path: get_file_name_from_path_buf(bin_path, "bin output path")?, format: CueFormat::Binary}); cue.push(CueSpecifier::Track{number: 1, data_type: CueDataType::Mode2_2352}); cue.push(CueSpecifier::Index{number: 1, time: Time::cue_start()}); Ok(cue) diff --git a/src/Tools/psxcdgen_ex/src/file_writer/mod.rs b/src/Tools/psxcdgen_ex/src/file_writer/mod.rs index 3eb4bd9d..bd2ef1ce 100644 --- a/src/Tools/psxcdgen_ex/src/file_writer/mod.rs +++ b/src/Tools/psxcdgen_ex/src/file_writer/mod.rs @@ -2,11 +2,13 @@ pub mod bin_cue; pub use cdtypes::{cd::sector::Sector, types::*}; -use super::{encoder::EncoderFunction, types::CDDesc}; +use super::{encoder::ImageEncoderFunction, types::CDDesc}; use bin_cue::BinCueWriter; +use clap::ValueEnum; use std::path::PathBuf; use tool_helper::{Error, open_output_file}; +#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)] pub enum ImageType { BinCue } @@ -35,7 +37,7 @@ pub trait SectorWriter { fn write(&mut self, sector: Sector) -> Result; } -pub fn write_image(cd_desc: CDDesc, encoder: EncoderFunction, image_type: ImageType, mut output_path: PathBuf) -> Result<(), Error> { +pub fn write_image(cd_desc: CDDesc, encoder: ImageEncoderFunction, image_type: ImageType, mut output_path: PathBuf) -> Result<(), Error> { match image_type { ImageType::BinCue => { output_path.set_extension("bin"); @@ -47,10 +49,10 @@ pub fn write_image(cd_desc: CDDesc, encoder: EncoderFunction, image_type: ImageT cue_output_path }; - let mut writer = BinCueWriter::new(open_output_file(output_path)?, &cue_output_path)?; + let mut writer = BinCueWriter::new(open_output_file(&output_path)?, &output_path)?; encoder(cd_desc, &mut writer)?; - writer.write_cue(open_output_file(cue_output_path)?) + writer.write_cue(open_output_file(&cue_output_path)?) } } } \ No newline at end of file diff --git a/src/Tools/psxcdgen_ex/src/main.rs b/src/Tools/psxcdgen_ex/src/main.rs index 1295306c..1e885f16 100644 --- a/src/Tools/psxcdgen_ex/src/main.rs +++ b/src/Tools/psxcdgen_ex/src/main.rs @@ -1,6 +1,6 @@ use clap::{Parser, ValueEnum}; -use psxcdgen_ex::{encoder::psx::{encode_psx_image, calculate_psx_lbas}, file_writer::{ImageType, write_image}, types::{layout::Layout}, config_reader}; -use std::{path::PathBuf, str::FromStr}; +use psxcdgen_ex::{encoder::{EncodingFunctions, psx::{encode_psx_image, calculate_psx_lbas}}, file_writer::{ImageType, write_image}, types::{layout::Layout}, config_reader}; +use std::{path::PathBuf, }; use tool_helper::Error; #[derive(Parser)] @@ -10,7 +10,7 @@ struct CommandLine { system_type: SystemType, #[clap(value_enum, value_parser)] - output_type: OutputType, + output_type: ImageType, #[clap(short='o')] output_file: PathBuf, @@ -24,13 +24,17 @@ enum SystemType { Psx } -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)] -enum OutputType { - BinCue +impl SystemType { + pub fn get_encoding_functions(self) -> EncodingFunctions { + match self { + SystemType::Psx => EncodingFunctions{lba_calculator: calculate_psx_lbas, encoder: encode_psx_image} + } + } } -fn run_main() -> Result<(), Error> { - let desc = psxcdgen_ex::process(config_reader::parse_xml(std::fs::read_to_string("../Tests/ISO_Planschbecken.xml")?)?, calculate_psx_lbas)?; +fn run_main(cmd_line: CommandLine) -> Result<(), Error> { + let encoding_functions = cmd_line.system_type.get_encoding_functions(); + let desc = psxcdgen_ex::process(config_reader::parse_xml(std::fs::read_to_string(cmd_line.input_file)?)?, encoding_functions.lba_calculator)?; println!("\n<== Planschbecken ==>"); for element in desc.get_memory_layout().iter() { @@ -48,13 +52,13 @@ fn run_main() -> Result<(), Error> { } println!("\n<== Planschbecken ==>\nStart encoding"); - write_image(desc, encode_psx_image, ImageType::BinCue, PathBuf::from_str("planschi")?) + write_image(desc, encoding_functions.encoder, cmd_line.output_type, cmd_line.output_file) } fn main() { match CommandLine::try_parse() { - Ok(_) => { - match run_main() { + Ok(cmd_line) => { + match run_main(cmd_line) { Ok(_) => println!("\n<== Planschbecken End ==>"), Err(error) => println!("{}", error) } diff --git a/src/Tools/tool_helper/src/lib.rs b/src/Tools/tool_helper/src/lib.rs index b5395f4d..2de80d64 100644 --- a/src/Tools/tool_helper/src/lib.rs +++ b/src/Tools/tool_helper/src/lib.rs @@ -100,13 +100,13 @@ impl std::convert::From for Error { } } -pub fn open_output_file(output_path: PathBuf) -> Result, Error> { +pub fn open_output_file(output_path: &PathBuf) -> Result, Error> { Ok(std::io::BufWriter::new(std::fs::File::create(output_path)?)) } pub fn open_output(output_file: Option) -> Result { match output_file { - Some(output_path) => Ok(Box::new(open_output_file(output_path)?)), + Some(output_path) => Ok(Box::new(open_output_file(&output_path)?)), None => Ok(Box::new(BufWriter::new(std::io::stdout()))), } }