Changed code to have elements calculate itself

This commit is contained in:
Jaby 2022-10-17 20:10:37 +02:00 committed by Jaby
parent 349a678ee7
commit df7c56bf9b
1 changed files with 38 additions and 18 deletions

View File

@ -21,8 +21,6 @@ pub struct CDDesc {
} }
impl CDDesc { impl CDDesc {
const DEFAULT_DATA_SIZE_FOR_NOW:usize = Mode2Form1::DATA_SIZE;
pub fn new() -> CDDesc { pub fn new() -> CDDesc {
match Directory::new("root") { match Directory::new("root") {
Ok(root) => CDDesc{system_area: new_shared_ptr(SystemArea::new()), pvd: new_shared_ptr(PrimaryVolumeDescriptor::new()), root: new_shared_ptr(root)}, Ok(root) => CDDesc{system_area: new_shared_ptr(SystemArea::new()), pvd: new_shared_ptr(PrimaryVolumeDescriptor::new()), root: new_shared_ptr(root)},
@ -47,7 +45,7 @@ impl CDDesc {
} }
pub fn calculate_lbas(&mut self) { pub fn calculate_lbas(&mut self) {
let mut path_table_properties = { let (mut path_table_properties, path_table_sector_count) = {
let mut size_bytes = 0; let mut size_bytes = 0;
helper::collect_path_table_member(self.root.clone()).into_iter().for_each(|element| { helper::collect_path_table_member(self.root.clone()).into_iter().for_each(|element| {
@ -56,7 +54,7 @@ impl CDDesc {
}); });
size_bytes = round_bytes_mode2_form1(size_bytes)*4; size_bytes = round_bytes_mode2_form1(size_bytes)*4;
Properties{lba: 0, overwrite_size_bytes: Some(size_bytes), is_hidden: false} (Properties{lba: 0, overwrite_size_bytes: None, is_hidden: false}, sector_count_mode2_form1(size_bytes))
}; };
let mut cur_lba = 0; let mut cur_lba = 0;
@ -64,30 +62,34 @@ impl CDDesc {
// Now layout iterate? // Now layout iterate?
for element in self.get_memory_layout() { for element in self.get_memory_layout() {
fn update_lba(properties: &mut Properties, cur_lba: usize, content_size: usize) -> usize { fn update_lba(properties: &mut Properties, cur_lba: usize, content_sector_size: usize) -> usize {
properties.lba = cur_lba; properties.lba = cur_lba;
cur_lba + properties.sector_count_xa_data(content_size) cur_lba + content_sector_size
} }
match element { match element {
Layout::SystemArea(system_area) => { Layout::SystemArea(system_area) => {
let mut system_area = system_area.borrow_mut(); let mut system_area = system_area.borrow_mut();
cur_lba = update_lba(&mut system_area.properties, cur_lba, Self::DEFAULT_DATA_SIZE_FOR_NOW); cur_lba = update_lba(&mut system_area.properties, cur_lba, SystemArea::get_sector_count());
}, },
Layout::PVD(pvd) => { Layout::PVD(pvd) => {
let mut pvd = pvd.borrow_mut(); let mut pvd = pvd.borrow_mut();
cur_lba = update_lba(&mut pvd.properties, cur_lba, Self::DEFAULT_DATA_SIZE_FOR_NOW); cur_lba = update_lba(&mut pvd.properties, cur_lba, PrimaryVolumeDescriptor::get_sector_count());
}, },
Layout::PathTables => { Layout::PathTables => {
cur_lba = update_lba(&mut path_table_properties, cur_lba, Self::DEFAULT_DATA_SIZE_FOR_NOW); cur_lba = update_lba(&mut path_table_properties, cur_lba, path_table_sector_count);
}, },
Layout::Directory(dir) => { Layout::Directory(dir) => {
let mut dir = dir.borrow_mut(); let sector_count = dir.borrow().get_sector_count();
cur_lba = update_lba(&mut dir.properties, cur_lba, Self::DEFAULT_DATA_SIZE_FOR_NOW) let mut dir = dir.borrow_mut();
cur_lba = update_lba(&mut dir.properties, cur_lba, sector_count)
}, },
Layout::File(file) => { Layout::File(file) => {
let mut file = file.borrow_mut(); let sector_count = file.borrow().get_sector_count();
cur_lba = update_lba(&mut file.properties, cur_lba, Self::DEFAULT_DATA_SIZE_FOR_NOW); let mut file = file.borrow_mut();
cur_lba = update_lba(&mut file.properties, cur_lba, sector_count);
} }
} }
@ -110,11 +112,16 @@ pub struct SystemArea {
} }
impl SystemArea { impl SystemArea {
const DEFAULT_SIZE:usize = (16*Mode2Form1::DATA_SIZE); const SECTOR_COUNT:usize = 16;
const DEFAULT_SIZE:usize = (Self::SECTOR_COUNT*Mode2Form1::DATA_SIZE);
pub fn new() -> SystemArea { pub fn new() -> SystemArea {
SystemArea{properties: Properties{lba: 0, overwrite_size_bytes: Some(Self::DEFAULT_SIZE), is_hidden: false}} SystemArea{properties: Properties{lba: 0, overwrite_size_bytes: Some(Self::DEFAULT_SIZE), is_hidden: false}}
} }
fn get_sector_count() -> usize {
Self::SECTOR_COUNT
}
} }
pub struct PrimaryVolumeDescriptor { pub struct PrimaryVolumeDescriptor {
@ -122,11 +129,16 @@ pub struct PrimaryVolumeDescriptor {
} }
impl PrimaryVolumeDescriptor { impl PrimaryVolumeDescriptor {
const DEFAULT_SIZE:usize = (2*Mode2Form1::DATA_SIZE); const SECTOR_COUNT:usize = 2;
const DEFAULT_SIZE:usize = (Self::SECTOR_COUNT*Mode2Form1::DATA_SIZE);
pub fn new() -> PrimaryVolumeDescriptor { pub fn new() -> PrimaryVolumeDescriptor {
PrimaryVolumeDescriptor{properties: Properties{lba: 0, overwrite_size_bytes: Some(Self::DEFAULT_SIZE), is_hidden: false}} PrimaryVolumeDescriptor{properties: Properties{lba: 0, overwrite_size_bytes: Some(Self::DEFAULT_SIZE), is_hidden: false}}
} }
fn get_sector_count() -> usize {
Self::SECTOR_COUNT
}
} }
pub struct Directory { pub struct Directory {
@ -168,6 +180,10 @@ impl Directory {
println!(">>> {}", member.get_name()); println!(">>> {}", member.get_name());
} }
} }
fn get_sector_count(&self) -> usize {
self.properties.sector_count_xa_data(0)
}
} }
impl std::fmt::Display for Directory { impl std::fmt::Display for Directory {
@ -186,6 +202,10 @@ impl File {
pub fn new(file_name: &str) -> Result<File, Error> { pub fn new(file_name: &str) -> Result<File, Error> {
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()})
} }
pub fn get_sector_count(&self) -> usize {
self.properties.sector_count_xa_data(0)
}
} }
impl std::fmt::Display for File { impl std::fmt::Display for File {