This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Mod.cs
246 lines (213 loc) · 7.64 KB
/
Mod.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
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
using AstroModIntegrator;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AstroModLoader
{
public class Mod : ICloneable
{
private bool _enabled;
[JsonIgnore]
public bool Dirty;
[JsonProperty("enabled")]
[DefaultValue(true)]
[DisplayName(" ")]
public bool Enabled
{
get
{
return _enabled;
}
set
{
_enabled = value;
Dirty = true;
}
}
[JsonProperty("optional")]
[DefaultValue(false)]
public bool IsOptional;
public bool ShouldSerializeIsOptional()
{
return IsOptional || TableHandler.ShouldContainOptionalColumn();
}
[JsonProperty("force_latest")]
[DefaultValue(false)]
public bool ForceLatest;
public bool ShouldSerializeForceLatest()
{
return ForceLatest;
}
[JsonConverter(typeof(VersionConverter))]
[DisplayName("Version")]
[JsonProperty("version")]
public Version InstalledVersion { get; set; }
[JsonIgnore]
public List<Version> AvailableVersions { get; set; }
[JsonProperty("priority")]
public int Priority = 0;
[JsonIgnore]
public Dictionary<Version, Metadata> AllModData;
[JsonIgnore]
public Metadata CurrentModData {
get
{
if (!AllModData.ContainsKey(InstalledVersion)) throw new KeyNotFoundException("This mod does not have the following version: " + InstalledVersion);
return AllModData[InstalledVersion];
}
}
[JsonIgnore]
public string NameOnDisk;
[JsonIgnore]
internal bool CannotCurrentlyUpdate = false;
public Mod(Metadata modData, string nameOnDisk)
{
if (modData == null && nameOnDisk == null) return;
if (AllModData == null) AllModData = new Dictionary<Version, Metadata>();
NameOnDisk = nameOnDisk;
PerformNameAnalysis();
Priority = newPriority;
InstalledVersion = newModVersion;
if (modData != null)
{
if (modData.ModVersion != null) InstalledVersion = modData.ModVersion;
}
AllModData[InstalledVersion] = modData;
if (modData == null)
{
AllModData[InstalledVersion] = JsonConvert.DeserializeObject<Metadata>("{}");
AllModData[InstalledVersion].Sync = SyncMode.None;
}
if (!string.IsNullOrEmpty(newModID) && string.IsNullOrEmpty(CurrentModData.ModID))
{
CurrentModData.ModID = newModID;
}
if (string.IsNullOrEmpty(CurrentModData.Name)) CurrentModData.Name = CurrentModData.ModID;
NameOnDisk = nameOnDisk;
AvailableVersions = new List<Version>();
if (InstalledVersion != null && !AvailableVersions.Contains(InstalledVersion)) AvailableVersions.Add(InstalledVersion);
if (AllModData[InstalledVersion].Name.Length > 32) AllModData[InstalledVersion].Name = AllModData[InstalledVersion].Name.Substring(0, 32);
}
private static readonly Regex ModIDFilterRegex = new Regex(@"[^A-Za-z0-9]", RegexOptions.Compiled);
public string ConstructName(int forcePriority = -1)
{
return AMLUtils.GeneratePriorityFromPositionInList(forcePriority >= 0 ? forcePriority : Priority) + "-" + ModIDFilterRegex.Replace(CurrentModData.ModID, "") + "-" + InstalledVersion + "_P.pak";
}
private int newPriority;
private string newModID;
private Version newModVersion;
private void PerformNameAnalysis()
{
if (NameOnDisk == null) NameOnDisk = "";
List<string> nameData = NameOnDisk.Split('_')[0].Split('-').ToList();
int origCount = nameData.Count;
if (origCount >= 1)
{
try
{
newPriority = int.Parse(nameData[0]);
}
catch (FormatException)
{
newPriority = 1;
}
nameData.RemoveAt(0);
}
else
{
newPriority = 1;
}
if (origCount >= 2)
{
if (!string.IsNullOrEmpty(nameData[0])) newModID = nameData[0];
nameData.RemoveAt(0);
}
else
{
newModID = NameOnDisk.Replace(".pak", "");
}
newModVersion = new Version(0, 1, 0);
if (origCount >= 3)
{
if (!string.IsNullOrEmpty(nameData[0])) newModVersion = new Version(nameData[0]);
nameData.RemoveAt(0);
}
}
public IndexFile GetIndexFile(List<string> duplicateURLs)
{
DownloadInfo di = CurrentModData.Download;
if (di == null) return null;
try
{
if (di.Type == DownloadMode.IndexFile && !string.IsNullOrEmpty(di.URL))
{
if (duplicateURLs != null && duplicateURLs.Contains(di.URL)) return null;
string rawIndexFileData = "";
using (var wb = new WebClient())
{
wb.Headers[HttpRequestHeader.UserAgent] = AMLUtils.UserAgent;
rawIndexFileData = wb.DownloadString(di.URL);
}
if (string.IsNullOrEmpty(rawIndexFileData)) return null;
IndexFile indexFile = JsonConvert.DeserializeObject<IndexFile>(rawIndexFileData);
indexFile.OriginalURL = di.URL;
return indexFile;
}
}
catch (Exception ex)
{
if (ex is WebException || ex is JsonException) return null;
throw;
}
return null;
}
public override bool Equals(object obj)
{
string comparer;
if (obj is Mod mobj)
{
comparer = mobj.NameOnDisk;
}
else if (obj is string sobj)
{
comparer = sobj;
}
else
{
return false;
}
return NameOnDisk.Equals(comparer);
}
public override int GetHashCode()
{
return NameOnDisk.GetHashCode();
}
public override string ToString()
{
return Enabled.ToString();
}
public object Clone()
{
var modClone = new Mod(null, this.NameOnDisk);
modClone.AvailableVersions = this.AvailableVersions.ToList();
modClone.AvailableVersions.ForEach(x => x.Clone());
modClone.AllModData = this.AllModData.ToDictionary(entry => (Version)entry.Key.Clone(), entry => (Metadata)entry.Value.Clone());
modClone.InstalledVersion = (Version)this.InstalledVersion.Clone();
modClone.ForceLatest = this.ForceLatest;
modClone.Enabled = this.Enabled;
modClone.IsOptional = this.IsOptional;
modClone.Priority = this.Priority;
return modClone;
}
}
}