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

KSP-CKAN / CKAN / 20049808028

09 Dec 2025 02:28AM UTC coverage: 85.3% (-0.04%) from 85.335%
20049808028

push

github

HebaruSan
Merge #4469 Refactor module installer to use relative paths internally

1998 of 2162 branches covered (92.41%)

Branch coverage included in aggregate %.

87 of 98 new or added lines in 10 files covered. (88.78%)

1 existing line in 1 file now uncovered.

11928 of 14164 relevant lines covered (84.21%)

1.76 hits per line

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

88.37
/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;
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
            log.DebugFormat("Creating ModuleInstaller for {0}", inst.GameDir);
2✔
36
            instance         = inst;
2✔
37
            // Make a transaction file manager that uses a temp dir in the instance's CKAN dir
38
            this.cache       = cache;
2✔
39
            this.config      = config;
2✔
40
            User             = user;
2✔
41
            this.cancelToken = cancelToken;
2✔
42
        }
2✔
43

44
        public IUser User { get; set; }
45

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

50
        #region Installation
51

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

463
        public static bool IsInternalCkan(ZipEntry ze)
464
            => ze.Name.EndsWith(".ckan", StringComparison.OrdinalIgnoreCase);
2✔
465

466
        #region File overwrites
467

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

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

539
        /// <summary>
540
        /// Remove files that the user chose to overwrite, so
541
        /// the installer can replace them.
542
        /// Uses a transaction so they can be undeleted if the install
543
        /// fails at a later stage.
544
        /// </summary>
545
        /// <param name="files">The files to overwrite</param>
546
        private void DeleteConflictingFiles(IEnumerable<InstallableFile> files)
547
        {
2✔
548
            var txFileMgr = new TxFileManager(instance.CkanDir);
2✔
549
            foreach (var absPath in files.Select(f => instance.ToAbsoluteGameDir(f.destination)))
8✔
550
            {
2✔
551
                log.DebugFormat("Trying to delete {0}", absPath);
2✔
552
                txFileMgr.Delete(absPath);
2✔
553
            }
2✔
554
        }
2✔
555

556
        #endregion
557

558
        #region Find files
559

560
        /// <summary>
561
        /// Given a module and an open zipfile, return all the files that would be installed
562
        /// for this module.
563
        ///
564
        /// If a KSP instance is provided, it will be used to generate output paths, otherwise these will be null.
565
        ///
566
        /// Throws a BadMetadataKraken if the stanza resulted in no files being returned.
567
        /// </summary>
568
        public static IEnumerable<InstallableFile> FindInstallableFiles(CkanModule module,
569
                                                                        ZipFile    zipfile,
570
                                                                        IGame      game)
571
            // Use the provided stanzas, or use the default install stanza if they're absent.
572
            => module.GetInstallStanzas(game)
2✔
573
                     .SelectMany(stanza => stanza.FindInstallableFiles(module, zipfile, game))
2✔
574
                     .ToArray();
575

576
        /// <summary>
577
        /// Given a module and a path to a zipfile, returns all the files that would be installed
578
        /// from that zip for this module.
579
        ///
580
        /// Throws a BadMetadataKraken if the stanza resulted in no files being returned.
581
        ///
582
        /// If a KSP instance is provided, it will be used to generate output paths, otherwise these will be null.
583
        /// </summary>
584
        public static IEnumerable<InstallableFile> FindInstallableFiles(CkanModule module,
585
                                                                        string     zip_filename,
586
                                                                        IGame      game)
587
        {
2✔
588
            // `using` makes sure our zipfile gets closed when we exit this block.
589
            using (ZipFile zipfile = new ZipFile(zip_filename))
2✔
590
            {
2✔
591
                log.DebugFormat("Searching {0} using {1} as module", zip_filename, module);
2✔
592
                return FindInstallableFiles(module, zipfile, game);
2✔
593
            }
594
        }
2✔
595

596
        /// <summary>
597
        /// Returns contents of an installed module
598
        /// </summary>
599
        public static IEnumerable<(string path, bool dir, bool exists)> GetModuleContents(
600
                GameInstance                instance,
601
                IReadOnlyCollection<string> installed,
602
                HashSet<string>             filters)
603
            => GetModuleContents(instance, installed,
2✔
604
                                 installed.SelectMany(f => f.TraverseNodes(Path.GetDirectoryName)
2✔
605
                                                            .Skip(1)
606
                                                            .Where(s => s.Length > 0)
2✔
607
                                                            .Select(CKANPathUtils.NormalizePath))
608
                                          .ToHashSet(),
609
                                 filters);
610

611
        private static IEnumerable<(string path, bool dir, bool exists)> GetModuleContents(
612
                GameInstance        instance,
613
                IEnumerable<string> installed,
614
                HashSet<string>     parents,
615
                HashSet<string>     filters)
UNCOV
616
            => installed.Where(f => !filters.Any(filt => f.Contains(filt)))
×
617
                        .GroupBy(parents.Contains)
618
                        .SelectMany(grp =>
619
                            grp.Select(p => (path:   p,
2✔
620
                                             dir:    grp.Key,
621
                                             exists: grp.Key ? Directory.Exists(instance.ToAbsoluteGameDir(p))
622
                                                             : File.Exists(instance.ToAbsoluteGameDir(p)))));
623

624
        /// <summary>
625
        /// Returns the module contents if and only if we have it
626
        /// available in our cache, empty sequence otherwise.
627
        ///
628
        /// Intended for previews.
629
        /// </summary>
630
        public static IEnumerable<(string path, bool dir, bool exists)> GetModuleContents(
631
                NetModuleCache  Cache,
632
                GameInstance    instance,
633
                CkanModule      module,
634
                HashSet<string> filters)
635
            => (Cache.GetCachedFilename(module) is string filename
2✔
636
                    ? GetModuleContents(Utilities.DefaultIfThrows(
637
                                            () => FindInstallableFiles(module, filename, instance.Game)),
2✔
638
                                        filters)
639
                    : null)
640
               ?? Enumerable.Empty<(string path, bool dir, bool exists)>();
641

642
        private static IEnumerable<(string path, bool dir, bool exists)>? GetModuleContents(
643
                IEnumerable<InstallableFile>? installable,
644
                HashSet<string>               filters)
645
            => installable?.Where(instF => !filters.Any(filt => instF.destination != null
×
646
                                                                && instF.destination.Contains(filt)))
647
                           .Select(f => (path:   f.destination,
2✔
648
                                         dir:    f.source.IsDirectory,
649
                                         exists: true));
650

651
        #endregion
652

653
        private string? InstallFile(ZipFile          zipfile,
654
                                    ZipEntry         entry,
655
                                    string           fullPath,
656
                                    bool             makeDirs,
657
                                    string[]         candidateDuplicates,
658
                                    IProgress<long>? progress)
659
            => InstallFile(zipfile, entry, fullPath, makeDirs,
2✔
660
                           new TxFileManager(instance.CkanDir),
661
                           candidateDuplicates, progress);
662

663
        /// <summary>
664
        /// Copy the entry from the opened zipfile to the path specified.
665
        /// </summary>
666
        /// <returns>
667
        /// Path of file or directory that was created.
668
        /// May differ from the input fullPath!
669
        /// Throws a FileExistsKraken if we were going to overwrite the file.
670
        /// </returns>
671
        internal static string? InstallFile(ZipFile          zipfile,
672
                                            ZipEntry         entry,
673
                                            string           fullPath,
674
                                            bool             makeDirs,
675
                                            IFileManager     txFileMgr,
676
                                            string[]         candidateDuplicates,
677
                                            IProgress<long>? progress)
