From 56fbcfd994e613baeeb0a568ab841835b383a60c Mon Sep 17 00:00:00 2001 From: Jaby Date: Wed, 7 Dec 2022 03:53:48 +0100 Subject: [PATCH] Read in LBA file path from header file --- src/Tools/psxcdgen_ex/Cargo.toml | 13 ++--- src/Tools/psxcdgen_ex/src/lib.rs | 4 +- .../psxcdgen_ex/src/types/overlay/mod.rs | 49 +++++++++++++++++-- 3 files changed, 55 insertions(+), 11 deletions(-) diff --git a/src/Tools/psxcdgen_ex/Cargo.toml b/src/Tools/psxcdgen_ex/Cargo.toml index acc65024..47f0ea5f 100644 --- a/src/Tools/psxcdgen_ex/Cargo.toml +++ b/src/Tools/psxcdgen_ex/Cargo.toml @@ -6,9 +6,10 @@ 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 = "*" -roxmltree = "*" -tool_helper = {path = "../tool_helper"} \ No newline at end of file +byteorder = "*" +cdtypes = {path = "../cdtypes"} +clap = {version = "*", features = ["derive"]} +no-comment = "*" +paste = "*" +roxmltree = "*" +tool_helper = {path = "../tool_helper"} diff --git a/src/Tools/psxcdgen_ex/src/lib.rs b/src/Tools/psxcdgen_ex/src/lib.rs index fa4d0ddd..6d9b6c2f 100644 --- a/src/Tools/psxcdgen_ex/src/lib.rs +++ b/src/Tools/psxcdgen_ex/src/lib.rs @@ -117,8 +117,8 @@ fn parse_configuration(config: config_reader::Configuration) -> Result<(CDDesc, config_reader::DirMember::File(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)?)?, false), - config_reader::FileKind::Overlay(_) => (types::overlay::load_from(file.common.name.as_str(), file.path)?, true) + config_reader::FileKind::Regular => (types::File::new_regular(file.common.name.as_str(), read_file(&file.path)?)?, false), + config_reader::FileKind::Overlay(lba_source) => (types::overlay::load_from(file.common.name.as_str(), file.path, lba_source)?, true) } }; diff --git a/src/Tools/psxcdgen_ex/src/types/overlay/mod.rs b/src/Tools/psxcdgen_ex/src/types/overlay/mod.rs index ed3296dc..9e1c9e6d 100644 --- a/src/Tools/psxcdgen_ex/src/types/overlay/mod.rs +++ b/src/Tools/psxcdgen_ex/src/types/overlay/mod.rs @@ -1,8 +1,11 @@ use super::File; use std::path::PathBuf; use byteorder::{ByteOrder, LittleEndian}; +use no_comment::{IntoWithoutComments as _, languages}; use tool_helper::{Error, format_if_error, read_file}; +pub type LBANameVec = Vec; + #[repr(packed)] struct OverlayHeader { pub start_adr: u32, @@ -16,7 +19,15 @@ impl OverlayHeader { } } -pub fn load_from(file_name: &str, file_path: PathBuf) -> Result { +pub fn load_from(file_name: &str, file_path: PathBuf, lba_source: PathBuf) -> Result { + let content = load_content(&file_path)?; + let _ = load_lba_names(lba_source)?; + 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)?) +} + +fn load_content(file_path: &PathBuf) -> Result, Error> { let overlay_header_size = std::mem::size_of::(); let mut content = read_file(&file_path)?; @@ -38,6 +49,38 @@ pub fn load_from(file_name: &str, file_path: PathBuf) -> Result { count += 1; } - 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)?) + Ok(content) +} + +fn load_lba_names(lba_source: PathBuf) -> Result { + const LBA_DECLARATION:&'static str = "__jabyengine_request_lba_for"; + fn get_part_of_interest(file: String, lba_source: &PathBuf) -> Result { + const LBA_START_POINT:&'static str = "__jabyengine_start_lba_request"; + const LBA_END_POINT:&'static str = "__jabyengine_end_lba_request"; + + let start = file.find(LBA_START_POINT).ok_or_else(|| { + Error::from_text(format!("No LBA start found in file {}", lba_source.to_string_lossy())) + })? + LBA_START_POINT.len(); + + let end = file.find(LBA_END_POINT).ok_or_else(|| { + Error::from_text(format!("No LBA end found in file {}", lba_source.to_string_lossy())) + })?; + + Ok(file[start..end].to_owned()) + } + + let file = std::fs::read_to_string(&lba_source)?.chars().without_comments(languages::c()).collect::(); + let file = get_part_of_interest(file, &lba_source)?; + let mut lba_names = Vec::new(); + + for pair in file.split(LBA_DECLARATION) { + if let Some(mut start) = pair.find('"') { + start += 1; + if let Some(end) = pair[start..].find('"') { + lba_names.push(pair[start..(start + end)].to_owned()); + } + } + } + + Ok(lba_names) } \ No newline at end of file