From e69aa8c4ee18d35fd4c9d80c682fc0d2b3385f9b Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 2 Mar 2022 12:01:51 -0500 Subject: [PATCH] Fixed items with components having incorrect hashes --- common/src/comp/inventory/item/mod.rs | 26 +++++++++++-------- common/src/comp/inventory/loadout.rs | 4 +-- common/src/comp/inventory/mod.rs | 4 +-- .../src/persistence/character/conversions.rs | 4 +-- 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/common/src/comp/inventory/item/mod.rs b/common/src/comp/inventory/item/mod.rs index ab666d038b..f5f3b96964 100644 --- a/common/src/comp/inventory/item/mod.rs +++ b/common/src/comp/inventory/item/mod.rs @@ -369,7 +369,7 @@ use std::hash::{Hash, Hasher}; impl Hash for Item { fn hash(&self, state: &mut H) { self.item_definition_id().hash(state); - self.components.hash(state); + self.components.iter().for_each(|comp| comp.hash(state)); } } @@ -666,23 +666,17 @@ impl Item { ability_map: &AbilityMap, msm: &MaterialStatManifest, ) -> Self { - let item_hash = { - let mut s = DefaultHasher::new(); - inner_item.item_definition_id().hash(&mut s); - components.hash(&mut s); - s.finish() - }; - let mut item = Item { item_id: Arc::new(AtomicCell::new(None)), amount: NonZeroU32::new(1).unwrap(), components, slots: vec![None; inner_item.num_slots() as usize], item_base: inner_item, + // Updated immediately below item_config: None, - hash: item_hash, + hash: 0, }; - item.update_item_config(ability_map, msm); + item.update_item_state(ability_map, msm); item } @@ -828,10 +822,20 @@ impl Item { self.components.get_mut(index) } - pub fn update_item_config(&mut self, ability_map: &AbilityMap, msm: &MaterialStatManifest) { + /// Updates state of an item (important for creation of new items, + /// persistence, and if components are ever added to items after initial + /// creation) + pub fn update_item_state(&mut self, ability_map: &AbilityMap, msm: &MaterialStatManifest) { + // Updates item config of an item if let Ok(item_config) = ItemConfig::try_from((&*self, ability_map, msm)) { self.item_config = Some(Box::new(item_config)); } + // Updates hash of an item + self.hash = { + let mut s = DefaultHasher::new(); + self.hash(&mut s); + s.finish() + }; } /// Returns an iterator that drains items contained within the item's slots diff --git a/common/src/comp/inventory/loadout.rs b/common/src/comp/inventory/loadout.rs index 4aaef69755..458383bd2b 100644 --- a/common/src/comp/inventory/loadout.rs +++ b/common/src/comp/inventory/loadout.rs @@ -403,14 +403,14 @@ impl Loadout { } } - pub fn persistence_update_all_item_configs( + pub fn persistence_update_all_item_states( &mut self, ability_map: &item::tool::AbilityMap, msm: &item::tool::MaterialStatManifest, ) { self.slots.iter_mut().for_each(|slot| { if let Some(item) = &mut slot.slot { - item.update_item_config(ability_map, msm); + item.update_item_state(ability_map, msm); } }); } diff --git a/common/src/comp/inventory/mod.rs b/common/src/comp/inventory/mod.rs index 4a62ea1245..f22303468c 100644 --- a/common/src/comp/inventory/mod.rs +++ b/common/src/comp/inventory/mod.rs @@ -784,14 +784,14 @@ impl Inventory { pub fn swap_equipped_weapons(&mut self) { self.loadout.swap_equipped_weapons() } - pub fn persistence_update_all_item_configs( + pub fn persistence_update_all_item_states( &mut self, ability_map: &item::tool::AbilityMap, msm: &item::tool::MaterialStatManifest, ) { self.slots_mut().for_each(|mut slot| { if let Some(item) = &mut slot { - item.update_item_config(ability_map, msm); + item.update_item_state(ability_map, msm); } }); } diff --git a/server/src/persistence/character/conversions.rs b/server/src/persistence/character/conversions.rs index 0aa8ecabac..687aebec4c 100644 --- a/server/src/persistence/character/conversions.rs +++ b/server/src/persistence/character/conversions.rs @@ -420,7 +420,7 @@ pub fn convert_inventory_from_database_items( // Some items may have had components added, so update the item config of each // item to ensure that it correctly accounts for components that were added - inventory.persistence_update_all_item_configs(&ABILITY_MAP, &MATERIAL_STATS_MANIFEST); + inventory.persistence_update_all_item_states(&ABILITY_MAP, &MATERIAL_STATS_MANIFEST); Ok(inventory) } @@ -483,7 +483,7 @@ pub fn convert_loadout_from_database_items( // Some items may have had components added, so update the item config of each // item to ensure that it correctly accounts for components that were added - loadout.persistence_update_all_item_configs(&ABILITY_MAP, &MATERIAL_STATS_MANIFEST); + loadout.persistence_update_all_item_states(&ABILITY_MAP, &MATERIAL_STATS_MANIFEST); Ok(loadout) }