From 02269ecc683a4327f6aa4ca80607b9cadc52c3fc Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Sun, 23 Feb 2025 17:04:26 -0500 Subject: [PATCH] Add roundtrip test for LocalProfile.sav --- src/lib.rs | 16 +++++++++++---- tests/resources/LocalProfile.bin | Bin 0 -> 347 bytes tests/roundtrip.rs | 33 +++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 tests/resources/LocalProfile.bin create mode 100644 tests/roundtrip.rs diff --git a/src/lib.rs b/src/lib.rs index 319b041..35d6eb7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,7 +14,7 @@ use binrw::helpers::{until, until_eof}; use crate::array_property::ArrayProperty; use crate::bool_property::BoolProperty; -use crate::common::read_string_with_length; +use crate::common::{read_string_with_length, write_string_with_length}; use crate::float_property::FloatProperty; use crate::int_property::IntProperty; use crate::map_property::MapProperty; @@ -52,12 +52,12 @@ pub enum Property { #[derive(Debug)] pub struct Entry { #[br(parse_with = read_string_with_length)] - #[bw(ignore)] + #[bw(write_with = write_string_with_length)] pub name: String, #[br(parse_with = read_string_with_length)] - #[bw(ignore)] - #[br(temp, if(name != "None"))] + #[bw(write_with = write_string_with_length)] + #[br(if(name != "None"))] pub type_name: String, #[br(if(name != "None"), args { magic: &type_name })] @@ -72,6 +72,14 @@ pub struct TaggedObject { pub entries: Vec, } +impl TaggedObject { + pub fn entry(&self, key: &str) -> Option<&Entry> { + let entries: Vec<&Entry> = self.entries.iter().filter(|e| e.name == key).collect(); + + entries.first().copied() + } +} + #[binrw] #[derive(Debug)] pub struct TaggedSerialization { diff --git a/tests/resources/LocalProfile.bin b/tests/resources/LocalProfile.bin new file mode 100644 index 0000000000000000000000000000000000000000..e7f9ac3ef04180e622a966c2087467e0b8b2dcd9 GIT binary patch literal 347 zcmZus!3x4K49!6Cq6hKj$xm<|M30r}RCMUZoY&4p8EmU`Mey%UiVhJ2p~>rec}d~` zM3jA?S3WTFq>(4ob*?Jt61b_%L907z^Mn@lP4uWrJuKCS&A>vbqM?7N4h*Oa@s`&H zHD-Y = Vec::new(); + { + let mut new_cursor = Cursor::new(&mut new_data); + local_profile.write_le(&mut new_cursor).unwrap(); + } + + // Ensure our written version is the same as retail + assert_eq!(new_data.as_slice(), &data[..]); +}