Skip to content

Commit

Permalink
Fix compile warnings, other clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Aug 27, 2023
1 parent b916779 commit e870e8e
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 38 deletions.
13 changes: 13 additions & 0 deletions Core/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ public static IEnumerable<V> ZipMany<T, U, V>(this IEnumerable<T> seq1, IEnumera
public static IEnumerable<T> DistinctBy<T, U>(this IEnumerable<T> seq, Func<T, U> func)
=> seq.GroupBy(func).Select(grp => grp.First());

/// <summary>
/// Generate a sequence from a linked list
/// </summary>
/// <param name="start">The first node</param>
/// <param name="getNext">Function to go from one node to the next</param>
/// <returns>All the nodes in the list as a sequence</returns>
public static IEnumerable<T> TraverseNodes<T>(this T start, Func<T, T> getNext)
{
for (T t = start; t != null; t = getNext(t))
{
yield return t;
}
}
}

/// <summary>
Expand Down
1 change: 0 additions & 1 deletion GUI/Controls/EditModSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ private bool SkipDelayIf(object sender, EventArgs e)
default:
return false;
}
break;
}
// Always refresh immediately on clear
return string.IsNullOrEmpty(FilterCombinedTextBox.Text)
Expand Down
15 changes: 6 additions & 9 deletions GUI/Controls/ManageMods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ private void MarkAllUpdatesToolButton_Click(object sender, EventArgs e)

private void ApplyToolButton_Click(object sender, EventArgs e)
{
Main.Instance.tabController.ShowTab("ChangesetTabPage", 1);
StartChangeSet?.Invoke(currentChangeSet);
}

public void MarkModForUpdate(string identifier, bool value)
Expand Down Expand Up @@ -759,7 +759,7 @@ private void ModGrid_CellMouseDoubleClick(object sender, DataGridViewCellMouseEv
ModGrid.CommitEdit(DataGridViewDataErrorContexts.Commit);
}

private async void ModGrid_CellValueChanged(object sender, DataGridViewCellEventArgs e)
private void ModGrid_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
int row_index = e.RowIndex;
int column_index = e.ColumnIndex;
Expand Down Expand Up @@ -804,10 +804,9 @@ private async void ModGrid_CellValueChanged(object sender, DataGridViewCellEvent
gui_mod.SetReplaceChecked(row, ReplaceCol);
break;
}
await UpdateChangeSetAndConflicts(
UpdateChangeSetAndConflicts(
Main.Instance.CurrentInstance,
RegistryManager.Instance(Main.Instance.CurrentInstance).registry
);
RegistryManager.Instance(Main.Instance.CurrentInstance).registry);
}
}
}
Expand Down Expand Up @@ -1227,8 +1226,7 @@ private void _UpdateModsList(Dictionary<string, bool> old_modules = null)
// Update our mod listing
mainModList.ConstructModList(gui_mods, Main.Instance.CurrentInstance.Name, Main.Instance.CurrentInstance.game, ChangeSet);

// C# 7.0: Executes the task and discards it
_ = UpdateChangeSetAndConflicts(Main.Instance.CurrentInstance, registry);
UpdateChangeSetAndConflicts(Main.Instance.CurrentInstance, registry);

Main.Instance.Wait.AddLogMessage(Properties.Resources.MainModListUpdatingFilters);

Expand Down Expand Up @@ -1651,7 +1649,7 @@ public void InstanceUpdated(GameInstance inst)
Conflicts = null;
}

