Skip to content

Commit

Permalink
Improved ergonomics for HashableIndexMap
Browse files Browse the repository at this point in the history
  • Loading branch information
scottanderson authored and localcc committed Oct 21, 2024
1 parent c6f183b commit f8d5d93
Show file tree
Hide file tree
Showing 15 changed files with 203 additions and 121 deletions.
39 changes: 19 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,24 +89,23 @@ use std::{
io::{Read, Seek, Write},
};

use crate::error::DeserializeError;
use crate::game_version::{
DeserializedGameVersion, GameVersion, PalworldCompressionType, PLZ_MAGIC,
};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use cursor_ext::{ReadExt, WriteExt};
use custom_version::FCustomVersion;
use engine_version::FEngineVersion;
use error::Error;
use flate2::read::ZlibDecoder;
use flate2::write::ZlibEncoder;
use flate2::Compression;
use indexmap::IndexMap;
use object_version::EUnrealEngineObjectUE5Version;
use ord_ext::OrdExt;
use properties::{Property, PropertyOptions, PropertyTrait};
use savegame_version::SaveGameVersion;
use types::Guid;

use crate::{
cursor_ext::{ReadExt, WriteExt},
custom_version::FCustomVersion,
engine_version::FEngineVersion,
error::{DeserializeError, Error},
game_version::{DeserializedGameVersion, GameVersion, PalworldCompressionType, PLZ_MAGIC},
object_version::EUnrealEngineObjectUE5Version,
ord_ext::OrdExt,
properties::{Property, PropertyOptions, PropertyTrait},
savegame_version::SaveGameVersion,
types::{map::HashableIndexMap, Guid},
};

/// The four bytes 'GVAS' appear at the beginning of every GVAS file.
pub const FILE_TYPE_GVAS: u32 = u32::from_le_bytes([b'G', b'V', b'A', b'S']);
Expand All @@ -125,7 +124,7 @@ pub enum GvasHeader {
/// Custom version format.
custom_version_format: u32,
/// Custom versions.
custom_versions: IndexMap<Guid, u32>,
custom_versions: HashableIndexMap<Guid, u32>,
/// Save game class name.
save_game_class_name: String,
},
Expand All @@ -140,7 +139,7 @@ pub enum GvasHeader {
/// Custom version format.
custom_version_format: u32,
/// Custom versions.
custom_versions: IndexMap<Guid, u32>,
custom_versions: HashableIndexMap<Guid, u32>,
/// Save game class name.
save_game_class_name: String,
},
Expand Down Expand Up @@ -222,7 +221,7 @@ impl GvasHeader {
}

let custom_versions_len = cursor.read_u32::<LittleEndian>()?;
let mut custom_versions = IndexMap::new();
let mut custom_versions = HashableIndexMap::with_capacity(custom_versions_len as usize);
for _ in 0..custom_versions_len {
let FCustomVersion { key, version } = FCustomVersion::read(cursor)?;
custom_versions.insert(key, version);
Expand Down Expand Up @@ -315,7 +314,7 @@ impl GvasHeader {
}

/// Get custom versions from this header
pub fn get_custom_versions(&self) -> &IndexMap<Guid, u32> {
pub fn get_custom_versions(&self) -> &HashableIndexMap<Guid, u32> {
match self {
GvasHeader::Version2 {
custom_versions, ..
Expand All @@ -340,7 +339,7 @@ pub struct GvasFile {
/// GVAS file header.
pub header: GvasHeader,
/// GVAS properties.
pub properties: IndexMap<String, Property>,
pub properties: HashableIndexMap<String, Property>,
}

trait GvasHeaderTrait {
Expand Down Expand Up @@ -481,7 +480,7 @@ impl GvasFile {
custom_versions: header.get_custom_versions(),
};

let mut properties = IndexMap::new();
let mut properties = HashableIndexMap::new();
loop {
let property_name = cursor.read_string()?;
if property_name == "None" {
Expand Down
27 changes: 13 additions & 14 deletions src/properties/map_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::{
};

use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use indexmap::IndexMap;

use crate::{
cursor_ext::{ReadExt, WriteExt},
Expand Down Expand Up @@ -103,7 +102,7 @@ impl MapProperty {
key_type: String,
value_type: String,
allocation_flags: u32,
value: IndexMap<Property, Property>,
value: HashableIndexMap<Property, Property>,
) -> Self {
match (key_type.as_str(), value_type.as_str(), allocation_flags) {
("EnumProperty", "BoolProperty", 0) => match value
Expand Down Expand Up @@ -132,7 +131,7 @@ impl MapProperty {
key_type,
value_type,
allocation_flags,
value: HashableIndexMap(value),
value,
},
},

Expand All @@ -157,7 +156,7 @@ impl MapProperty {
key_type,
value_type,
allocation_flags,
value: HashableIndexMap(value),
value,
},
},

Expand All @@ -184,7 +183,7 @@ impl MapProperty {
key_type,
value_type,
allocation_flags,
value: HashableIndexMap(value),
value,
},
}
}
Expand All @@ -210,7 +209,7 @@ impl MapProperty {
key_type,
value_type,
allocation_flags,
value: HashableIndexMap(value),
value,
},
},

Expand All @@ -235,7 +234,7 @@ impl MapProperty {
key_type,
value_type,
allocation_flags,
value: HashableIndexMap(value),
value,
},
},

Expand All @@ -262,7 +261,7 @@ impl MapProperty {
key_type,
value_type,
allocation_flags,
value: HashableIndexMap(value),
value,
},
}
}
Expand All @@ -285,7 +284,7 @@ impl MapProperty {
key_type,
value_type,
allocation_flags,
value: HashableIndexMap(value),
value,
},
},

Expand All @@ -307,7 +306,7 @@ impl MapProperty {
key_type,
value_type,
allocation_flags,
value: HashableIndexMap(value),
value,
},
},

Expand All @@ -329,7 +328,7 @@ impl MapProperty {
key_type,
value_type,
allocation_flags,
value: HashableIndexMap(value),
value,
},
},

