From 0024b5617991b39267b2cdca7106f276ea4b6963 Mon Sep 17 00:00:00 2001 From: Paul Hebble Date: Fri, 16 Feb 2024 18:58:38 -0600 Subject: [PATCH] Properly clear AD upgrades from changeset --- Core/Relationships/RelationshipResolver.cs | 4 +- GUI/Controls/ManageMods.Designer.cs | 2 +- GUI/Controls/ManageMods.cs | 156 +++++++++++++-------- GUI/Main/MainChangeset.cs | 1 - GUI/Model/GUIMod.cs | 32 +++-- 5 files changed, 116 insertions(+), 79 deletions(-) diff --git a/Core/Relationships/RelationshipResolver.cs b/Core/Relationships/RelationshipResolver.cs index bfa75a318e..250938d030 100644 --- a/Core/Relationships/RelationshipResolver.cs +++ b/Core/Relationships/RelationshipResolver.cs @@ -678,11 +678,9 @@ private void AddReason(CkanModule module, SelectionReason reason) private readonly Dictionary modlist = new Dictionary(); private readonly List user_requested_mods = new List(); - //TODO As the conflict detection gets more advanced there is a greater need to have messages in here - // as recreating them from reasons is no longer possible. private readonly List conflicts = new List(); private readonly Dictionary> reasons = - new Dictionary>(new NameComparer()); + new Dictionary>(); private readonly IRegistryQuerier registry; private readonly GameVersionCriteria versionCrit; diff --git a/GUI/Controls/ManageMods.Designer.cs b/GUI/Controls/ManageMods.Designer.cs index f65e72af6e..f161750ccd 100644 --- a/GUI/Controls/ManageMods.Designer.cs +++ b/GUI/Controls/ManageMods.Designer.cs @@ -154,7 +154,7 @@ private void InitializeComponent() this.UpdateAllToolButton.Name = "UpdateAllToolButton"; this.UpdateAllToolButton.Size = new System.Drawing.Size(232, 56); this.UpdateAllToolButton.Overflow = System.Windows.Forms.ToolStripItemOverflow.AsNeeded; - this.UpdateAllToolButton.Click += new System.EventHandler(this.MarkAllUpdatesToolButton_Click); + this.UpdateAllToolButton.Click += new System.EventHandler(this.UpdateAllToolButton_Click); resources.ApplyResources(this.UpdateAllToolButton, "UpdateAllToolButton"); // // ApplyToolButton diff --git a/GUI/Controls/ManageMods.cs b/GUI/Controls/ManageMods.cs index 8efffeccff..fa40800bd8 100644 --- a/GUI/Controls/ManageMods.cs +++ b/GUI/Controls/ManageMods.cs @@ -502,34 +502,36 @@ private static bool SearchesExcludeInstalled(List searches) public void MarkAllUpdates() { - foreach (DataGridViewRow row in mainModList.full_list_of_mod_rows.Values) + WithFrozenChangeset(() => { - var mod = row.Tag as GUIMod; - if (mod?.HasUpdate ?? false) + foreach (var row in mainModList.full_list_of_mod_rows.Values) { - if (!Main.Instance.LabelsHeld(mod.Identifier)) + var mod = row.Tag as GUIMod; + if (mod?.HasUpdate ?? false) { - mod.SetUpgradeChecked(row, UpdateCol, true); + if (!Main.Instance.LabelsHeld(mod.Identifier)) + { + mod.SetUpgradeChecked(row, UpdateCol, true); + } } } - } - // only sort by Update column if checkbox in settings checked - if (Main.Instance.configuration.AutoSortByUpdate) - { - // Retain their current sort as secondaries - AddSort(UpdateCol, true); - UpdateFilters(); - // Select the top row and scroll the list to it. - if (ModGrid.Rows.Count > 0) + // only sort by Update column if checkbox in settings checked + if (Main.Instance.configuration.AutoSortByUpdate) { - ModGrid.CurrentCell = ModGrid.Rows[0].Cells[SelectableColumnIndex()]; + // Retain their current sort as secondaries + AddSort(UpdateCol, true); + UpdateFilters(); + // Select the top row and scroll the list to it. + if (ModGrid.Rows.Count > 0) + { + ModGrid.CurrentCell = ModGrid.Rows[0].Cells[SelectableColumnIndex()]; + } } - } - ModGrid.Refresh(); + }); } - private void MarkAllUpdatesToolButton_Click(object sender, EventArgs e) + private void UpdateAllToolButton_Click(object sender, EventArgs e) { MarkAllUpdates(); } @@ -898,9 +900,9 @@ private void ModGrid_CellValueChanged(object sender, DataGridViewCellEventArgs e gui_mod.SetReplaceChecked(row, ReplaceCol); break; } - UpdateChangeSetAndConflicts( - Main.Instance.CurrentInstance, - RegistryManager.Instance(Main.Instance.CurrentInstance, repoData).registry); + var inst = Main.Instance.CurrentInstance; + UpdateChangeSetAndConflicts(inst, + RegistryManager.Instance(inst, repoData).registry); } } } @@ -922,11 +924,19 @@ public void RemoveChangesetItem(ModChange change) switch (change.ChangeType) { case GUIModChangeType.Install: - guiMod.SetInstallChecked(row, Installed, false); + if (guiMod.IsAutodetected) + { + guiMod.SetUpgradeChecked(row, UpdateCol, false); + } + else + { + guiMod.SetInstallChecked(row, Installed, false); + return; + } break; case GUIModChangeType.Remove: guiMod.SetInstallChecked(row, Installed, true); - break; + return; case GUIModChangeType.Update: guiMod.SetUpgradeChecked(row, UpdateCol, false); break; @@ -963,9 +973,8 @@ private void ModGrid_LostFocus(object sender, EventArgs e) private void InstallAllCheckbox_CheckChanged(object sender, EventArgs e) { - try + WithFrozenChangeset(() => { - freezeChangeSet = true; if (InstallAllCheckbox.Checked) { // Reset changeset @@ -973,56 +982,83 @@ private void InstallAllCheckbox_CheckChanged(object sender, EventArgs e) } else { - // Uninstall all - var checkedRows = mainModList.full_list_of_mod_rows.Values - .Where(r => (r.Tag as GUIMod)?.IsInstallChecked ?? false); - foreach (var row in checkedRows) + // Uninstall all and cancel upgrades + foreach (var row in mainModList.full_list_of_mod_rows.Values) { - (row.Tag as GUIMod)?.SetInstallChecked(row, Installed, false); + if (row.Tag is GUIMod gmod) + { + if (gmod.IsUpgradeChecked) + { + gmod.SetUpgradeChecked(row, UpdateCol, false); + } + if (gmod.IsInstallChecked) + { + gmod.SetInstallChecked(row, Installed, false); + } + } } } - } - finally - { - // Don't let anything ever prevent us from unfreezing the changeset - freezeChangeSet = false; - ModGrid.Refresh(); - UpdateChangeSetAndConflicts( - Main.Instance.CurrentInstance, - RegistryManager.Instance(Main.Instance.CurrentInstance, repoData).registry); - } + }); } public void ClearChangeSet() { - foreach (DataGridViewRow row in mainModList.full_list_of_mod_rows.Values) + WithFrozenChangeset(() => { - GUIMod mod = row.Tag as GUIMod; - if (mod.IsInstallChecked != mod.IsInstalled) + foreach (DataGridViewRow row in mainModList.full_list_of_mod_rows.Values) { - mod.SetInstallChecked(row, Installed, mod.IsInstalled); + GUIMod mod = row.Tag as GUIMod; + if (mod.IsInstallChecked != mod.IsInstalled) + { + mod.SetInstallChecked(row, Installed, mod.IsInstalled); + } + else if (mod.InstalledMod != null) + { + var registry = RegistryManager.Instance(Main.Instance.CurrentInstance, repoData).registry; + mod.SelectedMod = registry.GetModuleByVersion( + mod.InstalledMod.identifier, mod.InstalledMod.Module.version) + ?? mod.InstalledMod.Module; + } + mod.SetUpgradeChecked(row, UpdateCol, false); + mod.SetReplaceChecked(row, ReplaceCol, false); } - else if (mod.InstalledMod != null) + // Marking a mod as AutoInstalled can immediately queue it for removal if there is no dependent mod. + // Reset the state of the AutoInstalled checkbox for these by deducing it from the changeset. + foreach (DataGridViewRow row in mainModList.full_list_of_mod_rows.Values) { - var registry = RegistryManager.Instance(Main.Instance.CurrentInstance, repoData).registry; - mod.SelectedMod = registry.GetModuleByVersion( - mod.InstalledMod.identifier, mod.InstalledMod.Module.version) - ?? mod.InstalledMod.Module; + GUIMod mod = row.Tag as GUIMod; + if (mod.InstalledMod != null + && ChangeSet.Contains(new ModChange(mod.InstalledMod?.Module, + GUIModChangeType.Remove, + new SelectionReason.NoLongerUsed()))) + { + mod.SetAutoInstallChecked(row, AutoInstalled, false); + } } - mod.SetUpgradeChecked(row, UpdateCol, false); - mod.SetReplaceChecked(row, ReplaceCol, false); + }); + } + + private void WithFrozenChangeset(Action action) + { + if (freezeChangeSet) + { + // Already frozen by some outer block, let it handle the cleanup + action?.Invoke(); } - // Marking a mod as AutoInstalled can immediately queue it for removal if there is no dependent mod. - // Reset the state of the AutoInstalled checkbox for these by deducing it from the changeset. - foreach (DataGridViewRow row in mainModList.full_list_of_mod_rows.Values) + else { - GUIMod mod = row.Tag as GUIMod; - if (mod.InstalledMod != null - && ChangeSet.Contains(new ModChange(mod.InstalledMod?.Module, - GUIModChangeType.Remove, - new SelectionReason.NoLongerUsed()))) + freezeChangeSet = true; + try + { + action?.Invoke(); + } + finally { - mod.SetAutoInstallChecked(row, AutoInstalled, false); + // Don't let anything ever prevent us from unfreezing the changeset + freezeChangeSet = false; + ModGrid.Refresh(); + var inst = Main.Instance.CurrentInstance; + UpdateChangeSetAndConflicts(inst, RegistryManager.Instance(inst, repoData).registry); } } } diff --git a/GUI/Main/MainChangeset.cs b/GUI/Main/MainChangeset.cs index a5d1a3de84..cc21fe77e3 100644 --- a/GUI/Main/MainChangeset.cs +++ b/GUI/Main/MainChangeset.cs @@ -32,7 +32,6 @@ private void Changeset_OnCancelChanges(bool reset) if (reset) { ManageMods.ClearChangeSet(); - UpdateChangesDialog(null, null); } tabController.ShowTab("ManageModsTabPage"); } diff --git a/GUI/Model/GUIMod.cs b/GUI/Model/GUIMod.cs index 557815f0f6..37366a87dc 100644 --- a/GUI/Model/GUIMod.cs +++ b/GUI/Model/GUIMod.cs @@ -50,9 +50,10 @@ public CkanModule SelectedMod } Main.Instance.ManageMods.MarkModForInstall(Identifier, selectedMod == null); + var inst = Main.Instance.CurrentInstance; Main.Instance.ManageMods.UpdateChangeSetAndConflicts( - Main.Instance.Manager.CurrentInstance, - RegistryManager.Instance(Main.Instance.Manager.CurrentInstance, + inst, + RegistryManager.Instance(inst, ServiceLocator.Container.Resolve()).registry); OnPropertyChanged(); @@ -122,8 +123,8 @@ public string GameCompatibility /// otherwise false. /// public bool IsInstallable() - // Compatible mods are installable, but so are mods that are already installed - => !IsIncompatible || IsInstalled; + // Compatible mods are installable, but so are mods that are already installed + => !IsIncompatible || IsInstalled; public string Version => IsInstalled ? InstalledVersion : LatestVersion; @@ -218,13 +219,13 @@ public GUIMod(CkanModule mod, /// CKAN registry object for current game instance /// Current game version /// If true, mark this module as incompatible - public GUIMod(string identifier, - RepositoryDataManager repoDataMgr, - IRegistryQuerier registry, - GameVersionCriteria current_game_version, - bool? incompatible, - bool hideEpochs, - bool hideV) + private GUIMod(string identifier, + RepositoryDataManager repoDataMgr, + IRegistryQuerier registry, + GameVersionCriteria current_game_version, + bool? incompatible, + bool hideEpochs, + bool hideV) { Identifier = identifier; IsAutodetected = registry.IsAutodetected(identifier); @@ -330,7 +331,7 @@ public IEnumerable GetModChanges() ?? InstalledMod?.Module.Equals(SelectedMod) // Both null ?? true; - if (IsInstalled && (IsInstallChecked && HasUpdate && IsUpgradeChecked)) + if (IsInstalled && IsInstallChecked && HasUpdate && IsUpgradeChecked) { yield return new ModUpgrade(Mod, GUIModChangeType.Update, @@ -395,13 +396,16 @@ public void SetUpgradeChecked(DataGridViewRow row, DataGridViewColumn col, bool? if (old_value != value) { update_cell.Value = value; - SelectedMod = value ? LatestCompatibleMod : (SelectedMod ?? InstalledMod?.Module); + SelectedMod = value ? LatestCompatibleMod + : IsAutodetected ? null + : (SelectedMod ?? InstalledMod?.Module); } else if (!set_value_to.HasValue) { var isLatest = (LatestCompatibleMod?.Equals(selectedMod) ?? false); SelectedMod = value ? LatestCompatibleMod - : isLatest ? InstalledMod?.Module : SelectedMod; + : isLatest ? InstalledMod?.Module + : SelectedMod; } } }