Skip to content

Commit

Permalink
Refactor property length
Browse files Browse the repository at this point in the history
  • Loading branch information
scottanderson committed Jan 4, 2024
1 parent 491b004 commit 6e48a35
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 29 deletions.
8 changes: 4 additions & 4 deletions src/properties/array_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ impl ArrayProperty {
pub(crate) fn read_body<R: Read + Seek>(
cursor: &mut R,
options: &mut PropertyOptions,
length: u64,
length: u32,
property_type: String,
) -> Result<Self, Error> {
let property_count = cursor.read_u32::<LittleEndian>()? as usize;
let mut properties: Vec<Property> = Vec::with_capacity(property_count);
let property_count = cursor.read_u32::<LittleEndian>()?;
let mut properties: Vec<Property> = Vec::with_capacity(property_count as usize);

let mut array_struct_info = None;

Expand Down Expand Up @@ -139,7 +139,7 @@ impl ArrayProperty {
}
_ => {
let suggested_length = if property_count > 0 && length >= 4 {
Some((length - 4) / property_count as u64)
Some((length - 4) / property_count)
} else {
None
};
Expand Down
6 changes: 4 additions & 2 deletions src/properties/int_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,13 @@ impl ByteProperty {
pub(crate) fn read<R: Read + Seek>(
cursor: &mut R,
include_header: bool,
mut suggested_length: Option<u64>,
mut suggested_length: Option<u32>,
) -> Result<Self, Error> {
let mut name = None;
if include_header {
let length = cursor.read_u64::<LittleEndian>()?;
let length = cursor.read_u32::<LittleEndian>()?;
let unknown = cursor.read_u32::<LittleEndian>()?;
assert_eq!(unknown, 0);
suggested_length = Some(length);

name = Some(cursor.read_string()?);
Expand Down
26 changes: 16 additions & 10 deletions src/properties/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,19 @@ macro_rules! impl_read_header {
reader: &mut R,
options: &mut PropertyOptions,
) -> Result<Self, Error> {
let length = reader.read_u64::<LittleEndian>()?;
let length = reader.read_u32::<LittleEndian>()?;
let unknown = reader.read_u32::<LittleEndian>()?;
assert_eq!(unknown, 0, "Expected unknown value zero @ {:#x}", reader.stream_position()? - 4);
$(
let $var = reader.read_string()?;
)*
let separator = reader.read_u8()?;
assert_eq!(separator, 0);
assert_eq!(separator, 0, "Expected separator value zero @ {:#x}", reader.stream_position()?-1);

let start = reader.stream_position()?;
let result = Self::read_body(reader, options, length $(, $var)*)?;
let end = reader.stream_position()?;
assert_eq!(end - start, length);
assert_eq!(end - start, length as u64, "read_body did not read the expected length {:#x}", length);

Ok(result)
}
Expand All @@ -157,17 +159,19 @@ macro_rules! impl_read_header {
reader: &mut R,
options: &mut PropertyOptions,
) -> Result<Self, Error> {
let length = reader.read_u64::<LittleEndian>()?;
let length = reader.read_u32::<LittleEndian>()?;
let unknown = reader.read_u32::<LittleEndian>()?;
assert_eq!(unknown, 0, "Expected unknown value zero @ {:#x}", reader.stream_position()? - 4);
$(
let $var = reader.read_string()?;
)*
let separator = reader.read_u8()?;
assert_eq!(separator, 0);
assert_eq!(separator, 0, "Expected separator value zero @ {:#x}", reader.stream_position()?-1);

let start = reader.stream_position()?;
let result = Self::read_body(reader, options $(, $var)*)?;
let end = reader.stream_position()?;
assert_eq!(end - start, length);
assert_eq!(end - start, length as u64, "read_body did not read the expected length {:#x}", length);

Ok(result)
}
Expand All @@ -178,17 +182,19 @@ macro_rules! impl_read_header {
fn read_header<R: Read + Seek>(
reader: &mut R,
) -> Result<Self, Error> {
let length = reader.read_u64::<LittleEndian>()?;
let length = reader.read_u32::<LittleEndian>()?;
let unknown = reader.read_u32::<LittleEndian>()?;
assert_eq!(unknown, 0, "Expected unknown value zero @ {:#x}", reader.stream_position()? - 4);
$(
let $var = reader.read_string()?;
)*
let separator = reader.read_u8()?;
assert_eq!(separator, 0);
assert_eq!(separator, 0, "Expected separator value zero @ {:#x}", reader.stream_position()?-1);

let start = reader.stream_position()?;
let result = Self::read_body(reader $(, Some($var))*)?;
let end = reader.stream_position()?;
assert_eq!(end - start, length);
assert_eq!(end - start, length as u64, "read_body did not read the expected length {:#x}", length);

Ok(result)
}
Expand Down Expand Up @@ -456,7 +462,7 @@ impl Property {
value_type: &str,
include_header: bool,
options: &mut PropertyOptions,
suggested_length: Option<u64>,
suggested_length: Option<u32>,
) -> Result<Self, Error> {
let _stack_entry = ScopedStackEntry::new(options.properties_stack, value_type.to_string());
match value_type {
Expand Down
12 changes: 5 additions & 7 deletions src/properties/name_property.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use std::io::{Cursor, Read, Seek, Write};

use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use unreal_helpers::{UnrealReadExt, UnrealWriteExt};

use crate::{
cursor_ext::{ReadExt, WriteExt},
error::Error,
};
use crate::{cursor_ext::WriteExt, error::Error};

use super::{impl_read, impl_read_header, impl_write, PropertyOptions, PropertyTrait};

Expand All @@ -14,7 +12,7 @@ use super::{impl_read, impl_read_header, impl_write, PropertyOptions, PropertyTr
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct NameProperty {
/// Name value.
pub value: String,
pub value: Option<String>,
}

impl_write!(NameProperty);
Expand All @@ -25,13 +23,13 @@ impl NameProperty {

#[inline]
fn read_body<R: Read + Seek>(cursor: &mut R) -> Result<Self, Error> {
let value = cursor.read_string()?;
let value = cursor.read_fstring()?;
Ok(NameProperty { value })
}

#[inline]
fn write_body<W: Write>(&self, cursor: &mut W) -> Result<(), Error> {
cursor.write_string(&self.value)?;
cursor.write_fstring(self.value.as_deref())?;
Ok(())
}
}
8 changes: 4 additions & 4 deletions src/properties/set_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ impl SetProperty {
pub(crate) fn read_body<R: Read + Seek>(
cursor: &mut R,
options: &mut PropertyOptions,
length: u64,
length: u32,
property_type: String,
) -> Result<Self, Error> {
let allocation_flags = cursor.read_u32::<LittleEndian>()?;

let element_count = cursor.read_u32::<LittleEndian>()? as usize;
let mut properties: Vec<Property> = Vec::with_capacity(element_count);
let element_count = cursor.read_u32::<LittleEndian>()?;
let mut properties: Vec<Property> = Vec::with_capacity(element_count as usize);

let total_bytes_per_property = (length - 8) / element_count as u64;
let total_bytes_per_property = (length - 8) / element_count;

for _ in 0..element_count {
properties.push(Property::new(
Expand Down
6 changes: 4 additions & 2 deletions src/properties/unknown_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl UnknownProperty {
pub(crate) fn read_with_length<R: Read + Seek>(
cursor: &mut R,
property_name: String,
length: u64,
length: u32,
) -> Result<Self, Error> {
let mut data = vec![0u8; length as usize];
cursor.read_exact(&mut data)?;
Expand All @@ -41,7 +41,9 @@ impl UnknownProperty {
cursor: &mut R,
property_name: String,
) -> Result<Self, Error> {
let length = cursor.read_u64::<LittleEndian>()?;
let length = cursor.read_u32::<LittleEndian>()?;
let unknown = cursor.read_u32::<LittleEndian>()?;
assert_eq!(unknown, 0);
let separator = cursor.read_u8()?;
assert_eq!(separator, 0);

Expand Down

0 comments on commit 6e48a35

Please sign in to comment.