678
        {
2✔
679
            if (entry.IsDirectory)
2✔
680
            {
2✔
681
                // Skip if we're not making directories for this install.
682
                if (!makeDirs)
2✔
683
                {
×
684
                    log.DebugFormat("Skipping '{0}', we don't make directories for this path", fullPath);
×
685
                    return null;
×
686
                }
687

688
                // Windows silently trims trailing spaces, get the path it will actually use
689
                fullPath = Path.GetDirectoryName(Path.Combine(fullPath, "DUMMY")) is string p
2✔
690
                    ? CKANPathUtils.NormalizePath(p)
691
                    : fullPath;
692

693
                log.DebugFormat("Making directory '{0}'", fullPath);
2✔
694
                txFileMgr.CreateDirectory(fullPath);
2✔
695
            }
2✔
696
            else
697
            {
2✔
698
                log.DebugFormat("Writing file '{0}'", fullPath);
2✔
699

700
                // ZIP format does not require directory entries
701
                if (makeDirs && Path.GetDirectoryName(fullPath) is string d)
2✔
702
                {
2✔
703
                    log.DebugFormat("Making parent directory '{0}'", d);
2✔
704
                    txFileMgr.CreateDirectory(d);
2✔
705
                }
2✔
706

707
                // We don't allow for the overwriting of files. See #208.
708
                if (txFileMgr.FileExists(fullPath))
2✔
709
                {
2✔
710
                    throw new FileExistsKraken(fullPath);
2✔
711
                }
712

713
                // Snapshot whatever was there before. If there's nothing, this will just
714
                // remove our file on rollback. We still need this even though we won't
715
                // overwite files, as it ensures deletion on rollback.
716
                txFileMgr.Snapshot(fullPath);
2✔
717

718
                // Try making hard links if already installed in another instance (faster, less space)
719
                foreach (var installedSource in candidateDuplicates)
8✔
720
                {
2✔
721
                    try
722
                    {
2✔
723
                        HardLink.Create(installedSource, fullPath);
2✔
724
                        return fullPath;
2✔
725
                    }
726
                    catch
×
727
                    {
×
728
                        // If hard link creation fails, try more hard links, or copy if none work
729
                    }
×
730
                }
×
731

732
                try
733
                {
2✔
734
                    // It's a file! Prepare the streams
735
                    using (var zipStream = zipfile.GetInputStream(entry))
2✔
736
                    using (var writer = File.Create(fullPath))
2✔
737
                    {
2✔
738
                        // Windows silently changes paths ending with spaces, get the name it actually used
739
                        fullPath = CKANPathUtils.NormalizePath(writer.Name);
2✔
740
                        // 4k is the block size on practically every disk and OS.
741
                        byte[] buffer = new byte[4096];
2✔
742
                        progress?.Report(0);
2✔
743
                        StreamUtils.Copy(zipStream, writer, buffer,
2✔
744
                                         // This doesn't fire at all if the interval never elapses
745
                                         (sender, e) => progress?.Report(e.Processed),
2✔
746
                                         UnzipProgressInterval,
747
                                         entry, "InstallFile");
748
                    }
2✔
749
                }
2✔
750
                catch (DirectoryNotFoundException ex)
×
751
                {
×
752
                    throw new DirectoryNotFoundKraken("", ex.Message, ex);
×
753
                }
754
            }
2✔
755
            // Usually, this is the path we're given.
756
            // Sometimes it has trailing spaces trimmed by the OS.
757
            return fullPath;
2✔
758
        }
2✔
759

760
        private static readonly TimeSpan UnzipProgressInterval = TimeSpan.FromMilliseconds(200);
2✔
761

762
        #endregion
763

764
        #region Uninstallation
765

766
        /// <summary>
767
        /// Uninstalls all the mods provided, including things which depend upon them.
768
        /// This *DOES* save the registry.
769
        /// Preferred over Uninstall.
770
        /// </summary>
771
        public void UninstallList(IEnumerable<string>              mods,
772
                                  ref HashSet<string>?             possibleConfigOnlyDirs,
773
                                  RegistryManager                  registry_manager,
774
                                  bool                             ConfirmPrompt = true,
775
                                  IReadOnlyCollection<CkanModule>? installing    = null)
776
        {
2✔
777
            mods = mods.Memoize();
2✔
778
            installing ??= Array.Empty<CkanModule>();
2✔
779
            // Pre-check, have they even asked for things which are installed?
780

781
            foreach (string mod in mods.Where(mod => registry_manager.registry.InstalledModule(mod) == null))
2✔
782
            {
2✔
783
                throw new ModNotInstalledKraken(mod);
2✔
784
            }
785

786
            var instDlc = mods.Select(registry_manager.registry.InstalledModule)
2✔
787
                              .OfType<InstalledModule>()
788
                              .FirstOrDefault(m => m.Module.IsDLC);
2✔
789
            if (instDlc != null)
2✔
790
            {
×
791
                throw new ModuleIsDLCKraken(instDlc.Module);
×
792
            }
793

794
            // Find all the things which need uninstalling.
795
            var revdep = mods
2✔
796
                .Union(registry_manager.registry.FindReverseDependencies(
797
                    mods.Except(installing.Select(m => m.identifier)).ToArray(),
×
798
                    installing))
799
                .ToArray();
800

801
            var goners = revdep.Union(
2✔
802
                                registry_manager.registry.FindRemovableAutoInstalled(installing,
803
                                                                                     revdep.ToHashSet(),
804
                                                                                     instance)
805
                                                         .Select(im => im.identifier))
2✔
806
                               .Order()
807
                               .ToArray();
808

809
            // If there is nothing to uninstall, skip out.
810
            if (goners.Length == 0)
2✔
811
            {
2✔
812
                return;
2✔
813
            }
814

815
            User.RaiseMessage(Properties.Resources.ModuleInstallerAboutToRemove);
2✔
816
            User.RaiseMessage("");
2✔
817

818
            foreach (var module in goners.Select(registry_manager.registry.InstalledModule)
8✔
819
                                         .OfType<InstalledModule>())
820
            {
2✔
821
                User.RaiseMessage(" * {0} {1}", module.Module.name, module.Module.version);
2✔
822
            }
2✔
823

824
            if (ConfirmPrompt && !User.RaiseYesNoDialog(Properties.Resources.ModuleInstallerContinuePrompt))
2✔
825
            {
×
826
                throw new CancelledActionKraken(Properties.Resources.ModuleInstallerRemoveAborted);
×
827
            }
828

829
            using (var transaction = CkanTransaction.CreateTransactionScope())
2✔
830
            {
2✔
831
                var registry = registry_manager.registry;
2✔
832
                long removeBytes = goners.Select(registry.InstalledModule)
2✔
833
                                         .OfType<InstalledModule>()
834
                                         .Sum(m => m.Module.install_size);
2✔
835
                var rateCounter = new ByteRateCounter()
2✔
836
                {
837
                    Size      = removeBytes,
838
                    BytesLeft = removeBytes,
839
                };
840
                rateCounter.Start();
2✔
841

842
                long modRemoveCompletedBytes = 0;
2✔
843
                foreach (string ident in goners)
8✔
844
                {
2✔
845
                    if (registry.InstalledModule(ident) is InstalledModule instMod)
2✔
846
                    {
2✔
847
                        Uninstall(ident, ref possibleConfigOnlyDirs, registry,
2✔
848
                                  new ProgressImmediate<long>(bytes =>
849
                                  {
2✔
850
                                      RemoveProgress?.Invoke(instMod,
2✔
851
                                                             Math.Max(0,     instMod.Module.install_size - bytes),
852
                                                             Math.Max(bytes, instMod.Module.install_size));
853
                                      rateCounter.BytesLeft = removeBytes - (modRemoveCompletedBytes
2✔
854
                                                                             + Math.Min(bytes, instMod.Module.install_size));
855
                                      User.RaiseProgress(rateCounter);
2✔
856
                                  }));
2✔
857
                        modRemoveCompletedBytes += instMod?.Module.install_size ?? 0;
2✔
858
                    }
2✔
859
                }
2✔
860

861
                // Enforce consistency if we're not installing anything,
862
                // otherwise consistency will be enforced after the installs
863
                registry_manager.Save(installing == null);
2✔
864

865
                transaction.Complete();
2✔
866
            }
2✔
867

868
            User.RaiseProgress(Properties.Resources.ModuleInstallerDone, 100);
2✔
869
        }