Expand All @@ -352,7 +351,7 @@ impl MapProperty {
key_type,
value_type,
allocation_flags,
value: HashableIndexMap(value),
value,
},
}
}
Expand All @@ -361,7 +360,7 @@ impl MapProperty {
key_type,
value_type,
allocation_flags,
value: HashableIndexMap(value),
value,
},
}
}
Expand Down Expand Up @@ -466,7 +465,7 @@ impl MapProperty {
let allocation_flags = cursor.read_u32::<LittleEndian>()?;
let element_count = cursor.read_u32::<LittleEndian>()?;

let mut map = IndexMap::new();
let mut map = HashableIndexMap::with_capacity(element_count as usize);
for _ in 0..element_count {
let properties_stack = &mut options.properties_stack;
let key_stack_entry = ScopedStackEntry::new(properties_stack, "Key".to_string());
Expand Down
5 changes: 2 additions & 3 deletions src/properties/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ use std::{
};

use enum_dispatch::enum_dispatch;
use indexmap::IndexMap;

use crate::{
custom_version::{CustomVersionTrait, FCustomVersion},
error::{DeserializeError, Error},
scoped_stack_entry::ScopedStackEntry,
types::Guid,
types::{map::HashableIndexMap, Guid},
};

use self::{
Expand Down Expand Up @@ -442,7 +441,7 @@ pub struct PropertyOptions<'a> {
/// Tracks the property tree location in a GVAS file.
pub properties_stack: &'a mut Vec<String>,
/// Custom versions
pub custom_versions: &'a IndexMap<Guid, u32>,
pub custom_versions: &'a HashableIndexMap<Guid, u32>,
/// Enables large world coordinates.
pub large_world_coordinates: bool,
}
Expand Down
3 changes: 1 addition & 2 deletions src/properties/struct_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ impl StructProperty {
UInt32Property::read(cursor, false)?.value,
])),
_ => {
let mut properties = IndexMap::new();
let mut properties = HashableIndexMap::new();
loop {
let property_name = cursor.read_string()?;
if property_name == "None" {
Expand All @@ -231,7 +231,6 @@ impl StructProperty {
let property = Property::new(cursor, &property_type, true, options, None)?;
insert_property(&mut properties, property_name, property);
}
let properties = HashableIndexMap(properties);
StructPropertyValue::CustomStruct {
type_name,
properties,
Expand Down
4 changes: 1 addition & 3 deletions src/properties/text_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::{
};

use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use indexmap::IndexMap;
use num_enum::{IntoPrimitive, TryFromPrimitive};
use ordered_float::OrderedFloat;

Expand Down Expand Up @@ -333,15 +332,14 @@ impl FTextHistory {
let source_format = Box::new(FText::read(cursor, options)?);

let argument_count = cursor.read_i32::<LittleEndian>()?;
let mut arguments = IndexMap::with_capacity(argument_count as usize);
let mut arguments = HashableIndexMap::with_capacity(argument_count as usize);

for _ in 0..argument_count {
let key = cursor.read_string()?;
let value = FormatArgumentValue::read(cursor, options)?;
arguments.insert(key, value);
}

let arguments = HashableIndexMap(arguments);
FTextHistory::NamedFormat {
source_format,
arguments,
Expand Down
Loading

0 comments on commit f8d5d93

Please sign in to comment.