Added Inventory.OnDefinitionsUpdated

This commit is contained in:
Garry Newman 2018-03-21 11:27:18 +00:00
parent 144f401d2c
commit 7b7cfa29c4
3 changed files with 93 additions and 39 deletions

View File

@ -15,6 +15,12 @@ public void InventoryDefinitions()
{
using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{
while ( client.Inventory.Definitions == null )
{
client.Update();
System.Threading.Thread.Sleep( 10 );
}
Assert.IsNotNull( client.Inventory.Definitions );
Assert.AreNotEqual( 0, client.Inventory.Definitions.Length );
@ -36,6 +42,12 @@ public void InventoryDefinitionExchange()
{
using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{
while ( client.Inventory.Definitions == null )
{
client.Update();
System.Threading.Thread.Sleep( 10 );
}
Assert.IsNotNull( client.Inventory.Definitions );
Assert.AreNotEqual( 0, client.Inventory.Definitions.Length );
@ -58,6 +70,12 @@ public void InventoryDefinitionIngredients()
{
using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{
while ( client.Inventory.Definitions == null )
{
client.Update();
System.Threading.Thread.Sleep( 10 );
}
Assert.IsNotNull( client.Inventory.Definitions );
Assert.AreNotEqual( 0, client.Inventory.Definitions.Length );
@ -80,6 +98,12 @@ public void InventoryItemList()
{
using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{
while ( client.Inventory.Definitions == null )
{
client.Update();
System.Threading.Thread.Sleep( 10 );
}
bool CallbackCalled = false;
// OnUpdate hsould be called when we receive a list of our items
@ -146,6 +170,12 @@ public void Deserialize()
{
using ( var client = new Facepunch.Steamworks.Client( 252490 ) )
{
while ( client.Inventory.Definitions == null )
{
client.Update();
System.Threading.Thread.Sleep( 10 );
}
Assert.IsTrue( client.IsValid );
Assert.IsNotNull(client.Inventory.Definitions);
Assert.AreNotEqual(0, client.Inventory.Definitions.Length);
@ -202,6 +232,12 @@ public void PurchaseItems()
{
using (var client = new Facepunch.Steamworks.Client(252490))
{
while ( client.Inventory.Definitions == null )
{
client.Update();
System.Threading.Thread.Sleep( 10 );
}
Assert.IsNotNull(client.Inventory.Definitions);
Assert.AreNotEqual(0, client.Inventory.Definitions.Length);
@ -235,6 +271,12 @@ public void ListPrices()
{
using (var client = new Facepunch.Steamworks.Client(252490))
{
while ( client.Inventory.Definitions == null )
{
client.Update();
System.Threading.Thread.Sleep( 10 );
}
Assert.IsNotNull(client.Inventory.Definitions);
Assert.AreNotEqual(0, client.Inventory.Definitions.Length);

View File

@ -128,8 +128,6 @@ internal void UnregisterCallResult( SteamNative.CallResult handle )
public virtual void Update()
{
Inventory.Update();
Networking.Update();
RunUpdateCallbacks();
@ -169,6 +167,11 @@ public void UpdateWhile( Func<bool> func )
}
}
/// <summary>
/// Debug function, called for every callback. Only really used to confirm that callbacks are working properly.
/// </summary>
public Action<object> OnAnyCallback;
Dictionary<Type, List<Action<object>>> Callbacks = new Dictionary<Type, List<Action<object>>>();
internal List<Action<object>> CallbackList( Type T )
@ -192,6 +195,11 @@ internal void OnCallback<T>( T data )
{
i( data );
}
if ( OnAnyCallback != null )
{
OnAnyCallback.Invoke( data );
}
}
internal void RegisterCallback<T>( Action<T> func )

View File

@ -37,24 +37,26 @@ public partial class Inventory : IDisposable
internal SteamNative.SteamInventory inventory;
private Stopwatch fetchRetryTimer;
private bool IsServer { get; set; }
public event Action OnDefinitionsUpdated;
internal Inventory( BaseSteamworks steamworks, SteamNative.SteamInventory c, bool server )
{
IsServer = server;
inventory = c;
steamworks.RegisterCallback<SteamNative.SteamInventoryDefinitionUpdate_t>( onDefinitionsUpdated );
Result.Pending = new Dictionary<int, Result>();
FetchItemDefinitions();
UpdatePrices();
FetchItemDefinitions(); // onDefinitionsUpdated should get called on next Update
if ( !server )
{
steamworks.RegisterCallback<SteamNative.SteamInventoryResultReady_t>( onResultReady );
steamworks.RegisterCallback<SteamNative.SteamInventoryFullUpdate_t>( onFullUpdate );
//
// Get a list of our items immediately
@ -63,6 +65,37 @@ internal Inventory( BaseSteamworks steamworks, SteamNative.SteamInventory c, boo
}
}
/// <summary>
/// Should get called when the definitions get updated from Steam.
/// </summary>
private void onDefinitionsUpdated( SteamInventoryDefinitionUpdate_t obj )
{
Console.WriteLine( "onDefinitionsUpdated" );
LoadDefinitions();
UpdatePrices();
if ( OnDefinitionsUpdated != null )
{
OnDefinitionsUpdated.Invoke();
}
}
private bool LoadDefinitions()
{
var ids = inventory.GetItemDefinitionIDs();
if ( ids == null )
return false;
Definitions = ids.Select( x => CreateDefinition( x ) ).ToArray();
foreach ( var def in Definitions )
{
def.Link( Definitions );
}
return true;
}
/// <summary>
/// We've received a FULL update
/// </summary>
@ -202,22 +235,7 @@ public Definition CreateDefinition( int id )
/// </summary>
public void FetchItemDefinitions()
{
//
// Make sure item definitions are loaded, because we're going to be using them.
//
inventory.LoadItemDefinitions();
var ids = inventory.GetItemDefinitionIDs();
if ( ids == null )
return;
Definitions = ids.Select( x => CreateDefinition( x ) ).ToArray();
foreach ( var def in Definitions )
{
def.Link( Definitions );
}
}
/// <summary>
@ -225,24 +243,7 @@ public void FetchItemDefinitions()
/// </summary>
public void Update()
{
if ( Definitions == null )
{
//
// Don't try every frame, just try every 10 seconds.
//
{
if ( fetchRetryTimer != null && fetchRetryTimer.Elapsed.TotalSeconds < 10.0f )
return;
if ( fetchRetryTimer == null )
fetchRetryTimer = Stopwatch.StartNew();
fetchRetryTimer.Reset();
fetchRetryTimer.Start();
}
FetchItemDefinitions();
}
}
/// <summary>
@ -258,7 +259,10 @@ public IEnumerable<Definition> DefinitionsWithPrices
{
get
{
for( int i=0; i< Definitions.Length; i++ )
if ( Definitions == null )
yield break;
for ( int i=0; i< Definitions.Length; i++ )
{
if (Definitions[i].LocalPrice > 0)
yield return Definitions[i];