2✔
870

871
        /// <summary>
872
        /// Uninstall the module provided. For internal use only.
873
        /// Use UninstallList for user queries, it also does dependency handling.
874
        /// This does *NOT* save the registry.
875
        /// </summary>
876
        /// <param name="identifier">Identifier of module to uninstall</param>
877
        /// <param name="possibleConfigOnlyDirs">Directories that the user might want to remove after uninstall</param>
878
        /// <param name="registry">Registry to use</param>
879
        /// <param name="progress">Progress to report</param>
880
        private void Uninstall(string               identifier,
881
                               ref HashSet<string>? possibleConfigOnlyDirs,
882
                               Registry             registry,
883
                               IProgress<long>      progress)
884
        {
2✔
885
            var txFileMgr = new TxFileManager(instance.CkanDir);
2✔
886

887
            using (var transaction = CkanTransaction.CreateTransactionScope())
2✔
888
            {
2✔
889
                var instMod = registry.InstalledModule(identifier);
2✔
890

891
                if (instMod == null)
2✔
892
                {
×
893
                    log.ErrorFormat("Trying to uninstall {0} but it's not installed", identifier);
×
894
                    throw new ModNotInstalledKraken(identifier);
×
895
                }
896
                User.RaiseMessage(Properties.Resources.ModuleInstallerRemovingMod,
2✔
897
                                  $"{instMod.Module.name} {instMod.Module.version}");
898

899
                // Walk our registry to find all files for this mod.
900
                var modFiles = instMod.Files.ToArray();
2✔
901

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

905
                // Files that Windows refused to delete due to locking (probably)
906
                var undeletableFiles = new List<string>();
2✔
907

908
                long bytesDeleted = 0;
2✔
909
                foreach (string relPath in modFiles)
8✔
910
                {
2✔
911
                    if (cancelToken.IsCancellationRequested)
2✔
912
                    {
×
913
                        throw new CancelledActionKraken();
×
914
                    }
915

916
                    string absPath = instance.ToAbsoluteGameDir(relPath);
2✔
917

918
                    try
919
                    {
2✔
920
                        if (File.GetAttributes(absPath)
2✔
921
                                .HasFlag(FileAttributes.Directory))
922
                        {
2✔
923
                            directoriesToDelete.Add(absPath);
2✔
924
                        }
2✔
925
                        else
926
                        {
2✔
927
                            // Add this file's directory to the list for deletion if it isn't already there.
928
                            // Helps clean up directories when modules are uninstalled out of dependency order
929
                            // Since we check for directory contents when deleting, this should purge empty
930
                            // dirs, making less ModuleManager headaches for people.
931
                            if (Path.GetDirectoryName(absPath) is string p)
2✔
932
                            {
2✔
933
                                directoriesToDelete.Add(p);
2✔
934
                            }
2✔
935

936
                            bytesDeleted += new FileInfo(absPath).Length;
2✔
937
                            progress.Report(bytesDeleted);
2✔
938
                            log.DebugFormat("Removing {0}", relPath);
2✔
939
                            txFileMgr.Delete(absPath);
2✔
940
                        }
2✔
941
                    }
2✔
942
                    catch (FileNotFoundException exc)
2✔
943
                    {
2✔
944
                        log.Debug("Ignoring missing file while deleting", exc);
2✔
945
                    }
2✔
946
                    catch (DirectoryNotFoundException exc)
2✔
947
                    {
2✔
948
                        log.Debug("Ignoring missing directory while deleting", exc);
2✔
949
                    }
2✔
950
                    catch (IOException)
×
951
                    {
×
952
                        // "The specified file is in use."
953
                        undeletableFiles.Add(relPath);
×
954
                    }
×
955
                    catch (UnauthorizedAccessException)
×
956
                    {
×
957
                        // "The caller does not have the required permission."
958
                        // "The file is an executable file that is in use."
959
                        undeletableFiles.Add(relPath);
×
960
                    }
×
961
                    catch (Exception exc)
×
962
                    {
×
963
                        // We don't consider this problem serious enough to abort and revert,
964
                        // so treat it as a "--verbose" level log message.
965
                        log.InfoFormat("Failure in locating file {0}: {1}", absPath, exc.Message);
×
966
                    }
×
967
                }
2✔
968

969
                if (undeletableFiles.Count > 0)
2✔
970
                {
×
971
                    throw new FailedToDeleteFilesKraken(identifier, undeletableFiles);
×
972
                }
973

974
                // Remove from registry.
975
                registry.DeregisterModule(instance, identifier);
2✔
976

977
                // Our collection of directories may leave empty parent directories.
978
                directoriesToDelete = AddParentDirectories(directoriesToDelete);
2✔
979

980
                // Sort our directories from longest to shortest, to make sure we remove child directories
981
                // before parents. GH #78.
982
                foreach (string directory in directoriesToDelete.OrderByDescending(dir => dir.Length))
2✔
983
                {
2✔
984
                    log.DebugFormat("Checking {0}...", directory);
2✔
985
                    // It is bad if any of this directories gets removed
986
                    // So we protect them
987
                    // A few string comparisons will be cheaper than hitting the disk, so do this first
988
                    if (instance.Game.IsReservedDirectory(instance, directory))
2✔
989
                    {
×
990
                        log.DebugFormat("Directory {0} is reserved, skipping", directory);
×
991
                        continue;
×
992
                    }
993

994
                    // See what's left in this folder and what we can do about it
995
                    GroupFilesByRemovable(instance.ToRelativeGameDir(directory),
2✔
996
                                          registry, modFiles, instance.Game,
997
                                          (Directory.Exists(directory)
998
                                              ? Directory.EnumerateFileSystemEntries(directory, "*", SearchOption.AllDirectories)
999
                                              : Enumerable.Empty<string>())
1000
                                           .Select(instance.ToRelativeGameDir)
1001
                                           .ToArray(),
1002
                                          out string[] removable,
1003
                                          out string[] notRemovable);
1004

1005
                    // Delete the auto-removable files and dirs
1006
                    foreach (var absPath in removable.Select(instance.ToAbsoluteGameDir))
8✔
1007
                    {
2✔
1008
                        if (File.Exists(absPath))
2✔
1009
                        {
2✔
1010
                            log.DebugFormat("Attempting transaction deletion of file {0}", absPath);
2✔
1011
                            txFileMgr.Delete(absPath);
2✔
1012
                        }
2✔
1013
                        else if (Directory.Exists(absPath))
2✔
1014
                        {
2✔
1015
                            log.DebugFormat("Attempting deletion of directory {0}", absPath);
2✔
1016
                            try
1017
                            {
2✔
1018
                                Directory.Delete(absPath);
2✔
1019
                            }
2✔
1020
                            catch
×
1021
                            {
×
1022
                                // There might be files owned by other mods, oh well
1023
                                log.DebugFormat("Failed to delete {0}", absPath);
×
1024
                            }
×
1025
                        }
2✔
1026
                    }
2✔
1027

1028
                    if (notRemovable.Length < 1)
2✔
1029
                    {
2✔
1030
                        // We *don't* use our txFileMgr to delete files here, because
1031
                        // it fails if the system's temp directory is on a different device
1032
                        // to KSP. However we *can* safely delete it now we know it's empty,
1033
                        // because the TxFileMgr *will* put it back if there's a file inside that
1034
                        // needs it.
1035
                        //
1036
                        // This works around GH #251.
1037
                        // The filesystem boundary bug is described in https://transactionalfilemgr.codeplex.com/workitem/20
1038

1039
                        log.DebugFormat("Removing {0}", directory);
2✔
1040
                        Directory.Delete(directory);
2✔
1041
                    }
2✔
1042
                    else if (notRemovable.Except(possibleConfigOnlyDirs?.Select(instance.ToRelativeGameDir)
2✔
1043
                                                 ?? Enumerable.Empty<string>())
1044
                                         // Can't remove if owned by some other mod
1045
                                         .Any(relPath => registry.FileOwner(relPath) != null
2✔
1046
                                                         || modFiles.Contains(relPath)))
1047
                    {
×
1048
                        log.InfoFormat("Not removing directory {0}, it's not empty", directory);
×
1049
                    }
×
1050
                    else
1051
                    {
2✔
1052
                        log.DebugFormat("Directory {0} contains only non-registered files, ask user about it later: {1}",
2✔
1053
                                        directory,
1054
                                        string.Join(", ", notRemovable));
1055
                        possibleConfigOnlyDirs ??= new HashSet<string>(Platform.PathComparer);
2✔
1056
                        possibleConfigOnlyDirs.Add(directory);
2✔
1057
                    }
2✔
1058
                }
2✔
1059
                log.InfoFormat("Removed {0}", identifier);
2✔
1060
                transaction.Complete();
2✔
1061
                User.RaiseMessage(Properties.Resources.ModuleInstallerRemovedMod,
2✔
1062
                                  $"{instMod.Module.name} {instMod.Module.version}");
1063
            }
2✔
1064
        }
