2025-02-23 16:50:10 -05:00
|
|
|
use crate::common::{read_string_with_length, write_string_with_length};
|
2025-02-23 15:25:50 -05:00
|
|
|
use crate::map_property::MabSubProperty;
|
|
|
|
use binrw::{BinRead, BinResult, binrw};
|
2025-02-23 14:38:35 -05:00
|
|
|
|
|
|
|
#[binrw::parser(reader, endian)]
|
2025-02-23 15:02:08 -05:00
|
|
|
fn custom_parser(size_in_bytes: u32, value_type: &str) -> BinResult<Vec<ArrayEntry>> {
|
|
|
|
let mut result = Vec::<ArrayEntry>::new();
|
2025-02-23 14:38:35 -05:00
|
|
|
|
2025-02-23 15:26:52 -05:00
|
|
|
let mut current = reader.stream_position()?;
|
2025-02-23 14:38:35 -05:00
|
|
|
let end = current + size_in_bytes as u64 - 4;
|
|
|
|
|
|
|
|
while current < end {
|
2025-02-23 15:26:52 -05:00
|
|
|
result.push(ArrayEntry::read_options(reader, endian, (value_type,))?);
|
|
|
|
current = reader.stream_position()?;
|
2025-02-23 14:38:35 -05:00
|
|
|
}
|
|
|
|
Ok(result)
|
|
|
|
}
|
2025-02-19 19:34:13 -05:00
|
|
|
|
2025-02-23 15:02:08 -05:00
|
|
|
#[binrw]
|
|
|
|
#[derive(Debug)]
|
|
|
|
#[br(import(value_type: &str))]
|
|
|
|
pub struct ArrayEntry {
|
|
|
|
#[br(args { magic: &value_type })]
|
|
|
|
pub key: MabSubProperty,
|
|
|
|
}
|
|
|
|
|
2025-02-23 16:50:10 -05:00
|
|
|
fn calc_size_in_bytes(entries: &Vec<ArrayEntry>) -> u32 {
|
|
|
|
// TODO: stub
|
|
|
|
18
|
|
|
|
}
|
|
|
|
|
2025-02-19 19:34:13 -05:00
|
|
|
#[binrw]
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ArrayProperty {
|
2025-02-23 16:50:10 -05:00
|
|
|
#[bw(calc = calc_size_in_bytes(entries))]
|
2025-02-23 14:38:35 -05:00
|
|
|
pub size_in_bytes: u32,
|
|
|
|
|
2025-02-23 16:50:10 -05:00
|
|
|
#[brw(pad_before = 4)]
|
|
|
|
#[br(parse_with = read_string_with_length)]
|
|
|
|
#[bw(write_with = write_string_with_length)]
|
2025-02-19 19:34:13 -05:00
|
|
|
pub key_name: String,
|
|
|
|
|
2025-02-23 16:50:10 -05:00
|
|
|
#[brw(pad_before = 1)]
|
|
|
|
#[bw(calc = 1)]
|
2025-02-23 14:38:35 -05:00
|
|
|
pub unk: u32,
|
2025-02-19 19:34:13 -05:00
|
|
|
|
2025-02-23 15:02:08 -05:00
|
|
|
#[br(parse_with = custom_parser, args(size_in_bytes, &key_name))]
|
|
|
|
pub entries: Vec<ArrayEntry>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use crate::map_property::MabSubProperty::String;
|
2025-02-23 16:50:10 -05:00
|
|
|
use binrw::{BinRead, BinWrite};
|
2025-02-23 15:02:08 -05:00
|
|
|
use std::io::Cursor;
|
2025-02-23 16:50:10 -05:00
|
|
|
use crate::map_property::MapSubStrProperty;
|
2025-02-23 15:02:08 -05:00
|
|
|
|
|
|
|
#[test]
|
2025-02-23 16:50:10 -05:00
|
|
|
fn read_simple_array() {
|
2025-02-23 15:02:08 -05:00
|
|
|
// From Slot.sav
|
|
|
|
let data = [
|
2025-02-23 15:25:50 -05:00
|
|
|
0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x53, 0x74,
|
|
|
|
0x72, 0x50, 0x72, 0x6f, 0x70, 0x65, 0x72, 0x74, 0x79, 0x00, 0x00, 0x01, 0x00, 0x00,
|
|
|
|
0x00, 0x0a, 0x00, 0x00, 0x00, 0x72, 0x65, 0x64, 0x73, 0x74, 0x72, 0x61, 0x74, 0x65,
|
2025-02-23 15:02:08 -05:00
|
|
|
0x00,
|
|
|
|
];
|
|
|
|
let mut cursor = Cursor::new(data);
|
|
|
|
let decoded = ArrayProperty::read_le(&mut cursor).unwrap();
|
|
|
|
assert_eq!(decoded.key_name, "StrProperty");
|
|
|
|
let String(value_property) = &decoded.entries.first().unwrap().key else {
|
|
|
|
panic!("StrProperty!")
|
|
|
|
};
|
2025-02-23 15:25:50 -05:00
|
|
|
assert_eq!(value_property.value, "redstrate");
|
2025-02-23 15:02:08 -05:00
|
|
|
}
|
2025-02-23 16:50:10 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn write_simple_array() {
|
|
|
|
let expected_data: [u8; 43] = [
|
|
|
|
0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x53, 0x74,
|
|
|
|
0x72, 0x50, 0x72, 0x6f, 0x70, 0x65, 0x72, 0x74, 0x79, 0x00, 0x00, 0x01, 0x00, 0x00,
|
|
|
|
0x00, 0x0a, 0x00, 0x00, 0x00, 0x72, 0x65, 0x64, 0x73, 0x74, 0x72, 0x61, 0x74, 0x65,
|
|
|
|
0x00,
|
|
|
|
];
|
|
|
|
let property = ArrayProperty {
|
|
|
|
key_name: "StrProperty".to_string(),
|
|
|
|
entries: vec![
|
|
|
|
ArrayEntry {
|
|
|
|
key: String(MapSubStrProperty {
|
|
|
|
value: "redstrate".to_string(),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut buffer: Vec<u8> = Vec::new();
|
|
|
|
{
|
|
|
|
let mut cursor = Cursor::new(&mut buffer);
|
|
|
|
property.write_le(&mut cursor).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(expected_data, &buffer[..]);
|
|
|
|
}
|
2025-02-19 19:34:13 -05:00
|
|
|
}
|