GetDefinitions

This commit is contained in:
Garry Newman 2019-04-27 21:52:47 +01:00
parent 37f6192213
commit d2ce2fe13b
4 changed files with 90 additions and 25 deletions

View File

@ -13,29 +13,36 @@ namespace Steamworks
public class InventoryTest public class InventoryTest
{ {
[TestMethod] [TestMethod]
public async Task GetItemsWithPricesAsync() public async Task LoadItemDefinitionsAsync()
{ {
var items = await SteamInventory.GetItemsWithPricesAsync(); 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] [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}" ); Console.WriteLine( $"[{def.Id:0000000000}] {def.Name} [{def.LocalPriceFormatted}]" );
foreach ( var prop in item.Properties )
{
Console.WriteLine( $" {prop.Key}: {prop.Value}" );
}
Console.WriteLine( $"" );
} }
} }
} }

View File

@ -307,13 +307,13 @@ namespace Steamworks
#region FunctionMeta #region FunctionMeta
[UnmanagedFunctionPointer( CallingConvention.ThisCall )] [UnmanagedFunctionPointer( CallingConvention.ThisCall )]
[return: MarshalAs( UnmanagedType.I1 )] [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; private FGetItemDefinitionIDs _GetItemDefinitionIDs;
#endregion #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 #region FunctionMeta
@ -342,13 +342,13 @@ namespace Steamworks
#region FunctionMeta #region FunctionMeta
[UnmanagedFunctionPointer( CallingConvention.ThisCall )] [UnmanagedFunctionPointer( CallingConvention.ThisCall )]
[return: MarshalAs( UnmanagedType.I1 )] [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; private FGetEligiblePromoItemDefinitionIDs _GetEligiblePromoItemDefinitionIDs;
#endregion #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 #region FunctionMeta

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
@ -28,16 +29,56 @@ namespace Steamworks
internal static void InstallEvents() internal static void InstallEvents()
{ {
new Event<SteamInventoryFullUpdate_t>( x => OnInventoryUpdated?.Invoke() ); new Event<SteamInventoryFullUpdate_t>( x => OnInventoryUpdated?.Invoke() );
new Event<SteamInventoryDefinitionUpdate_t>( x => OnDefinitionsUpdated?.Invoke() ); new Event<SteamInventoryDefinitionUpdate_t>( x => DefinitionsUpdated() );
Internal.LoadItemDefinitions();
} }
public static event Action OnInventoryUpdated; public static event Action OnInventoryUpdated;
public static event Action OnDefinitionsUpdated; public static event Action OnDefinitionsUpdated;
public static async Task<SteamItemDef[]> GetItemsWithPricesAsync() internal static int defUpdateCount = 0;
internal static void DefinitionsUpdated()
{
Definitions = GetDefinitions();
defUpdateCount++;
OnDefinitionsUpdated?.Invoke();
}
/// <summary>
/// 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.
/// </summary>
public static void LoadItemDefinitions()
{
Internal.LoadItemDefinitions();
}
/// <summary>
/// Will call LoadItemDefinitions and wait until Definitions is not null
/// </summary>
public static async Task<bool> 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 string Currency { get; internal set; }
public static async Task<SteamItemDef[]> GetDefinitionsWithPricesAsync()
{ {
var priceRequest = await Internal.RequestPrices(); var priceRequest = await Internal.RequestPrices();
if ( !priceRequest.HasValue || priceRequest.Value.Result != Result.OK ) if ( !priceRequest.HasValue || priceRequest.Value.Result != Result.OK )
@ -58,7 +99,23 @@ namespace Steamworks
if ( !gotPrices ) if ( !gotPrices )
return null; 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();
} }
} }

View File

@ -68,6 +68,7 @@ internal class BaseType
if ( VarName == "pArrayItemDefs" ) return true; if ( VarName == "pArrayItemDefs" ) return true;
if ( VarName == "pBasePrices" ) return true; if ( VarName == "pBasePrices" ) return true;
if ( VarName == "pCurrentPrices" ) return true; if ( VarName == "pCurrentPrices" ) return true;
if ( VarName == "pItemDefIDs" ) return true;
if ( VarName == "pDetails" && Func == "GetDownloadedLeaderboardEntry" ) return true; if ( VarName == "pDetails" && Func == "GetDownloadedLeaderboardEntry" ) return true;
if ( VarName == "pData" && NativeType.EndsWith( "*" ) && Func.StartsWith( "GetGlobalStatHistory" ) ) return true; if ( VarName == "pData" && NativeType.EndsWith( "*" ) && Func.StartsWith( "GetGlobalStatHistory" ) ) return true;
if ( NativeType.EndsWith( "**" ) ) return true; if ( NativeType.EndsWith( "**" ) ) return true;