public async Task <UserInventoryAsset[]> GetAssets(long appId) { UserInventoryApp inventoryApp; lock (_inventoryApps) { inventoryApp = _inventoryApps.FirstOrDefault(app => app.AppId == appId); if (inventoryApp == null) { inventoryApp = new UserInventoryApp(appId); _inventoryApps.Add(inventoryApp); } } lock (_inventoryCache) { if (!_inventoryCache.ContainsKey(inventoryApp.AppId)) { _inventoryCache.Add(inventoryApp.AppId, new Dictionary <long, UserAppInventory>()); } } foreach (var appContext in inventoryApp.Contexts) { await FillAppContextCache(inventoryApp, appContext).ConfigureAwait(false); } lock (_inventoryCache) { return(_inventoryCache[inventoryApp.AppId] .SelectMany(pair => pair.Value.Assets) .ToArray()); } }
// ReSharper disable once MethodTooLong // ReSharper disable once ExcessiveIndentation private async Task FillAppContextCache(UserInventoryApp inventoryApp, UserInventoryAppContext appContext) { await _lockObject.WaitAsync().ConfigureAwait(false); try { bool doesContainsContext; lock (_inventoryCache) { doesContainsContext = _inventoryCache[inventoryApp.AppId].ContainsKey(appContext.ContextId); } if (!doesContainsContext) { try { var contextInventory = await _getInventoryDelegate(_steamWebAccess, SteamId, inventoryApp.AppId, appContext.ContextId) .ConfigureAwait(false); if (contextInventory != null) { lock (_inventoryCache) { _inventoryCache[inventoryApp.AppId].Add(appContext.ContextId, contextInventory); } foreach (var userInventoryApp in contextInventory.ExtraAppInformation) { lock (_inventoryApps) { var oldInventoryApp = _inventoryApps.FirstOrDefault(app => app.AppId == userInventoryApp.AppId); if (oldInventoryApp != null) { oldInventoryApp.Update(userInventoryApp); } else { _inventoryApps.Add(userInventoryApp); } } } } } catch (Exception e) { throw new UserInventoryFetchAssetsException(inventoryApp.AppId, appContext.ContextId, SteamId, e); } } } finally { _lockObject.Release(); } }
public async Task <UserInventoryAssetDescription> GetAssetDescription(UserInventoryAsset asset) { UserInventoryApp inventoryApp; UserInventoryAppContext appContext; lock (_inventoryApps) { inventoryApp = _inventoryApps.FirstOrDefault(app => app.AppId == asset.AppId); if (inventoryApp == null) { inventoryApp = new UserInventoryApp(asset.AppId); _inventoryApps.Add(inventoryApp); } appContext = inventoryApp.Contexts.FirstOrDefault(context => context.ContextId == asset.ContextId); if (appContext == null) { appContext = new UserInventoryAppContext(asset.ContextId); inventoryApp.Update( new UserInventoryApp(inventoryApp.AppId, null, null, new[] { appContext }, null, null, null) ); } } lock (_inventoryCache) { if (!_inventoryCache.ContainsKey(inventoryApp.AppId)) { _inventoryCache.Add(inventoryApp.AppId, new Dictionary <long, UserAppInventory>()); } } await FillAppContextCache(inventoryApp, appContext).ConfigureAwait(false); lock (_inventoryCache) { return(_inventoryCache[inventoryApp.AppId] .Where(pair => pair.Key == appContext.ContextId) .SelectMany(pair => pair.Value.AssetDescriptions) .FirstOrDefault(description => description.DoesDescribe(asset))); } }