Skip to content

Commit

Permalink
Update or refresh ConsoleUI mod list after repo or compat changes
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Apr 24, 2021
1 parent 6b2340c commit 644bf1c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
22 changes: 20 additions & 2 deletions ConsoleUI/ModListScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -477,16 +477,34 @@ private bool ScanForMods()
private bool SelectInstall(ConsoleTheme theme)
{
GameInstance prevInst = manager.CurrentInstance;
var prevRepos = new SortedDictionary<string, Repository>(registry.Repositories);
var prevVerCrit = prevInst.VersionCriteria();
LaunchSubScreen(theme, new GameInstanceListScreen(manager));
// Abort if same instance as before
if (!prevInst.Equals(manager.CurrentInstance)) {
// Game instance changed, reset everything
plan.Reset();
registry = RegistryManager.Instance(manager.CurrentInstance).registry;
RefreshList(theme);
} else if (!SortedDictionaryEquals(registry.Repositories, prevRepos)) {
// Repos changed, need to fetch them
UpdateRegistry(theme, false);
RefreshList(theme);
} else if (!manager.CurrentInstance.VersionCriteria().Equals(prevVerCrit)) {
// VersionCriteria changed, need to re-check what is compatible
RefreshList(theme);
}
return true;
}

private bool SortedDictionaryEquals<K, V>(SortedDictionary<K, V> a, SortedDictionary<K, V> b)
{
return a == null ? b == null
: b == null ? false
: a.Count == b.Count
&& a.Keys.All(k => b.ContainsKey(k))
&& b.Keys.All(k => a.ContainsKey(k) && a[k].Equals(b[k]));
}

private bool EditAuthTokens(ConsoleTheme theme)
{
LaunchSubScreen(theme, new AuthTokenScreen());
Expand All @@ -497,7 +515,7 @@ private void RefreshList(ConsoleTheme theme)
{
// In the constructor this is called while moduleList is being populated, just do nothing in this case.
// ModListScreen -> moduleList = (GetAllMods ...) -> UpdateRegistry -> RefreshList
moduleList?.SetData(GetAllMods(theme,true));
moduleList?.SetData(GetAllMods(theme, true));
}

private List<CkanModule> allMods = null;
Expand Down
21 changes: 19 additions & 2 deletions Core/Types/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@

namespace CKAN
{
public class Repository
public class Repository : IEquatable<Repository>
{
[JsonIgnore] public static readonly string default_ckan_repo_name = "default";

public string name;
public Uri uri;
public string last_server_etag;
public int priority = 0;
public Boolean ckan_mirror = false;

public Repository()
{
Expand All @@ -38,6 +37,24 @@ public Repository(string name, Uri uri)
this.uri = uri;
}

public override bool Equals(Object other)
{
return Equals(other as Repository);
}

public bool Equals(Repository other)
{
return other != null
&& name == other.name
&& uri == other.uri
&& priority == other.priority;
}

public override int GetHashCode()
{
return name.GetHashCode();
}

public override string ToString()
{
return String.Format("{0} ({1}, {2})", name, priority, uri);
Expand Down

0 comments on commit 644bf1c

Please sign in to comment.