2✔
1065

1066
        internal static void GroupFilesByRemovable(string                      relRoot,
1067
                                                   Registry                    registry,
1068
                                                   IReadOnlyCollection<string> alreadyRemoving,
1069
                                                   IGame                       game,
1070
                                                   IReadOnlyCollection<string> relPaths,
1071
                                                   out string[]                removable,
1072
                                                   out string[]                notRemovable)
1073
        {
2✔
1074
            if (relPaths.Count < 1)
2✔
1075
            {
2✔
1076
                removable    = Array.Empty<string>();
2✔
1077
                notRemovable = Array.Empty<string>();
2✔
1078
                return;
2✔
1079
            }
1080
            log.DebugFormat("Getting contents of {0}", relRoot);
2✔
1081
            var contents = relPaths
2✔
1082
                // Split into auto-removable and not-removable
1083
                // Removable must not be owned by other mods
1084
                .GroupBy(f => registry.FileOwner(f) == null
2✔
1085
                              // Also skip owned by this module since it's already deregistered
1086
                              && !alreadyRemoving.Contains(f)
1087
                              // Must have a removable dir name somewhere in path AFTER main dir
1088
                              && f[relRoot.Length..]
1089
                                  .Split('/')
1090
                                  .Where(piece => !string.IsNullOrEmpty(piece))
2✔
1091
                                  .Any(piece => game.AutoRemovableDirs.Contains(piece)))
2✔
1092
                .ToDictionary(grp => grp.Key,
2✔
1093
                              grp => grp.OrderByDescending(f => f.Length)
2✔
1094
                                        .ToArray());
1095
            removable    = contents.GetValueOrDefault(true)  ?? Array.Empty<string>();
2✔
1096
            notRemovable = contents.GetValueOrDefault(false) ?? Array.Empty<string>();
2✔
1097
            log.DebugFormat("Got removable: {0}",    string.Join(", ", removable));
2✔
1098
            log.DebugFormat("Got notRemovable: {0}", string.Join(", ", notRemovable));
2✔
1099
        }
2✔
1100

1101
        /// <summary>
1102
        /// Takes a collection of directories and adds all parent directories within the GameData structure.
1103
        /// </summary>
1104
        /// <param name="directories">The collection of directory path strings to examine</param>
1105
        public HashSet<string> AddParentDirectories(HashSet<string> directories)
1106
        {
2✔
1107
            var gameDir = CKANPathUtils.NormalizePath(instance.GameDir);
2✔
1108
            return directories
2✔
1109
                .Where(dir => !string.IsNullOrWhiteSpace(dir))
2✔
1110
                // Normalize all paths before deduplicate
1111
                .Select(CKANPathUtils.NormalizePath)
1112
                // Remove any duplicate paths
1113
                .Distinct()
1114
                .SelectMany(dir =>
1115
                {
2✔
1116
                    var results = new HashSet<string>(Platform.PathComparer);
2✔
1117
                    // Adding in the DirectorySeparatorChar fixes attempts on Windows
1118
                    // to parse "X:" which resolves to Environment.CurrentDirectory
1119
                    var dirInfo = new DirectoryInfo(
2✔
1120
                        dir.EndsWith("/") ? dir : dir + Path.DirectorySeparatorChar);
1121

1122
                    // If this is a parentless directory (Windows)
1123
                    // or if the Root equals the current directory (Mono)
1124
                    if (dirInfo.Parent == null || dirInfo.Root == dirInfo)
2✔
1125
                    {
2✔
1126
                        return results;
2✔
1127
                    }
1128

1129
                    if (!dir.StartsWith(gameDir, Platform.PathComparison))
2✔
1130
                    {
×
1131
                        dir = CKANPathUtils.ToAbsolute(dir, gameDir);
×
1132
                    }
×
1133

1134
                    // Remove the system paths, leaving the path under the instance directory
1135
                    var relativeHead = CKANPathUtils.ToRelative(dir, gameDir);
2✔
1136
                    // Don't try to remove GameRoot
1137
                    if (!string.IsNullOrEmpty(relativeHead))
2✔
1138
                    {
2✔
1139
                        var pathArray = relativeHead.Split('/');
2✔
1140
                        var builtPath = "";
2✔
1141
                        foreach (var path in pathArray)
8✔
1142
                        {
2✔
1143
                            builtPath += path + '/';
2✔
1144
                            results.Add(CKANPathUtils.ToAbsolute(builtPath, gameDir));
2✔
1145
                        }
2✔
1146
                    }
2✔
1147

1148
                    return results;
2✔
1149
                })
2✔
1150
                .Where(dir => !instance.Game.IsReservedDirectory(instance, dir))
2✔
1151
                .ToHashSet();
1152
        }
2✔
1153

1154
        #endregion
1155

1156
        #region AddRemove
1157

1158
        /// <summary>
1159
        /// Adds and removes the listed modules as a single transaction.
1160
        /// No relationships will be processed.
1161
        /// This *will* save the registry.
1162
        /// </summary>
1163
        /// <param name="possibleConfigOnlyDirs">Directories that the user might want to remove after uninstall</param>
1164
        /// <param name="registry_manager">Registry to use</param>
1165
        /// <param name="resolver">Relationship resolver to use</param>
1166
        /// <param name="add">Modules to add</param>
