Added Item.Properties

This commit is contained in:
Garry Newman 2018-01-31 13:30:51 +00:00
parent 627fa4c825
commit bda8b5b956
3 changed files with 80 additions and 34 deletions

View File

@ -113,6 +113,34 @@ namespace Facepunch.Steamworks.Test
} }
} }
[TestMethod]
public void InventoryItemProperties()
{
using (var client = new Facepunch.Steamworks.Client(252490))
{
while ( true )
{
client.Update();
if (client.Inventory.Items == null) continue;
foreach (var item in client.Inventory.Items)
{
Console.WriteLine($"{item.Id} ({item.Definition.Name})");
foreach (var property in item.Properties)
{
Console.WriteLine($" {property.Key} = {property.Value}");
}
Console.WriteLine("");
}
return;
}
}
}
[TestMethod] [TestMethod]
public void Deserialize() public void Deserialize()
{ {

View File

@ -24,6 +24,8 @@ namespace Facepunch.Steamworks
public int DefinitionId; public int DefinitionId;
public Dictionary<string, string> Properties { get; internal set; }
/// <summary> /// <summary>
/// Careful, this might not be available. Especially on a game server. /// Careful, this might not be available. Especially on a game server.
/// </summary> /// </summary>

View File

@ -86,6 +86,7 @@ namespace Facepunch.Steamworks
this.inventory = inventory; this.inventory = inventory;
} }
internal void Fill() internal void Fill()
{ {
if ( _gotResult ) if ( _gotResult )
@ -106,44 +107,31 @@ namespace Facepunch.Steamworks
if ( steamItems == null ) if ( steamItems == null )
return; return;
Items = steamItems.Where( x => ( (int)x.Flags & (int)SteamNative.SteamItemFlags.Removed ) != (int)SteamNative.SteamItemFlags.Removed && ( (int)x.Flags & (int)SteamNative.SteamItemFlags.Consumed ) != (int)SteamNative.SteamItemFlags.Consumed ) var tempItems = new List<Item>();
.Select( x => var tempRemoved = new List<Item>();
{ var tempConsumed = new List<Item>();
return new Inventory.Item()
{
Quantity = x.Quantity,
Id = x.ItemId,
DefinitionId = x.Definition,
TradeLocked = ( (int)x.Flags & (int)SteamNative.SteamItemFlags.NoTrade ) != 0,
Definition = inventory.FindDefinition( x.Definition )
};
} ).ToArray();
Removed = steamItems.Where( x => ( (int)x.Flags & (int)SteamNative.SteamItemFlags.Removed ) != 0 ) for ( int i=0; i< steamItems.Length; i++ )
.Select( x =>
{ {
return new Inventory.Item() var item = inventory.ItemFrom( Handle, steamItems[i], i );
{
Quantity = x.Quantity,
Id = x.ItemId,
DefinitionId = x.Definition,
TradeLocked = ( (int)x.Flags & (int)SteamNative.SteamItemFlags.NoTrade ) != 0,
Definition = inventory.FindDefinition( x.Definition )
};
} ).ToArray();
Consumed = steamItems.Where( x => ( (int)x.Flags & (int)SteamNative.SteamItemFlags.Consumed ) != 0 ) if ( ( steamItems[i].Flags & (int)SteamNative.SteamItemFlags.Removed ) != 0 )
.Select( x =>
{ {
return new Inventory.Item() tempRemoved.Add(item);
}
else if ((steamItems[i].Flags & (int)SteamNative.SteamItemFlags.Consumed) != 0)
{ {
Quantity = x.Quantity, tempConsumed.Add(item);
Id = x.ItemId, }
DefinitionId = x.Definition, else
TradeLocked = ( (int)x.Flags & (int)SteamNative.SteamItemFlags.NoTrade ) != 0, {
Definition = inventory.FindDefinition( x.Definition ) tempItems.Add(item);
}; }
} ).ToArray(); }
Items = tempItems.ToArray();
Removed = tempRemoved.ToArray();
Consumed = tempConsumed.ToArray();
if ( OnResult != null ) if ( OnResult != null )
{ {
@ -186,5 +174,33 @@ namespace Facepunch.Steamworks
inventory = null; inventory = null;
} }
} }
internal Item ItemFrom( SteamInventoryResult_t handle, SteamItemDetails_t detail, int index )
{
var props = new Dictionary<string, string>();
if ( inventory.GetResultItemProperty(handle, (uint) index, null, out string propertyNames) )
{
foreach ( var propertyName in propertyNames.Split( ',' ) )
{
if ( inventory.GetResultItemProperty(handle, (uint)index, propertyName, out string propertyValue ) )
{
props.Add(propertyName, propertyValue);
}
}
}
var item = new Item()
{
Quantity = detail.Quantity,
Id = detail.ItemId,
DefinitionId = detail.Definition,
TradeLocked = ((int)detail.Flags & (int)SteamNative.SteamItemFlags.NoTrade) != 0,
Definition = FindDefinition(detail.Definition),
Properties = props
};
return item;
}
} }
} }