-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathInstalledMods.cs
More file actions
341 lines (280 loc) · 10.7 KB
/
InstalledMods.cs
File metadata and controls
341 lines (280 loc) · 10.7 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
namespace ModAPI.Common
{
public class InstalledFile
{
public static readonly string ElementName = "file";
public string Name;
// public SporePath.Game PathType = SporePath.Game.None; // set to None if it's a dll
public string PathType;
public InstalledFile(string name, SporePath.Game pathType = SporePath.Game.GalacticAdventures)
{
this.Name = name;
this.PathType = pathType.ToString();
}
public InstalledFile(string name, string pathType)
{
this.Name = name;
this.PathType = pathType;
}
public void Save(XmlDocument document, XmlElement parent)
{
var element = document.CreateElement(InstalledFile.ElementName);
element.InnerText = Name;
if (PathType != null && PathType != SporePath.Game.None.ToString())
{
var attribute = document.CreateAttribute("game");
attribute.Value = PathType.ToString();
element.Attributes.Append(attribute);
}
parent.AppendChild(element);
}
}
public class ModConfiguration : IEquatable<ModConfiguration>
{
public static readonly string ElementName = "mod";
public string Name;
public string Unique;
public Version Version;
public string DisplayName;
public string ConfiguratorPath;
public string[] Dependencies;
public Version[] DependenciesVersions;
public List<InstalledFile> InstalledFiles = new List<InstalledFile>();
public ModConfiguration(string name, string unique)
{
Name = name;
Unique = unique;
}
public ModConfiguration(string name)
{
Name = name;
Unique = name;
}
public override string ToString()
{
return Name;
}
public void AddFile(string name, SporePath.Game pathType = SporePath.Game.None)
{
this.InstalledFiles.Add(new InstalledFile(name, pathType));
}
public void RemoveFile(InstalledFile file)
{
this.InstalledFiles.Remove(file);
}
public void Save(XmlDocument document, XmlElement parent)
{
var element = document.CreateElement(ModConfiguration.ElementName);
var attribute = document.CreateAttribute("name");
attribute.Value = this.Name;
element.Attributes.Append(attribute);
/*if (Unique != null)
{*/
var uniqueAttribute = document.CreateAttribute("unique");
uniqueAttribute.Value = Unique;
element.Attributes.Append(uniqueAttribute);
//}
if (Version != null)
{
var versionAttribute = document.CreateAttribute("version");
versionAttribute.Value = Version.ToString();
element.Attributes.Append(versionAttribute);
}
if (Dependencies != null && Dependencies.Length > 0)
{
var dependenciesAttribute = document.CreateAttribute("dependencies");
dependenciesAttribute.Value = String.Join("?", Dependencies);
element.Attributes.Append(dependenciesAttribute);
}
if (DependenciesVersions != null && DependenciesVersions.Length > 0)
{
var dependenciesVersionsAttribute = document.CreateAttribute("dependenciesVersions");
dependenciesVersionsAttribute.Value = String.Join("?", DependenciesVersions.Select(x => x.ToString()));
element.Attributes.Append(dependenciesVersionsAttribute);
}
if (this.ConfiguratorPath != null)
{
attribute = document.CreateAttribute("configurator");
attribute.Value = this.ConfiguratorPath;
element.Attributes.Append(attribute);
}
if ((DisplayName != null) && (DisplayName != Name))
{
var displayAttribute = document.CreateAttribute("displayName");
displayAttribute.Value = this.DisplayName;
element.Attributes.Append(displayAttribute);
}
foreach (InstalledFile file in InstalledFiles)
{
file.Save(document, element);
}
parent.AppendChild(element);
}
public void Load(XmlNode node)
{
var nameAttribute = node.Attributes.GetNamedItem("name");
if (nameAttribute != null)
this.Name = nameAttribute.Value;
var uniqueAttribute = node.Attributes.GetNamedItem("unique");
if (uniqueAttribute != null)
Unique = uniqueAttribute.Value;
else
Unique = nameAttribute.Value;
var versionAttribute = node.Attributes.GetNamedItem("version");
if (versionAttribute != null &&
Version.TryParse(versionAttribute.Value, out Version parsedVersion))
{
Version = parsedVersion;
}
else
{
Version = null;
}
var dependenciesAttribute = node.Attributes.GetNamedItem("dependencies");
if (dependenciesAttribute != null)
{
Dependencies = dependenciesAttribute.Value.Split('?');
}
var dependenciesVersionsAttribute = node.Attributes.GetNamedItem("dependenciesVersions");
if (dependenciesVersionsAttribute != null)
{
List<Version> versions = new List<Version>();
foreach (string version in dependenciesVersionsAttribute.Value.Split('?'))
{
if (Version.TryParse(version, out Version parsedDependencyVersion))
{
versions.Add(parsedDependencyVersion);
}
else
{
versions.Clear();
break;
}
}
if (versions.Count() != 0)
{
DependenciesVersions = versions.ToArray();
}
}
var displayNameAttribute = node.Attributes.GetNamedItem("displayName");
if (displayNameAttribute != null)
this.DisplayName = displayNameAttribute.Value;
else if (nameAttribute != null)
this.DisplayName = nameAttribute.Value;
var configuratorAttribute = node.Attributes.GetNamedItem("configurator");
if (configuratorAttribute != null)
{
this.ConfiguratorPath = configuratorAttribute.Value;
}
foreach (XmlNode child in node.ChildNodes)
{
if (child.Name == InstalledFile.ElementName)
{
// var game = SporePath.Game.None;
string game = null;
var attribute = child.Attributes.GetNamedItem("game");
if (attribute != null)
{
// game = (SporePath.Game) Enum.Parse(typeof(SporePath.Game), attribute.Value, true);
game = attribute.Value;
}
else
{
game = "None"; // that is, DLLs (when the value is None no attribute is written, so we fix it here)
}
InstalledFiles.Add( new InstalledFile(child.InnerText, game) );
}
}
}
public override int GetHashCode()
{
return Name == null ? 0 : Name.GetHashCode();
}
public override bool Equals(object obj)
{
return obj is ModConfiguration && Equals((ModConfiguration) obj);
}
public bool Equals(ModConfiguration obj)
{
return obj == null ? false : obj.Name == Name;
}
}
public class InstalledMods
{
public static readonly string FileName = "InstalledMods.config";
public static readonly string ElementName = "InstalledMods";
public HashSet<ModConfiguration> ModConfigurations = new HashSet<ModConfiguration>();
public ModConfiguration AddMod(string name)
{
var mod = new ModConfiguration(name);
// first remove anything that has the same name, then add the new one
ModConfigurations.Remove(mod);
ModConfigurations.Add(mod);
return mod;
}
public void RemoveMod(ModConfiguration mod)
{
this.ModConfigurations.Remove(mod);
}
public void Save()
{
string path = GetDefaultPath();
var document = new XmlDocument();
var mainNode = document.CreateElement(InstalledMods.ElementName);
document.AppendChild(mainNode);
foreach (ModConfiguration mod in ModConfigurations)
{
mod.Save(document, mainNode);
}
document.Save(path);
}
public void Load(XmlNode node)
{
foreach (XmlNode child in node.ChildNodes)
{
if (child.Name == ModConfiguration.ElementName)
{
var mod = new ModConfiguration(null);
mod.Load(child);
ModConfigurations.Add(mod);
}
}
}
public bool Load()
{
ModConfigurations.Clear();
string path = GetDefaultPath();
if (File.Exists(path))
{
string xmlIn = File.ReadAllText(path);
if (!String.IsNullOrEmpty(xmlIn))
{
var document = new XmlDocument();
document.LoadXml(xmlIn);
foreach (XmlNode node in document.ChildNodes)
{
if (node.Name == InstalledMods.ElementName)
{
Load(node);
}
}
}
return true;
}
else
{
return false;
}
}
public static string GetDefaultPath()
{
var programPath = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
return Path.Combine(programPath, FileName);
}
}
}