Prepare shared properties
This commit is contained in:
parent
8e2f7e02a1
commit
773571eeb9
|
@ -1,7 +1,7 @@
|
||||||
use super::{*, SectorWriter, {CDDesc, Error}};
|
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 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 SYSTEM_AREA_SECTOR_COUNT:usize = 16;
|
||||||
const PVD_SECTOR_COUNT:usize = 2;
|
const PVD_SECTOR_COUNT:usize = 2;
|
||||||
|
@ -44,17 +44,17 @@ pub fn calculate_psx_lbas(cd_desc: &mut CDDesc) {
|
||||||
},
|
},
|
||||||
|
|
||||||
Layout::Directory(dir) => {
|
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();
|
let mut dir = dir.borrow_mut();
|
||||||
|
|
||||||
cd_desc.vol_sector_count += sector_count;
|
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) => {
|
Layout::File(file) => {
|
||||||
let sector_count = {
|
let sector_count = {
|
||||||
let file = file.borrow();
|
let file = file.borrow();
|
||||||
let fake_size = file.get_size();
|
let fake_size = file.properties.get_extended_size();
|
||||||
|
|
||||||
sector_count_mode2_form1(fake_size)
|
sector_count_mode2_form1(fake_size)
|
||||||
};
|
};
|
||||||
|
@ -126,7 +126,7 @@ fn process_pvd(pvd: &PrimaryVolumeDescriptor, path_table: SharedPtr<PathTable>,
|
||||||
cd_pvd.path_table_4.write(path_table.get_track_rel_lba_for(4, sector_count_mode2_form1) as u32);
|
cd_pvd.path_table_4.write(path_table.get_track_rel_lba_for(4, sector_count_mode2_form1) as u32);
|
||||||
|
|
||||||
//Set Root Directory Record
|
//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
|
//Set other stuff
|
||||||
cd_pvd.publisher_id = AString::from_str(pvd.publisher.as_str())?;
|
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<Directory>, se
|
||||||
}
|
}
|
||||||
|
|
||||||
fn process_directory_record(dir: &Directory, _sec_writer: &mut dyn SectorWriter) -> Result<(), Error> {
|
fn process_directory_record(dir: &Directory, _sec_writer: &mut dyn SectorWriter) -> Result<(), Error> {
|
||||||
let mut dir_record = vec![0u8; dir.get_content_size()];
|
fn _create_dir_system_use() -> CDXASystemUse {
|
||||||
let dir_length = dir_record.len();
|
let mut system_use = CDXASystemUse::default();
|
||||||
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])};
|
|
||||||
|
|
||||||
|
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> {
|
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())
|
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<CDXASystemUse>) -> Result<&'a mut DirectoryRecord, Error> {
|
||||||
let name = name.unwrap_or("\x00");
|
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 {
|
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())));
|
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]);
|
let mut dir_record = std::mem::transmute::<&mut u8, &mut DirectoryRecord>(&mut dst[0]);
|
||||||
dir_record.new(name, false);
|
dir_record.new(name, false);
|
||||||
|
|
||||||
|
|
||||||
dir_record.data_block_number.write(track_rel_lba);
|
dir_record.data_block_number.write(track_rel_lba);
|
||||||
dir_record.data_size.write(size_bytes);
|
dir_record.data_size.write(size_bytes);
|
||||||
dir_record.time_stamp = SmallDate::now();
|
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)
|
Ok(dir_record)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn update_dir_record_with_dir(dir_record: &mut [u8], dir: &Directory, system_use: Option<CDXASystemUse>) -> Result<usize, Error> {
|
||||||
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.borrow().track_rel_lba as u32, dir.properties.borrow().get_real_size() as u32, system_use)?;
|
||||||
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)?;
|
|
||||||
|
|
||||||
dir_record.set_directory();
|
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<CDXASystemUse>) -> Result<usize, Error> {
|
||||||
|
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)*/
|
||||||
}
|
}
|
|
@ -45,8 +45,8 @@ fn run_main() -> Result<(), Error> {
|
||||||
Layout::SystemArea(_) => println!("SystemArea:"),
|
Layout::SystemArea(_) => println!("SystemArea:"),
|
||||||
Layout::PVD(_) => println!("PVD:"),
|
Layout::PVD(_) => println!("PVD:"),
|
||||||
Layout::PathTables(_) => println!("PathTables:"),
|
Layout::PathTables(_) => println!("PathTables:"),
|
||||||
Layout::Directory(dir) => println!("Dir: {} @{}-{}", dir.borrow().name, dir.borrow().get_track_rel_lba(), dir.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_size()),
|
Layout::File(file) => println!("File: {} @{}-{}", file.borrow(), file.borrow().get_track_rel_lba(), file.borrow().get_extended_size()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
println!("\n<== Planschbecken ==>");
|
println!("\n<== Planschbecken ==>");
|
||||||
|
|
|
@ -29,7 +29,7 @@ pub fn collect_path_table_member(root: SharedPtr<Directory>) -> Vec<PathTableMem
|
||||||
|
|
||||||
for dir in dirs {
|
for dir in dirs {
|
||||||
let dir = dir.borrow();
|
let dir = dir.borrow();
|
||||||
if !dir.properties.is_hidden {
|
if !dir.properties.borrow().is_hidden {
|
||||||
cur_dirs.push(PathTableMember{name: dir.name.as_string().unwrap_or("".to_owned()), track_rel_lba: dir.get_track_rel_lba(), parent_table_id: parent_id});
|
cur_dirs.push(PathTableMember{name: dir.name.as_string().unwrap_or("".to_owned()), track_rel_lba: dir.get_track_rel_lba(), parent_table_id: parent_id});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -67,7 +67,7 @@ pub fn collect_directory_record_member(files: &Vec<SharedPtr<File>>, dirs: &Vec<
|
||||||
}
|
}
|
||||||
|
|
||||||
for dir in dirs {
|
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() {
|
if let Some(name) = dir.borrow().name.as_string() {
|
||||||
collection.push(DirectoryRecordMember::Directory{name});
|
collection.push(DirectoryRecordMember::Directory{name});
|
||||||
}
|
}
|
||||||
|
|
|
@ -108,36 +108,34 @@ impl PrimaryVolumeDescriptor {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Directory {
|
pub struct Directory {
|
||||||
pub name: DirectoryName,
|
pub name: DirectoryName,
|
||||||
pub properties: Properties,
|
pub properties: SharedPtr<Properties>,
|
||||||
size_bytes: usize,
|
parent_properties: Option<SharedPtr<Properties>>,
|
||||||
files: Vec<SharedPtr<File>>,
|
files: Vec<SharedPtr<File>>,
|
||||||
dirs: Vec<SharedPtr<Directory>>
|
dirs: Vec<SharedPtr<Directory>>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Directory {
|
impl Directory {
|
||||||
pub fn new(dir_name: &str) -> Result<Directory, Error> {
|
pub fn new(dir_name: &str) -> Result<Directory, Error> {
|
||||||
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));
|
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));
|
self.files.push(new_shared_ptr(file));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_track_rel_lba(&self) -> usize {
|
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 {
|
pub fn get_extended_size(&self) -> usize {
|
||||||
self.properties.get_size(self.size_bytes)
|
self.properties.borrow().get_extended_size()
|
||||||
}
|
|
||||||
|
|
||||||
pub(super) fn get_content_size(&self) -> usize {
|
|
||||||
self.size_bytes
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn collect_member(&self) -> Vec<helper::DirectoryRecordMember>{
|
pub(super) fn collect_member(&self) -> Vec<helper::DirectoryRecordMember>{
|
||||||
|
@ -151,7 +149,7 @@ impl Directory {
|
||||||
size_bytes += DirectoryRecord::calculate_size_for(member.get_name().as_str(), true);
|
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 struct File {
|
||||||
pub name: FileName,
|
pub name: FileName,
|
||||||
pub properties: Properties,
|
pub properties: Properties,
|
||||||
content: FileType
|
parent_properties: Option<SharedPtr<Properties>>,
|
||||||
|
_content: FileType
|
||||||
}
|
}
|
||||||
|
|
||||||
impl File {
|
impl File {
|
||||||
pub fn new_regular(file_name: &str, content: Vec<u8>) -> Result<File, Error> {
|
pub fn new_regular(file_name: &str, content: Vec<u8>) -> Result<File, Error> {
|
||||||
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 {
|
pub fn get_track_rel_lba(&self) -> usize {
|
||||||
self.properties.track_rel_lba
|
self.properties.track_rel_lba
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_size(&self) -> usize {
|
pub fn get_extended_size(&self) -> usize {
|
||||||
self.properties.get_size(self.get_content_size())
|
self.properties.get_extended_size()
|
||||||
}
|
|
||||||
|
|
||||||
pub(super) fn get_content_size(&self) -> usize {
|
|
||||||
match &self.content {
|
|
||||||
FileType::Regular(content) => content.len()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -265,26 +262,31 @@ impl std::fmt::Display for FileName {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Properties {
|
pub struct Properties {
|
||||||
pub(in super) track_rel_lba: usize,
|
pub(super) track_rel_lba: usize,
|
||||||
pub(in super) overwrite_size_bytes: Option<usize>,
|
pub(super) size_bytes: usize,
|
||||||
pub(in super) is_hidden: bool
|
pub(super) overwrite_size_bytes: Option<usize>,
|
||||||
|
pub(super) is_hidden: bool
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Properties {
|
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 {
|
if let Some(forced_bytes) = self.overwrite_size_bytes {
|
||||||
forced_bytes
|
forced_bytes
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
content_size
|
self.size_bytes
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(super) fn get_real_size(&self) -> usize {
|
||||||
|
self.size_bytes
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Properties {
|
impl Default for Properties {
|
||||||
fn default() -> Self {
|
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}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue