From e2218d4d8e1aede974fa33361ebd67f39422cc9a Mon Sep 17 00:00:00 2001 From: Jaby Date: Wed, 16 Nov 2022 03:35:28 +0100 Subject: [PATCH] Write license info from file --- src/Tools/psxcdgen_ex/src/encoder/psx.rs | 56 +++++++++++++++++------- src/Tools/psxcdgen_ex/src/lib.rs | 2 +- src/Tools/psxcdgen_ex/src/types/mod.rs | 8 ++-- src/Tools/tool_helper/src/lib.rs | 11 +++-- 4 files changed, 54 insertions(+), 23 deletions(-) diff --git a/src/Tools/psxcdgen_ex/src/encoder/psx.rs b/src/Tools/psxcdgen_ex/src/encoder/psx.rs index d8e0a625..d55b9d26 100644 --- a/src/Tools/psxcdgen_ex/src/encoder/psx.rs +++ b/src/Tools/psxcdgen_ex/src/encoder/psx.rs @@ -2,6 +2,8 @@ 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 tool_helper::{BufferedInputFile, open_input_file_buffered}; +use std::io::{Read, Seek, SeekFrom}; const ROOT_DIR_NAME:&'static str = "\x00"; const SYSTEM_AREA_SECTOR_COUNT:usize = 16; @@ -76,7 +78,7 @@ pub fn encode_psx_image(cd_desc: CDDesc, sec_writer: &mut dyn SectorWriter) -> R for element in cd_desc.get_memory_layout() { match element { - Layout::SystemArea(system_area) => process_system_area(&system_area.borrow(), sec_writer)?, + Layout::SystemArea(system_area) => process_system_area(&mut system_area.borrow_mut(), sec_writer)?, Layout::PVD(pvd) => process_pvd(&pvd.borrow(), cd_desc.path_table.clone(), cd_desc.root.clone(), vol_sector_count, sec_writer)?, Layout::PathTables(path_table) => process_path_table(&path_table.borrow(), cd_desc.root.clone(), sec_writer)?, Layout::Directory(dir) => process_directory_record(&dir.borrow(), sec_writer)?, @@ -88,7 +90,7 @@ pub fn encode_psx_image(cd_desc: CDDesc, sec_writer: &mut dyn SectorWriter) -> R } fn process_system_area(system_area: &SystemArea, sec_writer: &mut dyn SectorWriter) -> Result<(), Error> { - fn write_license_file(sec_writer: &mut dyn SectorWriter) -> Result<(), Error> { + fn write_license_file(sec_writer: &mut dyn SectorWriter, mut license_file: BufferedInputFile) -> Result<(), Error> { fn write_data_zeros(sec_writer: &mut dyn SectorWriter) -> Result<(), Error> { for _ in 0..4 { sec_writer.write_cd_xa_data(builder::create_xa_data_zero())?; @@ -97,14 +99,37 @@ fn process_system_area(system_area: &SystemArea, sec_writer: &mut dyn SectorWrit Ok(()) } - fn write_license_string(sec_writer: &mut dyn SectorWriter) -> Result<(), Error> { - sec_writer.write_cd_xa_data(builder::create_xa_data_zero())?; + fn write_license_string(sec_writer: &mut dyn SectorWriter, license_file: &mut BufferedInputFile) -> Result<(), Error> { + const LICENSE_STRING_START:u64 = 0x2488; + + let mut license_string_buffer = [0u8; Mode2Form1::DATA_SIZE]; + + license_file.seek(SeekFrom::Start(LICENSE_STRING_START))?; + license_file.read(&mut license_string_buffer)?; + + let sub_mode = SubModeBuilder::new_mode1().create(); + let sector = builder::create_xa_data_for_raw(sub_mode, &license_string_buffer); + + sec_writer.write_cd_xa_data(sector)?; Ok(()) } - fn write_license_logo(sec_writer: &mut dyn SectorWriter) -> Result<(), Error> { + fn write_license_logo(sec_writer: &mut dyn SectorWriter, license_file: &mut BufferedInputFile) -> Result<(), Error> { + const LICENSE_LOGO_START:u64 = 0x2488; + + license_file.seek(SeekFrom::Start(LICENSE_LOGO_START))?; for _ in 0..4 { - sec_writer.write_cd_xa_data(builder::create_xa_data_zero())?; + const LICENSE_SECTOR_REST:i64 = 0x120; + + let mut license_logo_buffer = [0u8; Mode2Form1::DATA_SIZE]; + + license_file.read(&mut license_logo_buffer)?; + license_file.seek(SeekFrom::Current(LICENSE_SECTOR_REST))?; + + let sub_mode = SubModeBuilder::new_mode1().create(); + let sector = builder::create_xa_data_for_raw(sub_mode, &license_logo_buffer); + + sec_writer.write_cd_xa_data(sector)?; } Ok(()) } @@ -124,8 +149,8 @@ fn process_system_area(system_area: &SystemArea, sec_writer: &mut dyn SectorWrit } write_data_zeros(sec_writer)?; - write_license_string(sec_writer)?; - write_license_logo(sec_writer)?; + write_license_string(sec_writer, &mut license_file)?; + write_license_logo(sec_writer, &mut license_file)?; write_audio_zeros(sec_writer) } @@ -134,17 +159,18 @@ fn process_system_area(system_area: &SystemArea, sec_writer: &mut dyn SectorWrit return Err(Error::from_text(format!("System Area required to start at sector 0 of Track - found LBA: {}", system_area_lba))); } - if system_area.license_data.is_empty() { - // No license specified - filling it with zeros + if let Some(license_file) = &system_area.license_file_path { + let license_file = open_input_file_buffered(license_file)?; + write_license_file(sec_writer, license_file) + } + + else { + // No license specified - filling it with zeros for _ in 0..SYSTEM_AREA_SECTOR_COUNT { sec_writer.write_cd_xa_data(builder::create_xa_data_zero())?; } - Ok(()) - } - - else { - write_license_file(sec_writer) + Ok(()) } } diff --git a/src/Tools/psxcdgen_ex/src/lib.rs b/src/Tools/psxcdgen_ex/src/lib.rs index 47d5a6da..741b905d 100644 --- a/src/Tools/psxcdgen_ex/src/lib.rs +++ b/src/Tools/psxcdgen_ex/src/lib.rs @@ -44,7 +44,7 @@ fn parse_configuration(config: config_reader::Configuration) -> Result = Rc>; @@ -56,13 +56,13 @@ impl CDDesc { } pub struct SystemArea { - pub(in super) track_rel_lba: usize, - pub(in super) license_data: Vec, + pub(in super) track_rel_lba: usize, + pub(in super) license_file_path: Option, } impl SystemArea { pub fn new() -> SystemArea { - SystemArea{track_rel_lba: 0, license_data: Vec::new()} + SystemArea{track_rel_lba: 0, license_file_path: None} } } diff --git a/src/Tools/tool_helper/src/lib.rs b/src/Tools/tool_helper/src/lib.rs index 2de80d64..68976a72 100644 --- a/src/Tools/tool_helper/src/lib.rs +++ b/src/Tools/tool_helper/src/lib.rs @@ -3,8 +3,9 @@ use std::{boxed::Box, io::{BufReader, BufWriter, Read, Write}, path::PathBuf}; pub mod bits; pub mod raw; -pub type Output = Box; -pub type Input = Box; +pub type BufferedInputFile = BufReader; +pub type Output = Box; +pub type Input = Box; pub trait ErrorString { fn to_string(self) -> String; @@ -113,11 +114,15 @@ pub fn open_output(output_file: Option) -> Result { pub fn open_input(input_file: Option) -> Result { match input_file { - Some(input_path) => Ok(Box::new(BufReader::new(std::fs::File::open(input_path)?))), + Some(input_path) => Ok(Box::new(open_input_file_buffered(&input_path)?)), None => Ok(Box::new(BufReader::new(std::io::stdin()))), } } +pub fn open_input_file_buffered(input_path: &PathBuf) -> Result { + Ok(BufReader::new(std::fs::File::open(input_path)?)) +} + pub fn os_str_to_string(input: &std::ffi::OsStr, name: &str) -> Result { Ok(Error::ok_or_new(input.to_str(), ||format!("Converting {} to UTF-8 failed", name))?.to_owned()) }