1167
        /// <param name="autoInstalled">true or false for each item in `add`</param>
1168
        /// <param name="remove">Modules to remove</param>
1169
        /// <param name="downloader">Downloader to use</param>
1170
        /// <param name="deduper">Deduplicator to use</param>
1171
        /// <param name="enforceConsistency">Whether to enforce consistency</param>
1172
        private void AddRemove(ref HashSet<string>?                 possibleConfigOnlyDirs,
1173
                               RegistryManager                      registry_manager,
1174
                               RelationshipResolver                 resolver,
1175
                               IReadOnlyCollection<CkanModule>      add,
1176
                               IDictionary<CkanModule, bool>        autoInstalled,
1177
                               IReadOnlyCollection<InstalledModule> remove,
1178
                               IDownloader                          downloader,
1179
                               bool                                 enforceConsistency,
1180
                               InstalledFilesDeduplicator?          deduper = null)
1181
        {
2✔
1182
            using (var tx = CkanTransaction.CreateTransactionScope())
2✔
1183
            {
2✔
1184
                var groups = add.GroupBy(m => m.IsMetapackage || cache.IsCached(m));
2✔
1185
                var cached = groups.FirstOrDefault(grp => grp.Key)?.ToArray()
2✔
1186
                                                                  ?? Array.Empty<CkanModule>();
1187
                var toDownload = groups.FirstOrDefault(grp => !grp.Key)?.ToArray()
2✔
1188
                                                                       ?? Array.Empty<CkanModule>();
1189

1190
                long removeBytes     = remove.Sum(m => m.Module.install_size);
2✔
1191
                long removedBytes    = 0;
2✔
1192
                long downloadBytes   = toDownload.Sum(m => m.download_size);
2✔
1193
                long downloadedBytes = 0;
2✔
1194
                long installBytes    = add.Sum(m => m.install_size);
2✔
1195
                long installedBytes  = 0;
2✔
1196
                var rateCounter = new ByteRateCounter()
2✔
1197
                {
1198
                    Size      = removeBytes + downloadBytes + installBytes,
1199
                    BytesLeft = removeBytes + downloadBytes + installBytes,
1200
                };
1201
                rateCounter.Start();
2✔
1202

1203
                downloader.OverallDownloadProgress += brc =>
2✔
1204
                {
2✔
1205
                    downloadedBytes = downloadBytes - brc.BytesLeft;
2✔
1206
                    rateCounter.BytesLeft = removeBytes   - removedBytes
2✔
1207
                                          + downloadBytes - downloadedBytes
1208
                                          + installBytes  - installedBytes;
1209
                    User.RaiseProgress(rateCounter);
2✔
1210
                };
2✔
1211
                var toInstall = ModsInDependencyOrder(resolver, cached, toDownload, downloader);
2✔
1212

1213
                long modRemoveCompletedBytes = 0;
2✔
1214
                foreach (var instMod in remove)
8✔
1215
                {
2✔
1216
                    Uninstall(instMod.Module.identifier,
2✔
1217
                              ref possibleConfigOnlyDirs,
1218
                              registry_manager.registry,
1219
                              new ProgressImmediate<long>(bytes =>
1220
                              {
2✔
1221
                                  RemoveProgress?.Invoke(instMod,
2✔
1222
                                                         Math.Max(0,     instMod.Module.install_size - bytes),
1223
                                                         Math.Max(bytes, instMod.Module.install_size));
1224
                                  removedBytes = modRemoveCompletedBytes
2✔
1225
                                                 + Math.Min(bytes, instMod.Module.install_size);
1226
                                  rateCounter.BytesLeft = removeBytes   - removedBytes
2✔
1227
                                                        + downloadBytes - downloadedBytes
1228
                                                        + installBytes  - installedBytes;
1229
                                  User.RaiseProgress(rateCounter);
2✔
1230
                              }));
2✔
1231
                     modRemoveCompletedBytes += instMod.Module.install_size;
2✔
1232
                }
2✔
1233

1234
                var gameDir = new DirectoryInfo(instance.GameDir);
2✔
1235
                long modInstallCompletedBytes = 0;
2✔
1236
                foreach (var mod in toInstall)
8✔
1237
                {
2✔
1238
                    CKANPathUtils.CheckFreeSpace(gameDir, mod.install_size,
2✔
1239
                                                 Properties.Resources.NotEnoughSpaceToInstall);
1240
                    Install(mod,
2✔
1241
                            // For upgrading, new modules are dependencies and should be marked auto-installed,
1242
                            // for replacing, new modules are the replacements and should not be marked auto-installed
1243
                            remove?.FirstOrDefault(im => im.Module.identifier == mod.identifier)
2✔
1244
                                  ?.AutoInstalled
1245
                                  ?? autoInstalled[mod],
1246
                            registry_manager.registry,
1247
                            deduper?.ModuleCandidateDuplicates(mod.identifier, mod.version),
1248
                            ref possibleConfigOnlyDirs,
1249
                            new ProgressImmediate<long>(bytes =>
1250
                            {
2✔
1251
                                InstallProgress?.Invoke(mod,
2✔
1252
                                                        Math.Max(0,     mod.install_size - bytes),
1253
                                                        Math.Max(bytes, mod.install_size));
1254
                                installedBytes = modInstallCompletedBytes
2✔
1255
                                                 + Math.Min(bytes, mod.install_size);
1256
                                rateCounter.BytesLeft = removeBytes   - removedBytes
2✔
1257
                                                      + downloadBytes - downloadedBytes
1258
                                                      + installBytes  - installedBytes;
1259
                                User.RaiseProgress(rateCounter);
2✔
1260
                            }));
2✔
1261
                    modInstallCompletedBytes += mod.install_size;
2✔
1262
                }
2✔
1263

1264
                registry_manager.Save(enforceConsistency);
2✔
1265
                tx.Complete();
2✔
1266
                EnforceCacheSizeLimit(registry_manager.registry, cache, config);
2✔
1267
            }
2✔
1268
        }
2✔
1269

1270
        /// <summary>
1271
        /// Upgrades or installs the mods listed to the specified versions for the user's KSP.
1272
        /// Will *re-install* or *downgrade* (with a warning) as well as upgrade.
1273
        /// Throws ModuleNotFoundKraken if a module is not installed.
1274
        /// </summary>
1275
        public void Upgrade(in IReadOnlyCollection<CkanModule> modules,
1276
                            IDownloader                        downloader,
1277
                            ref HashSet<string>?               possibleConfigOnlyDirs,
1278
                            RegistryManager                    registry_manager,
1279
                            InstalledFilesDeduplicator?        deduper            = null,
1280
                            bool                               enforceConsistency = true,
1281
                            bool                               ConfirmPrompt      = true)
