From bda8b5b9562ab82e9b9f794bd762b7ee8bcf6e2f Mon Sep 17 00:00:00 2001 From: Garry Newman Date: Wed, 31 Jan 2018 13:30:51 +0000 Subject: [PATCH] Added Item.Properties --- .../Client/InventoryTest.cs | 28 +++++++ .../Interfaces/Inventory.Item.cs | 2 + .../Interfaces/Inventory.Result.cs | 84 +++++++++++-------- 3 files changed, 80 insertions(+), 34 deletions(-) diff --git a/Facepunch.Steamworks.Test/Client/InventoryTest.cs b/Facepunch.Steamworks.Test/Client/InventoryTest.cs index d4c363f..1bcdb8d 100644 --- a/Facepunch.Steamworks.Test/Client/InventoryTest.cs +++ b/Facepunch.Steamworks.Test/Client/InventoryTest.cs @@ -113,6 +113,34 @@ public void InventoryItemList() } } + [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] public void Deserialize() { diff --git a/Facepunch.Steamworks/Interfaces/Inventory.Item.cs b/Facepunch.Steamworks/Interfaces/Inventory.Item.cs index dbfb570..3475ce3 100644 --- a/Facepunch.Steamworks/Interfaces/Inventory.Item.cs +++ b/Facepunch.Steamworks/Interfaces/Inventory.Item.cs @@ -24,6 +24,8 @@ public struct Amount public int DefinitionId; + public Dictionary Properties { get; internal set; } + /// /// Careful, this might not be available. Especially on a game server. /// diff --git a/Facepunch.Steamworks/Interfaces/Inventory.Result.cs b/Facepunch.Steamworks/Interfaces/Inventory.Result.cs index 648b991..1c80b94 100644 --- a/Facepunch.Steamworks/Interfaces/Inventory.Result.cs +++ b/Facepunch.Steamworks/Interfaces/Inventory.Result.cs @@ -86,6 +86,7 @@ internal Result( Inventory inventory, int Handle, bool pending ) this.inventory = inventory; } + internal void Fill() { if ( _gotResult ) @@ -106,44 +107,31 @@ internal void Fill() if ( steamItems == null ) 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 ) - .Select( x => - { - 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(); + var tempItems = new List(); + var tempRemoved = new List(); + var tempConsumed = new List(); - Removed = steamItems.Where( x => ( (int)x.Flags & (int)SteamNative.SteamItemFlags.Removed ) != 0 ) - .Select( x => + for ( int i=0; i< steamItems.Length; i++ ) { - 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(); + var item = inventory.ItemFrom( Handle, steamItems[i], i ); - Consumed = steamItems.Where( x => ( (int)x.Flags & (int)SteamNative.SteamItemFlags.Consumed ) != 0 ) - .Select( x => - { - return new Inventory.Item() + if ( ( steamItems[i].Flags & (int)SteamNative.SteamItemFlags.Removed ) != 0 ) { - Quantity = x.Quantity, - Id = x.ItemId, - DefinitionId = x.Definition, - TradeLocked = ( (int)x.Flags & (int)SteamNative.SteamItemFlags.NoTrade ) != 0, - Definition = inventory.FindDefinition( x.Definition ) - }; - } ).ToArray(); + tempRemoved.Add(item); + } + else if ((steamItems[i].Flags & (int)SteamNative.SteamItemFlags.Consumed) != 0) + { + tempConsumed.Add(item); + } + else + { + tempItems.Add(item); + } + } + + Items = tempItems.ToArray(); + Removed = tempRemoved.ToArray(); + Consumed = tempConsumed.ToArray(); if ( OnResult != null ) { @@ -186,5 +174,33 @@ public void Dispose() inventory = null; } } + + internal Item ItemFrom( SteamInventoryResult_t handle, SteamItemDetails_t detail, int index ) + { + var props = new Dictionary(); + + 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; + } } }