diff --git a/Facepunch.Steamworks.Test/InventoryTest.cs b/Facepunch.Steamworks.Test/InventoryTest.cs index f099384..fa55ce8 100644 --- a/Facepunch.Steamworks.Test/InventoryTest.cs +++ b/Facepunch.Steamworks.Test/InventoryTest.cs @@ -13,9 +13,30 @@ namespace Steamworks public class InventoryTest { [TestMethod] - public async Task GetDefinitions() + public async Task GetItemsWithPricesAsync() { + var items = await SteamInventory.GetItemsWithPricesAsync(); + foreach ( var item in items ) + { + Console.WriteLine( $"[{item.LocalPrice}] {item.Name}" ); + } + } + + [TestMethod] + public async Task IutemDefs() + { + var items = await SteamInventory.GetItemsWithPricesAsync(); + + foreach ( var item in items ) + { + Console.WriteLine( $"{item.Id}" ); + foreach ( var prop in item.Properties ) + { + Console.WriteLine( $" {prop.Key}: {prop.Value}" ); + } + Console.WriteLine( $"" ); + } } } diff --git a/Facepunch.Steamworks/Generated/Interfaces/ISteamInventory.cs b/Facepunch.Steamworks/Generated/Interfaces/ISteamInventory.cs index 33aa62c..de35e84 100644 --- a/Facepunch.Steamworks/Generated/Interfaces/ISteamInventory.cs +++ b/Facepunch.Steamworks/Generated/Interfaces/ISteamInventory.cs @@ -387,13 +387,13 @@ namespace Steamworks #region FunctionMeta [UnmanagedFunctionPointer( CallingConvention.ThisCall )] [return: MarshalAs( UnmanagedType.I1 )] - private delegate bool FGetItemsWithPrices( IntPtr self, [In,Out] SteamItemDef_t[] pArrayItemDefs, ref ulong pCurrentPrices, ref ulong pBasePrices, uint unArrayLength ); + private delegate bool FGetItemsWithPrices( IntPtr self, [In,Out] SteamItemDef_t[] pArrayItemDefs, [In,Out] ulong[] pCurrentPrices, [In,Out] ulong[] pBasePrices, uint unArrayLength ); private FGetItemsWithPrices _GetItemsWithPrices; #endregion - internal bool GetItemsWithPrices( [In,Out] SteamItemDef_t[] pArrayItemDefs, ref ulong pCurrentPrices, ref ulong pBasePrices, uint unArrayLength ) + internal bool GetItemsWithPrices( [In,Out] SteamItemDef_t[] pArrayItemDefs, [In,Out] ulong[] pCurrentPrices, [In,Out] ulong[] pBasePrices, uint unArrayLength ) { - return _GetItemsWithPrices( Self, pArrayItemDefs, ref pCurrentPrices, ref pBasePrices, unArrayLength ); + return _GetItemsWithPrices( Self, pArrayItemDefs, pCurrentPrices, pBasePrices, unArrayLength ); } #region FunctionMeta diff --git a/Facepunch.Steamworks/SteamInventory.cs b/Facepunch.Steamworks/SteamInventory.cs index 05f3b9b..ad69f20 100644 --- a/Facepunch.Steamworks/SteamInventory.cs +++ b/Facepunch.Steamworks/SteamInventory.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; @@ -28,10 +29,37 @@ namespace Steamworks { new Event( x => OnInventoryUpdated?.Invoke() ); new Event( x => OnDefinitionsUpdated?.Invoke() ); + + Internal.LoadItemDefinitions(); } public static event Action OnInventoryUpdated; public static event Action OnDefinitionsUpdated; + public static async Task GetItemsWithPricesAsync() + { + var priceRequest = await Internal.RequestPrices(); + if ( !priceRequest.HasValue || priceRequest.Value.Result != Result.OK ) + return null; + + Console.WriteLine( $"Currency: {priceRequest?.Currency}" ); + + var num = Internal.GetNumItemsWithPrices(); + + Console.WriteLine( $"num: {num}" ); + if ( num <= 0 ) + return null; + + var defs = new SteamItemDef_t[num]; + var currentPrices = new ulong[num]; + var baseprices = new ulong[num]; + + var gotPrices = Internal.GetItemsWithPrices( defs, currentPrices, baseprices, num ); + if ( !gotPrices ) + return null; + + return defs.Select( x => new SteamItemDef { _id = x } ).ToArray(); + } + } } \ No newline at end of file diff --git a/Facepunch.Steamworks/Structs/SteamItemDef.cs b/Facepunch.Steamworks/Structs/SteamItemDef.cs new file mode 100644 index 0000000..fd1fa8f --- /dev/null +++ b/Facepunch.Steamworks/Structs/SteamItemDef.cs @@ -0,0 +1,170 @@ +using System; +using System.Collections.Generic; +using Steamworks.Data; + +namespace Steamworks +{ + public struct SteamItemDef + { + internal SteamItemDef_t _id; + + public int Id => _id.Value; + + /// + /// Shortcut to call GetProperty( "name" ) + /// + public string Name => GetProperty( "name" ); + + /// + /// Shortcut to call GetProperty( "description" ) + /// + public string Description => GetProperty( "description" ); + + /// + /// Shortcut to call GetProperty( "icon_url" ) + /// + public string IconUrl => GetProperty( "icon_url" ); + + /// + /// Shortcut to call GetProperty( "icon_url_large" ) + /// + public string IconUrlLarge => GetProperty( "icon_url_large" ); + + /// + /// Shortcut to call GetProperty( "price_category" ) + /// + public string PriceCategory => GetProperty( "price_category" ); + + /// + /// Shortcut to call GetProperty( "type" ) + /// + public string Type => GetProperty( "type" ); + + /// + /// Shortcut to call GetProperty( "exchange" ) + /// + public string Exchange => GetProperty( "exchange" ); + + /// + /// Shortcut to call GetBoolProperty( "marketable" ) + /// + public bool Marketable => GetBoolProperty( "marketable" ); + + /// + /// Shortcut to call GetBoolProperty( "tradable" ) + /// + public bool Tradable => GetBoolProperty( "tradable" ); + + /// + /// Gets the property timestamp + /// + public DateTime Created => GetProperty( "timestamp" ); + + /// + /// Gets the property modified + /// + public DateTime Modified => GetProperty( "modified" ); + + /// + /// Get a specific property by name + /// + public string GetProperty( string name ) + { + var sb = Helpers.TakeStringBuilder(); + uint _ = (uint)sb.Capacity; + + if ( !SteamInventory.Internal.GetItemDefinitionProperty( Id, name, sb, ref _ ) ) + return null; + + return sb.ToString(); + } + + /// + /// Read a raw property from the definition schema + /// + public bool GetBoolProperty( string name ) + { + string val = GetProperty( name ); + + if ( val.Length == 0 ) return false; + if ( val[0] == '0' || val[0] == 'F' || val[0] == 'f' ) return false; + + return true; + } + + /// + /// Read a raw property from the definition schema + /// + public T GetProperty( string name ) + { + string val = GetProperty( name ); + + if ( string.IsNullOrEmpty( val ) ) + return default( T ); + + try + { + return (T)Convert.ChangeType( val, typeof( T ) ); + } + catch ( System.Exception ) + { + return default( T ); + } + } + + /// + /// Gets a list of all properties on this item + /// + public IEnumerable> Properties + { + get + { + var list = GetProperty( null ); + var keys = list.Split( ',' ); + + foreach ( var key in keys ) + { + yield return new KeyValuePair( key, GetProperty( key ) ); + } + } + } + + /// + /// Returns the price of this item in the local currency (SteamInventory.Currency) + /// + public int LocalPrice + { + get + { + ulong curprice = 0; + ulong baseprice = 0; + + if ( !SteamInventory.Internal.GetItemPrice( Id, ref curprice, ref baseprice ) ) + return 0; + + return (int) curprice; + } + } + + /// + /// If the price has been discounted, LocalPrice will differ from LocalBasePrice + /// (assumed, this isn't documented) + /// + public int LocalBasePrice + { + get + { + ulong curprice = 0; + ulong baseprice = 0; + + if ( !SteamInventory.Internal.GetItemPrice( Id, ref curprice, ref baseprice ) ) + return 0; + + return (int)baseprice; + } + } + + public int CurrentPrice; + public int BasePrice; + } +} \ No newline at end of file diff --git a/Generator/CodeWriter/Types/BaseType.cs b/Generator/CodeWriter/Types/BaseType.cs index 978d9b9..838b055 100644 --- a/Generator/CodeWriter/Types/BaseType.cs +++ b/Generator/CodeWriter/Types/BaseType.cs @@ -66,6 +66,8 @@ internal class BaseType if ( VarName == "prgUsers" ) return true; if ( VarName == "punArrayQuantity" ) return true; if ( VarName == "pArrayItemDefs" ) return true; + if ( VarName == "pBasePrices" ) return true; + if ( VarName == "pCurrentPrices" ) return true; if ( VarName == "pDetails" && Func == "GetDownloadedLeaderboardEntry" ) return true; if ( VarName == "pData" && NativeType.EndsWith( "*" ) && Func.StartsWith( "GetGlobalStatHistory" ) ) return true; if ( NativeType.EndsWith( "**" ) ) return true;