1282
        {
2✔
1283
            var registry = registry_manager.registry;
2✔
1284

1285
            var removingIdents = registry.InstalledModules.Select(im => im.identifier)
2✔
1286
                                         .Intersect(modules.Select(m => m.identifier))
2✔
1287
                                         .ToHashSet();
1288
            var autoRemoving = registry
2✔
1289
                .FindRemovableAutoInstalled(modules, removingIdents, instance)
1290
                .ToHashSet();
1291

1292
            var resolver = new RelationshipResolver(
2✔
1293
                modules,
1294
                modules.Select(m => registry.InstalledModule(m.identifier)?.Module)
2✔
1295
                       .OfType<CkanModule>()
1296
                       .Concat(autoRemoving.Select(im => im.Module)),
2✔
1297
                RelationshipResolverOptions.DependsOnlyOpts(instance.StabilityToleranceConfig),
1298
                registry,
1299
                instance.Game, instance.VersionCriteria());
1300
            var fullChangeset = resolver.ModList()
2✔
1301
                                        .ToDictionary(m => m.identifier,
2✔
1302
                                                      m => m);
2✔
1303

1304
            // Skip removing ones we still need
1305
            var keepIdents = fullChangeset.Keys.Intersect(autoRemoving.Select(im => im.Module.identifier))
2✔
1306
                                               .ToHashSet();
1307
            autoRemoving.RemoveWhere(im => keepIdents.Contains(im.Module.identifier));
2✔
1308
            foreach (var ident in keepIdents)
8✔
1309
            {
2✔
1310
                fullChangeset.Remove(ident);
2✔
1311
            }
2✔
1312

1313
            // Only install stuff that's already there if explicitly requested in param
1314
            var toInstall = fullChangeset.Values
2✔
1315
                                         .Except(registry.InstalledModules
1316
                                                         .Select(im => im.Module)
2✔
1317
                                                         .Except(modules))
1318
                                         .ToArray();
1319
            var autoInstalled = toInstall.ToDictionary(m => m, resolver.IsAutoInstalled);
2✔
1320

1321
            User.RaiseMessage(Properties.Resources.ModuleInstallerAboutToUpgrade);
2✔
1322
            User.RaiseMessage("");
2✔
1323

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

1330
            // Let's discover what we need to do with each module!
1331
            foreach (CkanModule module in toInstall)
8✔
1332
            {
2✔
1333
                var installed_mod = registry.InstalledModule(module.identifier);
2✔
1334

1335
                if (installed_mod == null)
2✔
1336
                {
2✔
1337
                    if (!cache.IsMaybeCachedZip(module)
2✔
1338
                        && cache.GetInProgressFileName(module) is FileInfo inProgressFile)
1339
                    {
2✔
1340
                        if (inProgressFile.Exists)
2✔
1341
                        {
2✔
1342
                            User.RaiseMessage(Properties.Resources.ModuleInstallerUpgradeInstallingResuming,
2✔
1343
                                              module.name, module.version,
1344
                                              string.Join(", ", PrioritizedHosts(config, module.download)),
1345
                                              CkanModule.FmtSize(module.download_size - inProgressFile.Length));
1346
                        }
2✔
1347
                        else
1348
                        {
2✔
1349
                            User.RaiseMessage(Properties.Resources.ModuleInstallerUpgradeInstallingUncached,
2✔
1350
                                              module.name, module.version,
1351
                                              string.Join(", ", PrioritizedHosts(config, module.download)),
1352
                                              CkanModule.FmtSize(module.download_size));
1353
                        }
2✔
1354
                    }
2✔
1355
                    else
1356
                    {
2✔
1357
                        User.RaiseMessage(Properties.Resources.ModuleInstallerUpgradeInstallingCached,
2✔
1358
                                          module.name, module.version);
1359
                    }
2✔
1360
                }
2✔
1361
                else
1362
                {
2✔
1363
                    // Module already installed. We'll need to remove it first.
1364
                    toRemove.Add(installed_mod);
2✔
1365

1366
                    CkanModule installed = installed_mod.Module;
2✔
1367
                    if (installed.version.Equals(module.version))
2✔
1368
                    {
×
1369
                        User.RaiseMessage(Properties.Resources.ModuleInstallerUpgradeReinstalling,
×
1370
                                          module.name, module.version);
1371
                    }
×
1372
                    else if (installed.version.IsGreaterThan(module.version))
2✔
1373
                    {
2✔
1374
                        User.RaiseMessage(Properties.Resources.ModuleInstallerUpgradeDowngrading,
2✔
1375
                                          module.name, installed.version, module.version);
1376
                    }
2✔
1377
                    else
1378
                    {
2✔
1379
                        if (!cache.IsMaybeCachedZip(module)
2✔
1380
                            && cache.GetInProgressFileName(module) is FileInfo inProgressFile)
1381
                        {
2✔
1382
                            if (inProgressFile.Exists)
2✔
1383
                            {
2✔
1384
                                User.RaiseMessage(Properties.Resources.ModuleInstallerUpgradeUpgradingResuming,
2✔
1385
                                                  module.name, installed.version, module.version,
1386
                                                  string.Join(", ", PrioritizedHosts(config, module.download)),
1387
                                                  CkanModule.FmtSize(module.download_size - inProgressFile.Length));
1388
                            }
2✔
1389
                            else
1390
                            {
2✔
1391
                                User.RaiseMessage(Properties.Resources.ModuleInstallerUpgradeUpgradingUncached,
2✔
1392
                                                  module.name, installed.version, module.version,
1393
                                                  string.Join(", ", PrioritizedHosts(config, module.download)),
1394
                                                  CkanModule.FmtSize(module.download_size));
1395
                            }
2✔
1396
                        }
2✔
1397
                        else
1398
                        {
2✔
1399
                            User.RaiseMessage(Properties.Resources.ModuleInstallerUpgradeUpgradingCached,
2✔
1400
                                              module.name, installed.version, module.version);
1401
                        }
2✔
1402
                    }
2✔
1403
                }
2✔
1404
            }
2✔
1405

1406
            if (autoRemoving.Count > 0)
2✔
1407
            {
2✔
1408
                foreach (var im in autoRemoving)
8✔
1409
                {
2✔
1410
                    User.RaiseMessage(Properties.Resources.ModuleInstallerUpgradeAutoRemoving,
2✔
1411
                                      im.Module.name, im.Module.version);
1412
                }
2✔
1413
                toRemove.AddRange(autoRemoving);
2✔
1414
            }
2✔
1415

1416
            CheckAddRemoveFreeSpace(toInstall, toRemove);
2✔
1417

1418
            if (ConfirmPrompt && !User.RaiseYesNoDialog(Properties.Resources.ModuleInstallerContinuePrompt))
2✔
1419
            {
×
1420
                throw new CancelledActionKraken(Properties.Resources.ModuleInstallerUpgradeUserDeclined);
×
1421
            }
1422

1423
            AddRemove(ref possibleConfigOnlyDirs,
2✔
1424
                      registry_manager,
1425
                      resolver,
1426
                      toInstall,
1427
                      autoInstalled,
1428
                      toRemove,
1429
                      downloader,
1430
                      enforceConsistency,
1431
                      deduper);
1432
            User.RaiseProgress(Properties.Resources.ModuleInstallerDone, 100);
2✔
1433
        }
2✔
1434

1435
        /// <summary>
1436
        /// Enacts listed Module Replacements to the specified versions for the user's KSP.
1437
        /// Will *re-install* or *downgrade* (with a warning) as well as upgrade.
1438
        /// </summary>
1439
        /// <exception cref="DependenciesNotSatisfiedKraken">Thrown if a dependency for a replacing module couldn't be satisfied.</exception>
1440
        /// <exception cref="ModuleNotFoundKraken">Thrown if a module that should be replaced is not installed.</exception>
1441
        public void Replace(IEnumerable<ModuleReplacement> replacements,
1442
                            RelationshipResolverOptions    options,
1443
                            IDownloader                    downloader,
1444
                            ref HashSet<string>?           possibleConfigOnlyDirs,
1445
                            RegistryManager                registry_manager,
1446
                            InstalledFilesDeduplicator?    deduper = null,
1447
                            bool                           enforceConsistency = true)
