Skip to content

Commit

Permalink
Trivial coding style changes
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed May 8, 2024
1 parent 05d84be commit 9fb7436
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 85 deletions.
141 changes: 57 additions & 84 deletions ConsoleUI/DependencyScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
using System.Linq;
using System.ComponentModel;
using System.Collections.Generic;

using CKAN.Versioning;
#if NETFRAMEWORK
using CKAN.Extensions;
#endif
using CKAN.ConsoleUI.Toolkit;

namespace CKAN.ConsoleUI {
Expand All @@ -16,31 +21,26 @@ public class DependencyScreen : ConsoleScreen {
/// Initialize the screen
/// </summary>
/// <param name="mgr">Game instance manager containing instances</param>
/// <param name="registry">Registry of the current instance for finding mods</param>
/// <param name="reg">Registry of the current instance for finding mods</param>
/// <param name="cp">Plan of mods to add and remove</param>
/// <param name="rej">Mods that the user saw and did not select, in this pass or a previous pass</param>
/// <param name="dbg">True if debug options should be available, false otherwise</param>
public DependencyScreen(GameInstanceManager mgr, Registry registry, ChangePlan cp, HashSet<string> rej, bool dbg) : base()
public DependencyScreen(GameInstanceManager mgr, Registry reg, ChangePlan cp, HashSet<string> rej, bool dbg) : base()
{
debug = dbg;
manager = mgr;
plan = cp;
this.registry = registry;
installer = new ModuleInstaller(manager.CurrentInstance, manager.Cache, this);
rejected = rej;

AddObject(new ConsoleLabel(
1, 2, -1,
() => Properties.Resources.RecommendationsLabel
));

HashSet<CkanModule> sourceModules = new HashSet<CkanModule>();
sourceModules.UnionWith(plan.Install);
sourceModules.UnionWith(new HashSet<CkanModule>(
ReplacementIdentifiers(plan.Replace)
.Select(id => registry.InstalledModule(id).Module)
));
generateList(sourceModules);
debug = dbg;
manager = mgr;
plan = cp;
registry = reg;
rejected = rej;

AddObject(new ConsoleLabel(1, 2, -1,
() => Properties.Resources.RecommendationsLabel));

generateList(new ModuleInstaller(manager.CurrentInstance, manager.Cache, this),
plan.Install
.Concat(ReplacementModules(plan.Replace,
manager.CurrentInstance.VersionCriteria()))
.ToHashSet());

dependencyList = new ConsoleListBox<Dependency>(
1, 4, -1, -2,
Expand All @@ -49,17 +49,17 @@ public DependencyScreen(GameInstanceManager mgr, Registry registry, ChangePlan c
new ConsoleListBoxColumn<Dependency>() {
Header = Properties.Resources.RecommendationsInstallHeader,
Width = 7,
Renderer = (Dependency d) => StatusSymbol(d.module)
Renderer = (Dependency d) => StatusSymbol(d.module),
},
new ConsoleListBoxColumn<Dependency>() {
Header = Properties.Resources.RecommendationsNameHeader,
Width = null,
Renderer = (Dependency d) => d.module.ToString()
Renderer = (Dependency d) => d.module.ToString(),
},
new ConsoleListBoxColumn<Dependency>() {
Header = Properties.Resources.RecommendationsSourcesHeader,
Width = 42,
Renderer = (Dependency d) => string.Join(", ", d.dependents)
Renderer = (Dependency d) => string.Join(", ", d.dependents),
}
},
1, 0, ListSortDirection.Descending
Expand Down Expand Up @@ -89,10 +89,9 @@ public DependencyScreen(GameInstanceManager mgr, Registry registry, ChangePlan c
dependencyList.AddTip(Properties.Resources.Enter, Properties.Resources.Details);
dependencyList.AddBinding(Keys.Enter, (object sender, ConsoleTheme theme) => {
if (dependencyList.Selection != null) {
LaunchSubScreen(theme, new ModInfoScreen(
manager, registry, plan,
dependencyList.Selection.module,
debug));
LaunchSubScreen(theme, new ModInfoScreen(manager, reg, plan,
dependencyList.Selection.module,
debug));
}
return true;
});
Expand All @@ -102,9 +101,7 @@ public DependencyScreen(GameInstanceManager mgr, Registry registry, ChangePlan c
AddTip(Properties.Resources.Esc, Properties.Resources.Cancel);
AddBinding(Keys.Escape, (object sender, ConsoleTheme theme) => {
// Add everything to rejected
foreach (var kvp in dependencies) {
rejected.Add(kvp.Key.identifier);
}
rejected.UnionWith(dependencies.Keys.Select(m => m.identifier));
return false;
});

Expand All @@ -131,88 +128,69 @@ public DependencyScreen(GameInstanceManager mgr, Registry registry, ChangePlan c
/// <summary>
/// Put description in top center
/// </summary>
protected override string CenterHeader()
{
return Properties.Resources.RecommendationsTitle;
}
protected override string CenterHeader() => Properties.Resources.RecommendationsTitle;

/// <summary>
/// Return whether there are any options to show.
/// ModListScreen uses this to avoid showing this screen when empty.
/// </summary>
public bool HaveOptions()
{
return dependencies.Count > 0;
}
public bool HaveOptions() => dependencies.Count > 0;

private void generateList(HashSet<CkanModule> inst)
private void generateList(ModuleInstaller installer, HashSet<CkanModule> inst)
{
if (installer.FindRecommendations(
inst, new List<CkanModule>(inst), registry as Registry,
out Dictionary<CkanModule, Tuple<bool, List<string>>> recommendations,
out Dictionary<CkanModule, List<string>> suggestions,
out Dictionary<CkanModule, HashSet<string>> supporters
)) {
foreach (var kvp in recommendations) {
dependencies.Add(kvp.Key, new Dependency() {
module = kvp.Key,
defaultInstall = kvp.Value.Item1,
dependents = kvp.Value.Item2.OrderBy(d => d).ToList()
foreach ((CkanModule mod, Tuple<bool, List<string>> checkedAndDependents) in recommendations) {
dependencies.Add(mod, new Dependency() {
module = mod,
dependents = checkedAndDependents.Item2.OrderBy(d => d).ToList()
});
if (kvp.Value.Item1) {
accepted.Add(kvp.Key);
}
}
foreach (var kvp in suggestions) {
dependencies.Add(kvp.Key, new Dependency() {
module = kvp.Key,
defaultInstall = false,
dependents = kvp.Value.OrderBy(d => d).ToList()
foreach ((CkanModule mod, List<string> dependents) in suggestions) {
dependencies.Add(mod, new Dependency() {
module = mod,
dependents = dependents.OrderBy(d => d).ToList()
});
}
foreach (var kvp in supporters) {
dependencies.Add(kvp.Key, new Dependency() {
module = kvp.Key,
defaultInstall = false,
dependents = kvp.Value.OrderBy(d => d).ToList()
foreach ((CkanModule mod, HashSet<string> dependents) in supporters) {
dependencies.Add(mod, new Dependency() {
module = mod,
dependents = dependents.OrderBy(d => d).ToList()
});
}
// Check the default checkboxes
accepted.UnionWith(recommendations.Where(kvp => kvp.Value.Item1)
.Select(kvp => kvp.Key));
}
}

private IEnumerable<string> ReplacementIdentifiers(IEnumerable<string> replaced_identifiers)
{
foreach (string replaced in replaced_identifiers) {
ModuleReplacement repl = registry.GetReplacement(
replaced, manager.CurrentInstance.VersionCriteria()
);
if (repl != null) {
yield return repl.ReplaceWith.identifier;
}
}
}
private IEnumerable<CkanModule> ReplacementModules(IEnumerable<string> replaced_identifiers,
GameVersionCriteria crit)
=> replaced_identifiers.Select(replaced => registry.GetReplacement(replaced, crit))
.Where(repl => repl != null)
.Select(repl => repl.ReplaceWith);

private string StatusSymbol(CkanModule mod)
{
return accepted.Contains(mod)
? installing
: notinstalled;
}
=> accepted.Contains(mod) ? installing
: notinstalled;

private readonly HashSet<CkanModule> accepted = new HashSet<CkanModule>();
private readonly HashSet<string> rejected;

private readonly IRegistryQuerier registry;
private readonly GameInstanceManager manager;
private readonly ModuleInstaller installer;
private readonly ChangePlan plan;
private readonly bool debug;

private readonly Dictionary<CkanModule, Dependency> dependencies = new Dictionary<CkanModule, Dependency>();
private readonly ConsoleListBox<Dependency> dependencyList;

private static readonly string notinstalled = " ";
private static readonly string installing = "+";
private const string notinstalled = " ";
private const string installing = "+";
}

/// <summary>
Expand All @@ -221,14 +199,9 @@ private string StatusSymbol(CkanModule mod)
public class Dependency {

/// <summary>
/// Identifier of mod
/// </summary>
public CkanModule module;

/// <summary>
/// True if we default to installing, false otherwise
/// The mod
/// </summary>
public bool defaultInstall;
public CkanModule module;

/// <summary>
/// List of mods that recommended or suggested this mod
Expand Down
2 changes: 1 addition & 1 deletion Core/Repositories/RepositoryData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ private static CkanModule ProcessRegistryMetadataFromJSON(string metadata, strin
}

// If we haven't handled our exception, then it really was exceptional.
if (handled == false)
if (!handled)
{
if (exception == null)
{
Expand Down

0 comments on commit 9fb7436

Please sign in to comment.