From 994cdee49ee277726537c2e3ddaa8d11f27a1e81 Mon Sep 17 00:00:00 2001 From: Garry Newman Date: Wed, 1 May 2019 14:03:37 +0100 Subject: [PATCH] Round off some edges with ItemDefinitions --- Facepunch.Steamworks/SteamInventory.cs | 41 +++++++++++++++++--------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/Facepunch.Steamworks/SteamInventory.cs b/Facepunch.Steamworks/SteamInventory.cs index 78c9120..21962db 100644 --- a/Facepunch.Steamworks/SteamInventory.cs +++ b/Facepunch.Steamworks/SteamInventory.cs @@ -33,30 +33,26 @@ namespace Steamworks internal static void InstallEvents() { SteamInventoryFullUpdate_t.Install( x => OnInventoryUpdated?.Invoke( x.Handle ) ); - SteamInventoryDefinitionUpdate_t.Install( x => DefinitionsUpdated() ); + SteamInventoryDefinitionUpdate_t.Install( x => LoadDefinitions() ); } public static event Action OnInventoryUpdated; public static event Action OnDefinitionsUpdated; - internal static int defUpdateCount = 0; - - internal static void DefinitionsUpdated() + static void LoadDefinitions() { Definitions = GetDefinitions(); - if ( Definitions != null ) + if ( Definitions == null ) + return; + + _defMap = new Dictionary(); + + foreach ( var d in Definitions ) { - _defMap = new Dictionary(); - - foreach ( var d in Definitions ) - { - _defMap[d.Id] = d; - } + _defMap[d.Id] = d; } - defUpdateCount++; - OnDefinitionsUpdated?.Invoke(); } @@ -68,16 +64,33 @@ namespace Steamworks /// public static void LoadItemDefinitions() { + // If they're null, try to load them immediately + // my hunch is that this loads a disk cached version + // but waiting for LoadItemDefinitions downloads a new copy + // from Steam's servers. So this will give us immediate data + // where as Steam's inventory servers could be slow/down + if ( Definitions == null ) + { + LoadDefinitions(); + } + Internal.LoadItemDefinitions(); } /// /// Will call LoadItemDefinitions and wait until Definitions is not null /// - public static async Task WaitForDefinitions( float timeoutSeconds = 10 ) + public static async Task WaitForDefinitions( float timeoutSeconds = 30 ) { + if ( Definitions != null ) + return true; + + LoadDefinitions(); LoadItemDefinitions(); + if ( Definitions != null ) + return true; + var sw = Stopwatch.StartNew(); while ( Definitions == null )