From e7e76513ab608fe0c8580493e89c923ae0b86ca9 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Fri, 25 May 2018 23:01:37 +0300 Subject: [PATCH] Make _Games a Dictionary for some optimization --- SAM.Picker/GamePicker.cs | 35 ++++++++++++----------------------- 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/SAM.Picker/GamePicker.cs b/SAM.Picker/GamePicker.cs index 60aebeb..5d6a445 100644 --- a/SAM.Picker/GamePicker.cs +++ b/SAM.Picker/GamePicker.cs @@ -1,4 +1,4 @@ -/* Copyright (c) 2017 Rick (rick 'at' gibbed 'dot' us) +/* Copyright (c) 2017 Rick (rick 'at' gibbed 'dot' us) * * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any damages @@ -40,15 +40,10 @@ namespace SAM.Picker { private readonly API.Client _SteamClient; - private readonly List _Games; + private readonly Dictionary _Games; private readonly List _FilteredGames; private int _SelectedGameIndex; - public List Games - { - get { return _Games; } - } - private readonly List _LogosAttempted; private readonly ConcurrentQueue _LogoQueue; @@ -58,7 +53,7 @@ namespace SAM.Picker public GamePicker(API.Client client) { - this._Games = new List(); + this._Games = new Dictionary(); this._FilteredGames = new List(); this._SelectedGameIndex = -1; this._LogosAttempted = new List(); @@ -84,17 +79,12 @@ namespace SAM.Picker private void OnAppDataChanged(APITypes.AppDataChanged param) { - if (param.Result == true) + if (param.Result == true && this._Games.ContainsKey(param.Id)) { - foreach (GameInfo info in this._Games) - { - if (info.Id == param.Id) - { - info.Name = this._SteamClient.SteamApps001.GetAppData(info.Id, "name"); - this.AddGameToLogoQueue(info); - break; - } - } + var game = this._Games[param.Id]; + + game.Name = this._SteamClient.SteamApps001.GetAppData(game.Id, "name"); + this.AddGameToLogoQueue(game); } } @@ -148,7 +138,7 @@ namespace SAM.Picker private void RefreshGames() { this._FilteredGames.Clear(); - foreach (var info in this._Games.OrderBy(gi => gi.Name)) + foreach (var info in this._Games.Values.OrderBy(gi => gi.Name)) { if (info.Type == "normal" && _FilterGamesMenuItem.Checked == false) { @@ -226,8 +216,7 @@ namespace SAM.Picker } var logoInfo = (LogoInfo)e.Result; - var gameInfo = this._Games.FirstOrDefault(gi => gi.Id == logoInfo.Id); - if (gameInfo != null && logoInfo.Bitmap != null) + if (logoInfo.Bitmap != null && this._Games.TryGetValue(logoInfo.Id, out var gameInfo)) { this._GameListView.BeginUpdate(); var imageIndex = this._LogoImageList.Images.Count; @@ -292,7 +281,7 @@ namespace SAM.Picker private void AddGame(uint id, string type) { - if (this._Games.Any(i => i.Id == id) == true) + if (this._Games.ContainsKey(id)) { return; } @@ -305,7 +294,7 @@ namespace SAM.Picker var info = new GameInfo(id, type); info.Name = this._SteamClient.SteamApps001.GetAppData(info.Id, "name"); - this._Games.Add(info); + this._Games.Add(id, info); this.AddGameToLogoQueue(info); }