-
-
Notifications
You must be signed in to change notification settings - Fork 344
/
Copy pathModuleLabel.cs
148 lines (126 loc) · 5.39 KB
/
ModuleLabel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System.Drawing;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using CKAN.Games;
namespace CKAN.GUI
{
[JsonObject(MemberSerialization.OptIn)]
[JsonConverter(typeof(ModuleIdentifiersRenamedConverter))]
public class ModuleLabel
{
[JsonConstructor]
public ModuleLabel()
{
Name = "";
}
public ModuleLabel(string name)
{
Name = name;
}
[JsonProperty("name", NullValueHandling = NullValueHandling.Ignore)]
public string Name;
[JsonProperty("color", NullValueHandling = NullValueHandling.Ignore)]
public Color? Color;
[JsonProperty("instance_name", NullValueHandling = NullValueHandling.Ignore)]
public string? InstanceName;
[JsonProperty("hide", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
[DefaultValue(false)]
public bool Hide;
[JsonProperty("notify_on_change", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
[DefaultValue(false)]
public bool NotifyOnChange;
[JsonProperty("remove_on_change", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
[DefaultValue(false)]
public bool RemoveOnChange;
[JsonProperty("alert_on_install", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
[DefaultValue(false)]
public bool AlertOnInstall;
[JsonProperty("remove_on_install", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
[DefaultValue(false)]
public bool RemoveOnInstall;
[JsonProperty("hold_version", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
[DefaultValue(false)]
public bool HoldVersion;
[JsonProperty("ignore_missing_files", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
[DefaultValue(false)]
public bool IgnoreMissingFiles;
[JsonProperty("module_identifiers_by_game", NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(JsonToGamesDictionaryConverter))]
private readonly Dictionary<string, HashSet<string>> ModuleIdentifiers =
new Dictionary<string, HashSet<string>>();
/// <summary>
/// Return the number of modules associated with this label for a given game
/// </summary>
/// <param name="game">Game to check</param>
/// <returns>Number of modules</returns>
public int ModuleCount(IGame game)
=> ModuleIdentifiers.TryGetValue(game.ShortName, out HashSet<string>? identifiers)
? identifiers.Count
: 0;
/// <summary>
/// Return whether a given identifier is associated with this label for a given game
/// </summary>
/// <param name="game">The game to check</param>
/// <param name="identifier">The identifier to check</param>
/// <returns>true if this label applies to this identifier, false otherwise</returns>
public bool ContainsModule(IGame game, string identifier)
=> ModuleIdentifiers.TryGetValue(game.ShortName, out HashSet<string>? identifiers)
&& identifiers.Contains(identifier);
/// <summary>
/// Check whether this label is active for a given game instance
/// </summary>
/// <param name="instanceName">Name of the instance</param>
/// <returns>
/// True if active, false otherwise
/// </returns>
public bool AppliesTo(string instanceName)
=> InstanceName == null || InstanceName == instanceName;
public IEnumerable<string> IdentifiersFor(IGame game)
=> ModuleIdentifiers.TryGetValue(game.ShortName, out HashSet<string>? idents)
? idents
: Enumerable.Empty<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(IGame game, string identifier)
{
if (ModuleIdentifiers.TryGetValue(game.ShortName, out HashSet<string>? identifiers))
{
identifiers.Add(identifier);
}
else
{
ModuleIdentifiers.Add(game.ShortName, new HashSet<string> {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(IGame game, string identifier)
{
if (ModuleIdentifiers.TryGetValue(game.ShortName, out HashSet<string>? identifiers))
{
identifiers.Remove(identifier);
if (identifiers.Count < 1)
{
ModuleIdentifiers.Remove(game.ShortName);
}
}
}
}
/// <summary>
/// Protect old clients from trying to load a file they can't parse
/// </summary>
public class ModuleIdentifiersRenamedConverter : JsonPropertyNamesChangedConverter
{
protected override Dictionary<string, string> mapping
=> new Dictionary<string, string>
{
{ "module_identifiers", "module_identifiers_by_game" }
};
}
}