From 3c8e942fc4449189e9ad78f233f854058e1bfe02 Mon Sep 17 00:00:00 2001 From: Jaby Date: Wed, 12 Oct 2022 21:11:51 +0200 Subject: [PATCH] Calculate LBAs for PathTables --- src/Tools/psxcdgen_ex/src/types/helper.rs | 37 +++++++++++++++++++++-- src/Tools/psxcdgen_ex/src/types/mod.rs | 36 +++++++++++++++++----- 2 files changed, 64 insertions(+), 9 deletions(-) diff --git a/src/Tools/psxcdgen_ex/src/types/helper.rs b/src/Tools/psxcdgen_ex/src/types/helper.rs index 5f5a6098..733526c0 100644 --- a/src/Tools/psxcdgen_ex/src/types/helper.rs +++ b/src/Tools/psxcdgen_ex/src/types/helper.rs @@ -1,5 +1,13 @@ use super::*; +const CURRENT_DIR_NAME:&'static str = "/x00"; +const PARENT_DIR_NAME:&'static str = "/x01"; + +pub struct PathTableMember { + pub name: String, + pub parent_table_id: usize, +} + pub enum DirectoryRecordMember { Directory{name: String}, File{name: String}, @@ -14,11 +22,36 @@ impl DirectoryRecordMember { } } +pub fn collect_path_table_member(root: &Directory) -> Vec { + fn collect_path_table_for(collection: &mut Vec, dirs: &Vec, parent_id: usize) { + let mut cur_dirs = Vec::new(); + + for dir in dirs { + cur_dirs.push(PathTableMember{name: dir.name.as_string().unwrap_or("".to_owned()), parent_table_id: parent_id}); + } + + cur_dirs.sort_by(|a, b| { + a.name.cmp(&b.name) + }); + + collection.append(&mut cur_dirs); + + for dir in dirs { + collect_path_table_for(collection, &dir.dirs, parent_id + 1); + } + } + let mut collection = Vec::new(); + + collection.push(PathTableMember{name: CURRENT_DIR_NAME.to_owned(), parent_table_id: 1}); + collect_path_table_for(&mut collection, &root.dirs, 1); + collection +} + 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()}); + collection.push(DirectoryRecordMember::Directory{name: CURRENT_DIR_NAME.to_owned()}); + collection.push(DirectoryRecordMember::Directory{name: PARENT_DIR_NAME.to_owned()}); for file in files { if !file.properties.is_hidden { if let Some(name) = file.name.as_string() { diff --git a/src/Tools/psxcdgen_ex/src/types/mod.rs b/src/Tools/psxcdgen_ex/src/types/mod.rs index fe3ff7a8..f3f6e84c 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, dir_record::DirectoryRecord, helper::sector_count_mode2_form1, sector::*}; +use cdtypes::types::{cdstring::DString, dir_record::DirectoryRecord, helper::{round_bytes_mode2_form1, sector_count_mode2_form1}, sector::*, path_table::PathTableL}; use layout::LayoutMut; use tool_helper::Error; @@ -30,18 +30,37 @@ impl CDDesc { } pub fn calculate_lbas(&mut self) { - let mut cur_lba = 0; + let mut path_table_properties = { + let mut size_bytes = 0; + + helper::collect_path_table_member(&self.root).into_iter().for_each(|element| { + println!("PT: {} ^{}", element.name, element.parent_table_id); + size_bytes += PathTableL::calculate_size_for(element.name.as_ref()); + }); + + size_bytes = round_bytes_mode2_form1(size_bytes)*4; + Properties{lba: 0, overwrite_size_bytes: Some(size_bytes), is_hidden: false} + }; + let mut cur_lba = 0; Self::for_each_dir_mut(&mut self.root, &|dir| {dir.update_content_size();}); // Now layout iterate? for element in self.get_memory_layout_mut() { + fn update_lba(properties: &mut Properties, cur_lba: usize, content_size: usize) -> usize { + properties.lba = cur_lba; + cur_lba + properties.sector_count_xa_data(content_size) + } + match element { LayoutMut::SystemArea(system_area) => { - let system_area_props = &mut system_area.properties; - - system_area_props.lba = cur_lba; - cur_lba += system_area_props.sector_count_xa_data(Self::DEFAULT_DATA_SIZE_FOR_NOW); + cur_lba = update_lba(&mut system_area.properties, cur_lba, Self::DEFAULT_DATA_SIZE_FOR_NOW); + }, + LayoutMut::PVD(pvd) => { + cur_lba = update_lba(&mut pvd.properties, cur_lba, Self::DEFAULT_DATA_SIZE_FOR_NOW); + }, + LayoutMut::PathTables => { + cur_lba = update_lba(&mut path_table_properties, cur_lba, Self::DEFAULT_DATA_SIZE_FOR_NOW); }, _ => () } @@ -71,11 +90,14 @@ impl SystemArea { } pub struct PrimaryVolumeDescriptor { + pub properties: Properties } impl PrimaryVolumeDescriptor { + const DEFAULT_SIZE:usize = (2*Mode2Form1::DATA_SIZE); + pub fn new() -> PrimaryVolumeDescriptor { - PrimaryVolumeDescriptor{} + PrimaryVolumeDescriptor{properties: Properties{lba: 0, overwrite_size_bytes: Some(Self::DEFAULT_SIZE), is_hidden: false}} } }