From a2d628d190735350f91398391a4b5e3af33aa8bd Mon Sep 17 00:00:00 2001 From: jaby Date: Tue, 11 Oct 2022 21:12:28 +0200 Subject: [PATCH] Calculate size for DirectoryRecords --- src/Tools/psxcdgen_ex/src/main.rs | 6 +-- src/Tools/psxcdgen_ex/src/types/helper.rs | 4 +- src/Tools/psxcdgen_ex/src/types/mod.rs | 46 ++++++++++++++--------- 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/src/Tools/psxcdgen_ex/src/main.rs b/src/Tools/psxcdgen_ex/src/main.rs index 34309ab3..a09807ec 100644 --- a/src/Tools/psxcdgen_ex/src/main.rs +++ b/src/Tools/psxcdgen_ex/src/main.rs @@ -24,7 +24,7 @@ fn populate() -> Result { desc.root.add_file(file); desc.root.add_file(file2); - desc.update_dir_records(); + desc.calculate_lbas(); Ok(desc) } @@ -36,8 +36,8 @@ fn run_main() -> Result<(), Error> { match element { Layout::SystemArea(_) => println!("SystemArea:"), Layout::PVD(_) => println!("PVD:"), - Layout::Directory{name, properties} => println!("Dir: {} @{}", name, properties.lba), - Layout::File(file) => println!("File: {} @{}", file, file.properties.lba), + Layout::Directory{name, properties} => println!("Dir: {} @{}-{}", name, properties.lba, properties.overwrite_size_bytes.unwrap_or(0)), + Layout::File(file) => println!("File: {} @{}-{}", file, file.properties.lba, file.properties.overwrite_size_bytes.unwrap_or(0)), } } Ok(()) diff --git a/src/Tools/psxcdgen_ex/src/types/helper.rs b/src/Tools/psxcdgen_ex/src/types/helper.rs index 5ad7120d..5f5a6098 100644 --- a/src/Tools/psxcdgen_ex/src/types/helper.rs +++ b/src/Tools/psxcdgen_ex/src/types/helper.rs @@ -17,6 +17,8 @@ impl DirectoryRecordMember { pub fn collect_directory_record_member(files: &Vec, dirs: &Vec) -> Vec { let mut collection = Vec::new(); + collection.push(DirectoryRecordMember::Directory{name: "\x00".to_owned()}); + collection.push(DirectoryRecordMember::Directory{name: "\x01".to_owned()}); for file in files { if !file.properties.is_hidden { if let Some(name) = file.name.as_string() { @@ -35,4 +37,4 @@ pub fn collect_directory_record_member(files: &Vec, dirs: &Vec) collection.sort_by(|a, b| {a.get_name().cmp(b.get_name())}); collection -} \ No newline at end of file +} diff --git a/src/Tools/psxcdgen_ex/src/types/mod.rs b/src/Tools/psxcdgen_ex/src/types/mod.rs index 19229a6c..33165665 100644 --- a/src/Tools/psxcdgen_ex/src/types/mod.rs +++ b/src/Tools/psxcdgen_ex/src/types/mod.rs @@ -1,7 +1,7 @@ mod helper; pub mod layout; -use cdtypes::types::cdstring::DString; +use cdtypes::types::{cdstring::DString, dir_record::DirectoryRecord}; use tool_helper::Error; pub struct CDDesc { @@ -26,16 +26,17 @@ impl CDDesc { layout::DefaultLayoutMut::new(self) } - pub fn update_dir_records(&mut self) { - fn update_dir_record_of(dir: &mut Directory) { - dir.update_content(); - - for sub_dir in &mut dir.dirs { - update_dir_record_of(sub_dir); - } - } + pub fn calculate_lbas(&mut self) { + Self::for_each_dir_mut(&mut self.root, &|dir| {dir.update_content_size();}); - update_dir_record_of(&mut self.root); + // Now layout iterate? + } + + fn for_each_dir_mut(dir: &mut Directory, function: &T) { + function(dir); + for sub_dir in &mut dir.dirs { + Self::for_each_dir_mut(sub_dir, function); + } } } @@ -77,7 +78,18 @@ impl Directory { self.files.push(file); } - fn update_content(&mut self) { + fn update_content_size(&mut self) { + let dir_member = helper::collect_directory_record_member(&self.files, &self.dirs); + let mut size_bytes = 0; + + for member in dir_member { + size_bytes += DirectoryRecord::calculate_size_for(member.get_name().as_str(), true); + } + + self.properties.overwrite_size_bytes = Some(size_bytes); + } + + fn _update_content(&mut self) { let dir_member = helper::collect_directory_record_member(&self.files, &self.dirs); println!("{} updating content", self.name.as_string().unwrap_or("".to_owned())); @@ -96,12 +108,12 @@ impl std::fmt::Display for Directory { pub struct File { pub name: FileName, pub properties: Properties, - content: Vec + _content: Vec } impl File { pub fn new(file_name: &str) -> Result { - Ok(File{name: FileName::from_str(file_name)?, properties: Properties::default(), content: Vec::new()}) + Ok(File{name: FileName::from_str(file_name)?, properties: Properties::default(), _content: Vec::new()}) } } @@ -179,14 +191,14 @@ impl std::fmt::Display for FileName { } pub struct Properties { - pub lba: usize, - pub overwrite_size_sectors: Option, - pub is_hidden: bool + pub lba: usize, + pub overwrite_size_bytes: Option, + pub is_hidden: bool } impl Default for Properties { fn default() -> Self { - Properties{lba: 0, overwrite_size_sectors: None, is_hidden: false} + Properties{lba: 0, overwrite_size_bytes: None, is_hidden: false} } }