• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

KSP-CKAN / CKAN / 17904669167

22 Sep 2025 04:34AM UTC coverage: 75.604% (+1.2%) from 74.397%
17904669167

push

github

HebaruSan
Merge #4443 Report number of filtered files in install

5231 of 7236 branches covered (72.29%)

Branch coverage included in aggregate %.

192 of 218 new or added lines in 41 files covered. (88.07%)

35 existing lines in 7 files now uncovered.

11163 of 14448 relevant lines covered (77.26%)

1.58 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.51
/Core/IO/ModuleInstaller.cs
1
using System;
2
using System.IO;
3
using System.Linq;
4
using System.Collections.Generic;
5
using System.Threading;
6

7
using ICSharpCode.SharpZipLib.Core;
8
using ICSharpCode.SharpZipLib.Zip;
9
using log4net;
10
using ChinhDo.Transactions.FileManager;
11

12
using CKAN.Extensions;
13
using CKAN.Versioning;
14
using CKAN.Configuration;
15
using CKAN.Games;
16

17
namespace CKAN.IO
18
{
19
    public struct InstallableFile
20
    {
21
        public ZipEntry source;
22
        public string   destination;
23
        public bool     makedir;
24
    }
25

26
    public class ModuleInstaller
27
    {
28
        // Constructor
29
        public ModuleInstaller(GameInstance      inst,
2✔
30
                               NetModuleCache    cache,
31
                               IConfiguration    config,
32
                               IUser             user,
33
                               CancellationToken cancelToken = default)
34
        {
2✔
35
            User        = user;
2✔
36
            this.cache  = cache;
2✔
37
            this.config = config;
2✔
38
            instance    = inst;
2✔
39
            this.cancelToken = cancelToken;
2✔
40
            log.DebugFormat("Creating ModuleInstaller for {0}", instance.GameDir);
2✔
41
        }
2✔
42

43
        public IUser User { get; set; }
44

45
        public event Action<CkanModule, long, long>?      InstallProgress;
46
        public event Action<InstalledModule, long, long>? RemoveProgress;
47
        public event Action<CkanModule>?                  OneComplete;
48

49
        #region Installation
50

51
        /// <summary>
52
        ///     Installs all modules given a list of identifiers as a transaction. Resolves dependencies.
53
        ///     This *will* save the registry at the end of operation.
54
        ///
55
        /// Propagates a BadMetadataKraken if our install metadata is bad.
56
        /// Propagates a FileExistsKraken if we were going to overwrite a file.
57
        /// Propagates a CancelledActionKraken if the user cancelled the install.
58
        /// </summary>
59
        public void InstallList(IReadOnlyCollection<CkanModule> modules,
60
                                RelationshipResolverOptions     options,
61
                                RegistryManager                 registry_manager,
62
                                ref HashSet<string>?            possibleConfigOnlyDirs,
63
                                InstalledFilesDeduplicator?     deduper       = null,
64
                                string?                         userAgent     = null,
65
                                IDownloader?                    downloader    = null,
66
                                bool                            ConfirmPrompt = true)
67
        {
2✔
68
            if (modules.Count == 0)
2!
69
            {
×
70
                User.RaiseProgress(Properties.Resources.ModuleInstallerNothingToInstall, 100);
×
71
                return;
×
72
            }
73
            var resolver = new RelationshipResolver(modules, null, options,
2✔
74
                                                    registry_manager.registry,
75
                                                    instance.Game, instance.VersionCriteria());
76
            var modsToInstall = resolver.ModList().ToArray();
2✔
77
            // Alert about attempts to install DLC before downloading or installing anything
78
            var dlc = modsToInstall.Where(m => m.IsDLC).ToArray();
2✔
79
            if (dlc.Length > 0)
2✔
80
            {
2✔
81
                throw new ModuleIsDLCKraken(dlc.First());
2✔
82
            }
83

84
            // Make sure we have enough space to install this stuff
85
            var installBytes = modsToInstall.Sum(m => m.install_size);
2✔
86
            CKANPathUtils.CheckFreeSpace(new DirectoryInfo(instance.GameDir),
2✔
87
                                         installBytes,
88
                                         Properties.Resources.NotEnoughSpaceToInstall);
89

90
            var cached    = new List<CkanModule>();
2✔
91
            var downloads = new List<CkanModule>();
2✔
92
            User.RaiseMessage(Properties.Resources.ModuleInstallerAboutToInstall);
2✔
93
            User.RaiseMessage("");
2✔
94
            foreach (var module in modsToInstall)
5✔
95
            {
2✔
96
                User.RaiseMessage(" * {0}", cache.DescribeAvailability(config, module));
2✔
97
                if (!module.IsMetapackage && !cache.IsMaybeCachedZip(module))
2✔
98
                {
2✔
99
                    downloads.Add(module);
2✔
100
                }
2✔
101
                else
102
                {
2✔
103
                    cached.Add(module);
2✔
104
                }
2✔
105
            }
2✔
106
            if (ConfirmPrompt && !User.RaiseYesNoDialog(Properties.Resources.ModuleInstallerContinuePrompt))
2!
107
            {
×
108
                throw new CancelledActionKraken(Properties.Resources.ModuleInstallerUserDeclined);
×
109
            }
110

111
            var downloadBytes = CkanModule.GroupByDownloads(downloads)
2✔
112
                                          .Sum(grp => grp.First().download_size);
2✔
113
            var rateCounter = new ByteRateCounter()
2✔
114
            {
115
                Size      = downloadBytes + installBytes,
116
                BytesLeft = downloadBytes + installBytes,
117
            };
118
            rateCounter.Start();
2✔
119
            long downloadedBytes = 0;
2✔
120
            long installedBytes  = 0;
2✔
121
            if (downloads.Count > 0)
2✔
122
            {
2✔
123
                downloader ??= new NetAsyncModulesDownloader(User, cache, userAgent, cancelToken);
2✔
124
                downloader.OverallDownloadProgress += brc =>
2✔
125
                {
2✔
126
                    downloadedBytes = downloadBytes - brc.BytesLeft;
2✔
127
                    rateCounter.BytesLeft = downloadBytes - downloadedBytes
2✔
128
                                          + installBytes  - installedBytes;
129
                    User.RaiseProgress(rateCounter);
2✔
130
                };
2✔
131
            }
2✔
132

133
            // We're about to install all our mods; so begin our transaction.
134
            using (var transaction = CkanTransaction.CreateTransactionScope())
2✔
135
            {
2✔
136
                var gameDir = new DirectoryInfo(instance.GameDir);
2✔
137
                long modInstallCompletedBytes = 0;
2✔
138
                foreach (var mod in ModsInDependencyOrder(resolver, cached, downloads, downloader))
5✔
139
                {
2✔
140
                    // Re-check that there's enough free space in case game dir and cache are on same drive
141
                    CKANPathUtils.CheckFreeSpace(gameDir, mod.install_size,
2✔
142
                                                 Properties.Resources.NotEnoughSpaceToInstall);
143
                    Install(mod, resolver.IsAutoInstalled(mod),
2✔
144
                            registry_manager.registry,
145
                            deduper?.ModuleCandidateDuplicates(mod.identifier, mod.version),
146
                            ref possibleConfigOnlyDirs,
147
                            new ProgressImmediate<long>(bytes =>
148
                            {
2✔
149
                                InstallProgress?.Invoke(mod,
2!
150
                                                        Math.Max(0,     mod.install_size - bytes),
151
                                                        Math.Max(bytes, mod.install_size));
152
                                installedBytes = modInstallCompletedBytes
2✔
153
                                                 + Math.Min(bytes, mod.install_size);
154
                                rateCounter.BytesLeft = downloadBytes - downloadedBytes
2✔
155
                                                      + installBytes  - installedBytes;
156
                                User.RaiseProgress(rateCounter);
2✔
157
                            }));
2✔
158
                    modInstallCompletedBytes += mod.install_size;
2✔
159
                }
2✔
160
                rateCounter.Stop();
2✔
161

162
                User.RaiseProgress(Properties.Resources.ModuleInstallerUpdatingRegistry, 90);
2✔
163
                registry_manager.Save(!options.without_enforce_consistency);
2✔
164

165
                User.RaiseProgress(Properties.Resources.ModuleInstallerCommitting, 95);
2✔
166
                transaction.Complete();
2✔
167
            }
2✔
168

169
            EnforceCacheSizeLimit(registry_manager.registry, cache, config);
2✔
170
            User.RaiseProgress(Properties.Resources.ModuleInstallerDone, 100);
2✔
171
        }
2✔
172

173
        private static IEnumerable<CkanModule> ModsInDependencyOrder(RelationshipResolver            resolver,
174
                                                                     IReadOnlyCollection<CkanModule> cached,
175
                                                                     IReadOnlyCollection<CkanModule> toDownload,
176
                                                                     IDownloader?                    downloader)
177

178
            => ModsInDependencyOrder(resolver, cached,
2✔
179
                                     downloader != null && toDownload.Count > 0
180
                                         ? downloader.ModulesAsTheyFinish(cached, toDownload)
181
                                         : null);
182

183
        private static IEnumerable<CkanModule> ModsInDependencyOrder(RelationshipResolver            resolver,
184
                                                                     IReadOnlyCollection<CkanModule> cached,
185
                                                                     IEnumerable<CkanModule>?        downloading)
186
        {
2✔
187
            var waiting = new HashSet<CkanModule>();
2✔
188
            var done    = new HashSet<CkanModule>();
2✔
189
            if (downloading != null)
2✔
190
            {
2✔
191
                foreach (var newlyCached in downloading)
5✔
192
                {
2✔
193
                    waiting.Add(newlyCached);
2✔
194
                    foreach (var m in OnePass(resolver, waiting, done))
5✔
195
                    {
2✔
196
                        yield return m;
2✔
197
                    }
2✔
198
                }
2✔
199
            }
2✔
200
            else
201
            {
2✔
202
                waiting.UnionWith(cached);
2✔
203
                // Treat excluded mods as already done
204
                done.UnionWith(resolver.ModList().Except(waiting));
2✔
205
                foreach (var m in OnePass(resolver, waiting, done))
5✔
206
                {
2✔
207
                    yield return m;
2✔
208
                }
2✔
209
            }
2✔
210
            if (waiting.Count > 0)
2!
211
            {
×
212
                // Sanity check in case something goes haywire with the threading logic
213
                throw new InconsistentKraken(
×
214
                    string.Format("Mods should have been installed but were not: {0}",
215
                                  string.Join(", ", waiting)));
216
            }
217
        }
2✔
218

219
        private static IEnumerable<CkanModule> OnePass(RelationshipResolver resolver,
220
                                                       HashSet<CkanModule>  waiting,
221
                                                       HashSet<CkanModule>  done)
222
        {
2✔
223
            while (true)
2✔
224
            {
2✔
225
                var newlyDone = waiting.Where(m => resolver.ReadyToInstall(m, done))
2✔
226
                                       .OrderBy(m => m.identifier)
2✔
227
                                       .ToArray();
228
                if (newlyDone.Length == 0)
2✔
229
                {
2✔
230
                    // No mods ready to install
231
                    break;
2✔
232
                }
233
                foreach (var m in newlyDone)
5✔
234
                {
2✔
235
                    waiting.Remove(m);
2✔
236
                    done.Add(m);
2✔
237
                    yield return m;
2✔
238
                }
2✔
239
            }
2✔
240
        }
2✔
241

242
        /// <summary>
243
        ///     Install our mod from the filename supplied.
244
        ///     If no file is supplied, we will check the cache or throw FileNotFoundKraken.
245
        ///     Does *not* resolve dependencies; this does the heavy lifting.
246
        ///     Does *not* save the registry.
247
        ///     Do *not* call this directly, use InstallList() instead.
248
        ///
249
        /// Propagates a BadMetadataKraken if our install metadata is bad.
250
        /// Propagates a FileExistsKraken if we were going to overwrite a file.
251
        /// Throws a FileNotFoundKraken if we can't find the downloaded module.
252
        ///
253
        /// TODO: The name of this and InstallModule() need to be made more distinctive.
254
        /// </summary>
255
        private void Install(CkanModule                    module,
256
                             bool                          autoInstalled,
257
                             Registry                      registry,
258
                             Dictionary<string, string[]>? candidateDuplicates,
259
                             ref HashSet<string>?          possibleConfigOnlyDirs,
260
                             IProgress<long>?              progress)
261
        {
2✔
262
            CheckKindInstallationKraken(module);
2✔
263
            var version = registry.InstalledVersion(module.identifier);
2✔
264

265
            // TODO: This really should be handled by higher-up code.
266
            if (version is not null and not UnmanagedModuleVersion)
2✔
267
            {
2✔
268
                User.RaiseMessage(Properties.Resources.ModuleInstallerAlreadyInstalled,
2✔
269
                                  module.name, version);
270
                return;
2✔
271
            }
272

273
            string? filename = null;
2✔
274
            if (!module.IsMetapackage)
2!
275
            {
2✔
276
                // Find ZIP in the cache if we don't already have it.
277
                filename ??= cache.GetCachedFilename(module);
2!
278

279
                // If we *still* don't have a file, then kraken bitterly.
280
                if (filename == null)
2!
281
                {
×
282
                    throw new FileNotFoundKraken(null,
×
283
                                                 string.Format(Properties.Resources.ModuleInstallerZIPNotInCache,
284
                                                               module));
285
                }
286
            }
2✔
287

288
            User.RaiseMessage(Properties.Resources.ModuleInstallerInstallingMod,
2✔
289
                              $"{module.name} {module.version}");
290

291
            using (var transaction = CkanTransaction.CreateTransactionScope())
2✔
292
            {
2✔
293
                // Install all the things!
294
                var files = InstallModule(module, filename, registry, candidateDuplicates,
2✔
295
                                          ref possibleConfigOnlyDirs, out int filteredCount, progress);
296

297
                // Register our module and its files.
298
                registry.RegisterModule(module, files, instance, autoInstalled);
2✔
299

300
                // Finish our transaction, but *don't* save the registry; we may be in an
301
                // intermediate, inconsistent state.
302
                // This is fine from a transaction standpoint, as we may not have an enclosing
303
                // transaction, and if we do, they can always roll us back.
304
                transaction.Complete();
2✔
305

306
                if (filteredCount > 0)
2✔
307
                {
2✔
308
                    User.RaiseMessage(Properties.Resources.ModuleInstallerInstalledModFiltered,
2✔
309
                                      $"{module.name} {module.version}", filteredCount);
310
                }
2✔
311
                else
312
                {
2✔
313
                    User.RaiseMessage(Properties.Resources.ModuleInstallerInstalledMod,
2✔
314
                                      $"{module.name} {module.version}");
315
                }
2✔
316
            }
2✔
317

318
            // Fire our callback that we've installed a module, if we have one.
319
            OneComplete?.Invoke(module);
2!
320
        }
2✔
321

322
        /// <summary>
323
        /// Check if the given module is a DLC:
324
        /// if it is, throws ModuleIsDLCKraken.
325
        /// </summary>
326
        private static void CheckKindInstallationKraken(CkanModule module)
327
        {
2✔
328
            if (module.IsDLC)
2!
329
            {
×
330
                throw new ModuleIsDLCKraken(module);
×
331
            }
332
        }
2✔
333

334
        /// <summary>
335
        /// Installs the module from the zipfile provided.
336
        /// Returns a list of files installed.
337
        /// Propagates a DllLocationMismatchKraken if the user has a bad manual install.
338
        /// Propagates a BadMetadataKraken if our install metadata is bad.
339
        /// Propagates a CancelledActionKraken if the user decides not to overwite unowned files.
340
        /// Propagates a FileExistsKraken if we were going to overwrite a file.
341
        /// </summary>
342
        private List<string> InstallModule(CkanModule                    module,
343
                                           string?                       zip_filename,
344
                                           Registry                      registry,
345
                                           Dictionary<string, string[]>? candidateDuplicates,
346
                                           ref HashSet<string>?          possibleConfigOnlyDirs,
347
                                           out int                       filteredCount,
348
                                           IProgress<long>?              moduleProgress)
349
        {
2✔
350
            var createdPaths = new List<string>();
2✔
351
            if (module.IsMetapackage || zip_filename == null)
2!
352
            {
×
353
                // It's OK to include metapackages in changesets,
354
                // but there's no work to do for them
NEW
355
                filteredCount = 0;
×
UNCOV
356
                return createdPaths;
×
357
            }
358
            using (ZipFile zipfile = new ZipFile(zip_filename))
2✔
359
            {
2✔
360
                var filters = config.GetGlobalInstallFilters(instance.Game)
2✔
361
                                    .Concat(instance.InstallFilters)
362
                                    .ToHashSet();
363
                var groups = FindInstallableFiles(module, zipfile, instance, instance.Game)
2✔
364
                    // Skip the file if it's a ckan file, these should never be copied to GameData
365
                    .Where(instF => !IsInternalCkan(instF.source))
2✔
366
                    // Check whether each file matches any installation filter
367
                    .GroupBy(instF => filters.Any(filt => instF.destination != null
2✔
368
                                                          && instF.destination.Contains(filt)))
369
                    .ToDictionary(grp => grp.Key,
2✔
370
                                  grp => grp.ToArray());
2✔
371
                var files = groups.GetValueOrDefault(false) ?? Array.Empty<InstallableFile>();
2✔
372
                filteredCount = groups.GetValueOrDefault(true)?.Length ?? 0;
2✔
373
                try
374
                {
2✔
375
                    if (registry.DllPath(module.identifier)
2✔
376
                        is string { Length: > 0 } dll)
377
                    {
2✔
378
                        // Find where we're installing identifier.optionalversion.dll
379
                        // (file name might not be an exact match with manually installed)
380
                        var dllFolders = files
2✔
381
                            .Select(f => instance.ToRelativeGameDir(f.destination))
2✔
382
                            .Where(relPath => instance.DllPathToIdentifier(relPath) == module.identifier)
2✔
383
                            .Select(Path.GetDirectoryName)
384
                            .ToHashSet();
385
                        // Make sure that the DLL is actually included in the install
386
                        // (NearFutureElectrical, NearFutureElectrical-Core)
387
                        if (dllFolders.Count > 0 && registry.FileOwner(dll) == null)
2✔
388
                        {
2✔
389
                            if (!dllFolders.Contains(Path.GetDirectoryName(dll)))
2✔
390
                            {
2✔
391
                                // Manually installed DLL is somewhere else where we're not installing files,
392
                                // probable bad install, alert user and abort
393
                                throw new DllLocationMismatchKraken(dll, string.Format(
2✔
394
                                    Properties.Resources.ModuleInstallerBadDLLLocation, module.identifier, dll));
395
                            }
396
                            // Delete the manually installed DLL transaction-style because we believe we'll be replacing it
397
                            var toDelete = instance.ToAbsoluteGameDir(dll);
2✔
398
                            log.DebugFormat("Deleting manually installed DLL {0}", toDelete);
2✔
399
                            TxFileManager file_transaction = new TxFileManager();
2✔
400
                            file_transaction.Snapshot(toDelete);
2✔
401
                            file_transaction.Delete(toDelete);
2✔
402
                        }
2✔
403
                    }
2✔
404

405
                    // Look for overwritable files if session is interactive
406
                    if (!User.Headless)
2✔
407
                    {
2✔
408
                        if (FindConflictingFiles(zipfile, files, registry).ToArray()
2✔
409
                            is (InstallableFile file, bool same)[] { Length: > 0 } conflicting)
410
                        {
2✔
411
                            var fileMsg = string.Join(Environment.NewLine,
2✔
412
                                                      conflicting.OrderBy(tuple => tuple.same)
2✔
413
                                                                 .Select(tuple => $"- {instance.ToRelativeGameDir(tuple.file.destination)}  ({(tuple.same ? Properties.Resources.ModuleInstallerFileSame : Properties.Resources.ModuleInstallerFileDifferent)})"));
2✔
414
                            if (User.RaiseYesNoDialog(string.Format(Properties.Resources.ModuleInstallerOverwrite,
2✔
415
                                                                    module.name, fileMsg)))
416
                            {
2✔
417
                                DeleteConflictingFiles(conflicting.Select(tuple => tuple.file));
2✔
418
                            }
2✔
419
                            else
420
                            {
2✔
421
                                throw new CancelledActionKraken(string.Format(Properties.Resources.ModuleInstallerOverwriteCancelled,
2✔
422
                                                                              module.name));
423
                            }
424
                        }
2✔
425
                    }
2✔
426
                    long installedBytes = 0;
2✔
427
                    var fileProgress = new ProgressImmediate<long>(bytes => moduleProgress?.Report(installedBytes + bytes));
2✔
428
                    foreach (InstallableFile file in files)
5✔
429
                    {
2✔
430
                        if (cancelToken.IsCancellationRequested)
2!
431
                        {
×
432
                            throw new CancelledActionKraken();
×
433
                        }
434
                        log.DebugFormat("Copying {0}", file.source.Name);
2✔
435
                        var path = InstallFile(zipfile, file.source, file.destination, file.makedir,
2✔
436
                                               (candidateDuplicates != null
437
                                                && candidateDuplicates.TryGetValue(instance.ToRelativeGameDir(file.destination),
438
                                                                                   out string[]? duplicates))
439
                                                   ? duplicates
440
                                                   : Array.Empty<string>(),
441
                                               fileProgress);
442
                        installedBytes += file.source.Size;
2✔
443
                        if (path != null)
2!
444
                        {
2✔
445
                            createdPaths.Add(path);
2✔
446
                            if (file.source.IsDirectory && possibleConfigOnlyDirs != null)
2✔
447
                            {
2✔
448
                                possibleConfigOnlyDirs.Remove(file.destination);
2✔
449
                            }
2✔
450
                        }
2✔
451
                    }
2✔
452
                    log.InfoFormat("Installed {0}", module);
2✔
453
                }
2✔
454
                catch (FileExistsKraken kraken)
2✔
455
                {
2✔
456
                    // Decorate the kraken with our module and re-throw
457
                    kraken.filename = instance.ToRelativeGameDir(kraken.filename);
2✔
458
                    kraken.installingModule = module;
2✔
459
                    kraken.owningModule = registry.FileOwner(kraken.filename);
2✔
460
                    throw;
2✔
461
                }
462
                return createdPaths;
2✔
463
            }
464
        }
2✔
465

466
        public static bool IsInternalCkan(ZipEntry ze)
467
            => ze.Name.EndsWith(".ckan", StringComparison.OrdinalIgnoreCase);
2✔
468

469
        #region File overwrites
470

471
        /// <summary>
472
        /// Find files in the given list that are already installed and unowned.
473
        /// Note, this compares files on demand; Memoize for performance!
474
        /// </summary>
475
        /// <param name="zip">Zip file that we are installing from</param>
476
        /// <param name="files">Files that we want to install for a module</param>
477
        /// <param name="registry">Registry to check for file ownership</param>
478
        /// <returns>
479
        /// List of pairs: Key = file, Value = true if identical, false if different
480
        /// </returns>
481
        private IEnumerable<(InstallableFile file, bool same)> FindConflictingFiles(ZipFile                      zip,
482
                                                                                    IEnumerable<InstallableFile> files,
483
                                                                                    Registry                     registry)
484
            => files.Where(file => !file.source.IsDirectory
2✔
485
                                   && File.Exists(file.destination)
486
                                   && registry.FileOwner(instance.ToRelativeGameDir(file.destination)) == null)
487
                    .Select(file =>
488
                    {
2✔
489
                        log.DebugFormat("Comparing {0}", file.destination);
2✔
490
                        using (Stream     zipStream = zip.GetInputStream(file.source))
2✔
491
                        using (FileStream curFile   = new FileStream(file.destination,
2✔
492
                                                                     FileMode.Open,
493
                                                                     FileAccess.Read))
494
                        {
2✔
495
                            return (file,
2✔
496
                                    same: file.source.Size == curFile.Length
497
                                          && StreamsEqual(zipStream, curFile));
498
                        }
499
                    });
2✔
500

501
        /// <summary>
502
        /// Compare the contents of two streams
503
        /// </summary>
504
        /// <param name="s1">First stream to compare</param>
505
        /// <param name="s2">Second stream to compare</param>
506
        /// <returns>
507
        /// true if both streams contain same bytes, false otherwise
508
        /// </returns>
509
        private static bool StreamsEqual(Stream s1, Stream s2)
510
        {
2✔
511
            const int bufLen = 1024;
512
            byte[] bytes1 = new byte[bufLen];
2✔
513
            byte[] bytes2 = new byte[bufLen];
2✔
514
            int bytesChecked = 0;
2✔
515
            while (true)
2✔
516
            {
2✔
517
                int bytesFrom1 = s1.Read(bytes1, 0, bufLen);
2✔
518
                int bytesFrom2 = s2.Read(bytes2, 0, bufLen);
2✔
519
                if (bytesFrom1 == 0 && bytesFrom2 == 0)
2✔
520
                {
2✔
521
                    // Boths streams finished, all bytes are equal
522
                    return true;
2✔
523
                }
524
                if (bytesFrom1 != bytesFrom2)
2!
525
                {
×
526
                    // One ended early, not equal.
527
                    log.DebugFormat("Read {0} bytes from stream1 and {1} bytes from stream2", bytesFrom1, bytesFrom2);
×
528
                    return false;
×
529
                }
530
                for (int i = 0; i < bytesFrom1; ++i)
4✔
531
                {
2✔
532
                    if (bytes1[i] != bytes2[i])
2!
533
                    {
×
534
                        log.DebugFormat("Byte {0} doesn't match", bytesChecked + i);
×
535
                        // Bytes don't match, not equal.
536
                        return false;
×
537
                    }
538
                }
2✔
539
                bytesChecked += bytesFrom1;
2✔
540
            }
2✔
541
        }
2✔
542

543
        /// <summary>
544
        /// Remove files that the user chose to overwrite, so
545
        /// the installer can replace them.
546
        /// Uses a transaction so they can be undeleted if the install
547
        /// fails at a later stage.
548
        /// </summary>
549
        /// <param name="files">The files to overwrite</param>
550
        private static void DeleteConflictingFiles(IEnumerable<InstallableFile> files)
551
        {
2✔
552
            TxFileManager file_transaction = new TxFileManager();
2✔
553
            foreach (InstallableFile file in files)
5✔
554
            {
2✔
555
                log.DebugFormat("Trying to delete {0}", file.destination);
2✔
556
                file_transaction.Delete(file.destination);
2✔
557
            }
2✔
558
        }
2✔
559

560
        #endregion
561

562
        #region Find files
563

564
        /// <summary>
565
        /// Given a module and an open zipfile, return all the files that would be installed
566
        /// for this module.
567
        ///
568
        /// If a KSP instance is provided, it will be used to generate output paths, otherwise these will be null.
569
        ///
570
        /// Throws a BadMetadataKraken if the stanza resulted in no files being returned.
571
        /// </summary>
572

573
        public static List<InstallableFile> FindInstallableFiles(CkanModule    module,
574
                                                                 ZipFile       zipfile,
575
                                                                 GameInstance  inst)
576
            => FindInstallableFiles(module, zipfile, inst, inst.Game);
2✔
577

578
        public static List<InstallableFile> FindInstallableFiles(CkanModule module,
579
                                                                 ZipFile    zipfile,
580
                                                                 IGame      game)
581
            => FindInstallableFiles(module, zipfile, null, game);
2✔
582

583
        private static List<InstallableFile> FindInstallableFiles(CkanModule    module,
584
                                                                  ZipFile       zipfile,
585
                                                                  GameInstance? inst,
586
                                                                  IGame         game)
587
        {
2✔
588
            try
589
            {
2✔
590
                // Use the provided stanzas, or use the default install stanza if they're absent.
591
                return module.install is { Length: > 0 }
2✔
592
                    ? module.install
593
                            .SelectMany(stanza => stanza.FindInstallableFiles(zipfile, inst))
2✔
594
                            .ToList()
595
                    : ModuleInstallDescriptor.DefaultInstallStanza(game,
596
                                                                   module.identifier)
597
                                             .FindInstallableFiles(zipfile, inst);
598
            }
599
            catch (BadMetadataKraken kraken)
2✔
600
            {
2✔
601
                // Decorate our kraken with the current module, as the lower-level
602
                // methods won't know it.
603
                kraken.module ??= module;
2!
604
                throw;
2✔
605
            }
606
        }
2✔
607

608
        /// <summary>
609
        /// Given a module and a path to a zipfile, returns all the files that would be installed
610
        /// from that zip for this module.
611
        ///
612
        /// This *will* throw an exception if the file does not exist.
613
        ///
614
        /// Throws a BadMetadataKraken if the stanza resulted in no files being returned.
615
        ///
616
        /// If a KSP instance is provided, it will be used to generate output paths, otherwise these will be null.
617
        /// </summary>
618
        // TODO: Document which exception!
619
        public static List<InstallableFile> FindInstallableFiles(CkanModule  module,
620
                                                                 string      zip_filename,
621
                                                                 IGame       game)
622
        {
2✔
623
            // `using` makes sure our zipfile gets closed when we exit this block.
624
            using (ZipFile zipfile = new ZipFile(zip_filename))
2✔
625
            {
2✔
626
                log.DebugFormat("Searching {0} using {1} as module", zip_filename, module);
2✔
627
                return FindInstallableFiles(module, zipfile, null, game);
2✔
628
            }
629
        }
2✔
630

631
        public static List<InstallableFile> FindInstallableFiles(CkanModule   module,
632
                                                                 string       zip_filename,
633
                                                                 GameInstance inst)
634
        {
2✔
635
            // `using` makes sure our zipfile gets closed when we exit this block.
636
            using (ZipFile zipfile = new ZipFile(zip_filename))
2✔
637
            {
2✔
638
                log.DebugFormat("Searching {0} using {1} as module", zip_filename, module);
2✔
639
                return FindInstallableFiles(module, zipfile, inst);
2✔
640
            }
641
        }
2✔
642

643
        /// <summary>
644
        /// Returns contents of an installed module
645
        /// </summary>
646
        public static IEnumerable<(string path, bool dir, bool exists)> GetModuleContents(
647
                GameInstance                instance,
648
                IReadOnlyCollection<string> installed,
649
                HashSet<string>             filters)
650
            => GetModuleContents(instance, installed,
2✔
651
                                 installed.SelectMany(f => f.TraverseNodes(Path.GetDirectoryName)
2✔
652
                                                            .Skip(1)
653
                                                            .Where(s => s.Length > 0)
2✔
654
                                                            .Select(CKANPathUtils.NormalizePath))
655
                                          .ToHashSet(),
656
                                 filters);
657

658
        private static IEnumerable<(string path, bool dir, bool exists)> GetModuleContents(
659
                GameInstance                instance,
660
                IReadOnlyCollection<string> installed,
661
                HashSet<string>             parents,
662
                HashSet<string>             filters)
663
            => installed.Where(f => !filters.Any(filt => f.Contains(filt)))
1✔
664
                        .GroupBy(parents.Contains)
665
                        .SelectMany(grp =>
666
                            grp.Select(p => (path:   p,
2✔
667
                                             dir:    grp.Key,
668
                                             exists: grp.Key ? Directory.Exists(instance.ToAbsoluteGameDir(p))
669
                                                             : File.Exists(instance.ToAbsoluteGameDir(p)))));
670

671
        /// <summary>
672
        /// Returns the module contents if and only if we have it
673
        /// available in our cache, empty sequence otherwise.
674
        ///
675
        /// Intended for previews.
676
        /// </summary>
677
        public static IEnumerable<(string path, bool dir, bool exists)> GetModuleContents(
678
                NetModuleCache  Cache,
679
                GameInstance    instance,
680
                CkanModule      module,
681
                HashSet<string> filters)
682
            => (Cache.GetCachedFilename(module) is string filename
2!
683
                    ? GetModuleContents(instance,
684
                                        Utilities.DefaultIfThrows(
685
                                            () => FindInstallableFiles(module, filename, instance)),
2✔
686
                                        filters)
687
                    : null)
688
               ?? Enumerable.Empty<(string path, bool dir, bool exists)>();
689

690
        private static IEnumerable<(string path, bool dir, bool exists)>? GetModuleContents(
691
                GameInstance                  instance,
692
                IEnumerable<InstallableFile>? installable,
693
                HashSet<string>               filters)
694
            => installable?.Where(instF => !filters.Any(filt => instF.destination != null
1!
695
                                                                && instF.destination.Contains(filt)))
696
                           .Select(f => (path:   instance.ToRelativeGameDir(f.destination),
2✔
697
                                         dir:    f.source.IsDirectory,
698
                                         exists: true));
699

700
        #endregion
701

702
        /// <summary>
703
        /// Copy the entry from the opened zipfile to the path specified.
704
        /// </summary>
705
        /// <returns>
706
        /// Path of file or directory that was created.
707
        /// May differ from the input fullPath!
708
        /// Throws a FileExistsKraken if we were going to overwrite the file.
709
        /// </returns>
710
        internal static string? InstallFile(ZipFile          zipfile,
711
                                            ZipEntry         entry,
712
                                            string           fullPath,
713
                                            bool             makeDirs,
714
                                            string[]         candidateDuplicates,
715
                                            IProgress<long>? progress)
716
        {
2✔
717
            var file_transaction = new TxFileManager();
2✔
718

719
            if (entry.IsDirectory)
2✔
720
            {
2✔
721
                // Skip if we're not making directories for this install.
722
                if (!makeDirs)
2!
723
                {
×
724
                    log.DebugFormat("Skipping '{0}', we don't make directories for this path", fullPath);
×
725
                    return null;
×
726
                }
727

728
                // Windows silently trims trailing spaces, get the path it will actually use
729
                fullPath = Path.GetDirectoryName(Path.Combine(fullPath, "DUMMY")) is string p
2✔
730
                    ? CKANPathUtils.NormalizePath(p)
731
                    : fullPath;
732

733
                log.DebugFormat("Making directory '{0}'", fullPath);
2✔
734
                file_transaction.CreateDirectory(fullPath);
2✔
735
            }
2✔
736
            else
737
            {
2✔
738
                log.DebugFormat("Writing file '{0}'", fullPath);
2✔
739

740
                // ZIP format does not require directory entries
741
                if (makeDirs && Path.GetDirectoryName(fullPath) is string d)
2✔
742
                {
2✔
743
                    log.DebugFormat("Making parent directory '{0}'", d);
2✔
744
                    file_transaction.CreateDirectory(d);
2✔
745
                }
2✔
746

747
                // We don't allow for the overwriting of files. See #208.
748
                if (file_transaction.FileExists(fullPath))
2✔
749
                {
2✔
750
                    throw new FileExistsKraken(fullPath);
2✔
751
                }
752

753
                // Snapshot whatever was there before. If there's nothing, this will just
754
                // remove our file on rollback. We still need this even though we won't
755
                // overwite files, as it ensures deletion on rollback.
756
                file_transaction.Snapshot(fullPath);
2✔
757

758
                // Try making hard links if already installed in another instance (faster, less space)
759
                foreach (var installedSource in candidateDuplicates)
5✔
760
                {
2✔
761
                    try
762
                    {
2✔
763
                        HardLink.Create(installedSource, fullPath);
2✔
764
                        return fullPath;
2✔
765
                    }
766
                    catch
×
767
                    {
×
768
                        // If hard link creation fails, try more hard links, or copy if none work
769
                    }
×
770
                }
×
771

772
                try
773
                {
2✔
774
                    // It's a file! Prepare the streams
775
                    using (var zipStream = zipfile.GetInputStream(entry))
2✔
776
                    using (var writer = File.Create(fullPath))
2✔
777
                    {
2✔
778
                        // Windows silently changes paths ending with spaces, get the name it actually used
779
                        fullPath = CKANPathUtils.NormalizePath(writer.Name);
2✔
780
                        // 4k is the block size on practically every disk and OS.
781
                        byte[] buffer = new byte[4096];
2✔
782
                        progress?.Report(0);
2✔
783
                        StreamUtils.Copy(zipStream, writer, buffer,
2✔
784
                                         // This doesn't fire at all if the interval never elapses
785
                                         (sender, e) => progress?.Report(e.Processed),
2✔
786
                                         UnzipProgressInterval,
787
                                         entry, "InstallFile");
788
                    }
2✔
789
                }
2✔
790
                catch (DirectoryNotFoundException ex)
×
791
                {
×
792
                    throw new DirectoryNotFoundKraken("", ex.Message, ex);
×
793
                }
794
            }
2✔
795
            // Usually, this is the path we're given.
796
            // Sometimes it has trailing spaces trimmed by the OS.
797
            return fullPath;
2✔
798
        }
2✔
799

800
        private static readonly TimeSpan UnzipProgressInterval = TimeSpan.FromMilliseconds(200);
2✔
801

802
        #endregion
803

804
        #region Uninstallation
805

806
        /// <summary>
807
        /// Uninstalls all the mods provided, including things which depend upon them.
808
        /// This *DOES* save the registry.
809
        /// Preferred over Uninstall.
810
        /// </summary>
811
        public void UninstallList(IEnumerable<string>              mods,
812
                                  ref HashSet<string>?             possibleConfigOnlyDirs,
813
                                  RegistryManager                  registry_manager,
814
                                  bool                             ConfirmPrompt = true,
815
                                  IReadOnlyCollection<CkanModule>? installing    = null)
816
        {
2✔
817
            mods = mods.Memoize();
2✔
818
            // Pre-check, have they even asked for things which are installed?
819

820
            foreach (string mod in mods.Where(mod => registry_manager.registry.InstalledModule(mod) == null))
2!
821
            {
2✔
822
                throw new ModNotInstalledKraken(mod);
2✔
823
            }
824

825
            var instDlc = mods.Select(registry_manager.registry.InstalledModule)
2✔
826
                              .OfType<InstalledModule>()
827
                              .FirstOrDefault(m => m.Module.IsDLC);
2✔
828
            if (instDlc != null)
2!
829
            {
×
830
                throw new ModuleIsDLCKraken(instDlc.Module);
×
831
            }
832

833
            // Find all the things which need uninstalling.
834
            var revdep = mods
2✔
835
                .Union(registry_manager.registry.FindReverseDependencies(
836
                    mods.Except(installing?.Select(m => m.identifier) ?? Array.Empty<string>())
×
837
                        .ToArray(),
838
                    installing))
839
                .ToArray();
840

841
            var goners = revdep.Union(
2✔
842
                    registry_manager.registry.FindRemovableAutoInstalled(
843
                        registry_manager.registry.InstalledModules
844
                            .Where(im => !revdep.Contains(im.identifier))
2✔
845
                            .ToArray(),
846
                        installing ?? Array.Empty<CkanModule>(),
847
                        instance.Game, instance.StabilityToleranceConfig,
848
                        instance.VersionCriteria())
849
                    .Select(im => im.identifier))
2✔
850
                .Order()
851
                .ToArray();
852

853
            // If there is nothing to uninstall, skip out.
854
            if (goners.Length == 0)
2✔
855
            {
2✔
856
                return;
2✔
857
            }
858

859
            User.RaiseMessage(Properties.Resources.ModuleInstallerAboutToRemove);
2✔
860
            User.RaiseMessage("");
2✔
861

862
            foreach (var module in goners.Select(registry_manager.registry.InstalledModule)
5✔
863
                                         .OfType<InstalledModule>())
864
            {
2✔
865
                User.RaiseMessage(" * {0} {1}", module.Module.name, module.Module.version);
2✔
866
            }
2✔
867

868
            if (ConfirmPrompt && !User.RaiseYesNoDialog(Properties.Resources.ModuleInstallerContinuePrompt))
2!
869
            {
×
870
                throw new CancelledActionKraken(Properties.Resources.ModuleInstallerRemoveAborted);
×
871
            }
872

873
            using (var transaction = CkanTransaction.CreateTransactionScope())
2✔
874
            {
2✔
875
                var registry = registry_manager.registry;
2✔
876
                long removeBytes = goners.Select(registry.InstalledModule)
2✔
877
                                         .OfType<InstalledModule>()
878
                                         .Sum(m => m.Module.install_size);
2✔
879
                var rateCounter = new ByteRateCounter()
2✔
880
                {
881
                    Size      = removeBytes,
882
                    BytesLeft = removeBytes,
883
                };
884
                rateCounter.Start();
2✔
885

886
                long modRemoveCompletedBytes = 0;
2✔
887
                foreach (string ident in goners)
5✔
888
                {
2✔
889
                    if (registry.InstalledModule(ident) is InstalledModule instMod)
2!
890
                    {
2✔
891
                        Uninstall(ident, ref possibleConfigOnlyDirs, registry,
2✔
892
                                  new ProgressImmediate<long>(bytes =>
893
                                  {
2✔
894
                                      RemoveProgress?.Invoke(instMod,
2!
895
                                                             Math.Max(0,     instMod.Module.install_size - bytes),
896
                                                             Math.Max(bytes, instMod.Module.install_size));
897
                                      rateCounter.BytesLeft = removeBytes - (modRemoveCompletedBytes
2✔
898
                                                                             + Math.Min(bytes, instMod.Module.install_size));
899
                                      User.RaiseProgress(rateCounter);
2✔
900
                                  }));
2✔
901
                        modRemoveCompletedBytes += instMod?.Module.install_size ?? 0;
2✔
902
                    }
2✔
903
                }
2✔
904

905
                // Enforce consistency if we're not installing anything,
906
                // otherwise consistency will be enforced after the installs
907
                registry_manager.Save(installing == null);
2✔
908

909
                transaction.Complete();
2✔
910
            }
2✔
911

912
            User.RaiseProgress(Properties.Resources.ModuleInstallerDone, 100);
2✔
913
        }
2✔
914

915
        /// <summary>
916
        /// Uninstall the module provided. For internal use only.
917
        /// Use UninstallList for user queries, it also does dependency handling.
918
        /// This does *NOT* save the registry.
919
        /// </summary>
920
        /// <param name="identifier">Identifier of module to uninstall</param>
921
        /// <param name="possibleConfigOnlyDirs">Directories that the user might want to remove after uninstall</param>
922
        /// <param name="registry">Registry to use</param>
923
        /// <param name="progress">Progress to report</param>
924
        private void Uninstall(string               identifier,
925
                               ref HashSet<string>? possibleConfigOnlyDirs,
926
                               Registry             registry,
927
                               IProgress<long>      progress)
928
        {
2✔
929
            var file_transaction = new TxFileManager();
2✔
930

931
            using (var transaction = CkanTransaction.CreateTransactionScope())
2✔
932
            {
2✔
933
                var instMod = registry.InstalledModule(identifier);
2✔
934

935
                if (instMod == null)
2!
936
                {
×
937
                    log.ErrorFormat("Trying to uninstall {0} but it's not installed", identifier);
×
938
                    throw new ModNotInstalledKraken(identifier);
×
939
                }
940
                User.RaiseMessage(Properties.Resources.ModuleInstallerRemovingMod,
2✔
941
                                  $"{instMod.Module.name} {instMod.Module.version}");
942

943
                // Walk our registry to find all files for this mod.
944
                var modFiles = instMod.Files.ToArray();
2✔
945

946
                // We need case insensitive path matching on Windows
947
                var directoriesToDelete = new HashSet<string>(Platform.PathComparer);
2✔
948

949
                // Files that Windows refused to delete due to locking (probably)
950
                var undeletableFiles = new List<string>();
2✔
951

952
                long bytesDeleted = 0;
2✔
953
                foreach (string relPath in modFiles)
5✔
954
                {
2✔
955
                    if (cancelToken.IsCancellationRequested)
2!
956
                    {
×
957
                        throw new CancelledActionKraken();
×
958
                    }
959

960
                    string absPath = instance.ToAbsoluteGameDir(relPath);
2✔
961

962
                    try
963
                    {
2✔
964
                        if (File.GetAttributes(absPath)
2✔
965
                                .HasFlag(FileAttributes.Directory))
966
                        {
2✔
967
                            directoriesToDelete.Add(absPath);
2✔
968
                        }
2✔
969
                        else
970
                        {
2✔
971
                            // Add this file's directory to the list for deletion if it isn't already there.
972
                            // Helps clean up directories when modules are uninstalled out of dependency order
973
                            // Since we check for directory contents when deleting, this should purge empty
974
                            // dirs, making less ModuleManager headaches for people.
975
                            if (Path.GetDirectoryName(absPath) is string p)
2!
976
                            {
2✔
977
                                directoriesToDelete.Add(p);
2✔
978
                            }
2✔
979

980
                            bytesDeleted += new FileInfo(absPath).Length;
2✔
981
                            progress.Report(bytesDeleted);
2✔
982
                            log.DebugFormat("Removing {0}", relPath);
2✔
983
                            file_transaction.Delete(absPath);
2✔
984
                        }
2✔
985
                    }
2✔
986
                    catch (FileNotFoundException exc)
2✔
987
                    {
2✔
988
                        log.Debug("Ignoring missing file while deleting", exc);
2✔
989
                    }
2✔
990
                    catch (DirectoryNotFoundException exc)
2✔
991
                    {
2✔
992
                        log.Debug("Ignoring missing directory while deleting", exc);
2✔
993
                    }
2✔
994
                    catch (IOException)
×
995
                    {
×
996
                        // "The specified file is in use."
997
                        undeletableFiles.Add(relPath);
×
998
                    }
×
999
                    catch (UnauthorizedAccessException)
×
1000
                    {
×
1001
                        // "The caller does not have the required permission."
1002
                        // "The file is an executable file that is in use."
1003
                        undeletableFiles.Add(relPath);
×
1004
                    }
×
1005
                    catch (Exception exc)
×
1006
                    {
×
1007
                        // We don't consider this problem serious enough to abort and revert,
1008
                        // so treat it as a "--verbose" level log message.
1009
                        log.InfoFormat("Failure in locating file {0}: {1}", absPath, exc.Message);
×
1010
                    }
×
1011
                }
2✔
1012

1013
                if (undeletableFiles.Count > 0)
2!
1014
                {
×
1015
                    throw new FailedToDeleteFilesKraken(identifier, undeletableFiles);
×
1016
                }
1017

1018
                // Remove from registry.
1019
                registry.DeregisterModule(instance, identifier);
2✔
1020

1021
                // Our collection of directories may leave empty parent directories.
1022
                directoriesToDelete = AddParentDirectories(directoriesToDelete);
2✔
1023

1024
                // Sort our directories from longest to shortest, to make sure we remove child directories
1025
                // before parents. GH #78.
1026
                foreach (string directory in directoriesToDelete.OrderByDescending(dir => dir.Length))
2✔
1027
                {
2✔
1028
                    log.DebugFormat("Checking {0}...", directory);
2✔
1029
                    // It is bad if any of this directories gets removed
1030
                    // So we protect them
1031
                    // A few string comparisons will be cheaper than hitting the disk, so do this first
1032
                    if (instance.Game.IsReservedDirectory(instance, directory))
2!
1033
                    {
×
1034
                        log.DebugFormat("Directory {0} is reserved, skipping", directory);
×
1035
                        continue;
×
1036
                    }
1037

1038
                    // See what's left in this folder and what we can do about it
1039
                    GroupFilesByRemovable(instance.ToRelativeGameDir(directory),
2✔
1040
                                          registry, modFiles, instance.Game,
1041
                                          (Directory.Exists(directory)
1042
                                              ? Directory.EnumerateFileSystemEntries(directory, "*", SearchOption.AllDirectories)
1043
                                              : Enumerable.Empty<string>())
1044
                                           .Select(instance.ToRelativeGameDir)
1045
                                           .ToArray(),
1046
                                          out string[] removable,
1047
                                          out string[] notRemovable);
1048

1049
                    // Delete the auto-removable files and dirs
1050
                    foreach (var absPath in removable.Select(instance.ToAbsoluteGameDir))
5✔
1051
                    {
2✔
1052
                        if (File.Exists(absPath))
2✔
1053
                        {
2✔
1054
                            log.DebugFormat("Attempting transaction deletion of file {0}", absPath);
2✔
1055
                            file_transaction.Delete(absPath);
2✔
1056
                        }
2✔
1057
                        else if (Directory.Exists(absPath))
2!
1058
                        {
2✔
1059
                            log.DebugFormat("Attempting deletion of directory {0}", absPath);
2✔
1060
                            try
1061
                            {
2✔
1062
                                Directory.Delete(absPath);
2✔
1063
                            }
2✔
1064
                            catch
×
1065
                            {
×
1066
                                // There might be files owned by other mods, oh well
1067
                                log.DebugFormat("Failed to delete {0}", absPath);
×
1068
                            }
×
1069
                        }
2✔
1070
                    }
2✔
1071

1072
                    if (notRemovable.Length < 1)
2✔
1073
                    {
2✔
1074
                        // We *don't* use our file_transaction to delete files here, because
1075
                        // it fails if the system's temp directory is on a different device
1076
                        // to KSP. However we *can* safely delete it now we know it's empty,
1077
                        // because the TxFileMgr *will* put it back if there's a file inside that
1078
                        // needs it.
1079
                        //
1080
                        // This works around GH #251.
1081
                        // The filesystem boundary bug is described in https://transactionalfilemgr.codeplex.com/workitem/20
1082

1083
                        log.DebugFormat("Removing {0}", directory);
2✔
1084
                        Directory.Delete(directory);
2✔
1085
                    }
2✔
1086
                    else if (notRemovable.Except(possibleConfigOnlyDirs?.Select(instance.ToRelativeGameDir)
2!
1087
                                                 ?? Enumerable.Empty<string>())
1088
                                         // Can't remove if owned by some other mod
1089
                                         .Any(relPath => registry.FileOwner(relPath) != null
2!
1090
                                                         || modFiles.Contains(relPath)))
1091
                    {
×
1092
                        log.InfoFormat("Not removing directory {0}, it's not empty", directory);
×
1093
                    }
×
1094
                    else
1095
                    {
2✔
1096
                        log.DebugFormat("Directory {0} contains only non-registered files, ask user about it later: {1}",
2✔
1097
                                        directory,
1098
                                        string.Join(", ", notRemovable));
1099
                        possibleConfigOnlyDirs ??= new HashSet<string>(Platform.PathComparer);
2!
1100
                        possibleConfigOnlyDirs.Add(directory);
2✔
1101
                    }
2✔
1102
                }
2✔
1103
                log.InfoFormat("Removed {0}", identifier);
2✔
1104
                transaction.Complete();
2✔
1105
                User.RaiseMessage(Properties.Resources.ModuleInstallerRemovedMod,
2✔
1106
                                  $"{instMod.Module.name} {instMod.Module.version}");
1107
            }
2✔
1108
        }
2✔
1109

1110
        internal static void GroupFilesByRemovable(string                      relRoot,
1111
                                                   Registry                    registry,
1112
                                                   IReadOnlyCollection<string> alreadyRemoving,
1113
                                                   IGame                       game,
1114
                                                   IReadOnlyCollection<string> relPaths,
1115
                                                   out string[]                removable,
1116
                                                   out string[]                notRemovable)
1117
        {
2✔
1118
            if (relPaths.Count < 1)
2✔
1119
            {
2✔
1120
                removable    = Array.Empty<string>();
2✔
1121
                notRemovable = Array.Empty<string>();
2✔
1122
                return;
2✔
1123
            }
1124
            log.DebugFormat("Getting contents of {0}", relRoot);
2✔
1125
            var contents = relPaths
2✔
1126
                // Split into auto-removable and not-removable
1127
                // Removable must not be owned by other mods
1128
                .GroupBy(f => registry.FileOwner(f) == null
2✔
1129
                              // Also skip owned by this module since it's already deregistered
1130
                              && !alreadyRemoving.Contains(f)
1131
                              // Must have a removable dir name somewhere in path AFTER main dir
1132
                              && f[relRoot.Length..]
1133
                                  .Split('/')
1134
                                  .Where(piece => !string.IsNullOrEmpty(piece))
2✔
1135
                                  .Any(piece => game.AutoRemovableDirs.Contains(piece)))
2✔
1136
                .ToDictionary(grp => grp.Key,
2✔
1137
                              grp => grp.OrderByDescending(f => f.Length)
2✔
1138
                                        .ToArray());
1139
            removable    = contents.GetValueOrDefault(true)  ?? Array.Empty<string>();
2✔
1140
            notRemovable = contents.GetValueOrDefault(false) ?? Array.Empty<string>();
2✔
1141
            log.DebugFormat("Got removable: {0}",    string.Join(", ", removable));
2✔
1142
            log.DebugFormat("Got notRemovable: {0}", string.Join(", ", notRemovable));
2✔
1143
        }
2✔
1144

1145
        /// <summary>
1146
        /// Takes a collection of directories and adds all parent directories within the GameData structure.
1147
        /// </summary>
1148
        /// <param name="directories">The collection of directory path strings to examine</param>
1149
        public HashSet<string> AddParentDirectories(HashSet<string> directories)
1150
        {
2✔
1151
            var gameDir = CKANPathUtils.NormalizePath(instance.GameDir);
2✔
1152
            return directories
2✔
1153
                .Where(dir => !string.IsNullOrWhiteSpace(dir))
2✔
1154
                // Normalize all paths before deduplicate
1155
                .Select(CKANPathUtils.NormalizePath)
1156
                // Remove any duplicate paths
1157
                .Distinct()
1158
                .SelectMany(dir =>
1159
                {
2✔
1160
                    var results = new HashSet<string>(Platform.PathComparer);
2✔
1161
                    // Adding in the DirectorySeparatorChar fixes attempts on Windows
1162
                    // to parse "X:" which resolves to Environment.CurrentDirectory
1163
                    var dirInfo = new DirectoryInfo(
2✔
1164
                        dir.EndsWith("/") ? dir : dir + Path.DirectorySeparatorChar);
1165

1166
                    // If this is a parentless directory (Windows)
1167
                    // or if the Root equals the current directory (Mono)
1168
                    if (dirInfo.Parent == null || dirInfo.Root == dirInfo)
2✔
1169
                    {
2✔
1170
                        return results;
2✔
1171
                    }
1172

1173
                    if (!dir.StartsWith(gameDir, Platform.PathComparison))
2!
1174
                    {
×
1175
                        dir = CKANPathUtils.ToAbsolute(dir, gameDir);
×
1176
                    }
×
1177

1178
                    // Remove the system paths, leaving the path under the instance directory
1179
                    var relativeHead = CKANPathUtils.ToRelative(dir, gameDir);
2✔
1180
                    // Don't try to remove GameRoot
1181
                    if (!string.IsNullOrEmpty(relativeHead))
2✔
1182
                    {
2✔
1183
                        var pathArray = relativeHead.Split('/');
2✔
1184
                        var builtPath = "";
2✔
1185
                        foreach (var path in pathArray)
5✔
1186
                        {
2✔
1187
                            builtPath += path + '/';
2✔
1188
                            results.Add(CKANPathUtils.ToAbsolute(builtPath, gameDir));
2✔
1189
                        }
2✔
1190
                    }
2✔
1191

1192
                    return results;
2✔
1193
                })
2✔
1194
                .Where(dir => !instance.Game.IsReservedDirectory(instance, dir))
2✔
1195
                .ToHashSet();
1196
        }
2✔
1197

1198
        #endregion
1199

1200
        #region AddRemove
1201

1202
        /// <summary>
1203
        /// Adds and removes the listed modules as a single transaction.
1204
        /// No relationships will be processed.
1205
        /// This *will* save the registry.
1206
        /// </summary>
1207
        /// <param name="possibleConfigOnlyDirs">Directories that the user might want to remove after uninstall</param>
1208
        /// <param name="registry_manager">Registry to use</param>
1209
        /// <param name="resolver">Relationship resolver to use</param>
1210
        /// <param name="add">Modules to add</param>
1211
        /// <param name="autoInstalled">true or false for each item in `add`</param>
1212
        /// <param name="remove">Modules to remove</param>
1213
        /// <param name="downloader">Downloader to use</param>
1214
        /// <param name="deduper">Deduplicator to use</param>
1215
        /// <param name="enforceConsistency">Whether to enforce consistency</param>
1216
        private void AddRemove(ref HashSet<string>?                 possibleConfigOnlyDirs,
1217
                               RegistryManager                      registry_manager,
1218
                               RelationshipResolver                 resolver,
1219
                               IReadOnlyCollection<CkanModule>      add,
1220
                               IDictionary<CkanModule, bool>        autoInstalled,
1221
                               IReadOnlyCollection<InstalledModule> remove,
1222
                               IDownloader                          downloader,
1223
                               bool                                 enforceConsistency,
1224
                               InstalledFilesDeduplicator?          deduper = null)
1225
        {
2✔
1226
            using (var tx = CkanTransaction.CreateTransactionScope())
2✔
1227
            {
2✔
1228
                var groups = add.GroupBy(m => m.IsMetapackage || cache.IsCached(m));
2!
1229
                var cached = groups.FirstOrDefault(grp => grp.Key)?.ToArray()
2✔
1230
                                                                  ?? Array.Empty<CkanModule>();
1231
                var toDownload = groups.FirstOrDefault(grp => !grp.Key)?.ToArray()
2✔
1232
                                                                       ?? Array.Empty<CkanModule>();
1233

1234
                long removeBytes     = remove.Sum(m => m.Module.install_size);
2✔
1235
                long removedBytes    = 0;
2✔
1236
                long downloadBytes   = toDownload.Sum(m => m.download_size);
1✔
1237
                long downloadedBytes = 0;
2✔
1238
                long installBytes    = add.Sum(m => m.install_size);
2✔
1239
                long installedBytes  = 0;
2✔
1240
                var rateCounter = new ByteRateCounter()
2✔
1241
                {
1242
                    Size      = removeBytes + downloadBytes + installBytes,
1243
                    BytesLeft = removeBytes + downloadBytes + installBytes,
1244
                };
1245
                rateCounter.Start();
2✔
1246

1247
                downloader.OverallDownloadProgress += brc =>
2✔
1248
                {
×
1249
                    downloadedBytes = downloadBytes - brc.BytesLeft;
×
1250
                    rateCounter.BytesLeft = removeBytes   - removedBytes
×
1251
                                          + downloadBytes - downloadedBytes
1252
                                          + installBytes  - installedBytes;
1253
                    User.RaiseProgress(rateCounter);
×
1254
                };
×
1255
                var toInstall = ModsInDependencyOrder(resolver, cached, toDownload, downloader);
2✔
1256

1257
                long modRemoveCompletedBytes = 0;
2✔
1258
                foreach (var instMod in remove)
5✔
1259
                {
2✔
1260
                    Uninstall(instMod.Module.identifier,
2✔
1261
                              ref possibleConfigOnlyDirs,
1262
                              registry_manager.registry,
1263
                              new ProgressImmediate<long>(bytes =>
1264
                              {
2✔
1265
                                  RemoveProgress?.Invoke(instMod,
2!
1266
                                                         Math.Max(0,     instMod.Module.install_size - bytes),
1267
                                                         Math.Max(bytes, instMod.Module.install_size));
1268
                                  removedBytes = modRemoveCompletedBytes
2✔
1269
                                                 + Math.Min(bytes, instMod.Module.install_size);
1270
                                  rateCounter.BytesLeft = removeBytes   - removedBytes
2✔
1271
                                                        + downloadBytes - downloadedBytes
1272
                                                        + installBytes  - installedBytes;
1273
                                  User.RaiseProgress(rateCounter);
2✔
1274
                              }));
2✔
1275
                     modRemoveCompletedBytes += instMod.Module.install_size;
2✔
1276
                }
2✔
1277

1278
                var gameDir = new DirectoryInfo(instance.GameDir);
2✔
1279
                long modInstallCompletedBytes = 0;
2✔
1280
                foreach (var mod in toInstall)
5✔
1281
                {
2✔
1282
                    CKANPathUtils.CheckFreeSpace(gameDir, mod.install_size,
2✔
1283
                                                 Properties.Resources.NotEnoughSpaceToInstall);
1284
                    Install(mod,
2✔
1285
                            // For upgrading, new modules are dependencies and should be marked auto-installed,
1286
                            // for replacing, new modules are the replacements and should not be marked auto-installed
1287
                            remove?.FirstOrDefault(im => im.Module.identifier == mod.identifier)
2✔
1288
                                  ?.AutoInstalled
1289
                                  ?? autoInstalled[mod],
1290
                            registry_manager.registry,
1291
                            deduper?.ModuleCandidateDuplicates(mod.identifier, mod.version),
1292
                            ref possibleConfigOnlyDirs,
1293
                            new ProgressImmediate<long>(bytes =>
1294
                            {
2✔
1295
                                InstallProgress?.Invoke(mod,
2!
1296
                                                        Math.Max(0,     mod.install_size - bytes),
1297
                                                        Math.Max(bytes, mod.install_size));
1298
                                installedBytes = modInstallCompletedBytes
2✔
1299
                                                 + Math.Min(bytes, mod.install_size);
1300
                                rateCounter.BytesLeft = removeBytes   - removedBytes
2✔
1301
                                                      + downloadBytes - downloadedBytes
1302
                                                      + installBytes  - installedBytes;
1303
                                User.RaiseProgress(rateCounter);
2✔
1304
                            }));
2✔
1305
                    modInstallCompletedBytes += mod.install_size;
2✔
1306
                }
2✔
1307

1308
                registry_manager.Save(enforceConsistency);
2✔
1309
                tx.Complete();
2✔
1310
                EnforceCacheSizeLimit(registry_manager.registry, cache, config);
2✔
1311
            }
2✔
1312
        }
2✔
1313

1314
        /// <summary>
1315
        /// Upgrades or installs the mods listed to the specified versions for the user's KSP.
1316
        /// Will *re-install* or *downgrade* (with a warning) as well as upgrade.
1317
        /// Throws ModuleNotFoundKraken if a module is not installed.
1318
        /// </summary>
1319
        public void Upgrade(in IReadOnlyCollection<CkanModule> modules,
1320
                            IDownloader                        downloader,
1321
                            ref HashSet<string>?               possibleConfigOnlyDirs,
1322
                            RegistryManager                    registry_manager,
1323
                            InstalledFilesDeduplicator?        deduper            = null,
1324
                            bool                               enforceConsistency = true,
1325
                            bool                               ConfirmPrompt      = true)
1326
        {
2✔
1327
            var registry = registry_manager.registry;
2✔
1328

1329
            var removingIdents = registry.InstalledModules.Select(im => im.identifier)
2✔
1330
                                         .Intersect(modules.Select(m => m.identifier))
2✔
1331
                                         .ToHashSet();
1332
            var autoRemoving = registry
2✔
1333
                .FindRemovableAutoInstalled(
1334
                    // Conjure the future state of the installed modules list after upgrading
1335
                    registry.InstalledModules
1336
                            .Where(im => !removingIdents.Contains(im.identifier))
2✔
1337
                            .ToArray(),
1338
                    modules,
1339
                    instance.Game, instance.StabilityToleranceConfig, instance.VersionCriteria())
1340
                .ToHashSet();
1341

1342
            var resolver = new RelationshipResolver(
2✔
1343
                modules,
1344
                modules.Select(m => registry.InstalledModule(m.identifier)?.Module)
2!
1345
                       .OfType<CkanModule>()
1346
                       .Concat(autoRemoving.Select(im => im.Module)),
2✔
1347
                RelationshipResolverOptions.DependsOnlyOpts(instance.StabilityToleranceConfig),
1348
                registry,
1349
                instance.Game, instance.VersionCriteria());
1350
            var fullChangeset = resolver.ModList()
2✔
1351
                                        .ToDictionary(m => m.identifier,
2✔
1352
                                                      m => m);
2✔
1353

1354
            // Skip removing ones we still need
1355
            var keepIdents = fullChangeset.Keys.Intersect(autoRemoving.Select(im => im.Module.identifier))
2✔
1356
                                               .ToHashSet();
1357
            autoRemoving.RemoveWhere(im => keepIdents.Contains(im.Module.identifier));
2✔
1358
            foreach (var ident in keepIdents)
5✔
1359
            {
2✔
1360
                fullChangeset.Remove(ident);
2✔
1361
            }
2✔
1362

1363
            // Only install stuff that's already there if explicitly requested in param
1364
            var toInstall = fullChangeset.Values
2✔
1365
                                         .Except(registry.InstalledModules
1366
                                                         .Select(im => im.Module)
2✔
1367
                                                         .Except(modules))
1368
                                         .ToArray();
1369
            var autoInstalled = toInstall.ToDictionary(m => m, resolver.IsAutoInstalled);
2✔
1370

1371
            User.RaiseMessage(Properties.Resources.ModuleInstallerAboutToUpgrade);
2✔
1372
            User.RaiseMessage("");
2✔
1373

1374
            // Our upgrade involves removing everything that's currently installed, then
1375
            // adding everything that needs installing (which may involve new mods to
1376
            // satisfy dependencies). We always know the list passed in is what we need to
1377
            // install, but we need to calculate what needs to be removed.
1378
            var toRemove = new List<InstalledModule>();
2✔
1379

1380
            // Let's discover what we need to do with each module!
1381
            foreach (CkanModule module in toInstall)
5✔
1382
            {
2✔
1383
                var installed_mod = registry.InstalledModule(module.identifier);
2✔
1384

1385
                if (installed_mod == null)
2✔
1386
                {
2✔
1387
                    if (!cache.IsMaybeCachedZip(module)
2!
1388
                        && cache.GetInProgressFileName(module) is FileInfo inProgressFile)
1389
                    {
×
1390
                        if (inProgressFile.Exists)
×
1391
                        {
×
1392
                            User.RaiseMessage(Properties.Resources.ModuleInstallerUpgradeInstallingResuming,
×
1393
                                              module.name, module.version,
1394
                                              string.Join(", ", PrioritizedHosts(config, module.download)),
1395
                                              CkanModule.FmtSize(module.download_size - inProgressFile.Length));
1396
                        }
×
1397
                        else
1398
                        {
×
1399
                            User.RaiseMessage(Properties.Resources.ModuleInstallerUpgradeInstallingUncached,
×
1400
                                              module.name, module.version,
1401
                                              string.Join(", ", PrioritizedHosts(config, module.download)),
1402
                                              CkanModule.FmtSize(module.download_size));
1403
                        }
×
1404
                    }
×
1405
                    else
1406
                    {
2✔
1407
                        User.RaiseMessage(Properties.Resources.ModuleInstallerUpgradeInstallingCached,
2✔
1408
                                          module.name, module.version);
1409
                    }
2✔
1410
                }
2✔
1411
                else
1412
                {
2✔
1413
                    // Module already installed. We'll need to remove it first.
1414
                    toRemove.Add(installed_mod);
2✔
1415

1416
                    CkanModule installed = installed_mod.Module;
2✔
1417
                    if (installed.version.Equals(module.version))
2!
1418
                    {
×
1419
                        User.RaiseMessage(Properties.Resources.ModuleInstallerUpgradeReinstalling,
×
1420
                                          module.name, module.version);
1421
                    }
×
1422
                    else if (installed.version.IsGreaterThan(module.version))
2✔
1423
                    {
2✔
1424
                        User.RaiseMessage(Properties.Resources.ModuleInstallerUpgradeDowngrading,
2✔
1425
                                          module.name, installed.version, module.version);
1426
                    }
2✔
1427
                    else
1428
                    {
2✔
1429
                        if (!cache.IsMaybeCachedZip(module)
2!
1430
                            && cache.GetInProgressFileName(module) is FileInfo inProgressFile)
1431
                        {
×
1432
                            if (inProgressFile.Exists)
×
1433
                            {
×
1434
                                User.RaiseMessage(Properties.Resources.ModuleInstallerUpgradeUpgradingResuming,
×
1435
                                                  module.name, installed.version, module.version,
1436
                                                  string.Join(", ", PrioritizedHosts(config, module.download)),
1437
                                                  CkanModule.FmtSize(module.download_size - inProgressFile.Length));
1438
                            }
×
1439
                            else
1440
                            {
×
1441
                                User.RaiseMessage(Properties.Resources.ModuleInstallerUpgradeUpgradingUncached,
×
1442
                                                  module.name, installed.version, module.version,
1443
                                                  string.Join(", ", PrioritizedHosts(config, module.download)),
1444
                                                  CkanModule.FmtSize(module.download_size));
1445
                            }
×
1446
                        }
×
1447
                        else
1448
                        {
2✔
1449
                            User.RaiseMessage(Properties.Resources.ModuleInstallerUpgradeUpgradingCached,
2✔
1450
                                              module.name, installed.version, module.version);
1451
                        }
2✔
1452
                    }
2✔
1453
                }
2✔
1454
            }
2✔
1455

1456
            if (autoRemoving.Count > 0)
2✔
1457
            {
2✔
1458
                foreach (var im in autoRemoving)
5✔
1459
                {
2✔
1460
                    User.RaiseMessage(Properties.Resources.ModuleInstallerUpgradeAutoRemoving,
2✔
1461
                                      im.Module.name, im.Module.version);
1462
                }
2✔
1463
                toRemove.AddRange(autoRemoving);
2✔
1464
            }
2✔
1465

1466
            CheckAddRemoveFreeSpace(toInstall, toRemove);
2✔
1467

1468
            if (ConfirmPrompt && !User.RaiseYesNoDialog(Properties.Resources.ModuleInstallerContinuePrompt))
2!
1469
            {
×
1470
                throw new CancelledActionKraken(Properties.Resources.ModuleInstallerUpgradeUserDeclined);
×
1471
            }
1472

1473
            AddRemove(ref possibleConfigOnlyDirs,
2✔
1474
                      registry_manager,
1475
                      resolver,
1476
                      toInstall,
1477
                      autoInstalled,
1478
                      toRemove,
1479
                      downloader,
1480
                      enforceConsistency,
1481
                      deduper);
1482
            User.RaiseProgress(Properties.Resources.ModuleInstallerDone, 100);
2✔
1483
        }
2✔
1484

1485
        /// <summary>
1486
        /// Enacts listed Module Replacements to the specified versions for the user's KSP.
1487
        /// Will *re-install* or *downgrade* (with a warning) as well as upgrade.
1488
        /// </summary>
1489
        /// <exception cref="DependenciesNotSatisfiedKraken">Thrown if a dependency for a replacing module couldn't be satisfied.</exception>
1490
        /// <exception cref="ModuleNotFoundKraken">Thrown if a module that should be replaced is not installed.</exception>
1491
        public void Replace(IEnumerable<ModuleReplacement> replacements,
1492
                            RelationshipResolverOptions    options,
1493
                            IDownloader                    downloader,
1494
                            ref HashSet<string>?           possibleConfigOnlyDirs,
1495
                            RegistryManager                registry_manager,
1496
                            InstalledFilesDeduplicator?    deduper = null,
1497
                            bool                           enforceConsistency = true)
1498
        {
2✔
1499
            replacements = replacements.Memoize();
2✔
1500
            log.Debug("Using Replace method");
2✔
1501
            var modsToInstall = new List<CkanModule>();
2✔
1502
            var modsToRemove  = new List<InstalledModule>();
2✔
1503
            foreach (ModuleReplacement repl in replacements)
5✔
1504
            {
2✔
1505
                modsToInstall.Add(repl.ReplaceWith);
2✔
1506
                log.DebugFormat("We want to install {0} as a replacement for {1}", repl.ReplaceWith.identifier, repl.ToReplace.identifier);
2✔
1507
            }
2✔
1508

1509
            // Our replacement involves removing the currently installed mods, then
1510
            // adding everything that needs installing (which may involve new mods to
1511
            // satisfy dependencies).
1512

1513
            // Let's discover what we need to do with each module!
1514
            foreach (ModuleReplacement repl in replacements)
5✔
1515
            {
2✔
1516
                string ident = repl.ToReplace.identifier;
2✔
1517
                var installedMod = registry_manager.registry.InstalledModule(ident);
2✔
1518

1519
                if (installedMod == null)
2!
1520
                {
×
1521
                    log.WarnFormat("Wait, {0} is not actually installed?", ident);
×
1522
                    //Maybe ModuleNotInstalled ?
1523
                    if (registry_manager.registry.IsAutodetected(ident))
×
1524
                    {
×
1525
                        throw new ModuleNotFoundKraken(ident,
×
1526
                            repl.ToReplace.version.ToString(),
1527
                            string.Format(Properties.Resources.ModuleInstallerReplaceAutodetected, ident));
1528
                    }
1529

1530
                    throw new ModuleNotFoundKraken(ident,
×
1531
                        repl.ToReplace.version.ToString(),
1532
                        string.Format(Properties.Resources.ModuleInstallerReplaceNotInstalled, ident, repl.ReplaceWith.identifier));
1533
                }
1534
                else
1535
                {
2✔
1536
                    // Obviously, we need to remove the mod we are replacing
1537
                    modsToRemove.Add(installedMod);
2✔
1538

1539
                    log.DebugFormat("Ok, we are removing {0}", repl.ToReplace.identifier);
2✔
1540
                    //Check whether our Replacement target is already installed
1541
                    var installed_replacement = registry_manager.registry.InstalledModule(repl.ReplaceWith.identifier);
2✔
1542

1543
                    // If replacement is not installed, we've already added it to modsToInstall above
1544
                    if (installed_replacement != null)
2✔
1545
                    {
2✔
1546
                        //Module already installed. We'll need to treat it as an upgrade.
1547
                        log.DebugFormat("It turns out {0} is already installed, we'll upgrade it.", installed_replacement.identifier);
2✔
1548
                        modsToRemove.Add(installed_replacement);
2✔
1549

1550
                        CkanModule installed = installed_replacement.Module;
2✔
1551
                        if (installed.version.Equals(repl.ReplaceWith.version))
2!
1552
                        {
2✔
1553
                            log.InfoFormat("{0} is already at the latest version, reinstalling to replace {1}", repl.ReplaceWith.identifier, repl.ToReplace.identifier);
2✔
1554
                        }
2✔
1555
                        else if (installed.version.IsGreaterThan(repl.ReplaceWith.version))
×
1556
                        {
×
1557
                            log.WarnFormat("Downgrading {0} from {1} to {2} to replace {3}", repl.ReplaceWith.identifier, repl.ReplaceWith.version, repl.ReplaceWith.version, repl.ToReplace.identifier);
×
1558
                        }
×
1559
                        else
1560
                        {
×
1561
                            log.InfoFormat("Upgrading {0} to {1} to replace {2}", repl.ReplaceWith.identifier, repl.ReplaceWith.version, repl.ToReplace.identifier);
×
1562
                        }
×
1563
                    }
2✔
1564
                    else
1565
                    {
2✔
1566
                        log.InfoFormat("Replacing {0} with {1} {2}", repl.ToReplace.identifier, repl.ReplaceWith.identifier, repl.ReplaceWith.version);
2✔
1567
                    }
2✔
1568
                }
2✔
1569
            }
2✔
1570
            var resolver = new RelationshipResolver(modsToInstall, null, options, registry_manager.registry,
2✔
1571
                                                    instance.Game, instance.VersionCriteria());
1572
            var resolvedModsToInstall = resolver.ModList().ToArray();
2✔
1573

1574
            CheckAddRemoveFreeSpace(resolvedModsToInstall, modsToRemove);
2✔
1575
            AddRemove(ref possibleConfigOnlyDirs,
2✔
1576
                      registry_manager,
1577
                      resolver,
1578
                      resolvedModsToInstall,
1579
                      resolvedModsToInstall.ToDictionary(m => m, m => false),
2✔
1580
                      modsToRemove,
1581
                      downloader,
1582
                      enforceConsistency,
1583
                      deduper);
1584
            User.RaiseProgress(Properties.Resources.ModuleInstallerDone, 100);
2✔
1585
        }
2✔
1586

1587
        #endregion
1588

1589
        public static IEnumerable<string> PrioritizedHosts(IConfiguration    config,
1590
                                                           IEnumerable<Uri>? urls)
1591
            => urls?.OrderBy(u => u, new PreferredHostUriComparer(config.PreferredHosts))
2!
1592
                    .Select(dl => dl.Host)
2✔
1593
                    .Distinct()
1594
                   ?? Enumerable.Empty<string>();
1595

1596
        #region Recommendations
1597

1598
        /// <summary>
1599
        /// Looks for optional related modules that could be installed alongside the given modules
1600
        /// </summary>
1601
        /// <param name="instance">Game instance to use</param>
1602
        /// <param name="sourceModules">Modules to check for relationships, should contain the complete changeset including dependencies</param>
1603
        /// <param name="toInstall">Modules already being installed, to be omitted from search</param>
1604
        /// <param name="exclude">Modules the user has already seen and decided not to install</param>
1605
        /// <param name="registry">Registry to use</param>
1606
        /// <param name="recommendations">Modules that are recommended to install</param>
1607
        /// <param name="suggestions">Modules that are suggested to install</param>
1608
        /// <param name="supporters">Modules that support other modules we're installing</param>
1609
        /// <returns>
1610
        /// true if anything found, false otherwise
1611
        /// </returns>
1612
        public static bool FindRecommendations(GameInstance                                          instance,
1613
                                               IReadOnlyCollection<CkanModule>                       sourceModules,
1614
                                               IReadOnlyCollection<CkanModule>                       toInstall,
1615
                                               IReadOnlyCollection<CkanModule>                       exclude,
1616
                                               Registry                                              registry,
1617
                                               out Dictionary<CkanModule, Tuple<bool, List<string>>> recommendations,
1618
                                               out Dictionary<CkanModule, List<string>>              suggestions,
1619
                                               out Dictionary<CkanModule, HashSet<string>>           supporters)
1620
        {
2✔
1621
            log.DebugFormat("Finding recommendations for: {0}", string.Join(", ", sourceModules));
2✔
1622
            var crit     = instance.VersionCriteria();
2✔
1623
            var resolver = new RelationshipResolver(sourceModules.Where(m => !m.IsDLC),
2✔
1624
                                                    null,
1625
                                                    RelationshipResolverOptions.KitchenSinkOpts(instance.StabilityToleranceConfig),
1626
                                                    registry, instance.Game, crit);
1627
            var recommenders = resolver.Dependencies().ToHashSet();
2✔
1628
            log.DebugFormat("Recommenders: {0}", string.Join(", ", recommenders));
2✔
1629

1630
            var checkedRecs = resolver.Recommendations(recommenders)
2✔
1631
                                      .Except(exclude)
1632
                                      .Where(m => resolver.ReasonsFor(m)
2✔
1633
                                                          .Any(r => r is SelectionReason.Recommended { ProvidesIndex: 0 }))
2!
1634
                                      .ToHashSet();
1635
            var conflicting = new RelationshipResolver(toInstall.Concat(checkedRecs), null,
2✔
1636
                                                       RelationshipResolverOptions.ConflictsOpts(instance.StabilityToleranceConfig),
1637
                                                       registry, instance.Game, crit)
1638
                                  .ConflictList.Keys;
1639
            // Don't check recommendations that conflict with installed or installing mods
1640
            checkedRecs.ExceptWith(conflicting);
2✔
1641

1642
            recommendations = resolver.Recommendations(recommenders)
2✔
1643
                                      .Except(exclude)
1644
                                      .ToDictionary(m => m,
2✔
1645
                                                    m => new Tuple<bool, List<string>>(
2✔
1646
                                                             checkedRecs.Contains(m),
1647
                                                             resolver.ReasonsFor(m)
1648
                                                                     .OfType<SelectionReason.Recommended>()
1649
                                                                     .Where(r => recommenders.Contains(r.Parent))
2✔
1650
                                                                     .Select(r => r.Parent)
2✔
1651
                                                                     .OfType<CkanModule>()
1652
                                                                     .Select(m => m.identifier)
2✔
1653
                                                                     .ToList()));
1654
            suggestions = resolver.Suggestions(recommenders,
2✔
1655
                                               recommendations.Keys.ToList())
1656
                                  .Except(exclude)
1657
                                  .ToDictionary(m => m,
2✔
1658
                                                m => resolver.ReasonsFor(m)
2✔
1659
                                                             .OfType<SelectionReason.Suggested>()
1660
                                                             .Where(r => recommenders.Contains(r.Parent))
2✔
1661
                                                             .Select(r => r.Parent)
2✔
1662
                                                             .OfType<CkanModule>()
1663
                                                             .Select(m => m.identifier)
2✔
1664
                                                             .ToList());
1665

1666
            var opts = RelationshipResolverOptions.DependsOnlyOpts(instance.StabilityToleranceConfig);
2✔
1667
            supporters = resolver.Supporters(recommenders,
2✔
1668
                                             recommenders.Concat(recommendations.Keys)
1669
                                                         .Concat(suggestions.Keys))
1670
                                 .Where(kvp => !exclude.Contains(kvp.Key)
2!
1671
                                               && CanInstall(toInstall.Append(kvp.Key).ToList(),
1672
                                                          opts, registry, instance.Game, crit))
1673
                                 .ToDictionary();
1674

1675
            return recommendations.Count > 0
2✔
1676
                || suggestions.Count > 0
1677
                || supporters.Count > 0;
1678
        }
2✔
1679

1680
        /// <summary>
1681
        /// Determine whether there is any way to install the given set of mods.
1682
        /// Handles virtual dependencies, including recursively.
1683
        /// </summary>
1684
        /// <param name="opts">Installer options</param>
1685
        /// <param name="toInstall">Mods we want to install</param>
1686
        /// <param name="registry">Registry of instance into which we want to install</param>
1687
        /// <param name="game">Game instance</param>
1688
        /// <param name="crit">Game version criteria</param>
1689
        /// <returns>
1690
        /// True if it's possible to install these mods, false otherwise
1691
        /// </returns>
1692
        public static bool CanInstall(IReadOnlyCollection<CkanModule> toInstall,
1693
                                      RelationshipResolverOptions     opts,
1694
                                      IRegistryQuerier                registry,
1695
                                      IGame                           game,
1696
                                      GameVersionCriteria             crit)
1697
        {
2✔
1698
            string request = string.Join(", ", toInstall.Select(m => m.identifier));
2✔
1699
            try
1700
            {
2✔
1701
                var installed = toInstall.Select(m => registry.InstalledModule(m.identifier)?.Module)
2!
1702
                                         .OfType<CkanModule>();
1703
                var resolver = new RelationshipResolver(toInstall, installed, opts, registry, game, crit);
2✔
1704

1705
                var resolverModList = resolver.ModList(false).ToArray();
2✔
1706
                if (resolverModList.Length >= toInstall.Count(m => !m.IsMetapackage))
2!
1707
                {
2✔
1708
                    // We can install with no further dependencies
1709
                    string recipe = string.Join(", ", resolverModList.Select(m => m.identifier));
2✔
1710
                    log.Debug($"Installable: {request}: {recipe}");
2✔
1711
                    return true;
2✔
1712
                }
1713
                else
1714
                {
×
1715
                    log.DebugFormat("Can't install {0}: {1}", request, string.Join("; ", resolver.ConflictDescriptions));
×
1716
                    return false;
×
1717
                }
1718
            }
1719
            catch (TooManyModsProvideKraken k)
2✔
1720
            {
2✔
1721
                // One of the dependencies is virtual
1722
                foreach (var mod in k.modules)
5!
1723
                {
2✔
1724
                    // Try each option recursively to see if any are successful
1725
                    if (CanInstall(toInstall.Append(mod).ToArray(), opts, registry, game, crit))
2!
1726
                    {
2✔
1727
                        // Child call will emit debug output, so we don't need to here
1728
                        return true;
2✔
1729
                    }
1730
                }
×
1731
                log.Debug($"Can't install {request}: Can't install provider of {k.requested}");
×
1732
            }
×
1733
            catch (InconsistentKraken k)
×
1734
            {
×
1735
                log.Debug($"Can't install {request}: {k.ShortDescription}");
×
1736
            }
×
1737
            catch (Exception ex)
×
1738
            {
×
1739
                log.Debug($"Can't install {request}: {ex.Message}");
×
1740
            }
×
1741
            return false;
×
1742
        }
2✔
1743

1744
        #endregion
1745

1746
        private static void EnforceCacheSizeLimit(Registry       registry,
1747
                                                  NetModuleCache Cache,
1748
                                                  IConfiguration config)
1749
        {
2✔
1750
            // Purge old downloads if we're over the limit
1751
            if (config.CacheSizeLimit.HasValue)
2!
1752
            {
×
1753
                Cache.EnforceSizeLimit(config.CacheSizeLimit.Value, registry);
×
1754
            }
×
1755
        }
2✔
1756

1757
        private void CheckAddRemoveFreeSpace(IEnumerable<CkanModule>      toInstall,
1758
                                             IEnumerable<InstalledModule> toRemove)
1759
        {
2✔
1760
            if (toInstall.Sum(m => m.install_size) - toRemove.Sum(im => im.ActualInstallSize(instance))
2!
1761
                is > 0 and var spaceDelta)
1762
            {
×
NEW
1763
                CKANPathUtils.CheckFreeSpace(new DirectoryInfo(instance.GameDir),
×
1764
                                             spaceDelta,
1765
                                             Properties.Resources.NotEnoughSpaceToInstall);
1766
            }
×
1767
        }
2✔
1768

1769
        private readonly GameInstance      instance;
1770
        private readonly NetModuleCache    cache;
1771
        private readonly IConfiguration    config;
1772
        private readonly CancellationToken cancelToken;
1773

1774
        private static readonly ILog log = LogManager.GetLogger(typeof(ModuleInstaller));
2✔
1775
    }
1776
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc