-
Notifications
You must be signed in to change notification settings - Fork 7
/
Packages.cs
295 lines (249 loc) · 10.3 KB
/
Packages.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
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
using NuGet.Versioning;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Media;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
namespace TribesLauncherSharp
{
class PackageException : Exception
{
public PackageException() : base() { }
public PackageException(string message) : base(message) { }
public PackageException(string message, Exception inner) : base(message, inner) { }
}
class SemanticVersionConverter : IYamlTypeConverter
{
public bool Accepts(Type type) => type == typeof(SemanticVersion);
public object ReadYaml(IParser parser, Type type)
{
var val = parser.Expect<Scalar>();
try
{
SemanticVersion version = SemanticVersion.Parse(val.Value);
return version;
} catch (ArgumentException)
{
throw new YamlException(val.Start, val.End, "Expected a valid semantic version");
}
}
public void WriteYaml(IEmitter emitter, object value, Type type)
{
var version = (SemanticVersion)value;
emitter.Emit(new Scalar(version.ToString()));
}
}
class RemotePackage
{
// Unique identifier for the package
public string Id { get; set; }
// User-facing package name
public string DisplayName { get; set; }
// Package description
public string Description { get; set; }
// Version of the package; used to determine whether a package requires updates
public SemanticVersion Version { get; set; }
// The key of the zipped package in S3
public string ObjectKey { get; set; }
// Whether the package is mandatory for TAMods
public bool Required { get; set; }
// A list of dependency packages which must be installed alongside this package
public List<string> Dependencies { get; set; } = new List<string>();
public InstalledPackage ToInstalledPackage()
{
return new InstalledPackage(Id, Version);
}
}
class RemotePackageConfig
{
public List<RemotePackage> Packages { get; private set; }
private static IDeserializer deserializer = new DeserializerBuilder()
.WithNamingConvention(new CamelCaseNamingConvention())
.WithTypeConverter(new SemanticVersionConverter())
.IgnoreUnmatchedProperties()
.Build();
public static RemotePackageConfig DownloadPackageConfig(Config config)
{
string packageConfigName = config.Debug.AlternatePackageConfigFile ?? "packageconfig.yaml";
string rawData;
try
{
rawData = RemoteObjectManager.Instance.DownloadObjectAsString(packageConfigName);
}
catch (Exception ex)
{
throw new PackageException($"Failed to download package config: {ex.Message}", ex);
}
try
{
return deserializer.Deserialize<RemotePackageConfig>(rawData);
}
catch (Exception ex)
{
throw new PackageException($"Failed to parse package config: {ex.Message}", ex);
}
}
}
class InstalledPackage
{
// Unique identifier for the package, as per the remote PackageConfig
public string Id { get; private set; }
// Installed version of the package; used to determine whether a package requires updates
public SemanticVersion Version { get; private set; }
public InstalledPackage() { }
public InstalledPackage(string id, SemanticVersion version)
{
Id = id;
Version = version;
}
}
class LocalPackage
{
public RemotePackage Remote { get; private set; }
public InstalledPackage Local { get; private set; }
public LocalPackage(RemotePackage remote, InstalledPackage local)
{
Remote = remote;
Local = local;
}
public bool AvailableRemotely => Remote != null;
public bool IsInstalled => Local != null;
// Requires update if it's a required package or the package is installed and outdated
public bool RequiresUpdate =>
AvailableRemotely && ((!IsInstalled && Remote.Required) || (IsInstalled && Local.Version < Remote.Version));
static SolidColorBrush NeedsUpdateBrush = new SolidColorBrush(Colors.Red);
static SolidColorBrush InstalledBrush = new SolidColorBrush(Colors.Green);
static SolidColorBrush AvailableBrush = new SolidColorBrush(Colors.Black);
public SolidColorBrush DisplayColor
{
get
{
if (RequiresUpdate)
{
return NeedsUpdateBrush;
} else if (IsInstalled)
{
return InstalledBrush;
} else
{
return AvailableBrush;
}
}
}
}
class InstalledPackageState
{
private static string localFile = "packagestate.yaml";
public List<InstalledPackage> Packages { get; private set; }
private static ISerializer serializer = new SerializerBuilder()
.WithNamingConvention(new CamelCaseNamingConvention())
.WithTypeConverter(new SemanticVersionConverter())
.Build();
private static IDeserializer deserializer = new DeserializerBuilder()
.WithNamingConvention(new CamelCaseNamingConvention())
.WithTypeConverter(new SemanticVersionConverter())
.IgnoreUnmatchedProperties()
.Build();
public InstalledPackageState()
{
Packages = new List<InstalledPackage>();
}
public static InstalledPackageState Load()
{
if (!File.Exists(localFile))
{
// No state, nothing installed
return new InstalledPackageState();
}
try
{
string yaml = File.ReadAllText("packagestate.yaml");
return deserializer.Deserialize<InstalledPackageState>(yaml);
} catch (Exception ex)
{
throw new PackageException($"Failed to load installed package state: ${ex.Message}", ex);
}
}
public void Save()
{
try
{
var yaml = serializer.Serialize(this);
File.WriteAllText(localFile, yaml);
}
catch (Exception ex)
{
throw new PackageException($"Failed to save installed package state: ${ex.Message}", ex);
}
}
public void MarkInstalled(InstalledPackage installed)
{
InstalledPackage matching = Packages.Where((p) => p.Id == installed.Id).FirstOrDefault();
if (matching != null)
{
// Remove outdated entry
MarkUninstalled(installed.Id);
}
Packages.Add(installed);
}
public void MarkUninstalled(string packageId)
{
Packages.RemoveAll((p) => p.Id == packageId);
}
public static void Clear()
{
if (File.Exists(localFile))
{
File.Delete(localFile);
}
}
}
class PackageState
{
public List<LocalPackage> LocalPackages { get; private set; } = new List<LocalPackage>();
private LocalPackage GetById(string packageId) =>
LocalPackages.Where((p) => p.Remote.Id == packageId).FirstOrDefault();
// Common logic for determining deps by id which need update/install
private IEnumerable<LocalPackage> GetPackagesByIdRelevantForInstallOrUpdate(IEnumerable<string> ids) =>
ids.Select((id) => GetById(id)).Where((p) => p != null && (!p.IsInstalled || p.RequiresUpdate));
public List<LocalPackage> GetPackageDependenciesForInstall(LocalPackage package) =>
GetPackagesByIdRelevantForInstallOrUpdate(package.Remote.Dependencies).ToList();
public List<LocalPackage> PackagesRequiringUpdate()
{
// Direct packages needing update...
List<LocalPackage> directPackages = LocalPackages.Where((p) => p.RequiresUpdate).ToList();
// We must also update any packages we don't currently have installed, that are dependencies of packages we are updating
// (e.g. on update, a new dep has been added to an existing package)
// Need to dedupe dependencies both for common deps
// and also in case we happen to be updating the same package as both a dep and directly
HashSet<string> dedupedIdsToUpdate = new HashSet<string>(directPackages.SelectMany((p) => p.Remote.Dependencies));
dedupedIdsToUpdate.UnionWith(directPackages.Select((p) => p.Remote.Id));
return GetPackagesByIdRelevantForInstallOrUpdate(dedupedIdsToUpdate).ToList();
}
public bool UpdateRequired() => PackagesRequiringUpdate().Count > 0;
public static PackageState Load(Config config)
{
RemotePackageConfig remote = RemotePackageConfig.DownloadPackageConfig(config);
InstalledPackageState local = InstalledPackageState.Load();
// Match local packages to remote ones
List<LocalPackage> result =
(from remotePackage in remote.Packages
join localPackage in local.Packages on remotePackage.Id equals localPackage.Id
select new LocalPackage(remotePackage, localPackage)).ToList();
HashSet<string> matchedIds = new HashSet<string>(result.Select((p) => p.Local.Id));
// Find remote packages that don't exist locally
result.AddRange(remote.Packages
.Where((p) => !matchedIds.Contains(p.Id))
.Select((p) => new LocalPackage(p, null)));
// (Don't care about local packages that don't exist remotely)
return new PackageState
{
LocalPackages = result,
};
}
}
}