Remove old magic-based property enum
This is now replaced by the pre_assert and string-based version, which is superior.
This commit is contained in:
parent
ef0f4adb6f
commit
4eb1e7dd04
7 changed files with 24 additions and 71 deletions
|
@ -1,5 +1,5 @@
|
|||
use std::io::Seek;
|
||||
use binrw::{BinRead, BinReaderExt, BinResult};
|
||||
use binrw::BinRead;
|
||||
use binrw::BinResult;
|
||||
|
||||
pub(crate) fn read_bool_from<T: From<u8> + PartialEq>(x: T) -> bool {
|
||||
x == T::from(1u8)
|
||||
|
@ -9,7 +9,7 @@ pub(crate) fn read_bool_from<T: From<u8> + PartialEq>(x: T) -> bool {
|
|||
pub(crate) fn read_string_with_length() -> BinResult<String> {
|
||||
let length = u32::read_le(reader)? as usize;
|
||||
if length == 0 {
|
||||
return Ok(String::default());;
|
||||
return Ok(String::default());
|
||||
}
|
||||
// last byte is the null terminator which Rust ignores
|
||||
let length = length - 1;
|
||||
|
|
29
src/lib.rs
29
src/lib.rs
|
@ -13,6 +13,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::float_property::FloatProperty;
|
||||
use crate::int_property::IntProperty;
|
||||
use crate::map_property::MapProperty;
|
||||
|
@ -20,36 +21,12 @@ use crate::set_property::SetProperty;
|
|||
use crate::str_property::StrProperty;
|
||||
use crate::struct_property::StructProperty;
|
||||
use binrw::binrw;
|
||||
use crate::common::read_string_with_length;
|
||||
|
||||
#[binrw]
|
||||
#[derive(Debug)]
|
||||
pub enum Property {
|
||||
#[br(magic = b"IntProperty\0")]
|
||||
Int(IntProperty),
|
||||
#[br(magic = b"BoolProperty\0")]
|
||||
Bool(BoolProperty),
|
||||
#[br(magic = b"StructProperty\0")]
|
||||
Struct(StructProperty),
|
||||
#[br(magic = b"FloatProperty\0")]
|
||||
Float(FloatProperty),
|
||||
#[br(magic = b"StrProperty\0")]
|
||||
String(StrProperty),
|
||||
#[br(magic = b"NameProperty\0")]
|
||||
Name(StrProperty),
|
||||
#[br(magic = b"ArrayProperty\0")]
|
||||
Array(ArrayProperty),
|
||||
#[br(magic = b"MapProperty\0")]
|
||||
Map(MapProperty),
|
||||
#[br(magic = b"SetProperty\0")]
|
||||
Set(SetProperty),
|
||||
}
|
||||
|
||||
// Used in ArrayProperty exclusively, but could be used instead of magic above
|
||||
#[binrw]
|
||||
#[derive(Debug)]
|
||||
#[br(import { magic: &str })]
|
||||
pub enum StringBasedProperty {
|
||||
pub enum Property {
|
||||
#[br(pre_assert("NameProperty" == magic))]
|
||||
Name(StrProperty),
|
||||
#[br(pre_assert("StructProperty" == magic))]
|
||||
|
@ -83,7 +60,7 @@ pub struct Entry {
|
|||
pub type_name: String,
|
||||
|
||||
#[br(if(name != "None"), args { magic: &type_name })]
|
||||
pub r#type: Option<StringBasedProperty>,
|
||||
pub r#type: Option<Property>,
|
||||
}
|
||||
|
||||
#[binrw]
|
||||
|
|
|
@ -189,7 +189,11 @@ fn custom_parser(
|
|||
let end = current + size_in_bytes as u64 - 5 - 3;
|
||||
|
||||
while current < end {
|
||||
result.push(MapEntry::read_options(reader, endian, (key_type, value_type))?);
|
||||
result.push(MapEntry::read_options(
|
||||
reader,
|
||||
endian,
|
||||
(key_type, value_type),
|
||||
)?);
|
||||
current = reader.stream_position()?;
|
||||
}
|
||||
Ok(result)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::StringBasedProperty;
|
||||
use crate::Property;
|
||||
use crate::common::read_string_with_length;
|
||||
use binrw::binrw;
|
||||
|
||||
|
@ -14,7 +14,7 @@ pub struct SetEntry {
|
|||
pub unk_type: String,
|
||||
|
||||
#[br(args { magic: &unk_type })]
|
||||
pub key: StringBasedProperty,
|
||||
pub key: Property,
|
||||
}
|
||||
|
||||
#[binrw]
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use binrw::binrw;
|
||||
use crate::common::read_string_with_length;
|
||||
use binrw::binrw;
|
||||
|
||||
#[binrw]
|
||||
#[derive(Debug)]
|
||||
|
@ -22,16 +22,8 @@ mod tests {
|
|||
fn empty_string() {
|
||||
// From Slot.sav
|
||||
let data = [
|
||||
0x04, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00,
|
||||
0x00, 0x0b, 0x00,
|
||||
0x00, 0x00, 0x4c,
|
||||
0x6f, 0x61, 0x64,
|
||||
0x4f, 0x70, 0x74,
|
||||
0x69, 0x6f, 0x6e,
|
||||
0x00,
|
||||
0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b,
|
||||
0x00, 0x00, 0x00, 0x4c, 0x6f, 0x61, 0x64, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x00,
|
||||
];
|
||||
let mut cursor = Cursor::new(data);
|
||||
let decoded = StrProperty::read_le(&mut cursor).unwrap();
|
||||
|
@ -42,19 +34,9 @@ mod tests {
|
|||
fn regular_string() {
|
||||
// From Slot.sav
|
||||
let data = [
|
||||
0x1e, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00,
|
||||
0x1a, 0x00, 0x00,
|
||||
0x00, 0x41, 0x52,
|
||||
0x30, 0x58, 0x4a,
|
||||
0x47, 0x46, 0x57,
|
||||
0x41, 0x36, 0x48,
|
||||
0x4e, 0x49, 0x51,
|
||||
0x31, 0x41, 0x41,
|
||||
0x55, 0x4a, 0x39,
|
||||
0x55, 0x52, 0x38,
|
||||
0x32, 0x38, 0x00,
|
||||
0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1a, 0x00, 0x00, 0x00, 0x41,
|
||||
0x52, 0x30, 0x58, 0x4a, 0x47, 0x46, 0x57, 0x41, 0x36, 0x48, 0x4e, 0x49, 0x51, 0x31,
|
||||
0x41, 0x41, 0x55, 0x4a, 0x39, 0x55, 0x52, 0x38, 0x32, 0x38, 0x00,
|
||||
];
|
||||
let mut cursor = Cursor::new(data);
|
||||
let decoded = StrProperty::read_le(&mut cursor).unwrap();
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use crate::structs::{
|
||||
CarryCountProperty, DAAssembleIdDataStruct, DABuildDataStruct, DACharacterCommonStatusStruct,
|
||||
DALoadOptionStruct, DAMachineColoringDataStruct, DAModuleColorStruct, DAModuleItemDataStruct,
|
||||
DateTimeStruct, GuidStruct, LinearColorStruct, ParamsStruct, PrimaryAssetIdStruct,
|
||||
PrimaryAssetNameProperty, SaveSlotInfoStruct,
|
||||
DateTimeStruct, GuidStruct, LinearColorStruct, PrimaryAssetIdStruct, PrimaryAssetNameProperty,
|
||||
SaveSlotInfoStruct,
|
||||
};
|
||||
use binrw::binrw;
|
||||
|
||||
|
@ -17,8 +17,6 @@ pub enum Struct {
|
|||
SaveSlotInfo(SaveSlotInfoStruct),
|
||||
#[br(magic = b"DACharacterCommonStatus\0")]
|
||||
DACharacterCommonStatus(DACharacterCommonStatusStruct),
|
||||
#[br(magic = b"Params\0")]
|
||||
Params(ParamsStruct),
|
||||
#[br(magic = b"PrimaryAssetType\0")]
|
||||
PrimaryAssetType {
|
||||
#[br(pad_before = 17)]
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::{Property, StringBasedProperty};
|
||||
use crate::Property;
|
||||
use binrw::binrw;
|
||||
|
||||
#[binrw]
|
||||
|
@ -25,14 +25,6 @@ pub struct DACharacterCommonStatusStruct {
|
|||
pub unk: [u8; 17],
|
||||
}
|
||||
|
||||
#[binrw]
|
||||
#[derive(Debug)]
|
||||
pub struct ParamsStruct {
|
||||
pub params_value_type_length: u32,
|
||||
|
||||
pub params_value: Box<Property>,
|
||||
}
|
||||
|
||||
#[binrw]
|
||||
#[derive(Debug)]
|
||||
pub struct PrimaryAssetNameProperty {
|
||||
|
@ -57,7 +49,7 @@ pub struct PrimaryAssetNameProperty {
|
|||
#[br(if(property_name != "None"))]
|
||||
#[br(args { magic: &type_name})]
|
||||
//
|
||||
pub key: Option<Box<StringBasedProperty>>,
|
||||
pub key: Option<Box<Property>>,
|
||||
}
|
||||
|
||||
#[binrw]
|
||||
|
@ -73,7 +65,7 @@ pub struct CarryCountProperty {
|
|||
|
||||
#[br(args { magic: &property_name})]
|
||||
//
|
||||
pub key: Option<Box<StringBasedProperty>>,
|
||||
pub key: Option<Box<Property>>,
|
||||
}
|
||||
|
||||
#[binrw]
|
||||
|
|
Loading…
Add table
Reference in a new issue