diff --git a/src/Tools/psxcdgen_ex/src/encoder/psx.rs b/src/Tools/psxcdgen_ex/src/encoder/psx.rs index cfe1a61a..f054635a 100644 --- a/src/Tools/psxcdgen_ex/src/encoder/psx.rs +++ b/src/Tools/psxcdgen_ex/src/encoder/psx.rs @@ -1,7 +1,7 @@ use super::{*, SectorWriter, {CDDesc, Error}}; -use super::super::types::{helper::PathTableMember, layout::Layout, *}; +use super::super::types::{helper::{DirectoryRecordMember, PathTableMember}, layout::Layout, *}; use builder::SubModeBuilder; -use cdtypes::types::{cdstring::{AString, DString}, date::*, dir_record::DirectoryRecord, helper::sector_count_mode2_form1, path_table::*, pvd as cd_pvd, lsb_msb::*, sector::Mode2Form1}; +use cdtypes::types::{cdstring::{AString, DString}, date::*, dir_record::*, helper::sector_count_mode2_form1, path_table::*, pvd as cd_pvd, lsb_msb::*, sector::Mode2Form1}; const SYSTEM_AREA_SECTOR_COUNT:usize = 16; const PVD_SECTOR_COUNT:usize = 2; @@ -44,17 +44,17 @@ pub fn calculate_psx_lbas(cd_desc: &mut CDDesc) { }, Layout::Directory(dir) => { - let sector_count = sector_count_mode2_form1(dir.borrow().get_size()); + let sector_count = sector_count_mode2_form1(dir.borrow().properties.borrow().get_extended_size()); let mut dir = dir.borrow_mut(); cd_desc.vol_sector_count += sector_count; - cur_lba = update_lba(&mut dir.properties, cur_lba, sector_count); + cur_lba = update_lba(&mut dir.properties.borrow_mut(), cur_lba, sector_count); }, Layout::File(file) => { let sector_count = { let file = file.borrow(); - let fake_size = file.get_size(); + let fake_size = file.properties.get_extended_size(); sector_count_mode2_form1(fake_size) }; @@ -126,7 +126,7 @@ fn process_pvd(pvd: &PrimaryVolumeDescriptor, path_table: SharedPtr, cd_pvd.path_table_4.write(path_table.get_track_rel_lba_for(4, sector_count_mode2_form1) as u32); //Set Root Directory Record - update_dir_record_with_dir(&mut cd_pvd.root_dir_record, &root_dir)?; + update_dir_record_with_dir(&mut cd_pvd.root_dir_record, &root_dir, None)?; //Set other stuff cd_pvd.publisher_id = AString::from_str(pvd.publisher.as_str())?; @@ -176,18 +176,36 @@ fn process_path_table(path_table: &PathTable, root_dir: SharedPtr, se } fn process_directory_record(dir: &Directory, _sec_writer: &mut dyn SectorWriter) -> Result<(), Error> { - let mut dir_record = vec![0u8; dir.get_content_size()]; - let dir_length = dir_record.len(); - let raw_data = &mut dir_record[0..dir_length]; - - for _member in dir.collect_member() { - let mut _dir_record = unsafe{std::mem::transmute::<&mut u8, &mut DirectoryRecord>(&mut raw_data[0])}; + fn _create_dir_system_use() -> CDXASystemUse { + let mut system_use = CDXASystemUse::default(); + system_use.file_attribute.set_mode2(); + system_use.file_attribute.set_directory(); + system_use } + fn _create_file_system_use() -> CDXASystemUse { + let mut system_use = CDXASystemUse::default(); - Err(Error::not_implemented("process_directory_record")) + system_use.file_attribute.set_mode2(); + system_use + } + + let mut dir_record = vec![0u8; dir.properties.borrow().get_real_size()]; + let dir_length = dir_record.len(); + let mut raw_data = &mut dir_record[0..dir_length]; + + for _member in dir.collect_member() { + let raw_data_len = raw_data.len(); + let bytes_written = { + 0 + }; + + raw_data = &mut raw_data[bytes_written..raw_data_len]; + } + + Ok(()) } fn validate_path_table(path_table: &PathTable) -> Result<(), Error> { @@ -228,9 +246,9 @@ fn update_path_table_entry(path_table_l: &mut PathTableL, path_table_b: &mut Pat Ok(path_table_l.get_size()) } -fn create_dir_record_raw<'a>(dst: &'a mut [u8], name: Option<&str>, track_rel_lba: u32, size_bytes: u32) -> Result<&'a mut DirectoryRecord, Error> { +fn create_dir_record_raw<'a>(dst: &'a mut [u8], name: Option<&str>, track_rel_lba: u32, size_bytes: u32, system_use: Option) -> Result<&'a mut DirectoryRecord, Error> { let name = name.unwrap_or("\x00"); - let bytes_needed = DirectoryRecord::calculate_size_for(name, false); + let bytes_needed = DirectoryRecord::calculate_size_for(name, system_use.is_some()); if dst.len() < bytes_needed { return Err(Error::from_text(format!("DirectoryRecord for entry {} needs {} bytes but {} bytes were provided", name, bytes_needed, dst.len()))); @@ -240,18 +258,31 @@ fn create_dir_record_raw<'a>(dst: &'a mut [u8], name: Option<&str>, track_rel_lb let mut dir_record = std::mem::transmute::<&mut u8, &mut DirectoryRecord>(&mut dst[0]); dir_record.new(name, false); - dir_record.data_block_number.write(track_rel_lba); dir_record.data_size.write(size_bytes); dir_record.time_stamp = SmallDate::now(); + + if let Some(system_use) = system_use { + if let Some(record_system_use) = dir_record.get_cdxa_system_use_mut() { + *record_system_use = system_use; + } + } + Ok(dir_record) } } - -fn update_dir_record_with_dir(dir_record: &mut [u8], dir: &Directory) -> Result<(), Error> { - let dir_record = create_dir_record_raw(dir_record, dir.name.as_str(), dir.properties.track_rel_lba as u32, dir.get_size() as u32)?; +fn update_dir_record_with_dir(dir_record: &mut [u8], dir: &Directory, system_use: Option) -> Result { + let dir_record = create_dir_record_raw(dir_record, dir.name.as_str(), dir.properties.borrow().track_rel_lba as u32, dir.properties.borrow().get_real_size() as u32, system_use)?; dir_record.set_directory(); - Ok(()) + Ok(dir_record.length[0] as usize) +} + +fn _update_dir_record_with_file(_dir_record: &mut [u8], _file: &File, _system_use: Option) -> Result { + Err(Error::not_implemented("update_dir_record_with_file")) + /*let dir_record = create_dir_record_raw(dir_record, dir.name.as_str(), dir.properties.track_rel_lba as u32, dir.get_size() as u32, system_use)?; + + dir_record.set_file(); + Ok(dir_record.length[0] as usize)*/ } \ 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 27fbc547..40b2884b 100644 --- a/src/Tools/psxcdgen_ex/src/main.rs +++ b/src/Tools/psxcdgen_ex/src/main.rs @@ -45,8 +45,8 @@ fn run_main() -> Result<(), Error> { Layout::SystemArea(_) => println!("SystemArea:"), Layout::PVD(_) => println!("PVD:"), Layout::PathTables(_) => println!("PathTables:"), - Layout::Directory(dir) => println!("Dir: {} @{}-{}", dir.borrow().name, dir.borrow().get_track_rel_lba(), dir.borrow().get_size()), - Layout::File(file) => println!("File: {} @{}-{}", file.borrow(), file.borrow().get_track_rel_lba(), file.borrow().get_size()), + Layout::Directory(dir) => println!("Dir: {} @{}-{}", dir.borrow().name, dir.borrow().get_track_rel_lba(), dir.borrow().get_extended_size()), + Layout::File(file) => println!("File: {} @{}-{}", file.borrow(), file.borrow().get_track_rel_lba(), file.borrow().get_extended_size()), } } println!("\n<== Planschbecken ==>"); diff --git a/src/Tools/psxcdgen_ex/src/types/helper.rs b/src/Tools/psxcdgen_ex/src/types/helper.rs index 55e544e0..d1c1d4e4 100644 --- a/src/Tools/psxcdgen_ex/src/types/helper.rs +++ b/src/Tools/psxcdgen_ex/src/types/helper.rs @@ -29,7 +29,7 @@ pub fn collect_path_table_member(root: SharedPtr) -> Vec>, dirs: &Vec< } for dir in dirs { - if !dir.borrow().properties.is_hidden { + if !dir.borrow().properties.borrow().is_hidden { if let Some(name) = dir.borrow().name.as_string() { collection.push(DirectoryRecordMember::Directory{name}); } diff --git a/src/Tools/psxcdgen_ex/src/types/mod.rs b/src/Tools/psxcdgen_ex/src/types/mod.rs index 6363b354..7769866f 100644 --- a/src/Tools/psxcdgen_ex/src/types/mod.rs +++ b/src/Tools/psxcdgen_ex/src/types/mod.rs @@ -108,36 +108,34 @@ impl PrimaryVolumeDescriptor { } pub struct Directory { - pub name: DirectoryName, - pub properties: Properties, - size_bytes: usize, - files: Vec>, - dirs: Vec> + pub name: DirectoryName, + pub properties: SharedPtr, + parent_properties: Option>, + files: Vec>, + dirs: Vec> } impl Directory { pub fn new(dir_name: &str) -> Result { - Ok(Directory{name: DirectoryName::from_str(dir_name)?, properties: Properties::default(), size_bytes: 0, files: Vec::new(), dirs: Vec::new()}) + Ok(Directory{name: DirectoryName::from_str(dir_name)?, properties: new_shared_ptr(Properties::default()), parent_properties: None, files: Vec::new(), dirs: Vec::new()}) } - pub fn add_dir(&mut self, dir: Directory) { + pub fn add_dir(&mut self, mut dir: Directory) { + dir.parent_properties = Some(self.properties.clone()); self.dirs.push(new_shared_ptr(dir)); } - pub fn add_file(&mut self, file: File) { + pub fn add_file(&mut self, mut file: File) { + file.parent_properties = Some(self.properties.clone()); self.files.push(new_shared_ptr(file)); } pub fn get_track_rel_lba(&self) -> usize { - self.properties.track_rel_lba + self.properties.borrow().track_rel_lba } - pub fn get_size(&self) -> usize { - self.properties.get_size(self.size_bytes) - } - - pub(super) fn get_content_size(&self) -> usize { - self.size_bytes + pub fn get_extended_size(&self) -> usize { + self.properties.borrow().get_extended_size() } pub(super) fn collect_member(&self) -> Vec{ @@ -151,7 +149,7 @@ impl Directory { size_bytes += DirectoryRecord::calculate_size_for(member.get_name().as_str(), true); } - self.size_bytes = size_bytes; + self.properties.borrow_mut().size_bytes = size_bytes; } } @@ -166,28 +164,27 @@ enum FileType { } pub struct File { - pub name: FileName, - pub properties: Properties, - content: FileType + pub name: FileName, + pub properties: Properties, + parent_properties: Option>, + _content: FileType } impl File { pub fn new_regular(file_name: &str, content: Vec) -> Result { - Ok(File{name: FileName::from_str(file_name)?, properties: Properties::default(), content: FileType::Regular(content)}) + let content_size = content.len(); + let mut file = File{name: FileName::from_str(file_name)?, properties: Properties::default(), parent_properties: None, _content: FileType::Regular(content)}; + + file.properties.size_bytes = content_size; + Ok(file) } pub fn get_track_rel_lba(&self) -> usize { self.properties.track_rel_lba } - pub fn get_size(&self) -> usize { - self.properties.get_size(self.get_content_size()) - } - - pub(super) fn get_content_size(&self) -> usize { - match &self.content { - FileType::Regular(content) => content.len() - } + pub fn get_extended_size(&self) -> usize { + self.properties.get_extended_size() } } @@ -265,26 +262,31 @@ impl std::fmt::Display for FileName { } pub struct Properties { - pub(in super) track_rel_lba: usize, - pub(in super) overwrite_size_bytes: Option, - pub(in super) is_hidden: bool + pub(super) track_rel_lba: usize, + pub(super) size_bytes: usize, + pub(super) overwrite_size_bytes: Option, + pub(super) is_hidden: bool } impl Properties { - pub(super) fn get_size(&self, content_size: usize) -> usize { + pub(super) fn get_extended_size(&self) -> usize { if let Some(forced_bytes) = self.overwrite_size_bytes { forced_bytes } else { - content_size + self.size_bytes } } + + pub(super) fn get_real_size(&self) -> usize { + self.size_bytes + } } impl Default for Properties { fn default() -> Self { - Properties{track_rel_lba: 0, overwrite_size_bytes: None, is_hidden: false} + Properties{track_rel_lba: 0, size_bytes: 0, overwrite_size_bytes: None, is_hidden: false} } }