mirror of
https://github.com/Facepunch/Facepunch.Steamworks.git
synced 2025-01-12 14:48:02 +03:00
Added Inventory.CraftItem
This commit is contained in:
parent
eabf5b84e2
commit
4e4fdd63ee
@ -62,7 +62,7 @@ namespace Facepunch.Steamworks
|
|||||||
protected void SetupCommonInterfaces()
|
protected void SetupCommonInterfaces()
|
||||||
{
|
{
|
||||||
Networking = new Steamworks.Networking( this, native.networking );
|
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 );
|
Workshop = new Steamworks.Workshop( this, native.ugc, native.remoteStorage );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,6 +49,8 @@ namespace Facepunch.Steamworks
|
|||||||
{
|
{
|
||||||
this.Handle = Handle;
|
this.Handle = Handle;
|
||||||
this.inventory = inventory;
|
this.inventory = inventory;
|
||||||
|
|
||||||
|
TryFill();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Block( float maxWait = 5.0f )
|
public bool Block( float maxWait = 5.0f )
|
||||||
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using SteamNative;
|
||||||
|
|
||||||
namespace Facepunch.Steamworks
|
namespace Facepunch.Steamworks
|
||||||
{
|
{
|
||||||
@ -33,27 +34,46 @@ namespace Facepunch.Steamworks
|
|||||||
|
|
||||||
internal SteamNative.SteamInventory inventory;
|
internal SteamNative.SteamInventory inventory;
|
||||||
|
|
||||||
private Result LocalPlayerRequest;
|
|
||||||
|
|
||||||
private bool IsServer { get; set; }
|
private bool IsServer { get; set; }
|
||||||
|
|
||||||
internal Inventory( SteamNative.SteamInventory c, bool server )
|
internal Inventory( BaseSteamworks steamworks, SteamNative.SteamInventory c, bool server )
|
||||||
{
|
{
|
||||||
IsServer = server;
|
IsServer = server;
|
||||||
inventory = c;
|
inventory = c;
|
||||||
|
|
||||||
inventory.LoadItemDefinitions();
|
inventory.LoadItemDefinitions();
|
||||||
FetchItemDefinitions();
|
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()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
if ( LocalPlayerRequest != null )
|
|
||||||
{
|
|
||||||
LocalPlayerRequest.Dispose();
|
|
||||||
LocalPlayerRequest = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
inventory = null;
|
inventory = null;
|
||||||
|
|
||||||
Items = null;
|
Items = null;
|
||||||
@ -80,18 +100,12 @@ namespace Facepunch.Steamworks
|
|||||||
{
|
{
|
||||||
if ( IsServer ) return;
|
if ( IsServer ) return;
|
||||||
|
|
||||||
// Pending
|
|
||||||
if ( LocalPlayerRequest != null )
|
|
||||||
return;
|
|
||||||
|
|
||||||
SteamNative.SteamInventoryResult_t request = 0;
|
SteamNative.SteamInventoryResult_t request = 0;
|
||||||
if ( !inventory.GetAllItems( ref request ) || request == -1 )
|
if ( !inventory.GetAllItems( ref request ) || request == -1 )
|
||||||
{
|
{
|
||||||
Console.WriteLine( "GetAllItems failed!?" );
|
Console.WriteLine( "GetAllItems failed!?" );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
LocalPlayerRequest = new Result( this, request );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -133,52 +147,8 @@ namespace Facepunch.Steamworks
|
|||||||
if ( Definitions == null )
|
if ( Definitions == null )
|
||||||
{
|
{
|
||||||
FetchItemDefinitions();
|
FetchItemDefinitions();
|
||||||
|
|
||||||
inventory.LoadItemDefinitions();
|
inventory.LoadItemDefinitions();
|
||||||
}
|
}
|
||||||
|
|
||||||
UpdateLocalRequest();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// If we have a local player request process it.
|
|
||||||
/// </summary>
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -228,5 +198,26 @@ namespace Facepunch.Steamworks
|
|||||||
return new Result( this, resultHandle );
|
return new Result( this, resultHandle );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Crafting! Uses the passed items to buy the target item.
|
||||||
|
/// You need to have set up the appropriate exchange rules in your item
|
||||||
|
/// definitions.
|
||||||
|
/// </summary>
|
||||||
|
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 );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -49,9 +49,6 @@ namespace Facepunch.Steamworks
|
|||||||
|
|
||||||
SteamNative.DownloadItemResult_t.RegisterCallback( steamworks, onDownloadResult );
|
SteamNative.DownloadItemResult_t.RegisterCallback( steamworks, onDownloadResult );
|
||||||
SteamNative.ItemInstalled_t.RegisterCallback( steamworks, onItemInstalled );
|
SteamNative.ItemInstalled_t.RegisterCallback( steamworks, onItemInstalled );
|
||||||
// SteamNative.SteamUGCQueryCompleted_t.RegisterCallback( steamworks, onQueryComplete );
|
|
||||||
|
|
||||||
// steamworks.AddCallback<ItemInstalled, ItemInstalled.Small>( onItemInstalled, ItemInstalled.CallbackId );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user