mirror of
https://github.com/xavo95/repak.git
synced 2025-01-18 02:54:36 +00:00
Cleaning code
This commit is contained in:
parent
44d90fad11
commit
7f7e0b9f78
4 changed files with 126 additions and 95 deletions
|
@ -1,9 +1,11 @@
|
|||
use crate::Error;
|
||||
|
||||
use super::{ext::BoolExt, ext::ReadExt, Compression, Version, VersionMajor};
|
||||
use byteorder::{ReadBytesExt, WriteBytesExt, LE};
|
||||
use std::io;
|
||||
|
||||
use byteorder::{LE, ReadBytesExt, WriteBytesExt};
|
||||
|
||||
use crate::{Error, reader, writer};
|
||||
|
||||
use super::{Compression, ext::BoolExt, ext::ReadExt, Version, VersionMajor};
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||
pub(crate) enum EntryLocation {
|
||||
Data,
|
||||
|
@ -309,20 +311,7 @@ impl Entry {
|
|||
reader: &mut R,
|
||||
version: super::Version,
|
||||
) -> Result<Self, super::Error> {
|
||||
|
||||
let bits = {
|
||||
let tmp = reader.read_u32::<LE>()?;
|
||||
#[cfg(not(feature = "wuthering-waves"))]
|
||||
{ tmp }
|
||||
#[cfg(feature = "wuthering-waves")]
|
||||
if version == Version::V12 {
|
||||
reader.read_u8()?;
|
||||
(tmp >> 16) & 0x3f | (tmp & 0xFFFF) << 6 | (tmp & (1 << 28)) >> 6 |
|
||||
(tmp & 0x0FC00000) << 1 | tmp & 0xE0000000
|
||||
} else {
|
||||
tmp
|
||||
}
|
||||
};
|
||||
let bits = reader::flag_reader(reader, version)?;
|
||||
|
||||
let compression = match (bits >> 23) & 0x3f {
|
||||
0 => None,
|
||||
|
@ -339,30 +328,11 @@ impl Entry {
|
|||
compression_block_size <<= 11;
|
||||
}
|
||||
|
||||
let mut var_int = |bit: u32| -> Result<_, super::Error> {
|
||||
Ok(if (bits & (1 << bit)) != 0 {
|
||||
reader.read_u32::<LE>()? as u64
|
||||
} else {
|
||||
reader.read_u64::<LE>()?
|
||||
})
|
||||
};
|
||||
|
||||
let (offset, uncompressed) = {
|
||||
let offset = var_int(31)?;
|
||||
let uncompressed = var_int(30)?;
|
||||
#[cfg(not(feature = "wuthering-waves"))]
|
||||
{ (offset, uncompressed) }
|
||||
#[cfg(feature = "wuthering-waves")]
|
||||
if version == Version::V12 {
|
||||
(uncompressed, offset)
|
||||
} else {
|
||||
(offset, uncompressed)
|
||||
}
|
||||
};
|
||||
let (offset, uncompressed) = reader::offset_reader(reader, version, bits)?;
|
||||
|
||||
let compressed = match compression {
|
||||
None => uncompressed,
|
||||
_ => var_int(29)?,
|
||||
_ => reader::read_safe(reader, bits, 29)?,
|
||||
};
|
||||
|
||||
let offset_base = Entry::get_serialized_size(version, compression, compression_block_count);
|
||||
|
@ -429,69 +399,23 @@ impl Entry {
|
|||
| ((is_uncompressed_size_32_bit_safe as u32) << 30)
|
||||
| ((is_offset_32_bit_safe as u32) << 31);
|
||||
|
||||
#[cfg(not(feature = "wuthering-waves"))]
|
||||
writer.write_u32::<LE>(flags)?;
|
||||
#[cfg(feature = "wuthering-waves")]
|
||||
if version == Version::V12 {
|
||||
let tmp = ((flags & 0x3f) << 16) | ((flags >> 6) & 0xFFFF) |
|
||||
((flags << 6) & (1 << 28)) | ((flags >> 1) & 0x0FC00000) | flags & 0xE0000000;
|
||||
writer.write_u32::<LE>(tmp)?;
|
||||
writer.write_u8(0)?;
|
||||
} else {
|
||||
writer.write_u32::<LE>(flags)?;
|
||||
}
|
||||
writer::flag_writer(writer, version, flags)?;
|
||||
|
||||
if compression_block_size == 0x3f {
|
||||
writer.write_u32::<LE>(self.compression_block_size)?;
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "wuthering-waves"))]
|
||||
{
|
||||
if is_offset_32_bit_safe {
|
||||
writer.write_u32::<LE>(self.offset as u32)?;
|
||||
} else {
|
||||
writer.write_u64::<LE>(self.offset)?;
|
||||
}
|
||||
|
||||
if is_uncompressed_size_32_bit_safe {
|
||||
writer.write_u32::<LE>(self.uncompressed as u32)?
|
||||
} else {
|
||||
writer.write_u64::<LE>(self.uncompressed)?
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "wuthering-waves")]
|
||||
if version == Version::V12 {
|
||||
if is_uncompressed_size_32_bit_safe {
|
||||
writer.write_u32::<LE>(self.uncompressed as u32)?
|
||||
} else {
|
||||
writer.write_u64::<LE>(self.uncompressed)?
|
||||
}
|
||||
|
||||
if is_offset_32_bit_safe {
|
||||
writer.write_u32::<LE>(self.offset as u32)?;
|
||||
} else {
|
||||
writer.write_u64::<LE>(self.offset)?;
|
||||
}
|
||||
} else {
|
||||
if is_offset_32_bit_safe {
|
||||
writer.write_u32::<LE>(self.offset as u32)?;
|
||||
} else {
|
||||
writer.write_u64::<LE>(self.offset)?;
|
||||
}
|
||||
|
||||
if is_uncompressed_size_32_bit_safe {
|
||||
writer.write_u32::<LE>(self.uncompressed as u32)?
|
||||
} else {
|
||||
writer.write_u64::<LE>(self.uncompressed)?
|
||||
}
|
||||
}
|
||||
writer::offset_writer(
|
||||
writer,
|
||||
version,
|
||||
self.offset,
|
||||
is_offset_32_bit_safe,
|
||||
self.uncompressed,
|
||||
is_uncompressed_size_32_bit_safe,
|
||||
)?;
|
||||
|
||||
if self.compression_slot.is_some() {
|
||||
if is_size_32_bit_safe {
|
||||
writer.write_u32::<LE>(self.compressed as u32)?;
|
||||
} else {
|
||||
writer.write_u64::<LE>(self.compressed)?;
|
||||
}
|
||||
writer::write_safe(writer, is_size_32_bit_safe, self.compressed)?;
|
||||
|
||||
assert!(self.blocks.is_some());
|
||||
let blocks = self.blocks.as_ref().unwrap();
|
||||
|
|
|
@ -4,6 +4,8 @@ mod error;
|
|||
mod ext;
|
||||
mod footer;
|
||||
mod pak;
|
||||
mod reader;
|
||||
mod writer;
|
||||
|
||||
pub use {error::*, pak::*};
|
||||
|
||||
|
|
51
repak/src/reader.rs
Normal file
51
repak/src/reader.rs
Normal file
|
@ -0,0 +1,51 @@
|
|||
use std::io;
|
||||
|
||||
use byteorder::{LE, ReadBytesExt};
|
||||
|
||||
use crate::Version;
|
||||
|
||||
pub(crate) fn flag_reader<R: io::Read>(reader: &mut R,
|
||||
version: super::Version) -> Result<u32, super::Error> {
|
||||
let bits = reader.read_u32::<LE>()?;
|
||||
#[cfg(not(feature = "wuthering-waves"))]
|
||||
{ Ok(bits) }
|
||||
#[cfg(feature = "wuthering-waves")]
|
||||
if version == Version::V12 {
|
||||
reader.read_u8()?;
|
||||
Ok(
|
||||
(bits >> 16) & 0x3f |
|
||||
(bits & 0xFFFF) << 6 |
|
||||
(bits & (1 << 28)) >> 6 |
|
||||
(bits & 0x0FC00000) << 1 |
|
||||
bits & 0xE0000000
|
||||
)
|
||||
} else {
|
||||
Ok(bits)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn offset_reader<R: io::Read>(reader: &mut R,
|
||||
version: super::Version,
|
||||
bits: u32) -> Result<(u64, u64), super::Error> {
|
||||
let offset = read_safe(reader, bits, 31)?;
|
||||
let uncompressed = read_safe(reader, bits, 30)?;
|
||||
#[cfg(not(feature = "wuthering-waves"))]
|
||||
{ Ok((offset, uncompressed)) }
|
||||
#[cfg(feature = "wuthering-waves")]
|
||||
if version == Version::V12 {
|
||||
Ok((uncompressed, offset))
|
||||
} else {
|
||||
Ok((offset, uncompressed))
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn read_safe<R: io::Read>(reader: &mut R,
|
||||
bits: u32,
|
||||
bit: u32) -> Result<u64, super::Error> {
|
||||
Ok(if (bits & (1 << bit)) != 0 {
|
||||
reader.read_u32::<LE>()? as u64
|
||||
} else {
|
||||
reader.read_u64::<LE>()?
|
||||
})
|
||||
}
|
54
repak/src/writer.rs
Normal file
54
repak/src/writer.rs
Normal file
|
@ -0,0 +1,54 @@
|
|||
use std::io;
|
||||
|
||||
use byteorder::{LE, WriteBytesExt};
|
||||
|
||||
use crate::Version;
|
||||
|
||||
pub(crate) fn flag_writer<W: io::Write>(writer: &mut W,
|
||||
version: super::Version,
|
||||
flags: u32) -> Result<(), super::Error> {
|
||||
#[cfg(not(feature = "wuthering-waves"))]
|
||||
writer.write_u32::<LE>(flags)?;
|
||||
#[cfg(feature = "wuthering-waves")]
|
||||
if version == Version::V12 {
|
||||
let tmp = ((flags & 0x3f) << 16) | ((flags >> 6) & 0xFFFF) |
|
||||
((flags << 6) & (1 << 28)) | ((flags >> 1) & 0x0FC00000) | flags & 0xE0000000;
|
||||
writer.write_u32::<LE>(tmp)?;
|
||||
writer.write_u8(0)?;
|
||||
} else {
|
||||
writer.write_u32::<LE>(flags)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn offset_writer<W: io::Write>(writer: &mut W,
|
||||
version: super::Version,
|
||||
offset: u64,
|
||||
offset_safe_32: bool,
|
||||
uncompressed: u64,
|
||||
uncompressed_safe_32: bool) -> Result<(), super::Error> {
|
||||
#[cfg(not(feature = "wuthering-waves"))]
|
||||
{
|
||||
write_safe(writer, offset_safe_32, offset)?;
|
||||
write_safe(writer, uncompressed_safe_32, uncompressed)?;
|
||||
}
|
||||
#[cfg(feature = "wuthering-waves")]
|
||||
if version == Version::V12 {
|
||||
write_safe(writer, uncompressed_safe_32, uncompressed)?;
|
||||
write_safe(writer, offset_safe_32, offset)?;
|
||||
} else {
|
||||
write_safe(writer, offset_safe_32, offset)?;
|
||||
write_safe(writer, uncompressed_safe_32, uncompressed)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn write_safe<W: io::Write>(writer: &mut W, safe: bool, value: u64) -> Result<(), super::Error> {
|
||||
if safe {
|
||||
writer.write_u32::<LE>(value as u32)?
|
||||
} else {
|
||||
writer.write_u64::<LE>(value)?
|
||||
}
|
||||
Ok(())
|
||||
}
|
Loading…
Reference in a new issue