-
-
Notifications
You must be signed in to change notification settings - Fork 345
/
CkanModule.cs
769 lines (649 loc) · 26.4 KB
/
CkanModule.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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using Autofac;
using CKAN.Extensions;
using CKAN.Versioning;
using log4net;
using Newtonsoft.Json;
namespace CKAN
{
public class RelationshipDescriptor
{
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public ModuleVersion max_version;
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public ModuleVersion min_version;
//Why is the identifier called name?
public /* required */ string name;
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public ModuleVersion version;
/// <summary>
/// Returns if the other version satisfies this RelationshipDescriptor.
/// If the RelationshipDescriptor has version set it compares against that.
/// Else it uses the {min,max}_version fields treating nulls as unbounded.
/// Note: Uses inclusive inequalities.
/// </summary>
/// <param name="other"></param>
/// <returns>True if other_version is within the bounds</returns>
public bool WithinBounds(ModuleVersion other)
{
// UnmanagedModuleVersions with unknown versions always satisfy the bound
if (other is UnmanagedModuleVersion unmanagedModuleVersion && unmanagedModuleVersion.IsUnknownVersion)
return true;
if (version == null)
{
if (max_version == null && min_version == null)
return true;
var minSat = min_version == null || min_version <= other;
var maxSat = max_version == null || max_version >= other;
if (minSat && maxSat)
return true;
}
else
{
if (version.Equals(other))
return true;
}
return false;
}
/// <summary>
/// Check whether any of the modules in a given list match this descriptor.
/// NOTE: Only proper modules and DLC can be checked for versions!
/// DLLs match all versions, as do "provides" clauses.
/// </summary>
/// <param name="modules">Sequence of modules to consider</param>
/// <param name="dlls">Sequence of DLLs to consider</param>
/// <param name="dlc">DLC to consider</param>
/// <returns>
/// true if any of the modules match this descriptor, false otherwise.
/// </returns>
public bool MatchesAny(
IEnumerable<CkanModule> modules,
HashSet<string> dlls,
IDictionary<string, UnmanagedModuleVersion> dlc
)
{
modules = modules?.AsCollection();
// DLLs are considered to match any version
if (dlls != null && dlls.Contains(name))
{
return true;
}
if (modules != null)
{
// See if anyone else "provides" the target name
// Note that versions can't be checked for "provides" clauses
if (modules.Any(m => m.identifier != name && m.provides != null && m.provides.Contains(name)))
{
return true;
}
// See if the real thing is there
foreach (var m in modules.Where(m => m.identifier == name))
{
if (WithinBounds(m.version))
{
return true;
}
}
}
if (dlc != null)
{
foreach (var d in dlc.Where(i => i.Key == name))
{
if (WithinBounds(d.Value))
{
return true;
}
}
}
return false;
}
/// <summary>
/// A user friendly message for what versions satisfies this descriptor.
/// </summary>
[JsonIgnore]
public string RequiredVersion
{
get
{
if (version != null)
return version.ToString();
return string.Format("between {0} and {1} inclusive.",
min_version != null ? min_version.ToString() : "any version",
max_version != null ? max_version.ToString() : "any version");
}
}
/// <summary>
/// Generate a user readable description of the relationship
/// </summary>
/// <returns>
/// Depending on the version properties, one of:
/// name
/// name version
/// name min_version -- max_version
/// name min_version or later
/// name max_version or earlier
/// </returns>
public override string ToString()
{
return
version != null ? $"{name} {version}"
: min_version != null && max_version != null ? $"{name} {min_version} -- {max_version}"
: min_version != null ? $"{name} {min_version} or later"
: max_version != null ? $"{name} {max_version} or earlier"
: name;
}
}
public class ResourcesDescriptor
{
[JsonProperty("repository", NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(JsonIgnoreBadUrlConverter))]
public Uri repository;
[JsonProperty("homepage", NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(JsonIgnoreBadUrlConverter))]
public Uri homepage;
[JsonProperty("bugtracker", NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(JsonIgnoreBadUrlConverter))]
public Uri bugtracker;
[JsonProperty("spacedock", NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(JsonOldResourceUrlConverter))]
public Uri spacedock;
[JsonProperty("curse", NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(JsonOldResourceUrlConverter))]
public Uri curse;
}
public class DownloadHashesDescriptor
{
[JsonProperty("sha1")]
public string sha1;
[JsonProperty("sha256")]
public string sha256;
}
public class NameComparer : IEqualityComparer<CkanModule>
{
public bool Equals(CkanModule x, CkanModule y)
{
return x.identifier.Equals(y.identifier);
}
public int GetHashCode(CkanModule obj)
{
return obj.identifier.GetHashCode();
}
}
/// <summary>
/// Describes a CKAN module (ie, what's in the CKAN.schema file).
/// </summary>
// Base class for both modules (installed via the CKAN) and bundled
// modules (which are more lightweight)
[JsonObject(MemberSerialization.OptIn)]
public class CkanModule : IEquatable<CkanModule>
{
#region Fields
private static readonly ILog log = LogManager.GetLogger(typeof (CkanModule));
private static readonly string[] required_fields =
{
"spec_version",
"name",
"abstract",
"identifier",
"download",
"license",
"version"
};
// identifier, license, and version are always required, so we know
// what we've got.
[JsonProperty("abstract")]
public string @abstract;
[JsonProperty("description", NullValueHandling = NullValueHandling.Ignore)]
public string description;
// Package type: in spec v1.6 can be either "package" or "metapackage"
[JsonProperty("kind", NullValueHandling = NullValueHandling.Ignore)]
public string kind;
[JsonProperty("author", NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(JsonSingleOrArrayConverter<string>))]
public List<string> author;
[JsonProperty("comment", NullValueHandling = NullValueHandling.Ignore)]
public string comment;
[JsonProperty("conflicts", NullValueHandling = NullValueHandling.Ignore)]
public List<RelationshipDescriptor> conflicts;
[JsonProperty("depends", NullValueHandling = NullValueHandling.Ignore)]
public List<RelationshipDescriptor> depends;
[JsonProperty("download")]
public Uri download;
[JsonProperty("download_size")]
public long download_size;
[JsonProperty("download_hash", NullValueHandling = NullValueHandling.Ignore)]
public DownloadHashesDescriptor download_hash;
[JsonProperty("download_content_type", NullValueHandling = NullValueHandling.Ignore)]
public string download_content_type;
[JsonProperty("identifier", Required = Required.Always)]
public string identifier;
[JsonProperty("ksp_version", NullValueHandling = NullValueHandling.Ignore)]
public KspVersion ksp_version;
[JsonProperty("ksp_version_max", NullValueHandling = NullValueHandling.Ignore)]
public KspVersion ksp_version_max;
[JsonProperty("ksp_version_min", NullValueHandling = NullValueHandling.Ignore)]
public KspVersion ksp_version_min;
[JsonProperty("ksp_version_strict")]
public bool ksp_version_strict = false;
[JsonProperty("license")]
[JsonConverter(typeof(JsonSingleOrArrayConverter<License>))]
public List<License> license;
[JsonProperty("name")]
public string name;
[JsonProperty("provides", NullValueHandling = NullValueHandling.Ignore)]
public List<string> provides;
[JsonProperty("recommends", NullValueHandling = NullValueHandling.Ignore)]
public List<RelationshipDescriptor> recommends;
[JsonProperty("release_status", NullValueHandling = NullValueHandling.Ignore)]
public ReleaseStatus release_status;
[JsonProperty("resources", NullValueHandling = NullValueHandling.Ignore)]
public ResourcesDescriptor resources;
[JsonProperty("suggests", NullValueHandling = NullValueHandling.Ignore)]
public List<RelationshipDescriptor> suggests;
[JsonProperty("version", Required = Required.Always)]
public ModuleVersion version;
[JsonProperty("supports", NullValueHandling = NullValueHandling.Ignore)]
public List<RelationshipDescriptor> supports;
[JsonProperty("install", NullValueHandling = NullValueHandling.Ignore)]
public ModuleInstallDescriptor[] install;
// Used to see if we're compatible with a given game/KSP version or not.
private readonly IGameComparator _comparator;
[JsonIgnore]
[JsonProperty("specVersion", Required = Required.Default)]
private ModuleVersion specVersion;
// We integrated the Module and CkanModule into one class
// Since spec_version was only required for CkanModule before
// this change, we now need to make sure the user is converted
// and has the spec_version's in his installed_modules section
// We should return this to a simple Required.Always field some time in the future
// ~ Postremus, 03.09.2015
[JsonProperty("spec_version")]
public ModuleVersion spec_version
{
get
{
if (specVersion == null)
specVersion = new ModuleVersion("1");
return specVersion;
}
set
{
if (value == null)
specVersion = new ModuleVersion("1");
else
specVersion = value;
}
}
// A list of eveything this mod provides.
public List<string> ProvidesList
{
// TODO: Consider caching this, but not in a way that the serialiser will try and
// serialise it.
get
{
var provides = new List<string> { identifier };
if (this.provides != null)
{
provides.AddRange(this.provides);
}
return provides;
}
}
#endregion
#region Constructors
/// <summary>
/// To be used by test cases and cascaded json deserialisation only,
/// and even then I'm not sure this is a great idea.
/// </summary>
[JsonConstructor]
internal CkanModule()
{
// We don't have this passed in, so we'll ask the service locator
// directly. Yuck.
_comparator = ServiceLocator.Container.Resolve<IGameComparator>();
}
/// <summary>
/// Inflates a CKAN object from a JSON string.
/// </summary>
public CkanModule(string json, IGameComparator comparator)
{
_comparator = comparator;
try
{
// Use the json string to populate our object
JsonConvert.PopulateObject(json, this);
}
catch (JsonException ex)
{
throw new BadMetadataKraken(null, string.Format("JSON deserialization error: {0}", ex.Message), ex);
}
// NOTE: Many of these tests may be better in our Deserialisation handler.
if (!IsSpecSupported())
{
throw new UnsupportedKraken(
String.Format(
"{0} requires CKAN {1}, we can't read it.",
this,
spec_version
)
);
}
// Check everything in the spec if defined.
// TODO: This *can* and *should* be done with JSON attributes!
foreach (string field in required_fields)
{
object value = null;
if (GetType().GetField(field) != null)
{
value = typeof(CkanModule).GetField(field).GetValue(this);
}
else
{
// uh, maybe it is not a field, but a property?
value = typeof(CkanModule).GetProperty(field).GetValue(this, null);
}
if (value == null)
{
// Metapackages are allowed to have no download field
if (field == "download" && IsMetapackage) continue;
string error = String.Format("{0} missing required field {1}", identifier, field);
log.Error(error);
throw new BadMetadataKraken(null, error);
}
}
}
public string serialise()
{
return JsonConvert.SerializeObject(this);
}
[OnDeserialized]
private void DeSerialisationFixes(StreamingContext like_i_could_care)
{
// Make sure our version fields are populated.
// TODO: There's got to be a better way of doing this, right?
// Now see if we've got version with version min/max.
if (ksp_version != null && (ksp_version_max != null || ksp_version_min != null))
{
// KSP version mixed with min/max.
throw new InvalidModuleAttributesException("ksp_version mixed with ksp_version_(min|max)", this);
}
license = license ?? new List<License> { License.UnknownLicense };
@abstract = @abstract ?? string.Empty;
name = name ?? string.Empty;
}
/// <summary>
/// Tries to parse an identifier in the format Modname=version
/// If the module cannot be found in the registry, throws a ModuleNotFoundKraken.
/// </summary>
public static CkanModule FromIDandVersion(IRegistryQuerier registry, string mod, KspVersionCriteria ksp_version)
{
CkanModule module;
Match match = idAndVersionMatcher.Match(mod);
if (match.Success)
{
string ident = match.Groups["mod"].Value;
string version = match.Groups["version"].Value;
module = registry.GetModuleByVersion(ident, version);
if (module == null
|| (ksp_version != null && !module.IsCompatibleKSP(ksp_version)))
throw new ModuleNotFoundKraken(ident, version,
string.Format("Module {0} version {1} not available", ident, version));
}
else
{
module = registry.LatestAvailable(mod, ksp_version)
?? registry.InstalledModule(mod)?.Module;
if (module == null)
throw new ModuleNotFoundKraken(mod, null,
string.Format("Module {0} not installed or available", mod));
}
return module;
}
public static readonly Regex idAndVersionMatcher = new Regex(
@"^(?<mod>[^=]*)=(?<version>.*)$",
RegexOptions.Compiled
);
/// <summary> Generates a CKAN.Meta object given a filename</summary>
public static CkanModule FromFile(string filename)
{
string json = File.ReadAllText(filename);
return FromJson(json);
}
public static void ToFile(CkanModule module, string filename)
{
var json = ToJson(module);
File.WriteAllText(filename, json);
}
/// <summary>
/// Generates a CKAN.META object from a string.
/// Also validates that all required fields are present.
/// Throws a BadMetaDataKraken if any fields are missing.
/// </summary>
public static CkanModule FromJson(string json)
{
log.Debug("Inflating comparator object");
IGameComparator comparator = ServiceLocator.Container.Resolve<IGameComparator>();
log.Debug("Building CkanModule");
return new CkanModule(json, comparator);
}
#endregion
/// <summary>
/// Returns true if we conflict with the given module.
/// </summary>
public bool ConflictsWith(CkanModule module)
{
// We never conflict with ourselves, since we can't be installed at
// the same time as another version of ourselves.
if (module.identifier == this.identifier) return false;
return UniConflicts(this, module) || UniConflicts(module, this);
}
/// <summary>
/// Checks if A conflicts with B, but not if B conflicts with A.
/// Used by ConflictsWith.
/// </summary>
internal static bool UniConflicts(CkanModule mod1, CkanModule mod2)
{
return mod1?.conflicts?.Any(
conflict => conflict.MatchesAny(new CkanModule[] {mod2}, null, null)
) ?? false;
}
/// <summary>
/// Returns true if our mod is compatible with the KSP version specified.
/// </summary>
public bool IsCompatibleKSP(KspVersionCriteria version)
{
log.DebugFormat("Testing if {0} is compatible with KSP {1}", this, version);
return _comparator.Compatible(version, this);
}
/// <summary>
/// Returns a human readable string indicating the highest compatible
/// version of KSP this module will run with. (Eg: 1.0.2,
/// "All versions", etc).
///
/// This is for *human consumption only*, as the strings may change in the
/// future as we support additional locales.
/// </summary>
public string HighestCompatibleKSP()
{
KspVersion v = LatestCompatibleKSP();
if (v.IsAny)
return "All versions";
else
return v.ToString();
}
/// <summary>
/// Returns machine readable object indicating the highest compatible
/// version of KSP this module will run with.
/// </summary>
public KspVersion LatestCompatibleKSP()
{
// Find the highest compatible KSP version
if (ksp_version_max != null)
return ksp_version_max;
else if (ksp_version != null)
return ksp_version;
else
// No upper limit.
return KspVersion.Any;
}
/// <summary>
/// Returns machine readable object indicating the lowest compatible
/// version of KSP this module will run with.
/// </summary>
public KspVersion EarliestCompatibleKSP()
{
// Find the lowest compatible KSP version
if (ksp_version_min != null)
return ksp_version_min;
else if (ksp_version != null)
return ksp_version;
else
// No lower limit.
return KspVersion.Any;
}
/// <summary>
/// Returns true if this module provides the functionality requested.
/// </summary>
public bool DoesProvide(string identifier)
{
return this.identifier == identifier || provides.Contains(identifier);
}
public bool IsMetapackage
{
get { return (!string.IsNullOrEmpty(this.kind) && this.kind == "metapackage"); }
}
protected bool Equals(CkanModule other)
{
return string.Equals(identifier, other.identifier) && version.Equals(other.version);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((CkanModule)obj);
}
public override int GetHashCode()
{
unchecked
{
return (identifier.GetHashCode() * 397) ^ version.GetHashCode();
}
}
bool IEquatable<CkanModule>.Equals(CkanModule other)
{
return Equals(other);
}
public static string ToJson(CkanModule module)
{
return JsonConvert.SerializeObject(module);
}
/// <summary>
/// Returns true if we support at least spec_version of the CKAN spec.
/// </summary>
internal static bool IsSpecSupported(ModuleVersion spec_vesion)
{
// This could be a read-only state variable; do we have those in C#?
ModuleVersion release = new ModuleVersion(Meta.GetVersion(VersionFormat.Short));
return release == null || release.IsGreaterThan(spec_vesion);
}
/// <summary>
/// Returns true if we support the CKAN spec used by this module.
/// </summary>
private bool IsSpecSupported()
{
return IsSpecSupported(spec_version);
}
/// <summary>
/// Returns a standardised name for this module, in the form
/// "identifier-version.zip". For example, `RealSolarSystem-7.3.zip`
/// </summary>
public string StandardName()
{
return StandardName(identifier, version);
}
public static string StandardName(string identifier, ModuleVersion version)
{
// Versions can contain ALL SORTS OF WACKY THINGS! Colons, friggin newlines,
// slashes, and heaven knows what use mod authors try to smoosh into them.
// We'll reduce this down to "friendly" characters, replacing everything else with
// dashes. This doesn't change look-ups, as we use the hash prefix for that.
string version_string = Regex.Replace(version.ToString(), "[^A-Za-z0-9_.-]", "-");
return identifier + "-" + version_string + ".zip";
}
public override string ToString()
{
return string.Format("{0} {1}", identifier, version);
}
public string DescribeInstallStanzas()
{
List<string> descriptions = new List<string>();
foreach (ModuleInstallDescriptor mid in install) {
descriptions.Add(mid.DescribeMatch());
}
return string.Join(", ", descriptions);
}
/// <summary>
/// Return an archive.org URL for this download, or null if it's not there.
/// The filenames look a lot like the filenames in Net.Cache, but don't be fooled!
/// Here it's the first 8 characters of the SHA1 of the DOWNLOADED FILE, not the URL!
/// </summary>
public Uri InternetArchiveDownload
{
get
{
string verStr = version.ToString().Replace(':', '-');
// Some alternate registry repositories don't set download_hash
return (download_hash?.sha1 != null && license.All(l => l.Redistributable))
? new Uri(
$"https://archive.org/download/{identifier}-{verStr}/{download_hash.sha1.Substring(0, 8)}-{identifier}-{verStr}.zip")
: null;
}
}
/// <summary>
/// Format a byte count into readable file size
/// </summary>
/// <param name="bytes">Number of bytes in a file</param>
/// <returns>
/// ### bytes or ### KB or ### MB or ### GB
/// </returns>
public static string FmtSize(long bytes)
{
const double K = 1024;
if (bytes < K) {
return $"{bytes} B";
} else if (bytes < K * K) {
return $"{bytes / K :N1} KB";
} else if (bytes < K * K * K) {
return $"{bytes / K / K :N1} MB";
} else {
return $"{bytes / K / K / K :N1} GB";
}
}
}
public class InvalidModuleAttributesException : Exception
{
private readonly CkanModule module;
private readonly string why;
public InvalidModuleAttributesException(string why, CkanModule module = null)
: base(why)
{
this.why = why;
this.module = module;
}
public override string ToString()
{
string modname = "unknown";
if (module != null)
{
modname = module.identifier;
}
return string.Format("[InvalidModuleAttributesException] {0} in {1}", why, modname);
}
}
}