From cf2bc51c5490eb00e7316806af627332a1f07147 Mon Sep 17 00:00:00 2001 From: Garry Newman Date: Tue, 14 May 2019 20:17:15 +0100 Subject: [PATCH] InventoryDef and Recpie IEquatable --- Facepunch.Steamworks/Structs/InventoryDef.cs | 18 +++++++++++++++++- .../Structs/InventoryRecipe.cs | 17 +++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/Facepunch.Steamworks/Structs/InventoryDef.cs b/Facepunch.Steamworks/Structs/InventoryDef.cs index 45e6028..f438873 100644 --- a/Facepunch.Steamworks/Structs/InventoryDef.cs +++ b/Facepunch.Steamworks/Structs/InventoryDef.cs @@ -5,7 +5,7 @@ namespace Steamworks { - public class InventoryDef + public class InventoryDef : IEquatable { internal InventoryDefId _id; internal Dictionary _properties; @@ -220,5 +220,21 @@ public InventoryRecipe[] GetRecipesContainingThis() return _recContaining; } + public static bool operator ==( InventoryDef a, InventoryDef b ) + { + if ( Object.ReferenceEquals( a, null ) ) + return Object.ReferenceEquals( b, null ); + + return a.Equals( b ); + } + public static bool operator !=( InventoryDef a, InventoryDef b ) => !(a == b); + public override bool Equals( object p ) => this.Equals( (InventoryDef)p ); + public override int GetHashCode() => Id.GetHashCode(); + public bool Equals( InventoryDef p ) + { + if ( p == null ) return false; + return p.Id == Id; + } + } } \ No newline at end of file diff --git a/Facepunch.Steamworks/Structs/InventoryRecipe.cs b/Facepunch.Steamworks/Structs/InventoryRecipe.cs index 03384e8..1305eb8 100644 --- a/Facepunch.Steamworks/Structs/InventoryRecipe.cs +++ b/Facepunch.Steamworks/Structs/InventoryRecipe.cs @@ -9,7 +9,7 @@ namespace Steamworks /// /// A structured description of an item exchange /// - public struct InventoryRecipe + public struct InventoryRecipe : IEquatable { public struct Ingredient { @@ -70,11 +70,14 @@ internal static Ingredient FromString( string part ) /// public Ingredient[] Ingredients; + public string Source; + internal static InventoryRecipe FromString( string part, InventoryDef Result ) { var r = new InventoryRecipe { - Result = Result + Result = Result, + Source = part }; var parts = part.Split( new[] { ',' }, StringSplitOptions.RemoveEmptyEntries ); @@ -87,5 +90,15 @@ internal bool ContainsIngredient( InventoryDef inventoryDef ) { return Ingredients.Any( x => x.DefinitionId == inventoryDef.Id ); } + + public static bool operator ==( InventoryRecipe a, InventoryRecipe b ) => a.GetHashCode() == b.GetHashCode(); + public static bool operator !=( InventoryRecipe a, InventoryRecipe b ) => a.GetHashCode() != b.GetHashCode(); + public override bool Equals( object p ) => this.Equals( (InventoryRecipe)p ); + public override int GetHashCode() + { + return Source.GetHashCode(); + } + + public bool Equals( InventoryRecipe p ) => p.GetHashCode() == GetHashCode(); } } \ No newline at end of file