using System; using System.Collections.Generic; using System.Linq; using Steamworks.Data; namespace Steamworks { public class InventoryDef { internal InventoryDefId _id; internal Dictionary _properties; public InventoryDef( InventoryDefId defId ) { _id = defId; } public int Id => _id.Value; /// /// Shortcut to call GetProperty( "name" ) /// public string Name => GetProperty( "name" ); /// /// Shortcut to call GetProperty( "description" ) /// public string Description => GetProperty( "description" ); /// /// Shortcut to call GetProperty( "icon_url" ) /// public string IconUrl => GetProperty( "icon_url" ); /// /// Shortcut to call GetProperty( "icon_url_large" ) /// public string IconUrlLarge => GetProperty( "icon_url_large" ); /// /// Shortcut to call GetProperty( "price_category" ) /// public string PriceCategory => GetProperty( "price_category" ); /// /// Shortcut to call GetProperty( "type" ) /// public string Type => GetProperty( "type" ); /// /// Returns true if this is an item that generates an item, rather /// than something that is actual an item /// public bool IsGenerator => Type == "generator"; /// /// Shortcut to call GetProperty( "exchange" ) /// public string ExchangeSchema => GetProperty( "exchange" ); /// /// Get a list of exchanges that are available to make this item /// public InventoryRecipe[] GetRecipes() { if ( string.IsNullOrEmpty( ExchangeSchema ) ) return null; var parts = ExchangeSchema.Split( new[] { ';' }, StringSplitOptions.RemoveEmptyEntries ); return parts.Select( x => InventoryRecipe.FromString( x, this ) ).ToArray(); } /// /// Shortcut to call GetBoolProperty( "marketable" ) /// public bool Marketable => GetBoolProperty( "marketable" ); /// /// Shortcut to call GetBoolProperty( "tradable" ) /// public bool Tradable => GetBoolProperty( "tradable" ); /// /// Gets the property timestamp /// public DateTime Created => GetProperty( "timestamp" ); /// /// Gets the property modified /// public DateTime Modified => GetProperty( "modified" ); /// /// Get a specific property by name /// public string GetProperty( string name ) { if ( _properties!= null && _properties.TryGetValue( name, out string val ) ) return val; var sb = Helpers.TakeStringBuilder(); uint _ = (uint)sb.Capacity; if ( !SteamInventory.Internal.GetItemDefinitionProperty( Id, name, sb, ref _ ) ) return null; if ( _properties == null ) _properties = new Dictionary(); var vl = sb.ToString(); _properties[name] = vl; return vl; } /// /// Read a raw property from the definition schema /// public bool GetBoolProperty( string name ) { string val = GetProperty( name ); if ( val.Length == 0 ) return false; if ( val[0] == '0' || val[0] == 'F' || val[0] == 'f' ) return false; return true; } /// /// Read a raw property from the definition schema /// public T GetProperty( string name ) { string val = GetProperty( name ); if ( string.IsNullOrEmpty( val ) ) return default( T ); try { return (T)Convert.ChangeType( val, typeof( T ) ); } catch ( System.Exception ) { return default( T ); } } /// /// Gets a list of all properties on this item /// public IEnumerable> Properties { get { var list = GetProperty( null ); var keys = list.Split( ',' ); foreach ( var key in keys ) { yield return new KeyValuePair( key, GetProperty( key ) ); } } } /// /// Returns the price of this item in the local currency (SteamInventory.Currency) /// public int LocalPrice { get { ulong curprice = 0; ulong baseprice = 0; if ( !SteamInventory.Internal.GetItemPrice( Id, ref curprice, ref baseprice ) ) return 0; return (int) curprice; } } public string LocalPriceFormatted => Utility.FormatPrice( SteamInventory.Currency, LocalPrice / 100.0 ); /// /// If the price has been discounted, LocalPrice will differ from LocalBasePrice /// (assumed, this isn't documented) /// public int LocalBasePrice { get { ulong curprice = 0; ulong baseprice = 0; if ( !SteamInventory.Internal.GetItemPrice( Id, ref curprice, ref baseprice ) ) return 0; return (int)baseprice; } } public string LocalBasePriceFormatted => Utility.FormatPrice( SteamInventory.Currency, LocalPrice / 100.0 ); } }