From 4e4fdd63eef060f47470a24bb6e59e884bbf5697 Mon Sep 17 00:00:00 2001 From: Garry Newman Date: Tue, 8 Nov 2016 14:40:22 +0000 Subject: [PATCH] Added Inventory.CraftItem --- Facepunch.Steamworks/BaseSteamworks.cs | 2 +- .../Interfaces/Inventory.Result.cs | 2 + Facepunch.Steamworks/Interfaces/Inventory.cs | 109 ++++++++---------- Facepunch.Steamworks/Interfaces/Workshop.cs | 3 - 4 files changed, 53 insertions(+), 63 deletions(-) diff --git a/Facepunch.Steamworks/BaseSteamworks.cs b/Facepunch.Steamworks/BaseSteamworks.cs index 7c16784..04160fa 100644 --- a/Facepunch.Steamworks/BaseSteamworks.cs +++ b/Facepunch.Steamworks/BaseSteamworks.cs @@ -62,7 +62,7 @@ namespace Facepunch.Steamworks protected void SetupCommonInterfaces() { Networking = new Steamworks.Networking( this, native.networking ); - Inventory = new Steamworks.Inventory( native.inventory, IsGameServer ); + Inventory = new Steamworks.Inventory( this, native.inventory, IsGameServer ); Workshop = new Steamworks.Workshop( this, native.ugc, native.remoteStorage ); } diff --git a/Facepunch.Steamworks/Interfaces/Inventory.Result.cs b/Facepunch.Steamworks/Interfaces/Inventory.Result.cs index f48165a..3d7dd35 100644 --- a/Facepunch.Steamworks/Interfaces/Inventory.Result.cs +++ b/Facepunch.Steamworks/Interfaces/Inventory.Result.cs @@ -49,6 +49,8 @@ namespace Facepunch.Steamworks { this.Handle = Handle; this.inventory = inventory; + + TryFill(); } public bool Block( float maxWait = 5.0f ) diff --git a/Facepunch.Steamworks/Interfaces/Inventory.cs b/Facepunch.Steamworks/Interfaces/Inventory.cs index a6235d7..d3f85c4 100644 --- a/Facepunch.Steamworks/Interfaces/Inventory.cs +++ b/Facepunch.Steamworks/Interfaces/Inventory.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; +using SteamNative; namespace Facepunch.Steamworks { @@ -33,27 +34,46 @@ namespace Facepunch.Steamworks internal SteamNative.SteamInventory inventory; - private Result LocalPlayerRequest; - private bool IsServer { get; set; } - internal Inventory( SteamNative.SteamInventory c, bool server ) + internal Inventory( BaseSteamworks steamworks, SteamNative.SteamInventory c, bool server ) { IsServer = server; inventory = c; inventory.LoadItemDefinitions(); FetchItemDefinitions(); + + if ( !server ) + { + SteamNative.SteamInventoryFullUpdate_t.RegisterCallback( steamworks, onFullUpdate ); + Refresh(); + } + } + + private void onFullUpdate( SteamInventoryFullUpdate_t data, bool error ) + { + if ( error ) return; + + var r = new Result( this, data.Handle ); + + SerializedItems = r.Serialize(); + SerializedExpireTime = DateTime.Now.Add( TimeSpan.FromMinutes( 60 ) ); + + Items = r.Items; + + r.Dispose(); + r = null; + + // + // Tell everyone we've got new items! + // + OnUpdate?.Invoke(); + } public void Dispose() { - if ( LocalPlayerRequest != null ) - { - LocalPlayerRequest.Dispose(); - LocalPlayerRequest = null; - } - inventory = null; Items = null; @@ -80,18 +100,12 @@ namespace Facepunch.Steamworks { if ( IsServer ) return; - // Pending - if ( LocalPlayerRequest != null ) - return; - SteamNative.SteamInventoryResult_t request = 0; if ( !inventory.GetAllItems( ref request ) || request == -1 ) { Console.WriteLine( "GetAllItems failed!?" ); return; } - - LocalPlayerRequest = new Result( this, request ); } /// @@ -133,52 +147,8 @@ namespace Facepunch.Steamworks if ( Definitions == null ) { FetchItemDefinitions(); - inventory.LoadItemDefinitions(); } - - UpdateLocalRequest(); - } - - /// - /// If we have a local player request process it. - /// - private void UpdateLocalRequest() - { - if ( LocalPlayerRequest == null ) - return; - - if ( LocalPlayerRequest.IsPending ) - return; - - if ( LocalPlayerRequest.IsSuccess ) - { - // Try again. - RetrieveInventory(); - return; - } - - // Some other error - // Lets just retry. - LocalPlayerRequest.Dispose(); - LocalPlayerRequest = null; - Refresh(); - } - - private unsafe void RetrieveInventory() - { - SerializedItems = LocalPlayerRequest.Serialize(); - SerializedExpireTime = DateTime.Now.Add( TimeSpan.FromMinutes( 60 ) ); - - Items = LocalPlayerRequest.Items; - - LocalPlayerRequest.Dispose(); - LocalPlayerRequest = null; - - // - // Tell everyone we've got new items! - // - OnUpdate?.Invoke(); } /// @@ -228,5 +198,26 @@ namespace Facepunch.Steamworks return new Result( this, resultHandle ); } } + + /// + /// Crafting! Uses the passed items to buy the target item. + /// You need to have set up the appropriate exchange rules in your item + /// definitions. + /// + public Result CraftItem( Item[] list, Definition target ) + { + SteamNative.SteamInventoryResult_t resultHandle = -1; + + var newItems = new SteamNative.SteamItemDef_t[] { new SteamNative.SteamItemDef_t() { Value = target.Id } }; + var newItemC = new uint[] { 1 }; + + var takeItems = list.Select( x => (SteamNative.SteamItemInstanceID_t)x.Id ).ToArray(); + var takeItemsC = list.Select( x => (uint)1 ).ToArray(); + + if ( !inventory.ExchangeItems( ref resultHandle, newItems, newItemC, 1, takeItems, takeItemsC, (uint)takeItems.Length ) ) + return null; + + return new Result( this, resultHandle ); + } } } diff --git a/Facepunch.Steamworks/Interfaces/Workshop.cs b/Facepunch.Steamworks/Interfaces/Workshop.cs index 9fe513f..27e04e1 100644 --- a/Facepunch.Steamworks/Interfaces/Workshop.cs +++ b/Facepunch.Steamworks/Interfaces/Workshop.cs @@ -49,9 +49,6 @@ namespace Facepunch.Steamworks SteamNative.DownloadItemResult_t.RegisterCallback( steamworks, onDownloadResult ); SteamNative.ItemInstalled_t.RegisterCallback( steamworks, onItemInstalled ); - // SteamNative.SteamUGCQueryCompleted_t.RegisterCallback( steamworks, onQueryComplete ); - - // steamworks.AddCallback( onItemInstalled, ItemInstalled.CallbackId ); } ///