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 @@ public partial class Inventory
/// <summary>
/// An item in your inventory.
/// </summary>
public class Item
public struct Item : IEquatable<Item>
{
public ulong Id;
public int Quantity;
@ -24,6 +24,37 @@ public class Item
public Definition Definition;
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 @@ internal bool IsSuccess
}
}
internal uint Timestamp { get; private set; }
internal Callbacks.Result Status()
{
if ( Handle == -1 ) return Callbacks.Result.InvalidParam;
@ -75,6 +77,8 @@ internal void TryFill()
if ( !IsSuccess )
return;
Timestamp = inventory.inventory.GetResultTimestamp( Handle );
SteamNative.SteamItemDetails_t[] steamItems = inventory.inventory.GetResultItems( Handle );
if ( steamItems == null )
return;
@ -84,7 +88,7 @@ internal void TryFill()
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 =>
{
return new Inventory.Item()
@ -122,6 +126,8 @@ internal void TryFill()
Definition = inventory.FindDefinition( x.Definition )
};
} ).ToArray();
inventory.ApplyResult( this );
}
internal unsafe byte[] Serialize()
@ -133,7 +139,7 @@ internal unsafe byte[] Serialize()
fixed ( byte* ptr = data )
{
if ( !inventory.inventory.SerializeResult( Handle, (IntPtr) ptr, out size ) )
if ( !inventory.inventory.SerializeResult( Handle, (IntPtr)ptr, out size ) )
return null;
}
@ -147,6 +153,5 @@ public void Dispose()
inventory = null;
}
}
}
}

View File

@ -32,6 +32,8 @@ public partial class Inventory : IDisposable
/// </summary>
public DateTime SerializedExpireTime;
internal uint LastTimestamp = 0;
internal SteamNative.SteamInventory inventory;
private bool IsServer { get; set; }
@ -46,6 +48,7 @@ internal Inventory( BaseSteamworks steamworks, SteamNative.SteamInventory c, boo
if ( !server )
{
// SteamNative.SteamInventoryResultReady_t.RegisterCallback( steamworks, onResultReady );
SteamNative.SteamInventoryFullUpdate_t.RegisterCallback( steamworks, onFullUpdate );
Refresh();
}
@ -55,22 +58,61 @@ private void onFullUpdate( SteamInventoryFullUpdate_t data, bool error )
{
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 )
{
SerializedItems = r.Serialize();
SerializedExpireTime = DateTime.Now.Add( TimeSpan.FromMinutes( 60 ) );
if ( serialize )
{
if ( r.Timestamp < LastTimestamp )
return;
Items = r.Items;
LastTimestamp = r.Timestamp;
SerializedItems = r.Serialize();
SerializedExpireTime = DateTime.Now.Add( TimeSpan.FromMinutes( 60 ) );
}
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!
//
OnUpdate?.Invoke();
}
r.Dispose();
r = null;
}
public void Dispose()