1448
        {
2✔
1449
            replacements = replacements.Memoize();
2✔
1450
            log.Debug("Using Replace method");
2✔
1451
            var modsToInstall = new List<CkanModule>();
2✔
1452
            var modsToRemove  = new List<InstalledModule>();
2✔
1453
            foreach (ModuleReplacement repl in replacements)
8✔
1454
            {
2✔
1455
                modsToInstall.Add(repl.ReplaceWith);
2✔
1456
                log.DebugFormat("We want to install {0} as a replacement for {1}", repl.ReplaceWith.identifier, repl.ToReplace.identifier);
2✔
1457
            }
2✔
1458

1459
            // Our replacement involves removing the currently installed mods, then
1460
            // adding everything that needs installing (which may involve new mods to
1461
            // satisfy dependencies).
1462

1463
            // Let's discover what we need to do with each module!
1464
            foreach (ModuleReplacement repl in replacements)
8✔
1465
            {
2✔
1466
                string ident = repl.ToReplace.identifier;
2✔
1467
                var installedMod = registry_manager.registry.InstalledModule(ident);
2✔
1468

1469
                if (installedMod == null)
2✔
1470
                {
×
1471
                    log.WarnFormat("Wait, {0} is not actually installed?", ident);
×
1472
                    //Maybe ModuleNotInstalled ?
1473
                    if (registry_manager.registry.IsAutodetected(ident))
×
1474
                    {
×
1475
                        throw new ModuleNotFoundKraken(ident,
×
1476
                            repl.ToReplace.version.ToString(),
1477
                            string.Format(Properties.Resources.ModuleInstallerReplaceAutodetected, ident));
1478
                    }
1479

1480
                    throw new ModuleNotFoundKraken(ident,
×
1481
                        repl.ToReplace.version.ToString(),
1482
                        string.Format(Properties.Resources.ModuleInstallerReplaceNotInstalled, ident, repl.ReplaceWith.identifier));
1483
                }
1484
                else
1485
                {
2✔
1486
                    // Obviously, we need to remove the mod we are replacing
1487
                    modsToRemove.Add(installedMod);
2✔
1488

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

1493
                    // If replacement is not installed, we've already added it to modsToInstall above
1494
                    if (installed_replacement != null)
2✔
1495
                    {
2✔
1496
                        //Module already installed. We'll need to treat it as an upgrade.
1497
                        log.DebugFormat("It turns out {0} is already installed, we'll upgrade it.", installed_replacement.identifier);
2✔
1498
                        modsToRemove.Add(installed_replacement);
2✔
1499

1500
                        CkanModule installed = installed_replacement.Module;
2✔
1501
                        if (installed.version.Equals(repl.ReplaceWith.version))
2✔
1502
                        {
2✔
1503
                            log.InfoFormat("{0} is already at the latest version, reinstalling to replace {1}", repl.ReplaceWith.identifier, repl.ToReplace.identifier);
2✔
1504
                        }
2✔
1505
                        else if (installed.version.IsGreaterThan(repl.ReplaceWith.version))
×
1506
                        {
×
1507
                            log.WarnFormat("Downgrading {0} from {1} to {2} to replace {3}", repl.ReplaceWith.identifier, repl.ReplaceWith.version, repl.ReplaceWith.version, repl.ToReplace.identifier);
×
1508
                        }
×
1509
                        else
1510
                        {
×
1511
                            log.InfoFormat("Upgrading {0} to {1} to replace {2}", repl.ReplaceWith.identifier, repl.ReplaceWith.version, repl.ToReplace.identifier);
×
1512
                        }
×
1513
                    }
2✔
1514
                    else
1515
                    {
2✔
1516
                        log.InfoFormat("Replacing {0} with {1} {2}", repl.ToReplace.identifier, repl.ReplaceWith.identifier, repl.ReplaceWith.version);
2✔
1517
                    }
2✔
1518
                }
2✔
1519
            }
2✔
1520
            var resolver = new RelationshipResolver(modsToInstall, null, options, registry_manager.registry,
2✔
1521
                                                    instance.Game, instance.VersionCriteria());
1522
            var resolvedModsToInstall = resolver.ModList().ToArray();
2✔
1523

1524
            CheckAddRemoveFreeSpace(resolvedModsToInstall, modsToRemove);
2✔
1525
            AddRemove(ref possibleConfigOnlyDirs,
2✔
1526
                      registry_manager,
1527
                      resolver,
1528
                      resolvedModsToInstall,
1529
                      resolvedModsToInstall.ToDictionary(m => m, m => false),
2✔
1530
                      modsToRemove,
1531
                      downloader,
1532
                      enforceConsistency,
1533
                      deduper);
1534
            User.RaiseProgress(Properties.Resources.ModuleInstallerDone, 100);
2✔
1535
        }
2✔
1536

1537
        #endregion
1538

1539
        public static IEnumerable<string> PrioritizedHosts(IConfiguration    config,
1540
                                                           IEnumerable<Uri>? urls)
1541
            => urls?.OrderBy(u => u, new PreferredHostUriComparer(config.PreferredHosts))
2✔
1542
                    .Select(dl => dl.Host)
2✔
1543
                    .Distinct()
1544
                   ?? Enumerable.Empty<string>();
1545

1546
        #region Recommendations
1547

1548
        /// <summary>
1549
        /// Looks for optional related modules that could be installed alongside the given modules
1550
        /// </summary>
1551
        /// <param name="instance">Game instance to use</param>
1552
        /// <param name="sourceModules">Modules to check for relationships, should contain the complete changeset including dependencies</param>
1553
        /// <param name="toInstall">Modules already being installed, to be omitted from search</param>
1554
        /// <param name="toRemove">Modules being removed, to be excluded from relationship resolution</param>
1555
        /// <param name="exclude">Modules the user has already seen and decided not to install</param>
1556
        /// <param name="registry">Registry to use</param>
1557
        /// <param name="recommendations">Modules that are recommended to install</param>
1558
        /// <param name="suggestions">Modules that are suggested to install</param>
1559
        /// <param name="supporters">Modules that support other modules we're installing</param>
1560
        /// <returns>
1561
        /// true if anything found, false otherwise
1562
        /// </returns>
1563
        public static bool FindRecommendations(GameInstance                                          instance,
1564
                                               IReadOnlyCollection<CkanModule>                       sourceModules,
1565
                                               IReadOnlyCollection<CkanModule>                       toInstall,
1566
                                               IReadOnlyCollection<CkanModule>                       toRemove,
1567
                                               IReadOnlyCollection<CkanModule>                       exclude,
1568
                                               Registry                                              registry,
1569
                                               out Dictionary<CkanModule, Tuple<bool, List<string>>> recommendations,
1570
                                               out Dictionary<CkanModule, List<string>>              suggestions,
1571
                                               out Dictionary<CkanModule, HashSet<string>>           supporters)
