Tweaking how Inventory.Item's is updated

This commit is contained in:
Garry Newman 2016-11-17 16:43:29 +00:00
parent 88e2568e9f
commit a1d458de00
3 changed files with 89 additions and 11 deletions

View File

@ -11,7 +11,7 @@ namespace Facepunch.Steamworks
/// <summary> /// <summary>
/// An item in your inventory. /// An item in your inventory.
/// </summary> /// </summary>
public class Item public struct Item : IEquatable<Item>
{ {
public ulong Id; public ulong Id;
public int Quantity; public int Quantity;
@ -24,6 +24,37 @@ namespace Facepunch.Steamworks
public Definition Definition; public Definition Definition;
public bool TradeLocked; public bool TradeLocked;
public bool Equals( Item other )
{
return Equals( other, this );
}
public override bool Equals( object obj )
{
if ( obj == null || GetType() != obj.GetType() )
{
return false;
}
return ((Item)obj).Id == Id;
}
public override int GetHashCode()
{
return Id.GetHashCode();
}
public static bool operator ==( Item c1, Item c2 )
{
return c1.Equals( c2 );
}
public static bool operator !=( Item c1, Item c2 )
{
return !c1.Equals( c2 );
}
} }
} }
} }

View File

@ -42,6 +42,8 @@ namespace Facepunch.Steamworks
} }
} }
internal uint Timestamp { get; private set; }
internal Callbacks.Result Status() internal Callbacks.Result Status()
{ {
if ( Handle == -1 ) return Callbacks.Result.InvalidParam; if ( Handle == -1 ) return Callbacks.Result.InvalidParam;
@ -75,6 +77,8 @@ namespace Facepunch.Steamworks
if ( !IsSuccess ) if ( !IsSuccess )
return; return;
Timestamp = inventory.inventory.GetResultTimestamp( Handle );
SteamNative.SteamItemDetails_t[] steamItems = inventory.inventory.GetResultItems( Handle ); SteamNative.SteamItemDetails_t[] steamItems = inventory.inventory.GetResultItems( Handle );
if ( steamItems == null ) if ( steamItems == null )
return; return;
@ -84,7 +88,7 @@ namespace Facepunch.Steamworks
throw new System.Exception( "steamItems was null" ); throw new System.Exception( "steamItems was null" );
} }
Items = steamItems.Where( x => ((int)x.Flags & (int)SteamNative.SteamItemFlags.Removed) == 0 && ((int)x.Flags & (int)SteamNative.SteamItemFlags.Consumed ) == 0 ) Items = steamItems.Where( x => ( (int)x.Flags & (int)SteamNative.SteamItemFlags.Removed ) != (int)SteamNative.SteamItemFlags.Removed && ( (int)x.Flags & (int)SteamNative.SteamItemFlags.Consumed ) != (int)SteamNative.SteamItemFlags.Consumed )
.Select( x => .Select( x =>
{ {
return new Inventory.Item() return new Inventory.Item()
@ -122,6 +126,8 @@ namespace Facepunch.Steamworks
Definition = inventory.FindDefinition( x.Definition ) Definition = inventory.FindDefinition( x.Definition )
}; };
} ).ToArray(); } ).ToArray();
inventory.ApplyResult( this );
} }
internal unsafe byte[] Serialize() internal unsafe byte[] Serialize()
@ -147,6 +153,5 @@ namespace Facepunch.Steamworks
inventory = null; inventory = null;
} }
} }
} }
} }

View File

@ -32,6 +32,8 @@ namespace Facepunch.Steamworks
/// </summary> /// </summary>
public DateTime SerializedExpireTime; public DateTime SerializedExpireTime;
internal uint LastTimestamp = 0;
internal SteamNative.SteamInventory inventory; internal SteamNative.SteamInventory inventory;
private bool IsServer { get; set; } private bool IsServer { get; set; }
@ -46,6 +48,7 @@ namespace Facepunch.Steamworks
if ( !server ) if ( !server )
{ {
// SteamNative.SteamInventoryResultReady_t.RegisterCallback( steamworks, onResultReady );
SteamNative.SteamInventoryFullUpdate_t.RegisterCallback( steamworks, onFullUpdate ); SteamNative.SteamInventoryFullUpdate_t.RegisterCallback( steamworks, onFullUpdate );
Refresh(); Refresh();
} }
@ -55,22 +58,61 @@ namespace Facepunch.Steamworks
{ {
if ( error ) return; if ( error ) return;
var r = new Result( this, data.Handle ); onResult( data.Handle, true );
}
private void onResultReady( SteamInventoryResultReady_t data, bool error )
{
if ( error ) return;
if ( data.Esult != SteamNative.Result.OK ) return;
onResult( data.Handle, false );
}
private void onResult( int Handle, bool serialize )
{
var r = new Result( this, Handle );
if ( r.IsSuccess ) if ( r.IsSuccess )
{ {
if ( serialize )
{
if ( r.Timestamp < LastTimestamp )
return;
LastTimestamp = r.Timestamp;
SerializedItems = r.Serialize(); SerializedItems = r.Serialize();
SerializedExpireTime = DateTime.Now.Add( TimeSpan.FromMinutes( 60 ) ); SerializedExpireTime = DateTime.Now.Add( TimeSpan.FromMinutes( 60 ) );
}
Items = r.Items; ApplyResult( r );
}
r.Dispose();
r = null;
}
internal void ApplyResult( Result r )
{
if ( IsServer ) return;
if ( r.IsSuccess )
{
if ( Items == null )
Items = new Item[0];
Items = Items
.Union( r.Items )
.Distinct()
.Where( x => !r.Removed.Contains( x ) )
.Where( x => !r.Consumed.Contains( x ) )
.ToArray();
// //
// Tell everyone we've got new items! // Tell everyone we've got new items!
// //
OnUpdate?.Invoke(); OnUpdate?.Invoke();
} }
r.Dispose();
r = null;
} }
public void Dispose() public void Dispose()