mirror of
https://github.com/Facepunch/Facepunch.Steamworks.git
synced 2025-02-03 09:10:41 +03:00
GetItems, InventoryResult etc
This commit is contained in:
parent
67de9c8e96
commit
0d509bf126
@ -45,6 +45,31 @@ namespace Steamworks
|
||||
Console.WriteLine( $"[{def.Id:0000000000}] {def.Name} [{def.LocalPriceFormatted}]" );
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task GetAllItems()
|
||||
{
|
||||
await SteamInventory.WaitForDefinitions();
|
||||
|
||||
var result = await SteamInventory.GetItems();
|
||||
|
||||
Assert.IsTrue( result.HasValue );
|
||||
|
||||
using ( result )
|
||||
{
|
||||
var items = result?.GetItems( true );
|
||||
|
||||
foreach ( var item in items )
|
||||
{
|
||||
Console.WriteLine( $"{item.Id} / {item.DefId} / {item.Quantity} " );
|
||||
|
||||
foreach ( var prop in item.Properties )
|
||||
{
|
||||
Console.WriteLine( $" {prop.Key} : {prop.Value}" );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -78,7 +78,7 @@ namespace Steamworks
|
||||
|
||||
public static string Currency { get; internal set; }
|
||||
|
||||
public static async Task<SteamItemDef[]> GetDefinitionsWithPricesAsync()
|
||||
public static async Task<InventoryDef[]> GetDefinitionsWithPricesAsync()
|
||||
{
|
||||
var priceRequest = await Internal.RequestPrices();
|
||||
if ( !priceRequest.HasValue || priceRequest.Value.Result != Result.OK )
|
||||
@ -91,7 +91,7 @@ namespace Steamworks
|
||||
if ( num <= 0 )
|
||||
return null;
|
||||
|
||||
var defs = new SteamItemDef_t[num];
|
||||
var defs = new InventoryDefId[num];
|
||||
var currentPrices = new ulong[num];
|
||||
var baseprices = new ulong[num];
|
||||
|
||||
@ -99,23 +99,40 @@ namespace Steamworks
|
||||
if ( !gotPrices )
|
||||
return null;
|
||||
|
||||
return defs.Select( x => new SteamItemDef( x ) ).ToArray();
|
||||
return defs.Select( x => new InventoryDef( x ) ).ToArray();
|
||||
}
|
||||
|
||||
public static SteamItemDef[] Definitions { get; internal set; }
|
||||
public static InventoryDef[] Definitions { get; internal set; }
|
||||
|
||||
internal static SteamItemDef[] GetDefinitions()
|
||||
internal static InventoryDef[] GetDefinitions()
|
||||
{
|
||||
uint num = 0;
|
||||
if ( !Internal.GetItemDefinitionIDs( null, ref num ) )
|
||||
return null;
|
||||
|
||||
var defs = new SteamItemDef_t[num];
|
||||
var defs = new InventoryDefId[num];
|
||||
|
||||
if ( !Internal.GetItemDefinitionIDs( defs, ref num ) )
|
||||
return null;
|
||||
|
||||
return defs.Select( x => new SteamItemDef( x ) ).ToArray();
|
||||
return defs.Select( x => new InventoryDef( x ) ).ToArray();
|
||||
}
|
||||
|
||||
public static async Task<InventoryResult?> GetItems()
|
||||
{
|
||||
var sresult = default( SteamInventoryResult_t );
|
||||
|
||||
if ( !Internal.GetAllItems( ref sresult ) )
|
||||
return null;
|
||||
|
||||
var result = new InventoryResult( sresult );
|
||||
|
||||
if ( !await result.WaitUntilReadyAsync() )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -4,11 +4,11 @@ using Steamworks.Data;
|
||||
|
||||
namespace Steamworks
|
||||
{
|
||||
public struct SteamItemDef
|
||||
public struct InventoryDef
|
||||
{
|
||||
internal SteamItemDef_t _id;
|
||||
internal InventoryDefId _id;
|
||||
|
||||
public SteamItemDef( int defId )
|
||||
public InventoryDef( InventoryDefId defId )
|
||||
{
|
||||
_id = defId;
|
||||
}
|
83
Facepunch.Steamworks/Structs/InventoryItem.cs
Normal file
83
Facepunch.Steamworks/Structs/InventoryItem.cs
Normal file
@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Steamworks.Data;
|
||||
|
||||
namespace Steamworks
|
||||
{
|
||||
public struct InventoryItem
|
||||
{
|
||||
internal InventoryItemId _id;
|
||||
internal InventoryDefId _def;
|
||||
internal int _flags;
|
||||
internal ushort _quantity;
|
||||
internal Dictionary<string, string> _properties;
|
||||
|
||||
public InventoryItemId Id => _id;
|
||||
|
||||
public InventoryDefId DefId => _def;
|
||||
|
||||
public int Quantity => _quantity;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Only available if the result set was created with the getproperties
|
||||
/// </summary>
|
||||
public Dictionary<string, string> Properties => _properties;
|
||||
|
||||
/// <summary>
|
||||
/// This item is account-locked and cannot be traded or given away.
|
||||
/// This is an item status flag which is permanently attached to specific item instances
|
||||
/// </summary>
|
||||
public bool IsNoTrade => (_flags & 1 << 0) != 0;
|
||||
|
||||
/// <summary>
|
||||
/// The item has been destroyed, traded away, expired, or otherwise invalidated.
|
||||
/// This is an action confirmation flag which is only set one time, as part of a result set.
|
||||
/// </summary>
|
||||
public bool IsRemoved => (_flags & 1 << 8) != 0;
|
||||
|
||||
/// <summary>
|
||||
/// The item quantity has been decreased by 1 via ConsumeItem API.
|
||||
/// This is an action confirmation flag which is only set one time, as part of a result set.
|
||||
/// </summary>
|
||||
public bool IsConsumed => (_flags & 1 << 9) != 0;
|
||||
|
||||
internal static InventoryItem From( SteamItemDetails_t details )
|
||||
{
|
||||
var i = new InventoryItem
|
||||
{
|
||||
_id = details.ItemId,
|
||||
_def = details.Definition,
|
||||
_flags = details.Flags,
|
||||
_quantity = details.Quantity
|
||||
};
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
internal static Dictionary<string, string> GetProperties( SteamInventoryResult_t result, int index )
|
||||
{
|
||||
var sb = Helpers.TakeStringBuilder();
|
||||
var strlen = (uint) sb.Capacity;
|
||||
|
||||
if ( !SteamInventory.Internal.GetResultItemProperty( result, (uint)index, null, sb, ref strlen ) )
|
||||
return null;
|
||||
|
||||
var propNames = sb.ToString();
|
||||
|
||||
var props = new Dictionary<string, string>();
|
||||
|
||||
foreach ( var propertyName in propNames.Split( ',' ) )
|
||||
{
|
||||
strlen = (uint)sb.Capacity;
|
||||
|
||||
if ( SteamInventory.Internal.GetResultItemProperty( result, (uint)index, propertyName, sb, ref strlen ) )
|
||||
{
|
||||
props.Add( propertyName, sb.ToString() );
|
||||
}
|
||||
}
|
||||
|
||||
return props;
|
||||
}
|
||||
}
|
||||
}
|
74
Facepunch.Steamworks/Structs/InventoryResult.cs
Normal file
74
Facepunch.Steamworks/Structs/InventoryResult.cs
Normal file
@ -0,0 +1,74 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Steamworks.Data;
|
||||
|
||||
namespace Steamworks
|
||||
{
|
||||
public struct InventoryResult : IDisposable
|
||||
{
|
||||
internal SteamInventoryResult_t _id;
|
||||
internal Result _result;
|
||||
|
||||
internal InventoryResult( SteamInventoryResult_t id )
|
||||
{
|
||||
_id = id;
|
||||
_result = Result.Pending;
|
||||
}
|
||||
|
||||
internal async Task<bool> WaitUntilReadyAsync()
|
||||
{
|
||||
while ( _result == Result.Pending )
|
||||
{
|
||||
_result = SteamInventory.Internal.GetResultStatus( _id );
|
||||
Task.Delay( 10 );
|
||||
}
|
||||
|
||||
return _result == Result.OK || _result == Result.Expired;
|
||||
}
|
||||
|
||||
public int ItemCount
|
||||
{
|
||||
get
|
||||
{
|
||||
uint cnt = 0;
|
||||
|
||||
if ( !SteamInventory.Internal.GetResultItems( _id, null, ref cnt ) )
|
||||
return 0;
|
||||
|
||||
return (int) cnt;
|
||||
}
|
||||
}
|
||||
|
||||
public InventoryItem[] GetItems( bool includeProperties = false )
|
||||
{
|
||||
uint cnt = (uint) ItemCount;
|
||||
if ( cnt <= 0 ) return null;
|
||||
|
||||
var pOutItemsArray = new SteamItemDetails_t[cnt];
|
||||
|
||||
if ( !SteamInventory.Internal.GetResultItems( _id, pOutItemsArray, ref cnt ) )
|
||||
return null;
|
||||
|
||||
var items = new InventoryItem[cnt];
|
||||
|
||||
for( int i=0; i< cnt; i++ )
|
||||
{
|
||||
var item = InventoryItem.From( pOutItemsArray[i] );
|
||||
|
||||
if ( includeProperties )
|
||||
item._properties = InventoryItem.GetProperties( _id, i );
|
||||
|
||||
items[i] = item;
|
||||
}
|
||||
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
SteamInventory.Internal.DestroyResult( _id );
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user