1572
        {
2✔
1573
            log.DebugFormat("Finding recommendations for: {0}", string.Join(", ", sourceModules));
2✔
1574
            var crit     = instance.VersionCriteria();
2✔
1575

1576
            var rmvIdents = toRemove.Select(m => m.identifier).ToHashSet();
2✔
1577
            var allRemoving = toRemove
2✔
1578
                .Concat(registry.FindRemovableAutoInstalled(sourceModules, rmvIdents, instance)
1579
                                .Select(im => im.Module))
2✔
1580
                .ToArray();
1581

1582
            var resolver = new RelationshipResolver(sourceModules.Where(m => !m.IsDLC),
2✔
1583
                                                    allRemoving,
1584
                                                    RelationshipResolverOptions.KitchenSinkOpts(instance.StabilityToleranceConfig),
1585
                                                    registry, instance.Game, crit);
1586
            var recommenders = resolver.Dependencies().ToHashSet();
2✔
1587
            log.DebugFormat("Recommenders: {0}", string.Join(", ", recommenders));
2✔
1588

1589
            var checkedRecs = resolver.Recommendations(recommenders)
2✔
1590
                                      .Except(exclude)
1591
                                      .Where(m => resolver.ReasonsFor(m)
2✔
1592
                                                          .Any(r => r is SelectionReason.Recommended { ProvidesIndex: 0 }))
2✔
1593
                                      .ToHashSet();
1594
            var conflicting = new RelationshipResolver(toInstall.Concat(checkedRecs), allRemoving,
2✔
1595
                                                       RelationshipResolverOptions.ConflictsOpts(instance.StabilityToleranceConfig),
1596
                                                       registry, instance.Game, crit)
1597
                                  .ConflictList.Keys;
1598
            // Don't check recommendations that conflict with installed or installing mods
1599
            checkedRecs.ExceptWith(conflicting);
2✔
1600

1601
            recommendations = resolver.Recommendations(recommenders)
2✔
1602
                                      .Except(exclude)
1603
                                      .ToDictionary(m => m,
2✔
1604
                                                    m => new Tuple<bool, List<string>>(
2✔
1605
                                                             checkedRecs.Contains(m),
1606
                                                             resolver.ReasonsFor(m)
1607
                                                                     .OfType<SelectionReason.Recommended>()
1608
                                                                     .Where(r => recommenders.Contains(r.Parent))
2✔
1609
                                                                     .Select(r => r.Parent)
2✔
1610
                                                                     .OfType<CkanModule>()
1611
                                                                     .Select(m => m.identifier)
2✔
1612
                                                                     .ToList()));
1613
            suggestions = resolver.Suggestions(recommenders,
2✔
1614
                                               recommendations.Keys.ToList())
1615
                                  .Except(exclude)
1616
                                  .ToDictionary(m => m,
2✔
1617
                                                m => resolver.ReasonsFor(m)
2✔
1618
                                                             .OfType<SelectionReason.Suggested>()
1619
                                                             .Where(r => recommenders.Contains(r.Parent))
2✔
1620
                                                             .Select(r => r.Parent)
2✔
1621
                                                             .OfType<CkanModule>()
1622
                                                             .Select(m => m.identifier)
2✔
1623
                                                             .ToList());
1624

1625
            var opts = RelationshipResolverOptions.DependsOnlyOpts(instance.StabilityToleranceConfig);
2✔
1626
            supporters = resolver.Supporters(recommenders,
2✔
1627
                                             recommenders.Concat(recommendations.Keys)
1628
                                                         .Concat(suggestions.Keys))
1629
                                 .Where(kvp => !exclude.Contains(kvp.Key)
2✔
1630
                                               && CanInstall(toInstall.Append(kvp.Key).ToList(),
1631
                                                          opts, registry, instance.Game, crit))
1632
                                 .ToDictionary();
1633

1634
            return recommendations.Count > 0
2✔
1635
                || suggestions.Count > 0
1636
                || supporters.Count > 0;
1637
        }
2✔
1638

1639
        /// <summary>
1640
        /// Determine whether there is any way to install the given set of mods.
1641
        /// Handles virtual dependencies, including recursively.
1642
        /// </summary>
1643
        /// <param name="opts">Installer options</param>
1644
        /// <param name="toInstall">Mods we want to install</param>
1645
        /// <param name="registry">Registry of instance into which we want to install</param>
1646
        /// <param name="game">Game instance</param>
1647
        /// <param name="crit">Game version criteria</param>
1648
        /// <returns>
1649
        /// True if it's possible to install these mods, false otherwise
1650
        /// </returns>
1651
        public static bool CanInstall(IReadOnlyCollection<CkanModule> toInstall,
1652
                                      RelationshipResolverOptions     opts,
1653
                                      IRegistryQuerier                registry,
1654
                                      IGame                           game,
1655
                                      GameVersionCriteria             crit)
1656
        {
2✔
1657
            string request = string.Join(", ", toInstall.Select(m => m.identifier));
2✔
1658
            try
1659
            {
2✔
1660
                var installed = toInstall.Select(m => registry.InstalledModule(m.identifier)?.Module)
2✔
1661
                                         .OfType<CkanModule>();
1662
                var resolver = new RelationshipResolver(toInstall, installed, opts, registry, game, crit);
2✔
1663

1664
                var resolverModList = resolver.ModList(false).ToArray();
2✔
1665
                if (resolverModList.Length >= toInstall.Count(m => !m.IsMetapackage))
2✔
1666
                {
2✔
1667
                    // We can install with no further dependencies
1668
                    string recipe = string.Join(", ", resolverModList.Select(m => m.identifier));
2✔
1669
                    log.Debug($"Installable: {request}: {recipe}");
2✔
1670
                    return true;
2✔
1671
                }
1672
                else
1673
                {
×
1674
                    log.DebugFormat("Can't install {0}: {1}", request, string.Join("; ", resolver.ConflictDescriptions));
×
1675
                    return false;
×
1676
                }
1677
            }
1678
            catch (TooManyModsProvideKraken k)
2✔
1679
            {
2✔
1680
                // One of the dependencies is virtual
1681
                foreach (var mod in k.modules)
8✔
1682
                {
2✔
1683
                    // Try each option recursively to see if any are successful
1684
                    if (CanInstall(toInstall.Append(mod).ToArray(), opts, registry, game, crit))
2✔
1685
                    {
2✔
1686
                        // Child call will emit debug output, so we don't need to here
1687
                        return true;
2✔
1688
                    }
1689
                }
×
1690
                log.Debug($"Can't install {request}: Can't install provider of {k.requested}");
×
1691
            }
×
1692
            catch (InconsistentKraken k)
×
1693
            {
×
1694
                log.Debug($"Can't install {request}: {k.ShortDescription}");
×
1695
            }
×
1696
            catch (Exception ex)
×
1697
            {
×
1698
                log.Debug($"Can't install {request}: {ex.Message}");
×
1699
            }
×
1700
            return false;
×
1701
        }
2✔
1702

1703
        #endregion
1704

1705
        private static void EnforceCacheSizeLimit(Registry       registry,
1706
                                                  NetModuleCache Cache,
1707
                                                  IConfiguration config)
1708
        {
2✔
1709
            // Purge old downloads if we're over the limit
1710
            if (config.CacheSizeLimit.HasValue)
2✔
1711
            {
×
1712
                Cache.EnforceSizeLimit(config.CacheSizeLimit.Value, registry);
×
1713
            }
×
1714
        }
2✔
1715

1716
        private void CheckAddRemoveFreeSpace(IEnumerable<CkanModule>      toInstall,
1717
                                             IEnumerable<InstalledModule> toRemove)
1718
        {
2✔
1719
            if (toInstall.Sum(m => m.install_size) - toRemove.Sum(im => im.ActualInstallSize(instance))
2✔
1720
                is > 0 and var spaceDelta)
1721
            {
×
1722
                CKANPathUtils.CheckFreeSpace(new DirectoryInfo(instance.GameDir),
×
1723
                                             spaceDelta,
1724
                                             Properties.Resources.NotEnoughSpaceToInstall);
1725
            }
×
1726
        }
2✔
1727

1728
        private readonly GameInstance      instance;
1729
        private readonly NetModuleCache    cache;
1730
        private readonly IConfiguration    config;
1731
        private readonly CancellationToken cancelToken;
1732

1733
        private static readonly ILog log = LogManager.GetLogger(typeof(ModuleInstaller));
2✔
1734
    }
1735
}
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