From 7f7e0b9f780bfa715f7e6cb3d4b899d55939296e Mon Sep 17 00:00:00 2001 From: xavo95 Date: Mon, 13 Jan 2025 19:23:54 +0100 Subject: [PATCH] Cleaning code --- repak/src/entry.rs | 114 ++++++++------------------------------------ repak/src/lib.rs | 2 + repak/src/reader.rs | 51 ++++++++++++++++++++ repak/src/writer.rs | 54 +++++++++++++++++++++ 4 files changed, 126 insertions(+), 95 deletions(-) create mode 100644 repak/src/reader.rs create mode 100644 repak/src/writer.rs diff --git a/repak/src/entry.rs b/repak/src/entry.rs index a0a1643..517493d 100644 --- a/repak/src/entry.rs +++ b/repak/src/entry.rs @@ -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 { - - let bits = { - let tmp = reader.read_u32::()?; - #[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::()? as u64 - } else { - reader.read_u64::()? - }) - }; - - 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::(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::(tmp)?; - writer.write_u8(0)?; - } else { - writer.write_u32::(flags)?; - } + writer::flag_writer(writer, version, flags)?; if compression_block_size == 0x3f { writer.write_u32::(self.compression_block_size)?; } - #[cfg(not(feature = "wuthering-waves"))] - { - if is_offset_32_bit_safe { - writer.write_u32::(self.offset as u32)?; - } else { - writer.write_u64::(self.offset)?; - } - - if is_uncompressed_size_32_bit_safe { - writer.write_u32::(self.uncompressed as u32)? - } else { - writer.write_u64::(self.uncompressed)? - } - } - #[cfg(feature = "wuthering-waves")] - if version == Version::V12 { - if is_uncompressed_size_32_bit_safe { - writer.write_u32::(self.uncompressed as u32)? - } else { - writer.write_u64::(self.uncompressed)? - } - - if is_offset_32_bit_safe { - writer.write_u32::(self.offset as u32)?; - } else { - writer.write_u64::(self.offset)?; - } - } else { - if is_offset_32_bit_safe { - writer.write_u32::(self.offset as u32)?; - } else { - writer.write_u64::(self.offset)?; - } - - if is_uncompressed_size_32_bit_safe { - writer.write_u32::(self.uncompressed as u32)? - } else { - writer.write_u64::(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::(self.compressed as u32)?; - } else { - writer.write_u64::(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(); diff --git a/repak/src/lib.rs b/repak/src/lib.rs index 8fbf2f0..6b93f9d 100644 --- a/repak/src/lib.rs +++ b/repak/src/lib.rs @@ -4,6 +4,8 @@ mod error; mod ext; mod footer; mod pak; +mod reader; +mod writer; pub use {error::*, pak::*}; diff --git a/repak/src/reader.rs b/repak/src/reader.rs new file mode 100644 index 0000000..92986f9 --- /dev/null +++ b/repak/src/reader.rs @@ -0,0 +1,51 @@ +use std::io; + +use byteorder::{LE, ReadBytesExt}; + +use crate::Version; + +pub(crate) fn flag_reader(reader: &mut R, + version: super::Version) -> Result { + let bits = reader.read_u32::()?; + #[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(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(reader: &mut R, + bits: u32, + bit: u32) -> Result { + Ok(if (bits & (1 << bit)) != 0 { + reader.read_u32::()? as u64 + } else { + reader.read_u64::()? + }) +} \ No newline at end of file diff --git a/repak/src/writer.rs b/repak/src/writer.rs new file mode 100644 index 0000000..e000c03 --- /dev/null +++ b/repak/src/writer.rs @@ -0,0 +1,54 @@ +use std::io; + +use byteorder::{LE, WriteBytesExt}; + +use crate::Version; + +pub(crate) fn flag_writer(writer: &mut W, + version: super::Version, + flags: u32) -> Result<(), super::Error> { + #[cfg(not(feature = "wuthering-waves"))] + writer.write_u32::(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::(tmp)?; + writer.write_u8(0)?; + } else { + writer.write_u32::(flags)?; + } + Ok(()) +} + +pub(crate) fn offset_writer(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(writer: &mut W, safe: bool, value: u64) -> Result<(), super::Error> { + if safe { + writer.write_u32::(value as u32)? + } else { + writer.write_u64::(value)? + } + Ok(()) +} \ No newline at end of file