Read in LBA file path from header file

This commit is contained in:
jaby 2022-12-07 03:53:48 +01:00
parent 5f524b689b
commit 11220216d0
3 changed files with 55 additions and 11 deletions

View File

@ -9,6 +9,7 @@ edition = "2021"
byteorder = "*"
cdtypes = {path = "../cdtypes"}
clap = {version = "*", features = ["derive"]}
no-comment = "*"
paste = "*"
roxmltree = "*"
tool_helper = {path = "../tool_helper"}

View File

@ -118,7 +118,7 @@ fn parse_configuration(config: config_reader::Configuration) -> Result<(CDDesc,
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::Overlay(lba_source) => (types::overlay::load_from(file.common.name.as_str(), file.path, lba_source)?, true)
}
};

View File

@ -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<String>;
#[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<File, Error> {
pub fn load_from(file_name: &str, file_path: PathBuf, lba_source: PathBuf) -> Result<File, Error> {
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<Vec<u8>, Error> {
let overlay_header_size = std::mem::size_of::<OverlayHeader>();
let mut content = read_file(&file_path)?;
@ -38,6 +49,38 @@ pub fn load_from(file_name: &str, file_path: PathBuf) -> Result<File, Error> {
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<LBANameVec, Error> {
const LBA_DECLARATION:&'static str = "__jabyengine_request_lba_for";
fn get_part_of_interest(file: String, lba_source: &PathBuf) -> Result<String, Error> {
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::<String>();
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)
}