InventoryDef and Recpie IEquatable

This commit is contained in:
Garry Newman 2019-05-14 20:17:15 +01:00
parent 208af219d2
commit cf2bc51c54
2 changed files with 32 additions and 3 deletions

View File

@ -5,7 +5,7 @@
namespace Steamworks
{
public class InventoryDef
public class InventoryDef : IEquatable<InventoryDef>
{
internal InventoryDefId _id;
internal Dictionary<string, string> _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;
}
}
}

View File

@ -9,7 +9,7 @@ namespace Steamworks
/// <summary>
/// A structured description of an item exchange
/// </summary>
public struct InventoryRecipe
public struct InventoryRecipe : IEquatable<InventoryRecipe>
{
public struct Ingredient
{
@ -70,11 +70,14 @@ internal static Ingredient FromString( string part )
/// </summary>
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();
}
}