public async Task UpdateChangeSetAndConflicts(GameInstance inst, IRegistryQuerier registry)
public void UpdateChangeSetAndConflicts(GameInstance inst, IRegistryQuerier registry)
{
if (freezeChangeSet)
{
Expand All @@ -1662,7 +1660,6 @@ public async Task UpdateChangeSetAndConflicts(GameInstance inst, IRegistryQuerie
List<ModChange> full_change_set = null;
Dictionary<GUIMod, string> new_conflicts = null;

bool too_many_provides_thrown = false;
var user_change_set = mainModList.ComputeUserChangeSet(registry, inst.VersionCriteria());
try
{
Expand Down
2 changes: 1 addition & 1 deletion GUI/Controls/ModInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public GUIMod SelectedModule

public void RefreshModContentsTree()
{
Contents.Refresh();
Contents.RefreshModContentsTree();
}

public event Action<GUIMod> OnDownloadClick;
Expand Down
2 changes: 1 addition & 1 deletion GUI/Controls/ModInfoTabs/Contents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public GUIMod SelectedModule
get => selectedModule;
}

public void Refresh()
public void RefreshModContentsTree()
{
if (currentModContentsModule != null)
{
Expand Down
4 changes: 2 additions & 2 deletions GUI/Dialogs/PreferredHostsDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private void PreferredHostsListBox_SelectedIndexChanged(object sender, EventArgs
{
var haveSelection = PreferredHostsListBox.SelectedIndex > -1;
MoveLeftButton.Enabled = haveSelection
&& PreferredHostsListBox.SelectedItem != placeholder;
&& (string)PreferredHostsListBox.SelectedItem != placeholder;
MoveUpButton.Enabled = PreferredHostsListBox.SelectedIndex > 0;
MoveDownButton.Enabled = haveSelection
&& PreferredHostsListBox.SelectedIndex < PreferredHostsListBox.Items.Count - 1;
Expand Down Expand Up @@ -115,7 +115,7 @@ private void MoveLeftButton_Click(object sender, EventArgs e)
if (PreferredHostsListBox.SelectedIndex > -1)
{
var fromWhere = PreferredHostsListBox.SelectedIndex;
var selected = PreferredHostsListBox.SelectedItem;
var selected = (string)PreferredHostsListBox.SelectedItem;
if (selected != placeholder)
{
PreferredHostsListBox.Items.Remove(selected);
Expand Down
29 changes: 14 additions & 15 deletions GUI/Main/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,16 @@ public partial class Main : Form, IMessageFilter

// Stuff we set in the constructor and never change
public readonly GUIUser currentUser;
[Obsolete("Main.tabController should be private. Find a better way to access this object.")]
public readonly TabController tabController;
private readonly GameInstanceManager manager;
public GameInstanceManager Manager => manager;
public readonly GameInstanceManager Manager;
public GameInstance CurrentInstance => Manager.CurrentInstance;
private string focusIdent;

// Stuff we set when the game instance changes
public GUIConfiguration configuration;
public PluginController pluginController;

private readonly TabController tabController;
private string focusIdent;

private bool needRegistrySave = false;

[Obsolete("Main.Instance is a global singleton. Find a better way to access this object.")]
Expand Down Expand Up @@ -128,11 +127,11 @@ public Main(string[] cmdlineArgs, GameInstanceManager mgr)
{
// With a working GUI, assign a GUIUser to the GameInstanceManager to replace the ConsoleUser
mgr.User = currentUser;
manager = mgr;
Manager = mgr;
}
else
{
manager = new GameInstanceManager(currentUser);
Manager = new GameInstanceManager(currentUser);
}

tabController = new TabController(MainTabControl);
Expand All @@ -148,13 +147,13 @@ protected override void OnLoad(EventArgs e)
if (CurrentInstance == null)
{
// Maybe we can find an instance automatically (e.g., portable, only, default)
manager.GetPreferredInstance();
Manager.GetPreferredInstance();
}

// We need a config object to get the window geometry, but we don't need the registry lock yet
configuration = GUIConfigForInstance(
// Find the most recently used instance if no default instance
CurrentInstance ?? InstanceWithNewestGUIConfig(manager.Instances.Values));
CurrentInstance ?? InstanceWithNewestGUIConfig(Manager.Instances.Values));

// This must happen before Shown, and it depends on the configuration
SetStartPosition();
Expand Down Expand Up @@ -255,8 +254,8 @@ protected override void OnShown(EventArgs e)
else
{
// Couldn't get the lock, there is no current instance
manager.CurrentInstance = null;
if (manager.Instances.Values.All(inst => !inst.Valid || inst.IsMaybeLocked))
Manager.CurrentInstance = null;
if (Manager.Instances.Values.All(inst => !inst.Valid || inst.IsMaybeLocked))
{
// Everything's invalid or locked, give up
evt.Result = false;
Expand Down Expand Up @@ -336,7 +335,7 @@ private void manageGameInstancesMenuItem_Click(object sender, EventArgs e)
else
{
// Couldn't get the lock, revert to previous instance
manager.CurrentInstance = old_instance;
Manager.CurrentInstance = old_instance;
CurrentInstanceUpdated(false);
done = true;
}
Expand Down Expand Up @@ -443,7 +442,7 @@ protected override void OnFormClosed(FormClosedEventArgs e)
}

// Stop all running play time timers
foreach (var inst in manager.Instances.Values)
foreach (var inst in Manager.Instances.Values)
{
if (inst.Valid)
{
Expand Down Expand Up @@ -694,7 +693,7 @@ private void InstallFromCkanFiles(string[] files)
private void CompatibleGameVersionsToolStripMenuItem_Click(object sender, EventArgs e)
{
CompatibleGameVersionsDialog dialog = new CompatibleGameVersionsDialog(
Instance.manager.CurrentInstance,
Instance.Manager.CurrentInstance,
!actuallyVisible
);
if (dialog.ShowDialog(this) != DialogResult.Cancel)
Expand Down Expand Up @@ -839,7 +838,7 @@ private void Main_Resize(object sender, EventArgs e)

private void openGameDirectoryToolStripMenuItem_Click(object sender, EventArgs e)
{
Utilities.ProcessStartURL(manager.CurrentInstance.GameDir());
Utilities.ProcessStartURL(Manager.CurrentInstance.GameDir());
}

private void openGameToolStripMenuItem_Click(object sender, EventArgs e)
Expand Down
2 changes: 1 addition & 1 deletion GUI/Main/MainHistory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public partial class Main
{
private void installationHistoryStripMenuItem_Click(object sender, EventArgs e)
{
InstallationHistory.LoadHistory(manager.CurrentInstance, configuration);
InstallationHistory.LoadHistory(Manager.CurrentInstance, configuration);
tabController.ShowTab("InstallationHistoryTabPage", 2);
DisableMainWindow();
}
Expand Down
14 changes: 13 additions & 1 deletion GUI/Main/MainInstall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private void InstallMods(object sender, DoWorkEventArgs e)
bool canceled = false;
var opts = (KeyValuePair<ModChanges, RelationshipResolverOptions>) e.Argument;

RegistryManager registry_manager = RegistryManager.Instance(manager.CurrentInstance);
RegistryManager registry_manager = RegistryManager.Instance(Manager.CurrentInstance);
Registry registry = registry_manager.registry;
ModuleInstaller installer = new ModuleInstaller(CurrentInstance, Manager.Cache, currentUser);
// Avoid accumulating multiple event handlers
Expand Down Expand Up @@ -392,11 +392,23 @@ private void PostInstallMods(object sender, RunWorkerCompletedEventArgs e)
break;

case TransactionalKraken exc:
// Thrown when the Registry tries to enlist with multiple different transactions
// Want to see the stack trace for this one
currentUser.RaiseMessage(exc.ToString());
currentUser.RaiseError(exc.ToString());
break;

case TransactionException texc:
// "Failed to roll back" is useless by itself,
// so show all inner exceptions too
foreach (var exc in texc.TraverseNodes<Exception>(ex => ex.InnerException)
.Reverse())
{
log.Error(exc.Message, exc);
currentUser.RaiseMessage(exc.Message);
}
break;

default:
currentUser.RaiseMessage(e.Error.Message);
break;
Expand Down
18 changes: 17 additions & 1 deletion GUI/Main/MainRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
using System.Timers;
using System.Linq;
using System.Windows.Forms;
using System.Transactions;
using Timer = System.Timers.Timer;

using Newtonsoft.Json;
using Autofac;

using CKAN.Versioning;
using CKAN.Configuration;
using CKAN.Extensions;

// Don't warn if we use our own obsolete properties
#pragma warning disable 0618
Expand Down Expand Up @@ -158,9 +160,23 @@ private void PostUpdateRepo(object sender, RunWorkerCompletedEventArgs e)
EnableMainWindow();
break;

case Exception exc:
case TransactionException texc:
// "Failed to roll back" is useless by itself,
// so show all inner exceptions too
foreach (var exc in texc.TraverseNodes<Exception>(ex => ex.InnerException)
.Reverse())
{
log.Error(exc.Message, exc);
currentUser.RaiseMessage(exc.Message);
}
AddStatusMessage(Properties.Resources.MainRepoFailed);
Wait.Finish();
EnableMainWindow();
break;

case Exception exc:
currentUser.RaiseMessage(exc.Message);
AddStatusMessage(Properties.Resources.MainRepoFailed);
Wait.Finish();
EnableMainWindow();
break;
Expand Down
2 changes: 1 addition & 1 deletion GUI/Main/MainTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public partial class Main
{
private void viewPlayTimeStripMenuItem_Click(object sender, EventArgs e)
{
PlayTime.loadAllPlayTime(manager);
PlayTime.loadAllPlayTime(Manager);
tabController.ShowTab("PlayTimeTabPage", 2);
DisableMainWindow();
}
Expand Down
2 changes: 1 addition & 1 deletion GUI/Main/MainUnmanaged.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public partial class Main
{
private void viewUnmanagedFilesStripMenuItem_Click(object sender, EventArgs e)
{
UnmanagedFiles.LoadFiles(manager.CurrentInstance, currentUser);
UnmanagedFiles.LoadFiles(Manager.CurrentInstance, currentUser);
tabController.ShowTab("UnmanagedFilesTabPage", 2);
DisableMainWindow();
}
Expand Down
6 changes: 2 additions & 4 deletions GUI/Model/GUIMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,9 @@ public CkanModule SelectedMod
}
Main.Instance.ManageMods.MarkModForInstall(Identifier, selectedMod == null);

// C# 7.0: Executes the task and discards it
_ = Main.Instance.ManageMods.UpdateChangeSetAndConflicts(
Main.Instance.ManageMods.UpdateChangeSetAndConflicts(
Main.Instance.Manager.CurrentInstance,
RegistryManager.Instance(Main.Instance.Manager.CurrentInstance).registry
);
RegistryManager.Instance(Main.Instance.Manager.CurrentInstance).registry);

OnPropertyChanged();
}
Expand Down

0 comments on commit e870e8e

Please sign in to comment.