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