mirror of
https://github.com/Facepunch/Facepunch.Steamworks.git
synced 2025-04-10 19:40:04 +03:00
Item Prices
This commit is contained in:
parent
5a25d750f2
commit
aa666a3149
@ -13,9 +13,30 @@ namespace Steamworks
|
|||||||
public class InventoryTest
|
public class InventoryTest
|
||||||
{
|
{
|
||||||
[TestMethod]
|
[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( $"" );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -387,13 +387,13 @@ namespace Steamworks
|
|||||||
#region FunctionMeta
|
#region FunctionMeta
|
||||||
[UnmanagedFunctionPointer( CallingConvention.ThisCall )]
|
[UnmanagedFunctionPointer( CallingConvention.ThisCall )]
|
||||||
[return: MarshalAs( UnmanagedType.I1 )]
|
[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;
|
private FGetItemsWithPrices _GetItemsWithPrices;
|
||||||
|
|
||||||
#endregion
|
#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
|
#region FunctionMeta
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@ -28,10 +29,37 @@ namespace Steamworks
|
|||||||
{
|
{
|
||||||
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 => OnDefinitionsUpdated?.Invoke() );
|
||||||
|
|
||||||
|
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()
|
||||||
|
{
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
170
Facepunch.Steamworks/Structs/SteamItemDef.cs
Normal file
170
Facepunch.Steamworks/Structs/SteamItemDef.cs
Normal file
@ -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;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Shortcut to call GetProperty( "name" )
|
||||||
|
/// </summary>
|
||||||
|
public string Name => GetProperty( "name" );
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Shortcut to call GetProperty( "description" )
|
||||||
|
/// </summary>
|
||||||
|
public string Description => GetProperty( "description" );
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Shortcut to call GetProperty( "icon_url" )
|
||||||
|
/// </summary>
|
||||||
|
public string IconUrl => GetProperty( "icon_url" );
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Shortcut to call GetProperty( "icon_url_large" )
|
||||||
|
/// </summary>
|
||||||
|
public string IconUrlLarge => GetProperty( "icon_url_large" );
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Shortcut to call GetProperty( "price_category" )
|
||||||
|
/// </summary>
|
||||||
|
public string PriceCategory => GetProperty( "price_category" );
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Shortcut to call GetProperty( "type" )
|
||||||
|
/// </summary>
|
||||||
|
public string Type => GetProperty( "type" );
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Shortcut to call GetProperty( "exchange" )
|
||||||
|
/// </summary>
|
||||||
|
public string Exchange => GetProperty( "exchange" );
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Shortcut to call GetBoolProperty( "marketable" )
|
||||||
|
/// </summary>
|
||||||
|
public bool Marketable => GetBoolProperty( "marketable" );
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Shortcut to call GetBoolProperty( "tradable" )
|
||||||
|
/// </summary>
|
||||||
|
public bool Tradable => GetBoolProperty( "tradable" );
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the property timestamp
|
||||||
|
/// </summary>
|
||||||
|
public DateTime Created => GetProperty<DateTime>( "timestamp" );
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the property modified
|
||||||
|
/// </summary>
|
||||||
|
public DateTime Modified => GetProperty<DateTime>( "modified" );
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get a specific property by name
|
||||||
|
/// </summary>
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Read a raw property from the definition schema
|
||||||
|
/// </summary>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Read a raw property from the definition schema
|
||||||
|
/// </summary>
|
||||||
|
public T GetProperty<T>( 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 );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a list of all properties on this item
|
||||||
|
/// </summary>
|
||||||
|
public IEnumerable<KeyValuePair<string, string>> Properties
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
var list = GetProperty( null );
|
||||||
|
var keys = list.Split( ',' );
|
||||||
|
|
||||||
|
foreach ( var key in keys )
|
||||||
|
{
|
||||||
|
yield return new KeyValuePair<string, string>( key, GetProperty( key ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the price of this item in the local currency (SteamInventory.Currency)
|
||||||
|
/// </summary>
|
||||||
|
public int LocalPrice
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
ulong curprice = 0;
|
||||||
|
ulong baseprice = 0;
|
||||||
|
|
||||||
|
if ( !SteamInventory.Internal.GetItemPrice( Id, ref curprice, ref baseprice ) )
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return (int) curprice;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// If the price has been discounted, LocalPrice will differ from LocalBasePrice
|
||||||
|
/// (assumed, this isn't documented)
|
||||||
|
/// </summary>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
@ -66,6 +66,8 @@ internal class BaseType
|
|||||||
if ( VarName == "prgUsers" ) return true;
|
if ( VarName == "prgUsers" ) return true;
|
||||||
if ( VarName == "punArrayQuantity" ) return true;
|
if ( VarName == "punArrayQuantity" ) return true;
|
||||||
if ( VarName == "pArrayItemDefs" ) 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 == "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;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user