From 54a460b51437a823f0cb9de878b0a36ef68080c7 Mon Sep 17 00:00:00 2001 From: Jaby Date: Tue, 6 Dec 2022 02:31:05 +0100 Subject: [PATCH] Read Overlay header --- src/Tools/psxcdgen_ex/Cargo.toml | 1 + .../psxcdgen_ex/src/config_reader/xml.rs | 6 +++- src/Tools/psxcdgen_ex/src/encoder/psx.rs | 3 ++ src/Tools/psxcdgen_ex/src/lib.rs | 4 +-- src/Tools/psxcdgen_ex/src/types/mod.rs | 23 ++++++++++---- .../psxcdgen_ex/src/types/overlay/mod.rs | 30 +++++++++++++++++++ src/Tools/tool_helper/src/lib.rs | 4 +-- 7 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 src/Tools/psxcdgen_ex/src/types/overlay/mod.rs diff --git a/src/Tools/psxcdgen_ex/Cargo.toml b/src/Tools/psxcdgen_ex/Cargo.toml index f17a6680..acc65024 100644 --- a/src/Tools/psxcdgen_ex/Cargo.toml +++ b/src/Tools/psxcdgen_ex/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +byteorder = "*" cdtypes = {path = "../cdtypes"} clap = {version = "*", features = ["derive"]} paste = "*" diff --git a/src/Tools/psxcdgen_ex/src/config_reader/xml.rs b/src/Tools/psxcdgen_ex/src/config_reader/xml.rs index 3306d6c9..ea1e167d 100644 --- a/src/Tools/psxcdgen_ex/src/config_reader/xml.rs +++ b/src/Tools/psxcdgen_ex/src/config_reader/xml.rs @@ -60,7 +60,11 @@ fn parse_track(track: roxmltree::Node, config: &mut Configuration) -> Result<(), } fn parse_overlay_file(file: roxmltree::Node, is_hidden: bool) -> Result { - parse_regular_file(file, is_hidden) + Ok(File{ + common: read_common_properties(&file, is_hidden)?, + path: PathBuf::from(file.text().unwrap_or_default()), + kind: FileKind::Overlay + }) } fn parse_file_system(cur_node: roxmltree::Node, root: &mut Directory, mut is_hidden: bool) -> Result<(), Error> { diff --git a/src/Tools/psxcdgen_ex/src/encoder/psx.rs b/src/Tools/psxcdgen_ex/src/encoder/psx.rs index 68dc203d..bc05e949 100644 --- a/src/Tools/psxcdgen_ex/src/encoder/psx.rs +++ b/src/Tools/psxcdgen_ex/src/encoder/psx.rs @@ -313,6 +313,9 @@ fn process_file(file: &File, sec_writer: &mut dyn SectorWriter) -> Result<(), Er let content_sectors = { match &file.content { FileType::Regular(raw) => builder::create_xa_data_for_vec(None, raw), + FileType::Overlay(_) => { + return Err(Error::not_implemented("process_file for Overlay in psx.rs")); + } } }; let content_sector_count = content_sectors.len(); diff --git a/src/Tools/psxcdgen_ex/src/lib.rs b/src/Tools/psxcdgen_ex/src/lib.rs index 482636ea..474e8e31 100644 --- a/src/Tools/psxcdgen_ex/src/lib.rs +++ b/src/Tools/psxcdgen_ex/src/lib.rs @@ -116,8 +116,8 @@ fn parse_configuration(config: config_reader::Configuration) -> Result { let mut desc_file = { match file.kind { - config_reader::FileKind::Regular => types::File::new_regular(file.common.name.as_str(), read_file(file.path)?)?, - config_reader::FileKind::Overlay => types::File::new_regular(file.common.name.as_str(), read_file(file.path)?)?, + config_reader::FileKind::Regular => types::File::new_regular(file.common.name.as_str(), read_file(&file.path)?)?, + config_reader::FileKind::Overlay => types::overlay::load_from(file.path)?, } }; diff --git a/src/Tools/psxcdgen_ex/src/types/mod.rs b/src/Tools/psxcdgen_ex/src/types/mod.rs index 286e22c5..c068f75c 100644 --- a/src/Tools/psxcdgen_ex/src/types/mod.rs +++ b/src/Tools/psxcdgen_ex/src/types/mod.rs @@ -1,6 +1,7 @@ pub (super) mod helper; pub mod layout; pub mod file_map; +pub mod overlay; use cdtypes::types::{cdstring::DString, dir_record::DirectoryRecord, path_table::PathTableL}; use file_map::FileSystemMap; @@ -172,7 +173,8 @@ impl std::fmt::Display for Directory { } pub(super) enum FileType { - Regular(Vec) + Regular(Vec), + Overlay(Vec), } pub struct File { @@ -184,11 +186,15 @@ pub struct File { impl File { pub fn new_regular(file_name: &str, content: Vec) -> Result { - 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)}; + let content_size = content.len(); - file.properties.size_bytes = content_size; - Ok(file) + Self::new_from_content(file_name, FileType::Regular(content), content_size) + } + + pub fn new_overlay(file_name: &str, content: Vec) -> Result { + let content_size = content.len(); + + Self::new_from_content(file_name, FileType::Overlay(content), content_size) } pub fn get_track_rel_lba(&self) -> usize { @@ -198,6 +204,13 @@ impl File { pub fn get_extended_size(&self) -> usize { self.properties.get_padded_size() } + + fn new_from_content(file_name: &str, content: FileType, content_size: usize) -> Result { + let mut file = File{name: FileName::from_str(file_name)?, properties: Properties::default(), parent_properties: None, content: content}; + + file.properties.size_bytes = content_size; + Ok(file) + } } impl std::fmt::Display for File { diff --git a/src/Tools/psxcdgen_ex/src/types/overlay/mod.rs b/src/Tools/psxcdgen_ex/src/types/overlay/mod.rs new file mode 100644 index 00000000..197a1364 --- /dev/null +++ b/src/Tools/psxcdgen_ex/src/types/overlay/mod.rs @@ -0,0 +1,30 @@ +use super::File; +use std::path::PathBuf; +use byteorder::{ByteOrder, BigEndian, LittleEndian}; +use tool_helper::{Error, format_if_error, read_file}; + +#[repr(packed)] +struct OverlayHeader { + pub start_adr: u32, + pub _lba_count: u16, +} + +impl OverlayHeader { + pub fn lba_count_offset() -> usize { + let dummy = OverlayHeader{start_adr: 0, _lba_count: 0}.start_adr; + std::mem::size_of_val(&dummy) + } +} + +pub fn load_from(file_path: PathBuf) -> Result { + let content = read_file(&file_path)?; + + if content.len() < std::mem::size_of::() { + return Err(Error::from_text(format!("Overlay {} has no header!", file_path.to_string_lossy()))); + } + + let lba_count = LittleEndian::read_u16(&content[OverlayHeader::lba_count_offset()..]); + println!("Dino: {}", lba_count); + + Err(Error::not_implemented("load_from overlay")) +} \ No newline at end of file diff --git a/src/Tools/tool_helper/src/lib.rs b/src/Tools/tool_helper/src/lib.rs index caff774d..ab20d80e 100644 --- a/src/Tools/tool_helper/src/lib.rs +++ b/src/Tools/tool_helper/src/lib.rs @@ -162,8 +162,8 @@ pub fn input_to_vec(input: Input) -> Result, Error> { Ok(data) } -pub fn read_file(file_path: PathBuf) -> Result, Error> { - match std::fs::read(&file_path) { +pub fn read_file(file_path: &PathBuf) -> Result, Error> { + match std::fs::read(file_path) { Ok(data) => { Ok(data) },