Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom mod labels, favorites, hiding #2936

Merged
merged 19 commits into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Core/Extensions/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;

namespace CKAN.Extensions
{
public static class DictionaryExtensions
{

public static V GetOrDefault<K, V>(this Dictionary<K, V> dict, K key)
{
V val = default(V);
if (key != null)
{
dict.TryGetValue(key, out val);
}
return val;
}

}
}
13 changes: 12 additions & 1 deletion Core/Registry/Registry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public void SetDownloadCounts(SortedDictionary<string, int> counts)
// Index of which mods provide what, format:
// providers[provided] = { provider1, provider2, ... }
// Built by BuildProvidesIndex, makes LatestAvailableWithProvides much faster.
[JsonIgnore] private Dictionary<string, HashSet<AvailableModule>> providers
[JsonIgnore]
private Dictionary<string, HashSet<AvailableModule>> providers
= new Dictionary<string, HashSet<AvailableModule>>();

/// <summary>
Expand Down Expand Up @@ -686,6 +687,16 @@ private void BuildProvidesIndexFor(AvailableModule am)
}
}

public void BuildTagIndex(ModuleTagList tags)
{
tags.Tags.Clear();
tags.Untagged.Clear();
foreach (AvailableModule am in available_modules.Values)
{
tags.BuildTagIndexFor(am);
}
}

/// <summary>
/// <see cref="IRegistryQuerier.LatestAvailableWithProvides" />
/// </summary>
Expand Down
29 changes: 29 additions & 0 deletions Core/Registry/Tags/ModuleTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections.Generic;

namespace CKAN
{
public class ModuleTag
{
public string Name;
public bool Visible;
public HashSet<string> ModuleIdentifiers = new HashSet<string>();

/// <summary>
/// Add a module to this label's group
/// </summary>
/// <param name="identifier">The identifier of the module to add</param>
public void Add(string identifier)
{
ModuleIdentifiers.Add(identifier);
}

/// <summary>
/// Remove a module from this label's group
/// </summary>
/// <param name="identifier">The identifier of the module to remove</param>
public void Remove(string identifier)
{
ModuleIdentifiers.Remove(identifier);
}
}
}
80 changes: 80 additions & 0 deletions Core/Registry/Tags/ModuleTagList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using System.Linq;
using System.IO;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace CKAN
{
public class ModuleTagList
{
[JsonIgnore]
public Dictionary<string, ModuleTag> Tags = new Dictionary<string, ModuleTag>();

[JsonIgnore]
public HashSet<string> Untagged = new HashSet<string>();

[JsonProperty("hidden_tags")]
public HashSet<string> HiddenTags = new HashSet<string>();

public static readonly string DefaultPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"CKAN",
"tags.json"
);

public void BuildTagIndexFor(AvailableModule am)
{
bool tagged = false;
foreach (CkanModule m in am.AllAvailable())
{
if (m.Tags != null)
{
tagged = true;
foreach (string tagName in m.Tags)
{
ModuleTag tag = null;
if (Tags.TryGetValue(tagName, out tag))
tag.Add(m.identifier);
else
Tags.Add(tagName, new ModuleTag()
{
Name = tagName,
Visible = !HiddenTags.Contains(tagName),
ModuleIdentifiers = new HashSet<string>() { m.identifier },
});
}
}
}
if (!tagged)
{
Untagged.Add(am.AllAvailable().First().identifier);
}
}

public static ModuleTagList Load(string path)
{
try
{
return JsonConvert.DeserializeObject<ModuleTagList>(File.ReadAllText(path));
}
catch (FileNotFoundException ex)
{
return null;
}
}

public bool Save(string path)
{
try
{
File.WriteAllText(path, JsonConvert.SerializeObject(this, Formatting.Indented));
return true;
}
catch
{
return false;
}
}
}
}
5 changes: 4 additions & 1 deletion Core/Types/CkanModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ public class CkanModule : IEquatable<CkanModule>

[JsonProperty("install", NullValueHandling = NullValueHandling.Ignore)]
public ModuleInstallDescriptor[] install;

[JsonProperty("localizations", NullValueHandling = NullValueHandling.Ignore)]
public string[] localizations;

Expand Down Expand Up @@ -469,6 +469,9 @@ public ModuleVersion spec_version
}
}

[JsonProperty("tags", NullValueHandling = NullValueHandling.Ignore)]
public HashSet<string> Tags;

// A list of eveything this mod provides.
public List<string> ProvidesList
{
Expand Down
15 changes: 15 additions & 0 deletions GUI/CKAN-GUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@
<Compile Include="DropdownMenuButton.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="EditLabelsDialog.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="EditLabelsDialog.Designer.cs">
<DependentUpon>EditLabelsDialog.cs</DependentUpon>
</Compile>
<Compile Include="ErrorDialog.cs">
<SubType>Form</SubType>
</Compile>
Expand Down Expand Up @@ -144,6 +150,8 @@
<Compile Include="KSPCommandLineOptionsDialog.Designer.cs">
<DependentUpon>KSPCommandLineOptionsDialog.cs</DependentUpon>
</Compile>
<Compile Include="Labels\ModuleLabel.cs" />
<Compile Include="Labels\ModuleLabelList.cs" />
<Compile Include="Main.cs">
<SubType>Form</SubType>
</Compile>
Expand All @@ -168,6 +176,7 @@
<Compile Include="MainInstall.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="MainLabels.cs" />
<Compile Include="MainModInfo.cs">
<SubType>UserControl</SubType>
</Compile>
Expand Down Expand Up @@ -287,6 +296,12 @@
<EmbeddedResource Include="Localization\de-DE\CompatibleKspVersionsDialog.de-DE.resx">
<DependentUpon>..\..\CompatibleKspVersionsDialog.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="EditLabelsDialog.resx">
<DependentUpon>EditLabelsDialog.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Localization\de-DE\EditLabelsDialog.de-DE.resx">
<DependentUpon>..\..\EditLabelsDialog.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="ErrorDialog.resx">
<DependentUpon>ErrorDialog.cs</DependentUpon>
</EmbeddedResource>
Expand Down
Loading