Consume, Split, Transfer

This commit is contained in:
Garry Newman 2019-04-29 11:33:08 +01:00
parent b74d45814a
commit 683925430d
3 changed files with 61 additions and 22 deletions

View File

@ -145,14 +145,7 @@ internal static InventoryDef[] GetDefinitions()
if ( !Internal.GetAllItems( ref sresult ) )
return null;
var result = new InventoryResult( sresult );
if ( !await result.WaitUntilReadyAsync() )
{
return null;
}
return result;
return await InventoryResult.GetAsync( sresult );
}
}

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Steamworks.Data;
namespace Steamworks
@ -44,6 +45,46 @@ public struct InventoryItem
/// </summary>
public bool IsConsumed => (_flags & 1 << 9) != 0;
/// <summary>
/// Consumes items from a user's inventory. If the quantity of the given item goes to zero, it is permanently removed.
/// Once an item is removed it cannot be recovered.This is not for the faint of heart - if your game implements item removal at all,
/// a high-friction UI confirmation process is highly recommended.ConsumeItem can be restricted to certain item definitions or fully
/// blocked via the Steamworks website to minimize support/abuse issues such as the classic "my brother borrowed my laptop and deleted all of my rare items".
/// </summary>
public async Task<InventoryResult?> Consume( int amount = 1 )
{
var sresult = default( SteamInventoryResult_t );
if ( !SteamInventory.Internal.ConsumeItem( ref sresult, Id, (uint)amount ) )
return null;
return await InventoryResult.GetAsync( sresult );
}
/// <summary>
/// Split stack into two items
/// </summary>
public async Task<InventoryResult?> SplitStack( int quantity = 1 )
{
var sresult = default( SteamInventoryResult_t );
if ( !SteamInventory.Internal.TransferItemQuantity( ref sresult, Id, (uint)quantity, ulong.MaxValue ) )
return null;
return await InventoryResult.GetAsync( sresult );
}
/// <summary>
/// Transfer x quantity from this item to the target item
/// </summary>
public async Task<InventoryResult?> TransferItemQuantity( InventoryItem toItem, int quantity )
{
var sresult = default( SteamInventoryResult_t );
if ( !SteamInventory.Internal.TransferItemQuantity( ref sresult, Id, (uint)quantity, toItem.Id ) )
return null;
return await InventoryResult.GetAsync( sresult );
}
internal static InventoryItem From( SteamItemDetails_t details )
{
var i = new InventoryItem

View File

@ -8,23 +8,13 @@ namespace Steamworks
public struct InventoryResult : IDisposable
{
internal SteamInventoryResult_t _id;
internal Result _result;
public bool Expired { get; internal set; }
internal InventoryResult( SteamInventoryResult_t id )
internal InventoryResult( SteamInventoryResult_t id, bool expired )
{
_id = id;
_result = Result.Pending;
}
internal async Task<bool> WaitUntilReadyAsync()
{
while ( _result == Result.Pending )
{
_result = SteamInventory.Internal.GetResultStatus( _id );
await Task.Delay( 10 );
}
return _result == Result.OK || _result == Result.Expired;
Expired = expired;
}
public int ItemCount
@ -71,6 +61,21 @@ public void Dispose()
SteamInventory.Internal.DestroyResult( _id );
}
internal static async Task<InventoryResult?> GetAsync( SteamInventoryResult_t sresult )
{
var _result = Result.Pending;
while ( _result == Result.Pending )
{
_result = SteamInventory.Internal.GetResultStatus( sresult );
await Task.Delay( 10 );
}
if ( _result != Result.OK && _result != Result.Expired )
return null;
return new InventoryResult( sresult, _result == Result.Expired );
}
/// <summary>
/// Serialized result sets contain a short signature which can't be forged or replayed across different game sessions.
/// A result set can be serialized on the local client, transmitted to other players via your game networking, and