-
-
Notifications
You must be signed in to change notification settings - Fork 348
/
Registry.cs
1187 lines (1017 loc) · 44.4 KB
/
Registry.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
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Transactions;
using CKAN.Extensions;
using CKAN.Versioning;
using log4net;
using Newtonsoft.Json;
namespace CKAN
{
/// <summary>
/// This is the CKAN registry. All the modules that we know about or have installed
/// are contained in here.
/// </summary>
// TODO: It would be *great* for the registry to have a 'dirty' bit, that records if
// anything has changed. But that would involve catching access to a lot of the data
// structures we pass back, and we're not doing that yet.
public class Registry : IEnlistmentNotification, IRegistryQuerier
{
[JsonIgnore] private const int LATEST_REGISTRY_VERSION = 3;
[JsonIgnore] private static readonly ILog log = LogManager.GetLogger(typeof (Registry));
[JsonProperty] private int registry_version;
[JsonProperty("sorted_repositories")]
private SortedDictionary<string, Repository> repositories; // name => Repository
// TODO: These may be good as custom types, especially those which process
// paths (and flip from absolute to relative, and vice-versa).
[JsonProperty] internal Dictionary<string, AvailableModule> available_modules;
[JsonProperty] private Dictionary<string, string> installed_dlls; // name => path
[JsonProperty] private Dictionary<string, InstalledModule> installed_modules;
[JsonProperty] private Dictionary<string, string> installed_files; // filename => module
[JsonProperty] public readonly SortedDictionary<string, int> download_counts = new SortedDictionary<string, int>();
public int? DownloadCount(string identifier)
{
int count;
if (download_counts.TryGetValue(identifier, out count))
{
return count;
}
return null;
}
public void SetDownloadCounts(SortedDictionary<string, int> counts)
{
if (counts != null)
{
foreach (var kvp in counts)
{
download_counts[kvp.Key] = kvp.Value;
}
}
}
// Index of which mods provide what, format:
// providers[provided] = { provider1, provider2, ... }
// Built by BuildProvidesIndex, makes LatestAvailableWithProvides much faster.
[JsonIgnore] private Dictionary<string, HashSet<AvailableModule>> providers
= new Dictionary<string, HashSet<AvailableModule>>();
/// <summary>
/// A map between module identifiers and versions for official DLC that are installed.
/// </summary>
/// <remarks>
/// This shouldn't have a <see cref="JsonPropertyAttribute"/> as detection at runtime should be fast.
/// </remarks>
private readonly Dictionary<string, UnmanagedModuleVersion> _installedDlcModules =
new Dictionary<string, UnmanagedModuleVersion>();
[JsonIgnore] private string transaction_backup;
/// <summary>
/// Returns all the activated registries, sorted by priority and name
/// </summary>
[JsonIgnore] public SortedDictionary<string, Repository> Repositories
{
get { return this.repositories; }
// TODO writable only so it can be initialized, better ideas welcome
set { this.repositories = value; }
}
/// <summary>
/// Returns all the installed modules
/// </summary>
[JsonIgnore] public IEnumerable<InstalledModule> InstalledModules
{
get { return installed_modules.Values; }
}
/// <summary>
/// Returns the names of installed DLLs.
/// </summary>
[JsonIgnore] public IEnumerable<string> InstalledDlls
{
get { return installed_dlls.Keys; }
}
[JsonIgnore] public IDictionary<string, UnmanagedModuleVersion> InstalledDlc
{
get { return _installedDlcModules; }
}
/// <summary>
/// Find installed modules that are not compatible with the given versions
/// </summary>
/// <param name="crit">Version criteria against which to check modules</param>
/// <returns>
/// Installed modules that are incompatible, if any
/// </returns>
public IEnumerable<InstalledModule> IncompatibleInstalled(KspVersionCriteria crit)
{
return installed_modules.Values
.Where(im => !im.Module.IsCompatibleKSP(crit));
}
#region Registry Upgrades
[OnDeserialized]
private void DeSerialisationFixes(StreamingContext context)
{
// Our context is our KSP install.
KSP ksp = (KSP)context.Context;
// Older registries didn't have the installed_files list, so we create one
// if absent.
if (installed_files == null)
{
log.Warn("Older registry format detected, adding installed files manifest...");
ReindexInstalled();
}
// If we have no registry version at all, then we're from the pre-release period.
// We would check for a null here, but ints *can't* be null.
if (registry_version == 0)
{
log.Warn("Older registry format detected, normalising paths...");
var normalised_installed_files = new Dictionary<string, string>();
foreach (KeyValuePair<string,string> tuple in installed_files)
{
string path = KSPPathUtils.NormalizePath(tuple.Key);
if (Path.IsPathRooted(path))
{
path = ksp.ToRelativeGameDir(path);
normalised_installed_files[path] = tuple.Value;
}
else
{
// Already relative.
normalised_installed_files[path] = tuple.Value;
}
}
installed_files = normalised_installed_files;
// Now update all our module file manifests.
foreach (InstalledModule module in installed_modules.Values)
{
module.Renormalise(ksp);
}
// Our installed dlls have contained relative paths since forever,
// and the next `ckan scan` will fix them anyway. (We can't scan here,
// because that needs a registry, and we chicken-egg.)
log.Warn("Registry upgrade complete");
}
// Fix control lock, which previously was indexed with an invalid identifier.
if (registry_version < 2)
{
InstalledModule control_lock_entry;
const string old_ident = "001ControlLock";
const string new_ident = "ControlLock";
if (installed_modules.TryGetValue("001ControlLock", out control_lock_entry))
{
if (ksp == null)
{
throw new Kraken("Internal bug: No KSP instance provided on registry deserialisation");
}
log.WarnFormat("Older registry detected. Reindexing {0} as {1}. This may take a moment.", old_ident, new_ident);
// Remove old record.
installed_modules.Remove(old_ident);
// Extract the old module metadata
CkanModule control_lock_mod = control_lock_entry.Module;
// Change to the correct ident.
control_lock_mod.identifier = new_ident;
// Prepare to re-index.
var new_control_lock_installed = new InstalledModule(
ksp,
control_lock_mod,
control_lock_entry.Files
);
// Re-insert into registry.
installed_modules[new_control_lock_installed.identifier] = new_control_lock_installed;
// Re-index files.
ReindexInstalled();
}
}
// If we spot a default repo with the old .zip URL, flip it to the new .tar.gz URL
// Any other repo we leave *as-is*, even if it's the github meta-repo, as it's been
// custom-added by our user.
Repository default_repo;
var oldDefaultRepo = new Uri("https://github.com/KSP-CKAN/CKAN-meta/archive/master.zip");
if (repositories != null && repositories.TryGetValue(Repository.default_ckan_repo_name, out default_repo) && default_repo.uri == oldDefaultRepo)
{
log.InfoFormat("Updating default metadata URL from {0} to {1}", oldDefaultRepo, Repository.default_ckan_repo_uri);
repositories["default"].uri = Repository.default_ckan_repo_uri;
}
registry_version = LATEST_REGISTRY_VERSION;
BuildProvidesIndex();
}
/// <summary>
/// Rebuilds our master index of installed_files.
/// Called on registry format updates, but safe to be triggered at any time.
/// </summary>
public void ReindexInstalled()
{
installed_files = new Dictionary<string, string>();
foreach (InstalledModule module in installed_modules.Values)
{
foreach (string file in module.Files)
{
// Register each file we know about as belonging to the given module.
installed_files[file] = module.identifier;
}
}
}
/// <summary>
/// Do we what we can to repair/preen the registry.
/// </summary>
public void Repair()
{
ReindexInstalled();
}
#endregion
#region Constructors
public Registry(
Dictionary<string, InstalledModule> installed_modules,
Dictionary<string, string> installed_dlls,
Dictionary<string, AvailableModule> available_modules,
Dictionary<string, string> installed_files,
SortedDictionary<string, Repository> repositories)
{
// Is there a better way of writing constructors than this? Srsly?
this.installed_modules = installed_modules;
this.installed_dlls = installed_dlls;
this.available_modules = available_modules;
this.installed_files = installed_files;
this.repositories = repositories;
registry_version = LATEST_REGISTRY_VERSION;
BuildProvidesIndex();
}
// If deserialsing, we don't want everything put back directly,
// thus making sure our version number is preserved, letting us
// detect registry version upgrades.
[JsonConstructor]
private Registry()
{
}
public static Registry Empty()
{
return new Registry(
new Dictionary<string, InstalledModule>(),
new Dictionary<string, string>(),
new Dictionary<string, AvailableModule>(),
new Dictionary<string, string>(),
new SortedDictionary<string, Repository>()
);
}
#endregion
#region Transaction Handling
// We use this to record which transaction we're in.
private string enlisted_tx;
// This *doesn't* get called when we get enlisted in a Tx, it gets
// called when we're about to commit a transaction. We can *probably*
// get away with calling .Done() here and skipping the commit phase,
// but I'm not sure if we'd get InDoubt signalling if we did that.
public void Prepare(PreparingEnlistment preparingEnlistment)
{
log.Debug("Registry prepared to commit transaction");
preparingEnlistment.Prepared();
}
public void InDoubt(Enlistment enlistment)
{
// In doubt apparently means we don't know if we've committed or not.
// Since our TxFileMgr treats this as a rollback, so do we.
log.Warn("Transaction involving registry in doubt.");
Rollback(enlistment);
}
public void Commit(Enlistment enlistment)
{
// Hooray! All Tx participants have signalled they're ready.
// So we're done, and can clear our resources.
enlisted_tx = null;
transaction_backup = null;
enlistment.Done();
log.Debug("Registry transaction committed");
// TODO: Should we save to disk at the end of a Tx?
// TODO: If so, we should abort if we find a save that's while a Tx is in progress?
//
// In either case, do we want the registry_manager to be Tx aware?
}
public void Rollback(Enlistment enlistment)
{
log.Info("Aborted transaction, rolling back in-memory registry changes.");
// In theory, this should put everything back the way it was, overwriting whatever
// we had previously.
var options = new JsonSerializerSettings {ObjectCreationHandling = ObjectCreationHandling.Replace};
JsonConvert.PopulateObject(transaction_backup, this, options);
enlisted_tx = null;
transaction_backup = null;
enlistment.Done();
}
private void SaveState()
{
// Hey, you know what's a great way to back-up your own object?
// JSON. ;)
transaction_backup = JsonConvert.SerializeObject(this, Formatting.None);
log.Debug("State saved");
}
/// <summary>
/// "Pardon me, but I couldn't help but overhear you're in a Transaction..."
///
/// Adds our registry to the current transaction. This should be called whenever we
/// do anything which may dirty the registry.
/// </summary>
//
// http://wondermark.com/1k62/
private void SealionTransaction()
{
if (Transaction.Current != null)
{
string current_tx = Transaction.Current.TransactionInformation.LocalIdentifier;
if (enlisted_tx == null)
{
log.Debug("Pardon me, but I couldn't help overhear you're in a transaction...");
Transaction.Current.EnlistVolatile(this, EnlistmentOptions.None);
SaveState();
enlisted_tx = current_tx;
}
else if (enlisted_tx != current_tx)
{
log.Error("CKAN registry does not support nested transactions.");
throw new TransactionalKraken("CKAN registry does not support nested transactions.");
}
// If we're here, it's a transaction we're already participating in,
// so do nothing.
}
}
#endregion
public void SetAllAvailable(IEnumerable<CkanModule> newAvail)
{
SealionTransaction();
// Clear current modules
available_modules = new Dictionary<string, AvailableModule>();
providers.Clear();
// Add the new modules
foreach (CkanModule module in newAvail)
{
AddAvailable(module);
}
}
/// <summary>
/// Check whether the available_modules list is empty
/// </summary>
/// <returns>
/// True if we have at least one available mod, false otherwise.
/// </returns>
public bool HasAnyAvailable()
{
return available_modules.Count > 0;
}
/// <summary>
/// Mark a given module as available.
/// </summary>
public void AddAvailable(CkanModule module)
{
SealionTransaction();
var identifier = module.identifier;
// If we've never seen this module before, create an entry for it.
if (!available_modules.ContainsKey(identifier))
{
log.DebugFormat("Adding new available module {0}", identifier);
available_modules[identifier] = new AvailableModule(identifier);
}
// Now register the actual version that we have.
// (It's okay to have multiple versions of the same mod.)
log.DebugFormat("Available: {0} version {1}", identifier, module.version);
available_modules[identifier].Add(module);
BuildProvidesIndexFor(available_modules[identifier]);
}
/// <summary>
/// Remove the given module from the registry of available modules.
/// Does *nothing* if the module was not present to begin with.
/// </summary>
public void RemoveAvailable(string identifier, ModuleVersion version)
{
AvailableModule availableModule;
if (available_modules.TryGetValue(identifier, out availableModule))
{
SealionTransaction();
availableModule.Remove(version);
}
}
/// <summary>
/// Removes the given module from the registry of available modules.
/// Does *nothing* if the module was not present to begin with.</summary>
public void RemoveAvailable(CkanModule module)
{
RemoveAvailable(module.identifier, module.version);
}
/// <summary>
/// <see cref="IRegistryQuerier.Available"/>
/// </summary>
public List<CkanModule> Available(KspVersionCriteria ksp_version)
{
var candidates = new List<string>(available_modules.Keys);
var compatible = new List<CkanModule>();
// It's nice to see things in alphabetical order, so sort our keys first.
candidates.Sort();
// Now find what we can give our user.
foreach (string candidate in candidates)
{
CkanModule available = LatestAvailable(candidate, ksp_version);
if (available != null
&& allDependenciesCompatible(available, ksp_version))
{
compatible.Add(available);
}
}
return compatible;
}
/// <summary>
/// <see cref="IRegistryQuerier.Incompatible"/>
/// </summary>
public List<CkanModule> Incompatible(KspVersionCriteria ksp_version)
{
var candidates = new List<string>(available_modules.Keys);
var incompatible = new List<CkanModule>();
// It's nice to see things in alphabetical order, so sort our keys first.
candidates.Sort();
// Now find what we can give our user.
foreach (string candidate in candidates)
{
CkanModule available = LatestAvailable(candidate, ksp_version);
// If a mod is available, it might still have incompatible dependencies.
if (available == null
|| !allDependenciesCompatible(available, ksp_version))
{
incompatible.Add(LatestAvailable(candidate, null));
}
}
return incompatible;
}
private bool allDependenciesCompatible(CkanModule mod, KspVersionCriteria ksp_version)
{
// we need to check that we can get everything we depend on
if (mod.depends != null)
{
foreach (RelationshipDescriptor dependency in mod.depends)
{
try
{
if (!dependency.MatchesAny(null, InstalledDlls.ToHashSet(), InstalledDlc)
&& !dependency.LatestAvailableWithProvides(this, ksp_version).Any())
{
return false;
}
}
catch (KeyNotFoundException e)
{
log.ErrorFormat("Cannot find available version with provides for {0} in registry", dependency.ToString());
throw e;
}
catch (ModuleNotFoundKraken)
{
return false;
}
}
}
return true;
}
/// <summary>
/// <see cref = "IRegistryQuerier.LatestAvailable" />
/// </summary>
// TODO: Consider making this internal, because practically everything should
// be calling LatestAvailableWithProvides()
public CkanModule LatestAvailable(
string module,
KspVersionCriteria ksp_version,
RelationshipDescriptor relationship_descriptor = null)
{
log.DebugFormat("Finding latest available for {0}", module);
// TODO: Check user's stability tolerance (stable, unstable, testing, etc)
try
{
return available_modules[module].Latest(ksp_version, relationship_descriptor);
}
catch (KeyNotFoundException)
{
throw new ModuleNotFoundKraken(module);
}
}
public List<CkanModule> AllAvailable(string module)
{
log.DebugFormat("Finding all available versions for {0}", module);
try
{
return available_modules[module].AllAvailable();
}
catch (KeyNotFoundException)
{
throw new ModuleNotFoundKraken(module);
}
}
/// <summary>
/// Get full JSON metadata string for a mod's available versions
/// </summary>
/// <param name="identifier">Name of the mod to look up</param>
/// <returns>
/// JSON formatted string for all the available versions of the mod
/// </returns>
public string GetAvailableMetadata(string identifier)
{
try
{
return available_modules[identifier].FullMetadata();
}
catch
{
return null;
}
}
/// <summary>
/// Return the latest game version compatible with the given mod.
/// </summary>
/// <param name="identifier">Name of mod to check</param>
public KspVersion LatestCompatibleKSP(string identifier)
{
return available_modules.ContainsKey(identifier)
? available_modules[identifier].LatestCompatibleKSP()
: null;
}
/// <summary>
/// Find the minimum and maximum mod versions and compatible game versions
/// for a list of modules (presumably different versions of the same mod).
/// </summary>
/// <param name="modVersions">The modules to inspect</param>
/// <param name="minMod">Return parameter for the lowest mod version</param>
/// <param name="maxMod">Return parameter for the highest mod version</param>
/// <param name="minKsp">Return parameter for the lowest game version</param>
/// <param name="maxKsp">Return parameter for the highest game version</param>
public static void GetMinMaxVersions(IEnumerable<CkanModule> modVersions,
out ModuleVersion minMod, out ModuleVersion maxMod,
out KspVersion minKsp, out KspVersion maxKsp)
{
minMod = maxMod = null;
minKsp = maxKsp = null;
foreach (CkanModule rel in modVersions.Where(v => v != null))
{
if (minMod == null || minMod > rel.version)
{
minMod = rel.version;
}
if (maxMod == null || maxMod < rel.version)
{
maxMod = rel.version;
}
KspVersion relMin = rel.EarliestCompatibleKSP();
KspVersion relMax = rel.LatestCompatibleKSP();
if (minKsp == null || !minKsp.IsAny && (minKsp > relMin || relMin.IsAny))
{
minKsp = relMin;
}
if (maxKsp == null || !maxKsp.IsAny && (maxKsp < relMax || relMax.IsAny))
{
maxKsp = relMax;
}
}
}
/// <summary>
/// Generate the providers index so we can find providing modules quicker
/// </summary>
private void BuildProvidesIndex()
{
providers.Clear();
foreach (AvailableModule am in available_modules.Values)
{
BuildProvidesIndexFor(am);
}
}
/// <summary>
/// Ensure one AvailableModule is present in the right spots in the providers index
/// </summary>
private void BuildProvidesIndexFor(AvailableModule am)
{
foreach (CkanModule m in am.AllAvailable())
{
foreach (string provided in m.ProvidesList)
{
HashSet<AvailableModule> provs = null;
if (providers.TryGetValue(provided, out provs))
provs.Add(am);
else
providers.Add(provided, new HashSet<AvailableModule>() { am });
}
}
}
/// <summary>
/// <see cref="IRegistryQuerier.LatestAvailableWithProvides" />
/// </summary>
public List<CkanModule> LatestAvailableWithProvides(
string module,
KspVersionCriteria ksp_version,
RelationshipDescriptor relationship_descriptor = null,
IEnumerable<CkanModule> toInstall = null)
{
HashSet<AvailableModule> provs;
if (providers.TryGetValue(module, out provs))
{
// For each AvailableModule, we want the latest one matching our constraints
return provs
.Select(am => am.Latest(
ksp_version,
relationship_descriptor,
InstalledModules.Select(im => im.Module),
toInstall
))
.Where(m => m?.ProvidesList?.Contains(module) ?? false)
.ToList();
}
else
{
// Nothing provides this, return empty list
return new List<CkanModule>();
}
}
/// <summary>
/// Returns the specified CkanModule with the version specified,
/// or null if it does not exist.
/// <see cref = "IRegistryQuerier.GetModuleByVersion" />
/// </summary>
public CkanModule GetModuleByVersion(string ident, ModuleVersion version)
{
log.DebugFormat("Trying to find {0} version {1}", ident, version);
if (!available_modules.ContainsKey(ident))
{
return null;
}
AvailableModule available = available_modules[ident];
return available.ByVersion(version);
}
/// <summary>
/// Register the supplied module as having been installed, thereby keeping
/// track of its metadata and files.
/// </summary>
public void RegisterModule(CkanModule mod, IEnumerable<string> absolute_files, KSP ksp)
{
SealionTransaction();
// But we also want to keep track of all its files.
// We start by checking to see if any files are owned by another mod,
// if so, we abort with a list of errors.
var inconsistencies = new List<string>();
// We always work with relative files, so let's get some!
IEnumerable<string> relative_files = absolute_files.Select(x => ksp.ToRelativeGameDir(x));
// For now, it's always cool if a module wants to register a directory.
// We have to flip back to absolute paths to actually test this.
foreach (string file in relative_files.Where(file => !Directory.Exists(ksp.ToAbsoluteGameDir(file))))
{
string owner;
if (installed_files.TryGetValue(file, out owner))
{
// Woah! Registering an already owned file? Not cool!
// (Although if it existed, we should have thrown a kraken well before this.)
inconsistencies.Add(
string.Format("{0} wishes to install {1}, but this file is registered to {2}",
mod.identifier, file, owner
));
}
}
if (inconsistencies.Count > 0)
{
throw new InconsistentKraken(inconsistencies);
}
// If everything is fine, then we copy our files across. By not doing this
// in the loop above, we make sure we don't have a half-registered module
// when we throw our exceptinon.
// This *will* result in us overwriting who owns a directory, and that's cool,
// directories aren't really owned like files are. However because each mod maintains
// its own list of files, we'll remove directories when the last mod using them
// is uninstalled.
foreach (string file in relative_files)
{
installed_files[file] = mod.identifier;
}
// Finally, register our module proper.
var installed = new InstalledModule(ksp, mod, relative_files);
installed_modules.Add(mod.identifier, installed);
}
/// <summary>
/// Deregister a module, which must already have its files removed, thereby
/// forgetting abouts its metadata and files.
///
/// Throws an InconsistentKraken if not all files have been removed.
/// </summary>
public void DeregisterModule(KSP ksp, string module)
{
SealionTransaction();
var inconsistencies = new List<string>();
var absolute_files = installed_modules[module].Files.Select(ksp.ToAbsoluteGameDir);
// Note, this checks to see if a *file* exists; it doesn't
// trigger on directories, which we allow to still be present
// (they may be shared by multiple mods.
foreach (var absolute_file in absolute_files.Where(File.Exists))
{
inconsistencies.Add(string.Format(
"{0} is registered to {1} but has not been removed!",
absolute_file, module));
}
if (inconsistencies.Count > 0)
{
// Uh oh, what mess have we got ourselves into now, Inconsistency Kraken?
throw new InconsistentKraken(inconsistencies);
}
// Okay, all the files are gone. Let's clear our metadata.
foreach (string rel_file in installed_modules[module].Files)
{
installed_files.Remove(rel_file);
}
// Bye bye, module, it's been nice having you visit.
installed_modules.Remove(module);
}
/// <summary>
/// Registers the given DLL as having been installed. This provides some support
/// for pre-CKAN modules.
///
/// Does nothing if the DLL is already part of an installed module.
/// </summary>
public void RegisterDll(KSP ksp, string absolute_path)
{
SealionTransaction();
string relative_path = ksp.ToRelativeGameDir(absolute_path);
string owner;
if (installed_files.TryGetValue(relative_path, out owner))
{
log.InfoFormat(
"Not registering {0}, it belongs to {1}",
relative_path,
owner
);
return;
}
// http://xkcd.com/208/
// This regex works great for things like GameData/Foo/Foo-1.2.dll
Match match = Regex.Match(
relative_path, @"
^GameData/ # DLLs only live in GameData
(?:.*/)? # Intermediate paths (ending with /)
(?<modname>[^.]+) # Our DLL name, up until the first dot.
.*\.dll$ # Everything else, ending in dll
",
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace
);
string modName = match.Groups["modname"].Value;
if (modName.Length == 0)
{
log.WarnFormat("Attempted to index {0} which is not a DLL", relative_path);
return;
}
log.InfoFormat("Registering {0} from {1}", modName, relative_path);
// We're fine if we overwrite an existing key.
installed_dlls[modName] = relative_path;
}
/// <summary>
/// Clears knowledge of all DLLs from the registry.
/// </summary>
public void ClearDlls()
{
SealionTransaction();
installed_dlls = new Dictionary<string, string>();
}
public void RegisterDlc(string identifier, UnmanagedModuleVersion version)
{
_installedDlcModules[identifier] = version;
}
public void ClearDlc()
{
_installedDlcModules.Clear();
}
/// <summary>
/// <see cref = "IRegistryQuerier.Installed" />
/// </summary>
public Dictionary<string, ModuleVersion> Installed(bool withProvides = true)
{
var installed = new Dictionary<string, ModuleVersion>();
// Index our DLLs, as much as we dislike them.
foreach (var dllinfo in installed_dlls)
{
installed[dllinfo.Key] = new UnmanagedModuleVersion(null);
}
// Index our provides list, so users can see virtual packages
if (withProvides)
{
foreach (var provided in Provided())
{
installed[provided.Key] = provided.Value;
}
}
// Index our installed modules (which may overwrite the installed DLLs and provides)
foreach (var modinfo in installed_modules)
{
installed[modinfo.Key] = modinfo.Value.Module.version;
}
// Index our detected DLC (which overwrites everything)
foreach (var i in _installedDlcModules)
{
installed[i.Key] = i.Value;
}
return installed;
}
/// <summary>
/// <see cref = "IRegistryQuerier.InstalledModule" />
/// </summary>
public InstalledModule InstalledModule(string module)
{
// In theory, someone could then modify the data they get back from
// this, so we sea-lion just in case.
SealionTransaction();
InstalledModule installedModule;
return installed_modules.TryGetValue(module, out installedModule) ? installedModule : null;
}
/// <summary>
/// Returns a dictionary of provided (virtual) modules, and a
/// ProvidesVersion indicating what provides them.
/// </summary>
// TODO: In the future it would be nice to cache this list, and mark it for rebuild
// if our installed modules change.
internal Dictionary<string, ProvidesModuleVersion> Provided()
{
var installed = new Dictionary<string, ProvidesModuleVersion>();
foreach (var modinfo in installed_modules)
{
CkanModule module = modinfo.Value.Module;
// Skip if this module provides nothing.
if (module.provides == null)
{
continue;
}
foreach (string provided in module.provides)
{
installed[provided] = new ProvidesModuleVersion(module.identifier, module.version.ToString());
}
}
return installed;
}
/// <summary>
/// <see cref = "IRegistryQuerier.InstalledVersion" />
/// </summary>
public ModuleVersion InstalledVersion(string modIdentifier, bool with_provides=true)
{
InstalledModule installedModule;
// If it's in our DLC registry, return that
if (_installedDlcModules.ContainsKey(modIdentifier))
{
return _installedDlcModules[modIdentifier];
}
// If it's genuinely installed, return the details we have.