Fixed inventory.item reliance on definition

This commit is contained in:
Garry Newman 2018-03-21 14:41:59 +00:00
parent 835770772f
commit 80bf89c9e6
3 changed files with 48 additions and 26 deletions

View File

@ -11,8 +11,16 @@ public partial class Inventory
/// <summary>
/// An item in your inventory.
/// </summary>
public struct Item : IEquatable<Item>
public class Item : IEquatable<Item>
{
internal Item( Inventory Inventory, ulong Id, int Quantity, int DefinitionId )
{
this.Inventory = Inventory;
this.Id = Id;
this.Quantity = Quantity;
this.DefinitionId = DefinitionId;
}
public struct Amount
{
public Item Item;
@ -24,12 +32,26 @@ public struct Amount
public int DefinitionId;
internal Inventory Inventory;
public Dictionary<string, string> Properties { get; internal set; }
private Definition _cachedDefinition;
/// <summary>
/// Careful, this might not be available. Especially on a game server.
/// </summary>
public Definition Definition;
public Definition Definition
{
get
{
if ( _cachedDefinition != null )
return _cachedDefinition;
_cachedDefinition = Inventory.FindDefinition( DefinitionId );
return _cachedDefinition;
}
}
public bool TradeLocked;
@ -73,10 +95,10 @@ public override int GetHashCode()
public Result Consume( int amount = 1 )
{
SteamNative.SteamInventoryResult_t resultHandle = -1;
if ( !Definition.inventory.inventory.ConsumeItem( ref resultHandle, Id, (uint)amount ) )
if ( !Inventory.inventory.ConsumeItem( ref resultHandle, Id, (uint)amount ) )
return null;
return new Result( Definition.inventory, resultHandle, true );
return new Result( Inventory, resultHandle, true );
}
/// <summary>
@ -85,10 +107,10 @@ public Result Consume( int amount = 1 )
public Result SplitStack( int quantity = 1 )
{
SteamNative.SteamInventoryResult_t resultHandle = -1;
if ( !Definition.inventory.inventory.TransferItemQuantity( ref resultHandle, Id, (uint)quantity, ulong.MaxValue ) )
if ( !Inventory.inventory.TransferItemQuantity( ref resultHandle, Id, (uint)quantity, ulong.MaxValue ) )
return null;
return new Result( Definition.inventory, resultHandle, true );
return new Result( Inventory, resultHandle, true );
}
SteamNative.SteamInventoryUpdateHandle_t updateHandle;
@ -97,35 +119,35 @@ private void UpdatingProperties()
{
if (updateHandle != 0) return;
updateHandle = Definition.inventory.inventory.StartUpdateProperties();
updateHandle = Inventory.inventory.StartUpdateProperties();
}
public bool SetProperty( string name, string value )
{
UpdatingProperties();
Properties[name] = value.ToString();
return Definition.inventory.inventory.SetProperty(updateHandle, Id, name, value);
return Inventory.inventory.SetProperty(updateHandle, Id, name, value);
}
public bool SetProperty(string name, bool value)
{
UpdatingProperties();
Properties[name] = value.ToString();
return Definition.inventory.inventory.SetProperty0(updateHandle, Id, name, value);
return Inventory.inventory.SetProperty0(updateHandle, Id, name, value);
}
public bool SetProperty(string name, long value)
{
UpdatingProperties();
Properties[name] = value.ToString();
return Definition.inventory.inventory.SetProperty1(updateHandle, Id, name, value);
return Inventory.inventory.SetProperty1(updateHandle, Id, name, value);
}
public bool SetProperty(string name, float value)
{
UpdatingProperties();
Properties[name] = value.ToString();
return Definition.inventory.inventory.SetProperty2(updateHandle, Id, name, value);
return Inventory.inventory.SetProperty2(updateHandle, Id, name, value);
}
/// <summary>
@ -140,12 +162,12 @@ public bool SubmitProperties()
{
SteamNative.SteamInventoryResult_t result = -1;
if (!Definition.inventory.inventory.SubmitUpdateProperties(updateHandle, ref result))
if (!Inventory.inventory.SubmitUpdateProperties(updateHandle, ref result))
{
return false;
}
Definition.inventory.inventory.DestroyResult(result);
Inventory.inventory.DestroyResult(result);
return true;
}

View File

@ -194,15 +194,8 @@ internal Item ItemFrom( SteamInventoryResult_t handle, SteamItemDetails_t detail
}
}
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
};
var item = new Item( this, detail.ItemId, detail.Quantity, detail.Definition );
item.Properties = props;
return item;
}

View File

@ -50,7 +50,9 @@ internal Inventory( BaseSteamworks steamworks, SteamNative.SteamInventory c, boo
Result.Pending = new Dictionary<int, Result>();
FetchItemDefinitions(); // onDefinitionsUpdated should get called on next Update
FetchItemDefinitions();
LoadDefinitions();
UpdatePrices();
if ( !server )
{
@ -287,14 +289,19 @@ public static float PriceCategoryToFloat( string price )
}
/// <summary>
/// You really need me to explain what this does?
/// Use your brains.
/// We might be better off using a dictionary for this, once there's 1000+ definitions
/// </summary>
public Definition FindDefinition( int DefinitionId )
{
if ( Definitions == null ) return null;
return Definitions.FirstOrDefault( x => x.Id == DefinitionId );
for( int i=0; i< Definitions.Length; i++ )
{
if ( Definitions[i].Id == DefinitionId )
return Definitions[i];
}
return null;
}
public unsafe Result Deserialize( byte[] data, int dataLength = -1 )