diff --git a/Core/Net/NetModuleCache.cs b/Core/Net/NetModuleCache.cs index 8ca3b271b5..2ed1827727 100644 --- a/Core/Net/NetModuleCache.cs +++ b/Core/Net/NetModuleCache.cs @@ -42,6 +42,9 @@ public NetModuleCache(string path) cache = new NetFileCache(path); } + public event Action ModStored; + public event Action ModPurged; + // Simple passthrough wrappers public void Dispose() { @@ -50,6 +53,7 @@ public void Dispose() public void RemoveAll() { cache.RemoveAll(); + ModPurged?.Invoke(null); } public void MoveFrom(string fromDir) { @@ -215,6 +219,7 @@ public string Store(CkanModule module, string path, IProgress progress, st var success = cache.Store(module.download[0], path, description ?? module.StandardName(), move); // Make sure completion is signalled so progress bars go away progress?.Report(100); + ModStored?.Invoke(module); return success; } @@ -318,6 +323,7 @@ public bool Purge(CkanModule module) } } } + ModPurged?.Invoke(module); return true; } diff --git a/GUI/Controls/ManageMods.cs b/GUI/Controls/ManageMods.cs index d962b98e0c..89d8405fc8 100644 --- a/GUI/Controls/ManageMods.cs +++ b/GUI/Controls/ManageMods.cs @@ -1109,20 +1109,6 @@ private void purgeContentsToolStripMenuItem_Click(object sender, EventArgs e) { Main.Instance.Manager.Cache.Purge(mod); } - - // Update all mods that share the same ZIP - var allGuiMods = AllGUIMods(); - foreach (var otherMod in selected.ToModule().GetDownloadsGroup( - allGuiMods.Values.Select(guiMod => guiMod.ToModule()) - .Where(mod => mod != null))) - { - allGuiMods[otherMod.identifier].UpdateIsCached(); - } - - // Reapply searches in case is:cached or not:cached is active - UpdateFilters(); - - Main.Instance.RefreshModContentsTree(); } } diff --git a/GUI/Controls/ModInfo.cs b/GUI/Controls/ModInfo.cs index e51ce5d77b..3e8b4b76da 100644 --- a/GUI/Controls/ModInfo.cs +++ b/GUI/Controls/ModInfo.cs @@ -8,6 +8,7 @@ using Autofac; using CKAN.Versioning; +using CKAN.GUI.Attributes; namespace CKAN.GUI { @@ -45,6 +46,7 @@ public GUIMod SelectedModule get => selectedModule; } + [ForbidGUICalls] public void RefreshModContentsTree() { Contents.RefreshModContentsTree(); diff --git a/GUI/Controls/ModInfoTabs/Contents.cs b/GUI/Controls/ModInfoTabs/Contents.cs index b3c457efb2..f181e05752 100644 --- a/GUI/Controls/ModInfoTabs/Contents.cs +++ b/GUI/Controls/ModInfoTabs/Contents.cs @@ -9,6 +9,8 @@ using System.Runtime.Versioning; #endif +using CKAN.GUI.Attributes; + namespace CKAN.GUI { #if NET5_0_OR_GREATER @@ -35,6 +37,7 @@ public GUIMod SelectedModule get => selectedModule; } + [ForbidGUICalls] public void RefreshModContentsTree() { if (currentModContentsModule != null) diff --git a/GUI/Dialogs/SettingsDialog.cs b/GUI/Dialogs/SettingsDialog.cs index 8939f0860c..c69a47d924 100644 --- a/GUI/Dialogs/SettingsDialog.cs +++ b/GUI/Dialogs/SettingsDialog.cs @@ -294,19 +294,9 @@ private void PurgeAllMenuItem_Click(object sender, EventArgs e) if (deleteConfirmationDialog.ShowYesNoDialog(this, confirmationText) == DialogResult.Yes) { - // tell the cache object to nuke itself + // Tell the cache object to nuke itself Main.Instance.Manager.Cache.RemoveAll(); - // forcibly tell all mod rows to re-check cache state - foreach (DataGridViewRow row in Main.Instance.ManageMods.ModGrid.Rows) - { - var mod = row.Tag as GUIMod; - mod?.UpdateIsCached(); - } - - // finally, clear the preview contents list - Main.Instance.RefreshModContentsTree(); - UpdateCacheInfo(coreConfig.DownloadCacheDir); } } diff --git a/GUI/Main/Main.cs b/GUI/Main/Main.cs index 9b0a6eac31..ade1dd948c 100644 --- a/GUI/Main/Main.cs +++ b/GUI/Main/Main.cs @@ -151,6 +151,9 @@ public Main(string[] cmdlineArgs, GameInstanceManager mgr) Manager = new GameInstanceManager(currentUser); } + Manager.Cache.ModStored += OnModStoredOrPurged; + Manager.Cache.ModPurged += OnModStoredOrPurged; + tabController = new TabController(MainTabControl); tabController.ShowTab("ManageModsTabPage"); @@ -593,11 +596,6 @@ private void SetupDefaultSearch() } } - public void RefreshModContentsTree() - { - ModInfo.RefreshModContentsTree(); - } - private void ExitToolButton_Click(object sender, EventArgs e) { Close(); @@ -785,7 +783,10 @@ public bool PreFilterMessage(ref Message m) private void ManageMods_OnSelectedModuleChanged(GUIMod m) { - ActiveModInfo = m; + if (MainTabControl.SelectedTab == ManageModsTabPage) + { + ActiveModInfo = m; + } } private GUIMod ActiveModInfo diff --git a/GUI/Main/MainDownload.cs b/GUI/Main/MainDownload.cs index 7e3f74c943..c42d35fd86 100644 --- a/GUI/Main/MainDownload.cs +++ b/GUI/Main/MainDownload.cs @@ -30,7 +30,6 @@ public void StartDownload(GUIMod module) { // Just pass to the existing worker downloader.DownloadModules(new List { module.ToCkanModule() }); - UpdateCachedByDownloads(module); }); } else @@ -81,36 +80,41 @@ public void PostModCaching(object sender, RunWorkerCompletedEventArgs e) } else { - Util.Invoke(this, () => _PostModCaching((GUIMod)e.Result)); + // Close progress tab and switch back to mod list + HideWaitDialog(); + EnableMainWindow(); } } [ForbidGUICalls] - private void UpdateCachedByDownloads(GUIMod module) + private void UpdateCachedByDownloads(CkanModule module) { - // Update all mods that share the same ZIP var allGuiMods = ManageMods.AllGUIMods(); - foreach (var otherMod in module.ToModule().GetDownloadsGroup( - allGuiMods.Values.Select(guiMod => guiMod.ToModule()) - .Where(mod => mod != null))) + var affectedMods = + module?.GetDownloadsGroup(allGuiMods.Values + .Select(guiMod => guiMod.ToModule()) + .Where(mod => mod != null)) + .Select(other => allGuiMods[other.identifier]) + ?? allGuiMods.Values; + foreach (var otherMod in affectedMods) { - allGuiMods[otherMod.identifier].UpdateIsCached(); + otherMod.UpdateIsCached(); } } - private void _PostModCaching(GUIMod module) + [ForbidGUICalls] + private void OnModStoredOrPurged(CkanModule module) { UpdateCachedByDownloads(module); // Reapply searches in case is:cached or not:cached is active ManageMods.UpdateFilters(); - // User might have selected another row. Show current in tree. - RefreshModContentsTree(); - - // Close progress tab and switch back to mod list - HideWaitDialog(); - EnableMainWindow(); + if (module == null + || ModInfo.SelectedModule?.Identifier == module.identifier) + { + ModInfo.RefreshModContentsTree(); + } } } }