From d2ce2fe13b9a985323ca70556d2ac0e2afd4eeeb Mon Sep 17 00:00:00 2001 From: Garry Newman Date: Sat, 27 Apr 2019 21:52:47 +0100 Subject: [PATCH] GetDefinitions --- Facepunch.Steamworks.Test/InventoryTest.cs | 35 ++++++---- .../Generated/Interfaces/ISteamInventory.cs | 12 ++-- Facepunch.Steamworks/SteamInventory.cs | 67 +++++++++++++++++-- Generator/CodeWriter/Types/BaseType.cs | 1 + 4 files changed, 90 insertions(+), 25 deletions(-) diff --git a/Facepunch.Steamworks.Test/InventoryTest.cs b/Facepunch.Steamworks.Test/InventoryTest.cs index fa55ce8..69eb345 100644 --- a/Facepunch.Steamworks.Test/InventoryTest.cs +++ b/Facepunch.Steamworks.Test/InventoryTest.cs @@ -13,29 +13,36 @@ namespace Steamworks public class InventoryTest { [TestMethod] - public async Task GetItemsWithPricesAsync() - { - var items = await SteamInventory.GetItemsWithPricesAsync(); + public async Task LoadItemDefinitionsAsync() + { + var result = await SteamInventory.WaitForDefinitions( 5 ); + Assert.IsTrue( result ); - foreach ( var item in items ) + result = await SteamInventory.WaitForDefinitions( 5 ); + Assert.IsTrue( result ); + } + + [TestMethod] + public async Task GetDefinitions() + { + await SteamInventory.WaitForDefinitions(); + + Assert.IsNotNull( SteamInventory.Definitions ); + + foreach ( var def in SteamInventory.Definitions ) { - Console.WriteLine( $"[{item.LocalPrice}] {item.Name}" ); + Console.WriteLine( $"[{def.Id:0000000000}] {def.Name} [{def.Type}]" ); } } [TestMethod] - public async Task IutemDefs() + public async Task GetDefinitionsWithPrices() { - var items = await SteamInventory.GetItemsWithPricesAsync(); + var defs = await SteamInventory.GetDefinitionsWithPricesAsync(); - foreach ( var item in items ) + foreach ( var def in defs ) { - Console.WriteLine( $"{item.Id}" ); - foreach ( var prop in item.Properties ) - { - Console.WriteLine( $" {prop.Key}: {prop.Value}" ); - } - Console.WriteLine( $"" ); + Console.WriteLine( $"[{def.Id:0000000000}] {def.Name} [{def.LocalPriceFormatted}]" ); } } } diff --git a/Facepunch.Steamworks/Generated/Interfaces/ISteamInventory.cs b/Facepunch.Steamworks/Generated/Interfaces/ISteamInventory.cs index de35e84..3dc8e54 100644 --- a/Facepunch.Steamworks/Generated/Interfaces/ISteamInventory.cs +++ b/Facepunch.Steamworks/Generated/Interfaces/ISteamInventory.cs @@ -307,13 +307,13 @@ namespace Steamworks #region FunctionMeta [UnmanagedFunctionPointer( CallingConvention.ThisCall )] [return: MarshalAs( UnmanagedType.I1 )] - private delegate bool FGetItemDefinitionIDs( IntPtr self, ref SteamItemDef_t pItemDefIDs, ref uint punItemDefIDsArraySize ); + private delegate bool FGetItemDefinitionIDs( IntPtr self, [In,Out] SteamItemDef_t[] pItemDefIDs, ref uint punItemDefIDsArraySize ); private FGetItemDefinitionIDs _GetItemDefinitionIDs; #endregion - internal bool GetItemDefinitionIDs( ref SteamItemDef_t pItemDefIDs, ref uint punItemDefIDsArraySize ) + internal bool GetItemDefinitionIDs( [In,Out] SteamItemDef_t[] pItemDefIDs, ref uint punItemDefIDsArraySize ) { - return _GetItemDefinitionIDs( Self, ref pItemDefIDs, ref punItemDefIDsArraySize ); + return _GetItemDefinitionIDs( Self, pItemDefIDs, ref punItemDefIDsArraySize ); } #region FunctionMeta @@ -342,13 +342,13 @@ namespace Steamworks #region FunctionMeta [UnmanagedFunctionPointer( CallingConvention.ThisCall )] [return: MarshalAs( UnmanagedType.I1 )] - private delegate bool FGetEligiblePromoItemDefinitionIDs( IntPtr self, SteamId steamID, ref SteamItemDef_t pItemDefIDs, ref uint punItemDefIDsArraySize ); + private delegate bool FGetEligiblePromoItemDefinitionIDs( IntPtr self, SteamId steamID, [In,Out] SteamItemDef_t[] pItemDefIDs, ref uint punItemDefIDsArraySize ); private FGetEligiblePromoItemDefinitionIDs _GetEligiblePromoItemDefinitionIDs; #endregion - internal bool GetEligiblePromoItemDefinitionIDs( SteamId steamID, ref SteamItemDef_t pItemDefIDs, ref uint punItemDefIDsArraySize ) + internal bool GetEligiblePromoItemDefinitionIDs( SteamId steamID, [In,Out] SteamItemDef_t[] pItemDefIDs, ref uint punItemDefIDsArraySize ) { - return _GetEligiblePromoItemDefinitionIDs( Self, steamID, ref pItemDefIDs, ref punItemDefIDsArraySize ); + return _GetEligiblePromoItemDefinitionIDs( Self, steamID, pItemDefIDs, ref punItemDefIDsArraySize ); } #region FunctionMeta diff --git a/Facepunch.Steamworks/SteamInventory.cs b/Facepunch.Steamworks/SteamInventory.cs index d02d23f..68f0b55 100644 --- a/Facepunch.Steamworks/SteamInventory.cs +++ b/Facepunch.Steamworks/SteamInventory.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Runtime.InteropServices; using System.Text; @@ -28,16 +29,56 @@ namespace Steamworks internal static void InstallEvents() { new Event( x => OnInventoryUpdated?.Invoke() ); - new Event( x => OnDefinitionsUpdated?.Invoke() ); - - Internal.LoadItemDefinitions(); + new Event( x => DefinitionsUpdated() ); } public static event Action OnInventoryUpdated; public static event Action OnDefinitionsUpdated; - public static async Task GetItemsWithPricesAsync() + internal static int defUpdateCount = 0; + + internal static void DefinitionsUpdated() + { + Definitions = GetDefinitions(); + defUpdateCount++; + + OnDefinitionsUpdated?.Invoke(); + } + + + /// + /// Call this if you're going to want to access definition information. You should be able to get + /// away with calling this once at the start if your game, assuming your items don't change all the time. + /// This will trigger OnDefinitionsUpdated at which point Definitions should be set. + /// + public static void LoadItemDefinitions() + { + Internal.LoadItemDefinitions(); + } + + /// + /// Will call LoadItemDefinitions and wait until Definitions is not null + /// + public static async Task WaitForDefinitions( float timeoutSeconds = 10 ) + { + LoadItemDefinitions(); + + var sw = Stopwatch.StartNew(); + + while ( Definitions == null ) + { + if ( sw.Elapsed.TotalSeconds > timeoutSeconds ) + return false; + + await Task.Delay( 10 ); + } + + return true; + } + public static string Currency { get; internal set; } + + public static async Task GetDefinitionsWithPricesAsync() { var priceRequest = await Internal.RequestPrices(); if ( !priceRequest.HasValue || priceRequest.Value.Result != Result.OK ) @@ -58,7 +99,23 @@ namespace Steamworks if ( !gotPrices ) return null; - return defs.Select( x => new SteamItemDef { _id = x } ).ToArray(); + return defs.Select( x => new SteamItemDef( x ) ).ToArray(); + } + + public static SteamItemDef[] Definitions { get; internal set; } + + internal static SteamItemDef[] GetDefinitions() + { + uint num = 0; + if ( !Internal.GetItemDefinitionIDs( null, ref num ) ) + return null; + + var defs = new SteamItemDef_t[num]; + + if ( !Internal.GetItemDefinitionIDs( defs, ref num ) ) + return null; + + return defs.Select( x => new SteamItemDef( x ) ).ToArray(); } } diff --git a/Generator/CodeWriter/Types/BaseType.cs b/Generator/CodeWriter/Types/BaseType.cs index 838b055..58fef23 100644 --- a/Generator/CodeWriter/Types/BaseType.cs +++ b/Generator/CodeWriter/Types/BaseType.cs @@ -68,6 +68,7 @@ internal class BaseType if ( VarName == "pArrayItemDefs" ) return true; if ( VarName == "pBasePrices" ) return true; if ( VarName == "pCurrentPrices" ) return true; + if ( VarName == "pItemDefIDs" ) return true; if ( VarName == "pDetails" && Func == "GetDownloadedLeaderboardEntry" ) return true; if ( VarName == "pData" && NativeType.EndsWith( "*" ) && Func.StartsWith( "GetGlobalStatHistory" ) ) return true; if ( NativeType.EndsWith( "**" ) ) return true;