Add roundtrip test for LocalProfile.sav

This commit is contained in:
Joshua Goins 2025-02-23 17:04:26 -05:00
parent 46e0cb3fb2
commit 02269ecc68
3 changed files with 45 additions and 4 deletions

View file

@ -14,7 +14,7 @@ use binrw::helpers::{until, until_eof};
use crate::array_property::ArrayProperty; use crate::array_property::ArrayProperty;
use crate::bool_property::BoolProperty; 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::float_property::FloatProperty;
use crate::int_property::IntProperty; use crate::int_property::IntProperty;
use crate::map_property::MapProperty; use crate::map_property::MapProperty;
@ -52,12 +52,12 @@ pub enum Property {
#[derive(Debug)] #[derive(Debug)]
pub struct Entry { pub struct Entry {
#[br(parse_with = read_string_with_length)] #[br(parse_with = read_string_with_length)]
#[bw(ignore)] #[bw(write_with = write_string_with_length)]
pub name: String, pub name: String,
#[br(parse_with = read_string_with_length)] #[br(parse_with = read_string_with_length)]
#[bw(ignore)] #[bw(write_with = write_string_with_length)]
#[br(temp, if(name != "None"))] #[br(if(name != "None"))]
pub type_name: String, pub type_name: String,
#[br(if(name != "None"), args { magic: &type_name })] #[br(if(name != "None"), args { magic: &type_name })]
@ -72,6 +72,14 @@ pub struct TaggedObject {
pub entries: Vec<Entry>, pub entries: Vec<Entry>,
} }
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] #[binrw]
#[derive(Debug)] #[derive(Debug)]
pub struct TaggedSerialization { pub struct TaggedSerialization {

Binary file not shown.

33
tests/roundtrip.rs Normal file
View file

@ -0,0 +1,33 @@
use std::fs::read;
use std::io::Cursor;
use std::path::PathBuf;
use binrw::{BinRead, BinWrite};
use ireko::TaggedSerialization;
#[test]
fn roundtrip_localprofile() {
let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
d.push("tests/resources");
d.push("LocalProfile.bin");
let mut data = read(d).unwrap();
let mut cursor = Cursor::new(&mut data);
let local_profile = TaggedSerialization::read_le(&mut cursor).unwrap();
let tagged_object = &local_profile.objs[0];
assert_eq!(tagged_object.size_in_bytes, 339);
tagged_object.entry("SavedDataVersion").unwrap();
tagged_object.entry("bDemoVersion").unwrap();
// TODO: check values
let mut new_data: Vec<u8> = 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[..]);
}