From d137dec59f8fa4eac3407c0fe882a40e3e6d1909 Mon Sep 17 00:00:00 2001 From: Sour Date: Sun, 26 Jan 2020 12:36:28 -0500 Subject: [PATCH] UI: Fixed load performance issues with game selection screen --- GUI.NET/Controls/ctrlRecentGame.cs | 10 ++++++--- GUI.NET/Controls/ctrlRecentGames.cs | 33 ++++++++--------------------- 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/GUI.NET/Controls/ctrlRecentGame.cs b/GUI.NET/Controls/ctrlRecentGame.cs index af83ee5f..30427aad 100644 --- a/GUI.NET/Controls/ctrlRecentGame.cs +++ b/GUI.NET/Controls/ctrlRecentGame.cs @@ -43,8 +43,8 @@ namespace Mesen.GUI.Controls _recentGame = value; - lblGameName.Text = Path.GetFileNameWithoutExtension(_recentGame.RomName); - lblSaveDate.Text = _recentGame.Timestamp.ToString(); + lblGameName.Text = Path.GetFileNameWithoutExtension(_recentGame.FileName); + lblSaveDate.Text = new FileInfo(_recentGame.FileName).LastWriteTime.ToString(); lblGameName.Visible = true; lblSaveDate.Visible = true; @@ -53,13 +53,17 @@ namespace Mesen.GUI.Controls Task.Run(() => { Image img = null; try { - ZipArchive zip = new ZipArchive(new MemoryStream(File.ReadAllBytes(_recentGame.FileName))); + ZipArchive zip = new ZipArchive(new MemoryStream(File.ReadAllBytes(value.FileName))); ZipArchiveEntry entry = zip.GetEntry("Screenshot.png"); if(entry != null) { using(Stream stream = entry.Open()) { img = Image.FromStream(stream); } } + using(StreamReader sr = new StreamReader(zip.GetEntry("RomInfo.txt").Open())) { + string romName = sr.ReadLine(); + value.RomPath = sr.ReadLine(); + } } catch { } this.BeginInvoke((Action)(() => { diff --git a/GUI.NET/Controls/ctrlRecentGames.cs b/GUI.NET/Controls/ctrlRecentGames.cs index aac51237..72cab161 100644 --- a/GUI.NET/Controls/ctrlRecentGames.cs +++ b/GUI.NET/Controls/ctrlRecentGames.cs @@ -100,6 +100,9 @@ namespace Mesen.GUI.Controls } tlpGrid.ResumeLayout(); UpdateGameInfo(); + + picPrevGame.Visible = _recentGames.Count > _elementsPerPage; + picNextGame.Visible = _recentGames.Count > _elementsPerPage; } public new bool Visible @@ -134,31 +137,15 @@ namespace Mesen.GUI.Controls _recentGames = new List(); _currentIndex = 0; - foreach(string file in Directory.GetFiles(ConfigManager.RecentGamesFolder, "*.rgd")) { + List files = Directory.GetFiles(ConfigManager.RecentGamesFolder, "*.rgd").OrderByDescending((file) => new FileInfo(file).LastWriteTime).ToList(); + for(int i = 0; i < files.Count && _recentGames.Count < 36; i++) { try { RecentGameInfo info = new RecentGameInfo(); - ZipArchive zip = new ZipArchive(new MemoryStream(File.ReadAllBytes(file))); - - using(StreamReader sr = new StreamReader(zip.GetEntry("RomInfo.txt").Open())) { - info.RomName = sr.ReadLine(); - info.RomPath = sr.ReadLine(); - } - - info.Timestamp = new FileInfo(file).LastWriteTime; - info.FileName = file; - - if(info.RomPath.Exists) { - _recentGames.Add(info); - } + info.FileName = files[i]; + _recentGames.Add(info); } catch { } } - _recentGames = _recentGames.OrderBy((info) => info.Timestamp).Reverse().ToList(); - - if(_recentGames.Count > 36) { - _recentGames.RemoveRange(36, _recentGames.Count - 36); - } - InitGrid(); if(_recentGames.Count == 0) { @@ -169,8 +156,8 @@ namespace Mesen.GUI.Controls tmrInput.Enabled = true; } - picPrevGame.Visible = true; - picNextGame.Visible = true; + picPrevGame.Visible = _recentGames.Count > _elementsPerPage; + picNextGame.Visible = _recentGames.Count > _elementsPerPage; } public void UpdateGameInfo() @@ -356,8 +343,6 @@ namespace Mesen.GUI.Controls public class RecentGameInfo { public string FileName { get; set; } - public string RomName { get; set; } public ResourcePath RomPath { get; set; } - public DateTime Timestamp { get; set; } } }