diff --git a/Facepunch.Steamworks.Test/Client/InventoryTest.cs b/Facepunch.Steamworks.Test/Client/InventoryTest.cs index 1bcdb8d..837b1b8 100644 --- a/Facepunch.Steamworks.Test/Client/InventoryTest.cs +++ b/Facepunch.Steamworks.Test/Client/InventoryTest.cs @@ -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); diff --git a/Facepunch.Steamworks/BaseSteamworks.cs b/Facepunch.Steamworks/BaseSteamworks.cs index 9df2019..7d19b7b 100644 --- a/Facepunch.Steamworks/BaseSteamworks.cs +++ b/Facepunch.Steamworks/BaseSteamworks.cs @@ -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 func ) } } + /// + /// Debug function, called for every callback. Only really used to confirm that callbacks are working properly. + /// + public Action OnAnyCallback; + Dictionary>> Callbacks = new Dictionary>>(); internal List> CallbackList( Type T ) @@ -192,6 +195,11 @@ internal void OnCallback( T data ) { i( data ); } + + if ( OnAnyCallback != null ) + { + OnAnyCallback.Invoke( data ); + } } internal void RegisterCallback( Action func ) diff --git a/Facepunch.Steamworks/Interfaces/Inventory.cs b/Facepunch.Steamworks/Interfaces/Inventory.cs index 3953256..bc00919 100644 --- a/Facepunch.Steamworks/Interfaces/Inventory.cs +++ b/Facepunch.Steamworks/Interfaces/Inventory.cs @@ -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( onDefinitionsUpdated ); + Result.Pending = new Dictionary(); - FetchItemDefinitions(); - UpdatePrices(); + FetchItemDefinitions(); // onDefinitionsUpdated should get called on next Update if ( !server ) { steamworks.RegisterCallback( onResultReady ); steamworks.RegisterCallback( onFullUpdate ); + // // Get a list of our items immediately @@ -63,6 +65,37 @@ internal Inventory( BaseSteamworks steamworks, SteamNative.SteamInventory c, boo } } + /// + /// Should get called when the definitions get updated from Steam. + /// + 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; + } + /// /// We've received a FULL update /// @@ -202,22 +235,7 @@ public Definition CreateDefinition( int id ) /// 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 ); - } } /// @@ -225,24 +243,7 @@ public void FetchItemDefinitions() /// 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(); - } } /// @@ -258,7 +259,10 @@ public IEnumerable 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];