From 8269dab350e687ad4f2999d9dd21a331d80e9129 Mon Sep 17 00:00:00 2001 From: Jaby Date: Wed, 7 Dec 2022 02:50:46 +0100 Subject: [PATCH] Prepare reading in LBA Source for Overlay --- src/Tools/Tests/ISO_Planschbecken.xml | 2 +- .../psxcdgen_ex/src/config_reader/mod.rs | 9 +---- .../psxcdgen_ex/src/config_reader/xml.rs | 4 ++- src/Tools/psxcdgen_ex/src/lib.rs | 34 +++++++++++-------- src/Tools/psxcdgen_ex/src/main.rs | 4 +-- src/Tools/psxcdgen_ex/src/types/mod.rs | 11 +++--- .../psxcdgen_ex/src/types/overlay/mod.rs | 4 +-- src/Tools/tool_helper/src/compress.rs | 2 +- 8 files changed, 36 insertions(+), 34 deletions(-) diff --git a/src/Tools/Tests/ISO_Planschbecken.xml b/src/Tools/Tests/ISO_Planschbecken.xml index 74efcb77..22151d38 100644 --- a/src/Tools/Tests/ISO_Planschbecken.xml +++ b/src/Tools/Tests/ISO_Planschbecken.xml @@ -13,6 +13,6 @@ ../Tests/ISO_Planschbecken.xml - ../../../../JabyAdventure/application/bin/PSX-release/Overlay.main_area + ../../../../JabyAdventure/application/bin/PSX-release/Overlay.main_area \ No newline at end of file diff --git a/src/Tools/psxcdgen_ex/src/config_reader/mod.rs b/src/Tools/psxcdgen_ex/src/config_reader/mod.rs index 15c1f399..4c5cc098 100644 --- a/src/Tools/psxcdgen_ex/src/config_reader/mod.rs +++ b/src/Tools/psxcdgen_ex/src/config_reader/mod.rs @@ -22,7 +22,7 @@ pub struct CommonProperties { pub enum FileKind { Regular, - Overlay + Overlay(PathBuf) } pub struct File { @@ -30,13 +30,6 @@ pub struct File { pub path: PathBuf, pub kind: FileKind } - -pub struct LbaFile { - pub common: CommonProperties, - pub header: Option, - pub entries: Vec -} - pub struct Directory { pub name: String, pub is_hidden: bool, diff --git a/src/Tools/psxcdgen_ex/src/config_reader/xml.rs b/src/Tools/psxcdgen_ex/src/config_reader/xml.rs index ea1e167d..ee57833b 100644 --- a/src/Tools/psxcdgen_ex/src/config_reader/xml.rs +++ b/src/Tools/psxcdgen_ex/src/config_reader/xml.rs @@ -8,6 +8,7 @@ mod attribute_names { pub const NAME: &'static str = "name"; pub const HIDDEN: &'static str = "hidden"; pub const PADDED_SIZE: &'static str = "padded_size"; + pub const LBA_SOURCE: &'static str = "lba_source"; } pub fn parse(xml: String) -> Result { @@ -60,10 +61,11 @@ fn parse_track(track: roxmltree::Node, config: &mut Configuration) -> Result<(), } fn parse_overlay_file(file: roxmltree::Node, is_hidden: bool) -> Result { + Ok(File{ common: read_common_properties(&file, is_hidden)?, path: PathBuf::from(file.text().unwrap_or_default()), - kind: FileKind::Overlay + kind: FileKind::Overlay(PathBuf::from(file.attribute(attribute_names::LBA_SOURCE).unwrap_or_default())), }) } diff --git a/src/Tools/psxcdgen_ex/src/lib.rs b/src/Tools/psxcdgen_ex/src/lib.rs index 7238fd3b..fa4d0ddd 100644 --- a/src/Tools/psxcdgen_ex/src/lib.rs +++ b/src/Tools/psxcdgen_ex/src/lib.rs @@ -6,9 +6,10 @@ pub mod file_writer; pub mod types; use tool_helper::{format_if_error, Output, read_file}; -use types::{CDDesc, Directory, Properties}; +use types::{CDDesc, Directory, File, Properties, SharedPtr}; pub type CalculateLBAFunction = fn(&mut types::CDDesc); +pub type LBAEmbeddedFiles = Vec>; struct ContentDumpAlignment { name: usize, @@ -19,11 +20,11 @@ struct ContentDumpAlignment { const DEFAULT_CONTENT_ALIGNMENT:ContentDumpAlignment = ContentDumpAlignment{name: 24, lba: 8, size: 8, ex_size: 8}; -pub fn process(config: config_reader::Configuration, calculate_lba: CalculateLBAFunction) -> Result { - let mut cd_desc = parse_configuration(config)?; +pub fn process(config: config_reader::Configuration, calculate_lba: CalculateLBAFunction) -> Result<(CDDesc, LBAEmbeddedFiles), Error> { + let (mut cd_desc, lba_embedded_files) = parse_configuration(config)?; calculate_lba(&mut cd_desc); - Ok(cd_desc) + Ok((cd_desc, lba_embedded_files)) } pub fn dump_content(cd_desc: &CDDesc, mut out: Output) -> Result<(), Error> { @@ -101,29 +102,33 @@ pub fn dump_content(cd_desc: &CDDesc, mut out: Output) -> Result<(), Error> { format_if_error!(dump_dir(&cd_desc.root.borrow(), &mut out, 0), "Creating content dump failed with: {error_text}") } -fn parse_configuration(config: config_reader::Configuration) -> Result { - fn parse_dir(dst_dir: &mut types::Directory, src_dir: config_reader::Directory) -> Result<(), Error> { +fn parse_configuration(config: config_reader::Configuration) -> Result<(CDDesc, LBAEmbeddedFiles), Error> { + fn parse_dir(dst_dir: &mut types::Directory, src_dir: config_reader::Directory, lba_embedded_files: &mut LBAEmbeddedFiles) -> Result<(), Error> { for member in src_dir.into_iter() { match member { config_reader::DirMember::Directory(dir) => { let mut new_dir = types::Directory::new(dir.name.as_str())?; new_dir.properties.borrow_mut().is_hidden = dir.is_hidden; - parse_dir(&mut new_dir, dir)?; + parse_dir(&mut new_dir, dir, lba_embedded_files)?; dst_dir.add_dir(new_dir); }, config_reader::DirMember::File(file) => { - let mut desc_file = { + let (mut desc_file, needs_treatment) = { 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::overlay::load_from(file.common.name.as_str(), file.path)?, + config_reader::FileKind::Regular => (types::File::new_regular(file.common.name.as_str(), read_file(&file.path)?)?, false), + config_reader::FileKind::Overlay(_) => (types::overlay::load_from(file.common.name.as_str(), file.path)?, true) } }; desc_file.properties.padded_size_bytes = file.common.padded_size; desc_file.properties.is_hidden = file.common.is_hidden; - dst_dir.add_file(desc_file); + + let new_file = dst_dir.add_file(desc_file); + if needs_treatment { + lba_embedded_files.push(new_file); + } }, } } @@ -131,7 +136,8 @@ fn parse_configuration(config: config_reader::Configuration) -> Result Result Result<(), Error> { - let encoding_functions = cmd_line.system_type.get_encoding_functions(); - let desc = psxcdgen_ex::process(config_reader::parse_xml(std::fs::read_to_string(cmd_line.input_file)?)?, encoding_functions.lba_calculator)?; + let encoding_functions = cmd_line.system_type.get_encoding_functions(); + let (desc, _lba_embbeded_files) = psxcdgen_ex::process(config_reader::parse_xml(std::fs::read_to_string(cmd_line.input_file)?)?, encoding_functions.lba_calculator)?; /* // Describes how to use the the file_map diff --git a/src/Tools/psxcdgen_ex/src/types/mod.rs b/src/Tools/psxcdgen_ex/src/types/mod.rs index c068f75c..8087b4a8 100644 --- a/src/Tools/psxcdgen_ex/src/types/mod.rs +++ b/src/Tools/psxcdgen_ex/src/types/mod.rs @@ -130,9 +130,12 @@ impl Directory { self.dirs.push(new_shared_ptr(dir)); } - pub fn add_file(&mut self, mut file: File) { + pub fn add_file(&mut self, mut file: File) -> SharedPtr { file.parent_properties = Some(self.properties.clone()); - self.files.push(new_shared_ptr(file)); + + let file = new_shared_ptr(file); + self.files.push(file.clone()); + file } pub fn get_track_rel_lba(&self) -> usize { @@ -191,9 +194,7 @@ impl 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(); - + pub fn new_overlay(file_name: &str, content: Vec, content_size: usize) -> Result { Self::new_from_content(file_name, FileType::Overlay(content), content_size) } diff --git a/src/Tools/psxcdgen_ex/src/types/overlay/mod.rs b/src/Tools/psxcdgen_ex/src/types/overlay/mod.rs index 6c65cbc4..ed3296dc 100644 --- a/src/Tools/psxcdgen_ex/src/types/overlay/mod.rs +++ b/src/Tools/psxcdgen_ex/src/types/overlay/mod.rs @@ -38,6 +38,6 @@ pub fn load_from(file_name: &str, file_path: PathBuf) -> Result { count += 1; } - let content = format_if_error!(tool_helper::compress::lz4(content, 16), "Compressing {} failed with \"{error_text}\"", file_path.to_string_lossy())?; - Ok(File::new_overlay(file_name, content)?) + let content_size = format_if_error!(tool_helper::compress::lz4(&content, 16), "Compressing {} failed with \"{error_text}\"", file_path.to_string_lossy())?.len(); + Ok(File::new_overlay(file_name, content, content_size)?) } \ No newline at end of file diff --git a/src/Tools/tool_helper/src/compress.rs b/src/Tools/tool_helper/src/compress.rs index 88377098..5f456d9b 100644 --- a/src/Tools/tool_helper/src/compress.rs +++ b/src/Tools/tool_helper/src/compress.rs @@ -1,7 +1,7 @@ use super::Error; use lz4::EncoderBuilder; -pub fn lz4(data: Vec, compression_level: u32) -> Result, Error> { +pub fn lz4(data: &Vec, compression_level: u32) -> Result, Error> { let mut lz4_encoder = EncoderBuilder::new().level(compression_level).build(Vec::::new())?; std::io::copy(&mut&data[..], &mut lz4_encoder)?;