From 74ef109c73384a8b04122e7d00128dd84dc3ab66 Mon Sep 17 00:00:00 2001 From: Jaby Date: Sun, 16 Oct 2022 15:58:38 +0200 Subject: [PATCH] Inital setup --- src/Tools/psxcdgen_ex/src/main.rs | 10 +++--- src/Tools/psxcdgen_ex/src/types/helper.rs | 17 ++++++---- src/Tools/psxcdgen_ex/src/types/layout.rs | 31 ++++++++++-------- src/Tools/psxcdgen_ex/src/types/mod.rs | 39 +++++++++++++---------- 4 files changed, 55 insertions(+), 42 deletions(-) diff --git a/src/Tools/psxcdgen_ex/src/main.rs b/src/Tools/psxcdgen_ex/src/main.rs index 61563b31..52a7cfa6 100644 --- a/src/Tools/psxcdgen_ex/src/main.rs +++ b/src/Tools/psxcdgen_ex/src/main.rs @@ -20,9 +20,9 @@ fn populate() -> Result { folder }; - desc.root.add_dir(folder); - desc.root.add_file(file); - desc.root.add_file(file2); + desc.root.borrow_mut().add_dir(folder); + desc.root.borrow_mut().add_file(file); + desc.root.borrow_mut().add_file(file2); desc.calculate_lbas(); Ok(desc) @@ -37,8 +37,8 @@ fn run_main() -> Result<(), Error> { Layout::SystemArea(_) => println!("SystemArea:"), Layout::PVD(_) => println!("PVD:"), Layout::PathTables => println!("PathTables:"), - 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)), + Layout::Directory(dir) => println!("Dir: {} @{}-{}", dir.borrow().name, dir.borrow().properties.lba, dir.borrow().properties.overwrite_size_bytes.unwrap_or(0)), + Layout::File(file) => println!("File: {} @{}-{}", file.borrow(), file.borrow().properties.lba, file.borrow().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 733526c0..67bd8339 100644 --- a/src/Tools/psxcdgen_ex/src/types/helper.rs +++ b/src/Tools/psxcdgen_ex/src/types/helper.rs @@ -1,4 +1,5 @@ use super::*; +use std::{cell::RefCell, rc::Rc}; const CURRENT_DIR_NAME:&'static str = "/x00"; const PARENT_DIR_NAME:&'static str = "/x01"; @@ -22,12 +23,12 @@ impl DirectoryRecordMember { } } -pub fn collect_path_table_member(root: &Directory) -> Vec { - fn collect_path_table_for(collection: &mut Vec, dirs: &Vec, parent_id: usize) { +pub fn collect_path_table_member(root: Rc>) -> 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.push(PathTableMember{name: dir.borrow().name.as_string().unwrap_or("".to_owned()), parent_table_id: parent_id}); } cur_dirs.sort_by(|a, b| { @@ -37,22 +38,24 @@ pub fn collect_path_table_member(root: &Directory) -> Vec { collection.append(&mut cur_dirs); for dir in dirs { - collect_path_table_for(collection, &dir.dirs, parent_id + 1); + collect_path_table_for(collection, &dir.borrow().dirs, parent_id + 1); } } let mut collection = Vec::new(); + let root = root.borrow(); 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 { +pub fn collect_directory_record_member(files: &Vec>>, dirs: &Vec>>) -> Vec { let mut collection = Vec::new(); collection.push(DirectoryRecordMember::Directory{name: CURRENT_DIR_NAME.to_owned()}); collection.push(DirectoryRecordMember::Directory{name: PARENT_DIR_NAME.to_owned()}); for file in files { + let file = file.borrow(); if !file.properties.is_hidden { if let Some(name) = file.name.as_string() { collection.push(DirectoryRecordMember::File{name}); @@ -61,8 +64,8 @@ pub fn collect_directory_record_member(files: &Vec, dirs: &Vec) } for dir in dirs { - if !dir.properties.is_hidden { - if let Some(name) = dir.name.as_string() { + if !dir.borrow().properties.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/layout.rs b/src/Tools/psxcdgen_ex/src/types/layout.rs index 2ceb0a8f..51c629b5 100644 --- a/src/Tools/psxcdgen_ex/src/types/layout.rs +++ b/src/Tools/psxcdgen_ex/src/types/layout.rs @@ -1,7 +1,8 @@ use super::*; +use std::cell::RefCell; -pub type MemoryLayout<'a> = Vec>; -pub type MemoryLayoutMut<'a> = Vec>; +pub type MemoryLayout = Vec; +pub type MemoryLayoutMut = Vec; macro_rules! declare_memory_layout { ($($val:ident),*) => { @@ -18,31 +19,33 @@ macro_rules! declare_memory_layout { pub fn new(parent: &$($val),* CDDesc) -> Vec<[< Layout$($val:camel),* >]> { let mut layout = Vec::new(); - layout.push([< Layout$($val:camel),* >]::SystemArea(&$($val),* parent.system_area)); - layout.push([< Layout$($val:camel),* >]::PVD(&$($val),* parent.pvd)); + layout.push([< Layout$($val:camel),* >]::SystemArea(parent.system_area.clone())); + layout.push([< Layout$($val:camel),* >]::PVD(parent.pvd.clone())); layout.push(([< Layout$($val:camel),* >]::PathTables)); - [< add_dir_and_subdir $(_$val),* >](&mut layout, &$($val),* parent.root); + [< add_dir_and_subdir $(_$val),* >](&mut layout, parent.root.clone()); layout } } - pub enum [< Layout$($val:camel),* >]<'a> { - SystemArea(&'a $($val),* SystemArea), - PVD(&'a $($val),* PrimaryVolumeDescriptor), + pub enum [< Layout$($val:camel),* >] { + SystemArea(Rc>), + PVD(Rc>), PathTables, - Directory{name: &'a $($val),* DirectoryName, properties: &'a $($val),* Properties}, - File(&'a $($val),* File) + Directory(Rc>), + File(Rc>) } - fn [< add_dir_and_subdir $(_$val),* >]<'a>(layout: &mut Vec<[< Layout$($val:camel),* >]::<'a>>, dir: &'a $($val),* Directory) { - layout.push([< Layout$($val:camel),* >]::Directory{name: &$($val),* dir.name, properties: &$($val),* dir.properties}); + fn [< add_dir_and_subdir $(_$val),* >]<'a>(layout: &mut Vec<[< Layout$($val:camel),* >]>, dir: Rc>) { + layout.push([< Layout$($val:camel),* >]::Directory(dir.clone())); + + let $($val),* dir = dir.[< borrow $(_$val),* >](); for file in dir.files.[< iter$(_$val),* >]() { - layout.push([< Layout$($val:camel),* >]::File(file)); + layout.push([< Layout$($val:camel),* >]::File(file.clone())); } for dir in dir.dirs.[< iter$(_$val),* >]() { - [< add_dir_and_subdir $(_$val),* >](layout, dir); + [< add_dir_and_subdir $(_$val),* >](layout, dir.clone()); } } } diff --git a/src/Tools/psxcdgen_ex/src/types/mod.rs b/src/Tools/psxcdgen_ex/src/types/mod.rs index 8017a415..14ef8043 100644 --- a/src/Tools/psxcdgen_ex/src/types/mod.rs +++ b/src/Tools/psxcdgen_ex/src/types/mod.rs @@ -4,11 +4,12 @@ pub mod layout; 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; +use std::{cell::RefCell, rc::Rc}; pub struct CDDesc { - system_area: SystemArea, - pvd: PrimaryVolumeDescriptor, - pub root: Directory + system_area: Rc>, + pvd: Rc>, + pub root: Rc> } impl CDDesc { @@ -16,7 +17,7 @@ impl CDDesc { pub fn new() -> CDDesc { match Directory::new("root") { - Ok(root) => CDDesc{system_area: SystemArea::new(), pvd: PrimaryVolumeDescriptor::new(), root}, + Ok(root) => CDDesc{system_area: Rc::new(RefCell::new(SystemArea::new())), pvd: Rc::new(RefCell::new(PrimaryVolumeDescriptor::new())), root: Rc::new(RefCell::new(root))}, Err(error) => panic!("Creating root directory failed with: {}", error) } } @@ -33,7 +34,7 @@ impl CDDesc { let mut path_table_properties = { let mut size_bytes = 0; - helper::collect_path_table_member(&self.root).into_iter().for_each(|element| { + helper::collect_path_table_member(self.root.clone()).into_iter().for_each(|element| { println!("PT: {} ^{}", element.name, element.parent_table_id); size_bytes += PathTableL::calculate_size_for(element.name.as_ref()); }); @@ -41,9 +42,9 @@ impl CDDesc { 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; + let mut cur_lba = 0; - Self::for_each_dir_mut(&mut self.root, &|dir| {dir.update_content_size();}); + Self::for_each_dir_mut(self.root.clone(), &|dir| {dir.update_content_size();}); // Now layout iterate? for element in self.get_memory_layout_mut() { @@ -54,18 +55,22 @@ impl CDDesc { match element { LayoutMut::SystemArea(system_area) => { + let mut system_area = system_area.borrow_mut(); cur_lba = update_lba(&mut system_area.properties, cur_lba, Self::DEFAULT_DATA_SIZE_FOR_NOW); }, LayoutMut::PVD(pvd) => { + let mut pvd = pvd.borrow_mut(); 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); }, - LayoutMut::Directory{name: _, properties} => { - cur_lba = update_lba(properties, cur_lba, Self::DEFAULT_DATA_SIZE_FOR_NOW) + LayoutMut::Directory(dir) => { + let mut dir = dir.borrow_mut(); + cur_lba = update_lba(&mut dir.properties, cur_lba, Self::DEFAULT_DATA_SIZE_FOR_NOW) }, LayoutMut::File(file) => { + let mut file = file.borrow_mut(); cur_lba = update_lba(&mut file.properties, cur_lba, Self::DEFAULT_DATA_SIZE_FOR_NOW); } } @@ -74,10 +79,12 @@ impl CDDesc { } } - fn for_each_dir_mut(dir: &mut Directory, function: &T) { - function(dir); + fn for_each_dir_mut(dir: Rc>, function: &T) { + let mut dir = dir.borrow_mut(); + + function(&mut dir); for sub_dir in &mut dir.dirs { - Self::for_each_dir_mut(sub_dir, function); + Self::for_each_dir_mut(sub_dir.clone(), function); } } } @@ -109,8 +116,8 @@ impl PrimaryVolumeDescriptor { pub struct Directory { pub name: DirectoryName, pub properties: Properties, - files: Vec, - dirs: Vec + files: Vec>>, + dirs: Vec>> } impl Directory { @@ -119,11 +126,11 @@ impl Directory { } pub fn add_dir(&mut self, dir: Directory) { - self.dirs.push(dir); + self.dirs.push(Rc::new(RefCell::new(dir))); } pub fn add_file(&mut self, file: File) { - self.files.push(file); + self.files.push(Rc::new(RefCell::new(file))); } fn update_content_size(&mut self) {