mirror of
https://github.com/G2-Games/lbee-utils.git
synced 2025-04-19 23:32:55 -05:00
Fixed an edge case
If the offset size was equal to 2048, the program set it to 2048 instead of 0 incorrectly
This commit is contained in:
parent
f6374eecfc
commit
002eda0612
3 changed files with 36 additions and 13 deletions
|
@ -33,6 +33,10 @@ impl Entry {
|
||||||
&self.name
|
&self.name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn index(&self) -> usize {
|
||||||
|
self.index
|
||||||
|
}
|
||||||
|
|
||||||
pub fn id(&self) -> u32 {
|
pub fn id(&self) -> u32 {
|
||||||
self.id
|
self.id
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,8 @@ pub struct Header {
|
||||||
pub(super) id_start: u32,
|
pub(super) id_start: u32,
|
||||||
pub(super) block_size: u32,
|
pub(super) block_size: u32,
|
||||||
|
|
||||||
pub(super) unknown1: u32,
|
/// The offset of the subdirectory name within the PAK
|
||||||
|
pub(super) subdir_offset: u32,
|
||||||
pub(super) unknown2: u32,
|
pub(super) unknown2: u32,
|
||||||
pub(super) unknown3: u32,
|
pub(super) unknown3: u32,
|
||||||
pub(super) unknown4: u32,
|
pub(super) unknown4: u32,
|
||||||
|
@ -28,7 +29,7 @@ impl Header {
|
||||||
output.write_u32::<LE>(self.entry_count)?;
|
output.write_u32::<LE>(self.entry_count)?;
|
||||||
output.write_u32::<LE>(self.id_start)?;
|
output.write_u32::<LE>(self.id_start)?;
|
||||||
output.write_u32::<LE>(self.block_size)?;
|
output.write_u32::<LE>(self.block_size)?;
|
||||||
output.write_u32::<LE>(self.unknown1)?;
|
output.write_u32::<LE>(self.subdir_offset)?;
|
||||||
output.write_u32::<LE>(self.unknown2)?;
|
output.write_u32::<LE>(self.unknown2)?;
|
||||||
output.write_u32::<LE>(self.unknown3)?;
|
output.write_u32::<LE>(self.unknown3)?;
|
||||||
output.write_u32::<LE>(self.unknown4)?;
|
output.write_u32::<LE>(self.unknown4)?;
|
||||||
|
|
|
@ -33,6 +33,8 @@ pub enum PakError {
|
||||||
/// A full PAK file with a header and its contents
|
/// A full PAK file with a header and its contents
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Pak {
|
pub struct Pak {
|
||||||
|
subdirectory: Option<String>,
|
||||||
|
|
||||||
/// The path of the PAK file, can serve as an identifier or name as the
|
/// The path of the PAK file, can serve as an identifier or name as the
|
||||||
/// header has no name for the file.
|
/// header has no name for the file.
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
|
@ -72,7 +74,7 @@ impl Pak {
|
||||||
entry_count: input.read_u32::<LE>()?,
|
entry_count: input.read_u32::<LE>()?,
|
||||||
id_start: input.read_u32::<LE>()?,
|
id_start: input.read_u32::<LE>()?,
|
||||||
block_size: input.read_u32::<LE>()?,
|
block_size: input.read_u32::<LE>()?,
|
||||||
unknown1: input.read_u32::<LE>()?,
|
subdir_offset: input.read_u32::<LE>()?,
|
||||||
unknown2: input.read_u32::<LE>()?,
|
unknown2: input.read_u32::<LE>()?,
|
||||||
unknown3: input.read_u32::<LE>()?,
|
unknown3: input.read_u32::<LE>()?,
|
||||||
unknown4: input.read_u32::<LE>()?,
|
unknown4: input.read_u32::<LE>()?,
|
||||||
|
@ -130,16 +132,15 @@ impl Pak {
|
||||||
|
|
||||||
// Read all the file names
|
// Read all the file names
|
||||||
let mut file_names = None;
|
let mut file_names = None;
|
||||||
|
let mut subdirectory = None;
|
||||||
if header.flags.has_names() {
|
if header.flags.has_names() {
|
||||||
debug!("READING: file_names");
|
debug!("READING: file_names");
|
||||||
let mut string_buf = Vec::new();
|
if header.subdir_offset != 0 {
|
||||||
|
subdirectory = Some(read_cstring(&mut input)?);
|
||||||
|
}
|
||||||
file_names = Some(Vec::new());
|
file_names = Some(Vec::new());
|
||||||
for _ in 0..header.entry_count() {
|
for _ in 0..header.entry_count() {
|
||||||
string_buf.clear();
|
let strbuf = read_cstring(&mut input)?;
|
||||||
input.read_until(0x00, &mut string_buf)?;
|
|
||||||
string_buf.pop();
|
|
||||||
|
|
||||||
let strbuf = String::from_utf8_lossy(&string_buf).to_string();
|
|
||||||
file_names.as_mut().unwrap().push(strbuf.clone());
|
file_names.as_mut().unwrap().push(strbuf.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -189,6 +190,7 @@ impl Pak {
|
||||||
debug!("Entry list contains {} entries", entries.len());
|
debug!("Entry list contains {} entries", entries.len());
|
||||||
|
|
||||||
Ok(Pak {
|
Ok(Pak {
|
||||||
|
subdirectory,
|
||||||
header,
|
header,
|
||||||
unknown_pre_data,
|
unknown_pre_data,
|
||||||
entries,
|
entries,
|
||||||
|
@ -237,6 +239,11 @@ impl Pak {
|
||||||
|
|
||||||
// Write names if the flags indicate it should have them
|
// Write names if the flags indicate it should have them
|
||||||
if self.header.flags().has_names() {
|
if self.header.flags().has_names() {
|
||||||
|
if let Some(subdir) = &self.subdirectory {
|
||||||
|
output.write_all(
|
||||||
|
CString::new(subdir.as_bytes()).unwrap().to_bytes_with_nul()
|
||||||
|
)?;
|
||||||
|
}
|
||||||
for entry in self.entries() {
|
for entry in self.entries() {
|
||||||
let name = entry.name.as_ref().unwrap();
|
let name = entry.name.as_ref().unwrap();
|
||||||
output.write_all(
|
output.write_all(
|
||||||
|
@ -251,11 +258,14 @@ impl Pak {
|
||||||
|
|
||||||
for entry in self.entries() {
|
for entry in self.entries() {
|
||||||
let block_size = entry.data.len().div_ceil(self.header().block_size as usize);
|
let block_size = entry.data.len().div_ceil(self.header().block_size as usize);
|
||||||
let remainder = 2048 - entry.data.len().rem_euclid(self.header().block_size as usize);
|
let mut remainder = 2048 - entry.data.len().rem_euclid(self.header().block_size as usize);
|
||||||
|
if remainder == 2048 {
|
||||||
|
remainder = 0;
|
||||||
|
}
|
||||||
|
|
||||||
debug!("entry {:?} len {}", entry.name(), entry.data.len());
|
println!("entry len {}", entry.data.len());
|
||||||
debug!("remainder {}", remainder);
|
println!("remainder {}", remainder);
|
||||||
debug!("block_offset {} - expected offset {}", block_offset, entry.offset);
|
println!("block_offset {} - expected offset {}", block_offset, entry.offset);
|
||||||
output.write_all(&entry.data)?;
|
output.write_all(&entry.data)?;
|
||||||
output.write_all(&vec![0u8; remainder])?;
|
output.write_all(&vec![0u8; remainder])?;
|
||||||
block_offset += block_size as u32;
|
block_offset += block_size as u32;
|
||||||
|
@ -384,3 +394,11 @@ impl Pak {
|
||||||
.is_some_and(|n| n == name))
|
.is_some_and(|n| n == name))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn read_cstring<T: Seek + Read + BufRead>(input: &mut T) -> Result<String, io::Error> {
|
||||||
|
let mut string_buf = vec![];
|
||||||
|
input.read_until(0x00, &mut string_buf)?;
|
||||||
|
string_buf.pop();
|
||||||
|
|
||||||
|
Ok(String::from_utf8_lossy(&string_buf).to_string())
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue