mirror of
https://github.com/Facepunch/Facepunch.Steamworks.git
synced 2025-01-12 06:38:01 +03:00
Tweaking how Inventory.Item's is updated
This commit is contained in:
parent
88e2568e9f
commit
a1d458de00
@ -11,7 +11,7 @@ namespace Facepunch.Steamworks
|
||||
/// <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 @@ namespace Facepunch.Steamworks
|
||||
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 );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -42,6 +42,8 @@ namespace Facepunch.Steamworks
|
||||
}
|
||||
}
|
||||
|
||||
internal uint Timestamp { get; private set; }
|
||||
|
||||
internal Callbacks.Result Status()
|
||||
{
|
||||
if ( Handle == -1 ) return Callbacks.Result.InvalidParam;
|
||||
@ -75,6 +77,8 @@ namespace Facepunch.Steamworks
|
||||
if ( !IsSuccess )
|
||||
return;
|
||||
|
||||
Timestamp = inventory.inventory.GetResultTimestamp( Handle );
|
||||
|
||||
SteamNative.SteamItemDetails_t[] steamItems = inventory.inventory.GetResultItems( Handle );
|
||||
if ( steamItems == null )
|
||||
return;
|
||||
@ -84,7 +88,7 @@ namespace Facepunch.Steamworks
|
||||
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 @@ namespace Facepunch.Steamworks
|
||||
Definition = inventory.FindDefinition( x.Definition )
|
||||
};
|
||||
} ).ToArray();
|
||||
|
||||
inventory.ApplyResult( this );
|
||||
}
|
||||
|
||||
internal unsafe byte[] Serialize()
|
||||
@ -133,7 +139,7 @@ namespace Facepunch.Steamworks
|
||||
|
||||
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 @@ namespace Facepunch.Steamworks
|
||||
inventory = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -32,6 +32,8 @@ namespace Facepunch.Steamworks
|
||||
/// </summary>
|
||||
public DateTime SerializedExpireTime;
|
||||
|
||||
internal uint LastTimestamp = 0;
|
||||
|
||||
internal SteamNative.SteamInventory inventory;
|
||||
|
||||
private bool IsServer { get; set; }
|
||||
@ -46,6 +48,7 @@ namespace Facepunch.Steamworks
|
||||
|
||||
if ( !server )
|
||||
{
|
||||
// SteamNative.SteamInventoryResultReady_t.RegisterCallback( steamworks, onResultReady );
|
||||
SteamNative.SteamInventoryFullUpdate_t.RegisterCallback( steamworks, onFullUpdate );
|
||||
Refresh();
|
||||
}
|
||||
@ -55,22 +58,61 @@ namespace Facepunch.Steamworks
|
||||
{
|
||||
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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user