forked from KSP-CKAN/CKAN-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModuleInstaller.cs
934 lines (788 loc) · 36.7 KB
/
ModuleInstaller.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Transactions;
using ChinhDo.Transactions;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
using log4net;
namespace CKAN
{
public delegate void ModuleInstallerReportModInstalled(CkanModule module);
public struct InstallableFile
{
public ZipEntry source;
public string destination;
public bool makedir;
}
public class ModuleInstaller
{
public IUser User { get; set; }
// To allow the ModuleInstaller to work on multiple KSP instances, keep a list of each ModuleInstaller and return the correct one upon request.
private static SortedList<string, ModuleInstaller> instances = new SortedList<string, ModuleInstaller>();
private static readonly ILog log = LogManager.GetLogger(typeof(ModuleInstaller));
private static readonly TxFileManager file_transaction = new TxFileManager ();
private RegistryManager registry_manager;
private KSP ksp;
public ModuleInstallerReportModInstalled onReportModInstalled = null;
// Our own cache is that of the KSP instance we're using.
public NetFileCache Cache
{
get
{
return ksp.Cache;
}
}
// Constructor
private ModuleInstaller(KSP ksp, IUser user)
{
User = user;
this.ksp = ksp;
registry_manager = RegistryManager.Instance(ksp);
log.DebugFormat("Creating ModuleInstaller for {0}", ksp.GameDir());
}
/// <summary>
/// Gets the ModuleInstaller instance associated with the passed KSP instance. Creates a new ModuleInstaller instance if none exists.
/// </summary>
/// <returns>The ModuleInstaller instance.</returns>
/// <param name="ksp_instance">Current KSP instance.</param>
/// <param name="user">IUser implementation.</param>
public static ModuleInstaller GetInstance(KSP ksp_instance, IUser user)
{
ModuleInstaller instance = null;
// Check in the list of instances if we have already created a ModuleInstaller instance for this KSP instance.
if (!instances.TryGetValue(ksp_instance.GameDir().ToLower(), out instance))
{
// Create a new instance and insert it in the static list.
instance = new ModuleInstaller(ksp_instance, user);
instances.Add(ksp_instance.GameDir().ToLower(), instance);
}
return instance;
}
/// <summary>
/// Downloads the given mod to the cache. Returns the filename it was saved to.
/// </summary>
public string Download(Uri url, string filename)
{
User.RaiseProgress(String.Format("Downloading \"{0}\"", url), 0);
return Download(url, filename, Cache);
}
/// <summary>
/// Downloads the given mod to the cache. Returns the filename it was saved to.
/// </summary>
public static string Download(Uri url, string filename, NetFileCache cache)
{
log.Info("Downloading " + filename);
string tmp_file = Net.Download(url);
return cache.Store(url, tmp_file, filename, true);
}
/// <summary>
/// Returns the path to a cached copy of a module if it exists, or downloads
/// and returns the downloaded copy otherwise.
///
/// If no filename is provided, the module's standard name will be used.
/// Chcecks the CKAN cache first.
/// </summary>
public string CachedOrDownload(CkanModule module, string filename = null)
{
return CachedOrDownload(module.identifier, module.version, module.download, Cache, filename);
}
/// <summary>
/// Returns the path to a cached copy of a module if it exists, or downloads
/// and returns the downloaded copy otherwise.
///
/// If no filename is provided, the module's standard name will be used.
/// Chcecks the CKAN cache first.
/// </summary>
public string CachedOrDownload(string identifier, Version version, Uri url, string filename = null)
{
return CachedOrDownload(identifier, version, url, Cache, filename);
}
/// <summary>
/// Returns the path to a cached copy of a module if it exists, or downloads
/// and returns the downloaded copy otherwise.
///
/// If no filename is provided, the module's standard name will be used.
/// Chcecks provided cache first.
/// </summary>
public static string CachedOrDownload(string identifier, Version version, Uri url, NetFileCache cache, string filename = null)
{
if (filename == null)
{
filename = CkanModule.StandardName(identifier, version);
}
string full_path = cache.GetCachedZip(url);
if (full_path == null)
{
return Download(url, filename, cache);
}
log.DebugFormat("Using {0} (cached)", filename);
return full_path;
}
public void InstallList(
List<string> modules,
RelationshipResolverOptions options,
NetAsyncDownloader downloader = null
)
{
var resolver = new RelationshipResolver(modules, options, registry_manager.registry, ksp.Version());
List<CkanModule> modsToInstall = resolver.ModList();
InstallList(modsToInstall, options, downloader = null);
}
/// <summary>
/// Installs all modules given a list of identifiers as a transaction. Resolves dependencies.
/// This *will* save the registry at the end of operation.
///
/// Propagates a BadMetadataKraken if our install metadata is bad.
/// Propagates a FileExistsKraken if we were going to overwrite a file.
/// Propagates a CancelledActionKraken if the user cancelled the install.
/// </summary>
//
// TODO: Break this up into smaller pieces! It's huge!
public void InstallList(
ICollection<CkanModule> modules,
RelationshipResolverOptions options,
NetAsyncDownloader downloader = null
)
{
var resolver = new RelationshipResolver(modules, options, registry_manager.registry, ksp.Version());
List<CkanModule> modsToInstall = resolver.ModList();
List<CkanModule> downloads = new List<CkanModule> ();
// TODO: All this user-stuff should be happening in another method!
// We should just be installing mods as a transaction.
User.RaiseMessage("About to install...\n");
foreach (CkanModule module in modsToInstall)
{
if (!ksp.Cache.IsCachedZip(module.download))
{
User.RaiseMessage(" * {0}", module);
downloads.Add(module);
}
else
{
User.RaiseMessage(" * {0} (cached)", module);
}
}
bool ok = User.RaiseYesNoDialog("\nContinue?");
if (!ok)
{
throw new CancelledActionKraken("User declined install list");
}
User.RaiseMessage(String.Empty); // Just to look tidy.
if (downloads.Count > 0)
{
if (downloader == null)
{
downloader = new NetAsyncDownloader(User);
}
downloader.DownloadModules(ksp.Cache, downloads);
}
// We're about to install all our mods; so begin our transaction.
using (TransactionScope transaction = CkanTransaction.CreateTransactionScope())
{
for (int i = 0; i < modsToInstall.Count; i++)
{
int percent_complete = (i * 100) / modsToInstall.Count;
User.RaiseProgress(String.Format("Installing mod \"{0}\"", modsToInstall[i]),
percent_complete);
Install(modsToInstall[i]);
}
User.RaiseProgress("Updating registry", 70);
registry_manager.Save(!options.without_enforce_consistency);
User.RaiseProgress("Commiting filesystem changes", 80);
transaction.Complete();
}
// We can scan GameData as a separate transaction. Installing the mods
// leaves everything consistent, and this is just gravy. (And ScanGameData
// acts as a Tx, anyway, so we don't need to provide our own.)
User.RaiseProgress("Rescanning GameData", 90);
if (!options.without_enforce_consistency)
{
ksp.ScanGameData();
}
User.RaiseProgress("Done!\n", 100);
}
/// <summary>
/// Returns the module contents if and only if we have it
/// available in our cache. Returns null, otherwise.
///
/// Intended for previews.
/// </summary>
// TODO: Return files relative to GameRoot
public IEnumerable<string> GetModuleContentsList(CkanModule module)
{
string filename = ksp.Cache.GetCachedZip(module.download);
if (filename == null)
{
return null;
}
return FindInstallableFiles(module, filename, ksp)
.Select(x => x.destination);
}
/// <summary>
/// Install our mod from the filename supplied.
/// If no file is supplied, we will check the cache or throw FileNotFoundKraken.
/// Does *not* resolve dependencies; this actually does the heavy listing.
/// Does *not* save the registry.
/// Do *not* call this directly, use InstallList() instead.
///
/// Propagates a BadMetadataKraken if our install metadata is bad.
/// Propagates a FileExistsKraken if we were going to overwrite a file.
/// Throws a FileNotFoundKraken if we can't find the downloaded module.
///
/// </summary>
//
// TODO: The name of this and InstallModule() need to be made more distinctive.
private void Install(CkanModule module, string filename = null)
{
CheckMetapackageInstallationKraken(module);
Version version = registry_manager.registry.InstalledVersion(module.identifier);
// TODO: This really should be handled by higher-up code.
if (version != null)
{
User.RaiseMessage(" {0} {1} already installed, skipped", module.identifier, version);
return;
}
// Find our in the cache if we don't already have it.
filename = filename ?? Cache.GetCachedZip(module.download);
// If we *still* don't have a file, then kraken bitterly.
if (filename == null)
{
throw new FileNotFoundKraken(
null,
String.Format("Trying to install {0}, but it's not downloaded", module)
);
}
// We'll need our registry to record which files we've installed.
Registry registry = registry_manager.registry;
using (var transaction = CkanTransaction.CreateTransactionScope())
{
// Install all the things!
IEnumerable<string> files = InstallModule(module, filename);
// Register our module and its files.
registry.RegisterModule(module, files, ksp);
// Finish our transaction, but *don't* save the registry; we may be in an
// intermediate, inconsistent state.
// This is fine from a transaction standpoint, as we may not have an enclosing
// transaction, and if we do, they can always roll us back.
transaction.Complete();
}
// Fire our callback that we've installed a module, if we have one.
if (onReportModInstalled != null)
{
onReportModInstalled(module);
}
}
/// <summary>
/// Check if the given module is a metapackage:
/// if it is, throws a BadCommandKraken.
/// </summary>
private static void CheckMetapackageInstallationKraken(CkanModule module)
{
if (module.IsMetapackage)
{
throw new BadCommandKraken("Metapackages can not be installed!");
}
}
/// <summary>
/// Installs the module from the zipfile provided.
/// Returns a list of files installed.
/// Propagates a BadMetadataKraken if our install metadata is bad.
/// Propagates a FileExistsKraken if we were going to overwrite a file.
/// </summary>
private IEnumerable<string> InstallModule(CkanModule module, string zip_filename)
{
CheckMetapackageInstallationKraken(module);
using (ZipFile zipfile = new ZipFile(zip_filename))
{
IEnumerable<InstallableFile> files = FindInstallableFiles(module, zipfile, ksp);
try
{
foreach (InstallableFile file in files)
{
log.InfoFormat("Copying {0}", file.source.Name);
CopyZipEntry(zipfile, file.source, file.destination, file.makedir);
}
}
catch (FileExistsKraken kraken)
{
// Decorate the kraken with our module and re-throw
kraken.filename = ksp.ToRelativeGameDir(kraken.filename);
kraken.installing_module = module;
kraken.owning_module = registry_manager.registry.FileOwner(kraken.filename);
throw;
}
return files.Select(x => x.destination);
}
}
/// <summary>
/// Given a stanza and an open zipfile, returns all files that would be installed
/// for this stanza.
///
/// If a KSP instance is provided, it will be used to generate output paths, otherwise these will be null.
///
/// Throws a BadInstallLocationKraken if the install stanza targets an
/// unknown install location (eg: not GameData, Ships, etc)
///
/// Throws a BadMetadataKraken if the stanza resulted in no files being returned.
/// </summary>
/// <exception cref="BadInstallLocationKraken">Thrown when the installation path is not valid according to the spec.</exception>
internal static List<InstallableFile> FindInstallableFiles(ModuleInstallDescriptor stanza, ZipFile zipfile, KSP ksp)
{
string installDir;
bool makeDirs;
var files = new List<InstallableFile> ();
// Normalize the path before doing everything else
// TODO: This really should happen in the ModuleInstallDescriptor itself.
stanza.install_to = KSPPathUtils.NormalizePath(stanza.install_to);
// Convert our stanza to a standard `file` type. This is a no-op if it's
// already the basic type.
stanza = stanza.ConvertFindToFile(zipfile);
if (stanza.install_to == "GameData" || stanza.install_to.StartsWith("GameData/"))
{
// The installation path can be either "GameData" or a sub-directory of "GameData"
// but it cannot contain updirs
if (stanza.install_to.Contains("/../") || stanza.install_to.EndsWith("/.."))
throw new BadInstallLocationKraken("Invalid installation path: " + stanza.install_to);
string subDir = stanza.install_to.Substring("GameData".Length); // remove "GameData"
subDir = subDir.StartsWith("/") ? subDir.Substring(1) : subDir; // remove a "/" at the beginning, if present
// Add the extracted subdirectory to the path of KSP's GameData
installDir = ksp == null ? null : (KSPPathUtils.NormalizePath(ksp.GameData() + "/" + subDir));
makeDirs = true;
}
else switch (stanza.install_to)
{
case "Ships":
installDir = ksp == null ? null : ksp.Ships();
makeDirs = false; // Don't allow directory creation in ships directory
break;
case "Tutorial":
installDir = ksp == null ? null : ksp.Tutorial();
makeDirs = true;
break;
case "GameRoot":
installDir = ksp == null ? null : ksp.GameDir();
makeDirs = false;
break;
default:
throw new BadInstallLocationKraken("Unknown install_to " + stanza.install_to);
}
// O(N^2) solution, as we're walking the zipfile for each stanza.
// Surely there's a better way, although this is fast enough we may not care.
foreach (ZipEntry entry in zipfile)
{
// Skips things not prescribed by our install stanza.
if (! stanza.IsWanted(entry.Name)) {
continue;
}
// Prepare our file info.
InstallableFile file_info = new InstallableFile
{
source = entry,
makedir = makeDirs,
destination = null
};
// If we have a place to install it, fill that in...
if (installDir != null)
{
// Get the full name of the file.
string outputName = entry.Name;
// Update our file info with the install location
file_info.destination = TransformOutputName(stanza.file, outputName, installDir);
}
files.Add(file_info);
}
// If we have no files, then something is wrong! (KSP-CKAN/CKAN#93)
if (files.Count == 0)
{
// We have null as the first argument here, because we don't know which module we're installing
throw new BadMetadataKraken(null, String.Format("No files found in {0} to install!", stanza.file));
}
return files;
}
/// <summary>
/// Transforms the name of the output. This will strip the leading directories from the stanza file from
/// output name and then combine it with the installDir.
/// EX: "kOS-1.1/GameData/kOS", "kOS-1.1/GameData/kOS/Plugins/kOS.dll", "GameData" will be transformed
/// to "GameData/kOS/Plugins/kOS.dll"
/// </summary>
/// <returns>The output name.</returns>
/// <param name="file">The file directive of the stanza.</param>
/// <param name="outputName">The name of the file to transform.</param>
/// <param name="installDir">The installation dir where the file should end up with.</param>
internal static string TransformOutputName(string file, string outputName, string installDir)
{
string leadingPathToRemove = KSPPathUtils.GetLeadingPathElements(file);
// Special-casing, if stanza.file is just "GameData" or "Ships", strip it.
// TODO: Do we need to do anything special for tutorials or GameRoot?
if (
leadingPathToRemove == string.Empty &&
(file == "GameData" || file == "Ships")
)
{
leadingPathToRemove = file;
}
// If there's a leading path to remove, then we have some extra work that
// needs doing...
if (leadingPathToRemove != string.Empty)
{
string leadingRegEx = "^" + Regex.Escape(leadingPathToRemove) + "/";
if (!Regex.IsMatch(outputName, leadingRegEx))
{
throw new BadMetadataKraken(null,
String.Format("Output file name ({0}) not matching leading path of stanza.file ({1})",
outputName, leadingRegEx
)
);
}
// Strip off leading path name
outputName = Regex.Replace(outputName, leadingRegEx, "");
}
// Return our snipped, normalised, and ready to go output filename!
return KSPPathUtils.NormalizePath(
Path.Combine(installDir, outputName)
);
}
/// <summary>
/// Given a module and an open zipfile, return all the files that would be installed
/// for this module.
///
/// If a KSP instance is provided, it will be used to generate output paths, otherwise these will be null.
///
/// Throws a BadMetadataKraken if the stanza resulted in no files being returned.
/// </summary>
public static List<InstallableFile> FindInstallableFiles(CkanModule module, ZipFile zipfile, KSP ksp)
{
var files = new List<InstallableFile> ();
try
{
// Use the provided stanzas, or use the default install stanza if they're absent.
if (module.install != null && module.install.Length != 0)
{
foreach (ModuleInstallDescriptor stanza in module.install)
{
files.AddRange(FindInstallableFiles(stanza, zipfile, ksp));
}
}
else
{
ModuleInstallDescriptor default_stanza = ModuleInstallDescriptor.DefaultInstallStanza(module.identifier, zipfile);
files.AddRange(FindInstallableFiles(default_stanza, zipfile, ksp));
}
}
catch (BadMetadataKraken kraken)
{
// Decorate our kraken with the current module, as the lower-level
// methods won't know it.
kraken.module = module;
throw;
}
return files;
}
/// <summary>
/// Given a module and a path to a zipfile, returns all the files that would be installed
/// from that zip for this module.
///
/// This *will* throw an exception if the file does not exist.
///
/// Throws a BadMetadataKraken if the stanza resulted in no files being returned.
///
/// If a KSP instance is provided, it will be used to generate output paths, otherwise these will be null.
/// </summary>
// TODO: Document which exception!
public static List<InstallableFile> FindInstallableFiles(CkanModule module, string zip_filename, KSP ksp)
{
// `using` makes sure our zipfile gets closed when we exit this block.
using (ZipFile zipfile = new ZipFile(zip_filename))
{
log.DebugFormat("Searching {0} using {1} as module", zip_filename, module);
return FindInstallableFiles(module, zipfile, ksp);
}
}
/// <summary>
/// Copy the entry from the opened zipfile to the path specified.
/// </summary>
internal static void CopyZipEntry(ZipFile zipfile, ZipEntry entry, string fullPath, bool makeDirs)
{
if (entry.IsDirectory)
{
// Skip if we're not making directories for this install.
if (!makeDirs)
{
log.DebugFormat ("Skipping {0}, we don't make directories for this path", fullPath);
return;
}
log.DebugFormat("Making directory {0}", fullPath);
file_transaction.CreateDirectory(fullPath);
}
else
{
log.DebugFormat("Writing file {0}", fullPath);
// Sometimes there are zipfiles that don't contain entries for the
// directories their files are in. No, I understand either, but
// the result is we have to make sure our directories exist, just in case.
if (makeDirs)
{
string directory = Path.GetDirectoryName(fullPath);
file_transaction.CreateDirectory(directory);
}
// We don't allow for the overwriting of files. See #208.
if (File.Exists (fullPath))
{
throw new FileExistsKraken(fullPath, string.Format("Trying to write {0} but it already exists.", fullPath));
}
// Snapshot whatever was there before. If there's nothing, this will just
// remove our file on rollback. We still need this even thought we won't
// overwite files, as it ensures deletiion on rollback.
file_transaction.Snapshot(fullPath);
try
{
// It's a file! Prepare the streams
using (Stream zipStream = zipfile.GetInputStream(entry))
using (FileStream writer = File.Create(fullPath))
{
// 4k is the block size on practically every disk and OS.
byte[] buffer = new byte[4096];
StreamUtils.Copy(zipStream, writer, buffer);
}
}
catch (DirectoryNotFoundException ex)
{
throw new DirectoryNotFoundKraken("", ex.Message, ex);
}
}
}
/// <summary>
/// Uninstalls all the mods provided, including things which depend upon them.
/// This *DOES* save the registry.
/// Preferred over Uninstall.
/// </summary>
public void UninstallList(IEnumerable<string> mods)
{
// Pre-check, have they even asked for things which are installed?
foreach (string mod in mods.Where(mod => registry_manager.registry.InstalledModule(mod) == null))
{
throw new ModNotInstalledKraken(mod);
}
// Find all the things which need uninstalling.
IEnumerable<string> goners = registry_manager.registry.FindReverseDependencies(mods);
User.RaiseMessage("About to remove:\n");
foreach (string mod in goners)
{
User.RaiseMessage(" * {0}", mod);
}
bool ok = User.RaiseYesNoDialog("\nContinue?");
if (!ok)
{
User.RaiseMessage("Mod removal aborted at user request.");
return;
}
using (var transaction = CkanTransaction.CreateTransactionScope())
{
foreach (string mod in goners)
{
User.RaiseMessage("Removing {0}...", mod);
Uninstall(mod);
}
registry_manager.Save();
transaction.Complete();
}
User.RaiseMessage("Done!\n");
}
public void UninstallList(string mod)
{
var list = new List<string> {mod};
UninstallList(list);
}
/// <summary>
/// Uninstall the module provided. For internal use only.
/// Use UninstallList for user queries, it also does dependency handling.
/// This does *NOT* save the registry.
/// </summary>
private void Uninstall(string modName)
{
using (var transaction = CkanTransaction.CreateTransactionScope())
{
InstalledModule mod = registry_manager.registry.InstalledModule(modName);
if (mod == null)
{
log.ErrorFormat("Trying to uninstall {0} but it's not installed", modName);
throw new ModNotInstalledKraken(modName);
}
// Walk our registry to find all files for this mod.
IEnumerable<string> files = mod.Files;
var directoriesToDelete = new HashSet<string>();
foreach (string file in files)
{
string path = ksp.ToAbsoluteGameDir(file);
try
{
FileAttributes attr = File.GetAttributes(path);
if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
{
directoriesToDelete.Add(path);
}
else
{
log.InfoFormat("Removing {0}", file);
file_transaction.Delete(path);
}
}
catch (Exception ex)
{
// XXX: This is terrible, we're catching all exceptions.
log.ErrorFormat("Failure in locating file {0} : {1}", path, ex.Message);
}
}
// Remove from registry.
registry_manager.registry.DeregisterModule(ksp, modName);
// Sort our directories from longest to shortest, to make sure we remove child directories
// before parents. GH #78.
foreach (string directory in directoriesToDelete.OrderBy(dir => dir.Length).Reverse())
{
if (!Directory.EnumerateFileSystemEntries(directory).Any())
{
// Skip Ships/VAB ans Ships/SPH
if (directory == KSPPathUtils.ToAbsolute("VAB", ksp.Ships())
|| directory == KSPPathUtils.ToAbsolute("SPH", ksp.Ships()))
{
continue;
}
// We *don't* use our file_transaction to delete files here, because
// it fails if the system's temp directory is on a different device
// to KSP. However we *can* safely delete it now we know it's empty,
// because the TxFileMgr *will* put it back if there's a file inside that
// needs it.
//
// This works around GH #251.
// The filesystem boundry bug is described in https://transactionalfilemgr.codeplex.com/workitem/20
log.InfoFormat("Removing {0}", directory);
Directory.Delete(directory);
}
else
{
log.InfoFormat("Not removing directory {0}, it's not empty", directory);
}
}
transaction.Complete();
}
}
#region AddRemove
/// <summary>
/// Adds and removes the listed modules as a single transaction.
/// No relationships will be processed.
/// This *will* save the registry.
/// </summary>
/// <param name="add">Add.</param>
/// <param name="remove">Remove.</param>
public void AddRemove(IEnumerable<CkanModule> add = null, IEnumerable<string> remove = null)
{
// TODO: We should do a consistency check up-front, rather than relying
// upon our registry catching inconsistencies at the end.
// TODO: Download our files.
using (var tx = CkanTransaction.CreateTransactionScope())
{
foreach (string identifier in remove)
{
Uninstall(identifier);
}
foreach (CkanModule module in add)
{
Install(module);
}
registry_manager.Save();
tx.Complete();
}
}
/// <summary>
/// Upgrades the mods listed to the latest versions for the user's KSP.
/// Will *re-install* with warning even if an upgrade is not available.
/// Throws ModuleNotFoundKraken if module is not installed, or not available.
/// </summary>
public void Upgrade(IEnumerable<string> identifiers, NetAsyncDownloader netAsyncDownloader)
{
var options = new RelationshipResolverOptions();
// We do not wish to pull in any suggested or recommended mods.
options.with_recommends = false;
options.with_suggests = false;
var resolver = new RelationshipResolver(identifiers.ToList(), options, registry_manager.registry, ksp.Version());
List<CkanModule> upgrades = resolver.ModList();
Upgrade(upgrades, netAsyncDownloader);
}
/// <summary>
/// Upgrades or installs the mods listed to the specified versions for the user's KSP.
/// Will *re-install* or *downgrade* (with a warning) as well as upgrade.
/// Throws ModuleNotFoundKraken if a module is not installed.
/// </summary>
public void Upgrade(IEnumerable<CkanModule> modules, NetAsyncDownloader netAsyncDownloader)
{
// Start by making sure we've downloaded everything.
DownloadModules(modules, netAsyncDownloader);
// Our upgrade involves removing everything that's currently installed, then
// adding everything that needs installing (which may involve new mods to
// satisfy dependencies). We always know the list passed in is what we need to
// install, but we need to calculate what needs to be removed.
var to_remove = new List<string>();
// Let's discover what we need to do with each module!
foreach (CkanModule module in modules)
{
string ident = module.identifier;
InstalledModule installed_mod = registry_manager.registry.InstalledModule(ident);
if (installed_mod == null)
{
//Maybe ModuleNotInstalled ?
if (registry_manager.registry.IsAutodetected(ident))
{
throw new ModuleNotFoundKraken(ident, module.version.ToString(), String.Format("Can't upgrade {0} as it was not installed by CKAN. \n Please remove manually before trying to install it.", ident));
}
User.RaiseMessage("Installing previously uninstalled mod {0}", ident);
}
else
{
// Module already installed. We'll need to remove it first.
to_remove.Add(module.identifier);
Module installed = installed_mod.Module;
if (installed.version.IsEqualTo(module.version))
{
log.WarnFormat("{0} is already at the latest version, reinstalling", installed.identifier);
}
else if (installed.version.IsGreaterThan(module.version))
{
log.WarnFormat("Downgrading {0} from {1} to {2}", ident, installed.version, module.version);
}
else
{
log.InfoFormat("Upgrading {0} to {1}", ident, module.version);
}
}
}
AddRemove(
modules,
to_remove
);
}
#endregion
/// <summary>
/// Makes sure all the specified mods are downloaded.
/// </summary>
private void DownloadModules(IEnumerable<CkanModule> mods, NetAsyncDownloader downloader)
{
List<CkanModule> downloads = mods.Where(module => !ksp.Cache.IsCachedZip(module.download)).ToList();
if (downloads.Count > 0)
{
downloader.DownloadModules(ksp.Cache, downloads);
}
}
/// <summary>
/// Don't use this. Use Registry.FindReverseDependencies instead.
/// This method may be deprecated in the future.
/// </summary>
// Here for now to keep the GUI happy.
public HashSet<string> FindReverseDependencies(string module)
{
return registry_manager.registry.FindReverseDependencies(module);
}
}
}