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

KSP-CKAN / CKAN / 18955927993

30 Oct 2025 09:45PM UTC coverage: 85.243% (+3.4%) from 81.873%
18955927993

Pull #4454

github

HebaruSan
Build on Windows, upload multi-platform coverage
Pull Request #4454: Build on Windows, upload multi-platform coverage

2003 of 2167 branches covered (92.43%)

Branch coverage included in aggregate %.

9 of 9 new or added lines in 3 files covered. (100.0%)

42 existing lines in 20 files now uncovered.

11959 of 14212 relevant lines covered (84.15%)

1.76 hits per line

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

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

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

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

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

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

43
        public IUser User { get; set; }
44

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

49
        #region Installation
50

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

465
        #region File overwrites
466

467
        /// <summary>
468
        /// Find files in the given list that are already installed and unowned.
469
        /// Note, this compares files on demand; Memoize for performance!
470
        /// </summary>
471
        /// <param name="zip">Zip file that we are installing from</param>
472
        /// <param name="files">Files that we want to install for a module</param>
473
        /// <param name="registry">Registry to check for file ownership</param>
474
        /// <returns>
475
        /// List of pairs: Key = file, Value = true if identical, false if different
476
        /// </returns>
477
        private IEnumerable<(InstallableFile file, bool same)> FindConflictingFiles(ZipFile                      zip,
478
                                                                                    IEnumerable<InstallableFile> files,
479
                                                                                    Registry                     registry)
480
            => files.Where(file => !file.source.IsDirectory
2✔
481
                                   && File.Exists(file.destination)
482
                                   && registry.FileOwner(instance.ToRelativeGameDir(file.destination)) == null)
483
                    .Select(file =>
484
                    {
2✔
485
                        log.DebugFormat("Comparing {0}", file.destination);
2✔
486
                        using (Stream     zipStream = zip.GetInputStream(file.source))
2✔
487
                        using (FileStream curFile   = new FileStream(file.destination,
2✔
488
                                                                     FileMode.Open,
489
                                                                     FileAccess.Read))
490
                        {
2✔
491
                            return (file,
2✔
492
                                    same: 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 static void DeleteConflictingFiles(IEnumerable<InstallableFile> files)
547
        {
2✔
548
            TxFileManager file_transaction = new TxFileManager();
2✔
549
            foreach (InstallableFile file in files)
8✔
550
            {
2✔
551
                log.DebugFormat("Trying to delete {0}", file.destination);
2✔
552
                file_transaction.Delete(file.destination);
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

569
        public static List<InstallableFile> FindInstallableFiles(CkanModule    module,
570
                                                                 ZipFile       zipfile,
571
                                                                 GameInstance  inst)
572
            => FindInstallableFiles(module, zipfile, inst, inst.Game);
2✔
573

574
        public static List<InstallableFile> FindInstallableFiles(CkanModule module,
575
                                                                 ZipFile    zipfile,
576
                                                                 IGame      game)
577
            => FindInstallableFiles(module, zipfile, null, game);
2✔
578

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

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

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

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

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

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

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

696
        #endregion
697

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

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

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

729
                log.DebugFormat("Making directory '{0}'", fullPath);
2✔
730
                file_transaction.CreateDirectory(fullPath);
2✔
731
            }
2✔
732
            else
733
            {
2✔
734
                log.DebugFormat("Writing file '{0}'", fullPath);
2✔
735

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

743
                // We don't allow for the overwriting of files. See #208.
744
                if (file_transaction.FileExists(fullPath))
2✔
745
                {
2✔
746
                    throw new FileExistsKraken(fullPath);
2✔
747
                }
748

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

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

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

796
        private static readonly TimeSpan UnzipProgressInterval = TimeSpan.FromMilliseconds(200);
2✔
797

798
        #endregion
799

800
        #region Uninstallation
801

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

817
            foreach (string mod in mods.Where(mod => registry_manager.registry.InstalledModule(mod) == null))
2✔
818
            {
2✔
819
                throw new ModNotInstalledKraken(mod);
2✔
820
            }
821

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

830
            // Find all the things which need uninstalling.
831
            var revdep = mods
2✔
832
                .Union(registry_manager.registry.FindReverseDependencies(
833
                    mods.Except(installing.Select(m => m.identifier)).ToArray(),
×
834
                    installing))
835
                .ToArray();
836

837
            var goners = revdep.Union(
2✔
838
                                registry_manager.registry.FindRemovableAutoInstalled(installing,
839
                                                                                     revdep.ToHashSet(),
840
                                                                                     instance)
841
                                                         .Select(im => im.identifier))
2✔
842
                               .Order()
843
                               .ToArray();
844

845
            // If there is nothing to uninstall, skip out.
846
            if (goners.Length == 0)
2✔
847
            {
2✔
848
                return;
2✔
849
            }
850

851
            User.RaiseMessage(Properties.Resources.ModuleInstallerAboutToRemove);
2✔
852
            User.RaiseMessage("");
2✔
853

854
            foreach (var module in goners.Select(registry_manager.registry.InstalledModule)
8✔
855
                                         .OfType<InstalledModule>())
856
            {
2✔
857
                User.RaiseMessage(" * {0} {1}", module.Module.name, module.Module.version);
2✔
858
            }
2✔
859

860
            if (ConfirmPrompt && !User.RaiseYesNoDialog(Properties.Resources.ModuleInstallerContinuePrompt))
2✔
861
            {
×
862
                throw new CancelledActionKraken(Properties.Resources.ModuleInstallerRemoveAborted);
×
863
            }
864

865
            using (var transaction = CkanTransaction.CreateTransactionScope())
2✔
866
            {
2✔
867
                var registry = registry_manager.registry;
2✔
868
                long removeBytes = goners.Select(registry.InstalledModule)
2✔
869
                                         .OfType<InstalledModule>()
870
                                         .Sum(m => m.Module.install_size);
2✔
871
                var rateCounter = new ByteRateCounter()
2✔
872
                {
873
                    Size      = removeBytes,
874
                    BytesLeft = removeBytes,
875
                };
876
                rateCounter.Start();
2✔
877

878
                long modRemoveCompletedBytes = 0;
2✔
879
                foreach (string ident in goners)
8✔
880
                {
2✔
881
                    if (registry.InstalledModule(ident) is InstalledModule instMod)
2✔
882
                    {
2✔
883
                        Uninstall(ident, ref possibleConfigOnlyDirs, registry,
2✔
884
                                  new ProgressImmediate<long>(bytes =>
885
                                  {
2✔
886
                                      RemoveProgress?.Invoke(instMod,
2✔
887
                                                             Math.Max(0,     instMod.Module.install_size - bytes),
888
                                                             Math.Max(bytes, instMod.Module.install_size));
889
                                      rateCounter.BytesLeft = removeBytes - (modRemoveCompletedBytes
2✔
890
                                                                             + Math.Min(bytes, instMod.Module.install_size));
891
                                      User.RaiseProgress(rateCounter);
2✔
892
                                  }));
2✔
893
                        modRemoveCompletedBytes += instMod?.Module.install_size ?? 0;
2✔
894
                    }
2✔
895
                }
2✔
896

897
                // Enforce consistency if we're not installing anything,
898
                // otherwise consistency will be enforced after the installs
899
                registry_manager.Save(installing == null);
2✔
900

901
                transaction.Complete();
2✔
902
            }
2✔
903

904
            User.RaiseProgress(Properties.Resources.ModuleInstallerDone, 100);
2✔
905
        }
2✔
906

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

923
            using (var transaction = CkanTransaction.CreateTransactionScope())
2✔
924
            {
2✔
925
                var instMod = registry.InstalledModule(identifier);
2✔
926

927
                if (instMod == null)
2✔
928
                {
×
929
                    log.ErrorFormat("Trying to uninstall {0} but it's not installed", identifier);
×
930
                    throw new ModNotInstalledKraken(identifier);
×
931
                }
932
                User.RaiseMessage(Properties.Resources.ModuleInstallerRemovingMod,
2✔
933
                                  $"{instMod.Module.name} {instMod.Module.version}");
934

935
                // Walk our registry to find all files for this mod.
936
                var modFiles = instMod.Files.ToArray();
2✔
937

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

941
                // Files that Windows refused to delete due to locking (probably)
942
                var undeletableFiles = new List<string>();
2✔
943

944
                long bytesDeleted = 0;
2✔
945
                foreach (string relPath in modFiles)
8✔
946
                {
2✔
947
                    if (cancelToken.IsCancellationRequested)
2✔
948
                    {
×
949
                        throw new CancelledActionKraken();
×
950
                    }
951

952
                    string absPath = instance.ToAbsoluteGameDir(relPath);
2✔
953

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

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

1005
                if (undeletableFiles.Count > 0)
2✔
1006
                {
×
1007
                    throw new FailedToDeleteFilesKraken(identifier, undeletableFiles);
×
1008
                }
1009

1010
                // Remove from registry.
1011
                registry.DeregisterModule(instance, identifier);
2✔
1012

1013
                // Our collection of directories may leave empty parent directories.
1014
                directoriesToDelete = AddParentDirectories(directoriesToDelete);
2✔
1015

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

1030
                    // See what's left in this folder and what we can do about it
1031
                    GroupFilesByRemovable(instance.ToRelativeGameDir(directory),
2✔
1032
                                          registry, modFiles, instance.Game,
1033
                                          (Directory.Exists(directory)
1034
                                              ? Directory.EnumerateFileSystemEntries(directory, "*", SearchOption.AllDirectories)
1035
                                              : Enumerable.Empty<string>())
1036
                                           .Select(instance.ToRelativeGameDir)
1037
                                           .ToArray(),
1038
                                          out string[] removable,
1039
                                          out string[] notRemovable);
1040

1041
                    // Delete the auto-removable files and dirs
1042
                    foreach (var absPath in removable.Select(instance.ToAbsoluteGameDir))
8✔
1043
                    {
2✔
1044
                        if (File.Exists(absPath))
2✔
1045
                        {
2✔
1046
                            log.DebugFormat("Attempting transaction deletion of file {0}", absPath);
2✔
1047
                            file_transaction.Delete(absPath);
2✔
1048
                        }
2✔
1049
                        else if (Directory.Exists(absPath))
2✔
1050
                        {
2✔
1051
                            log.DebugFormat("Attempting deletion of directory {0}", absPath);
2✔
1052
                            try
1053
                            {
2✔
1054
                                Directory.Delete(absPath);
2✔
1055
                            }
2✔
1056
                            catch
×
1057
                            {
×
1058
                                // There might be files owned by other mods, oh well
1059
                                log.DebugFormat("Failed to delete {0}", absPath);
×
1060
                            }
×
1061
                        }
2✔
1062
                    }
2✔
1063

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

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

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

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

1158
                    // If this is a parentless directory (Windows)
1159
                    // or if the Root equals the current directory (Mono)
1160
                    if (dirInfo.Parent == null || dirInfo.Root == dirInfo)
2✔
1161
                    {
2✔
1162
                        return results;
2✔
1163
                    }
1164

1165
                    if (!dir.StartsWith(gameDir, Platform.PathComparison))
2✔
1166
                    {
×
1167
                        dir = CKANPathUtils.ToAbsolute(dir, gameDir);
×
1168
                    }
×
1169

1170
                    // Remove the system paths, leaving the path under the instance directory
1171
                    var relativeHead = CKANPathUtils.ToRelative(dir, gameDir);
2✔
1172
                    // Don't try to remove GameRoot
1173
                    if (!string.IsNullOrEmpty(relativeHead))
2✔
1174
                    {
2✔
1175
                        var pathArray = relativeHead.Split('/');
2✔
1176
                        var builtPath = "";
2✔
1177
                        foreach (var path in pathArray)
8✔
1178
                        {
2✔
1179
                            builtPath += path + '/';
2✔
1180
                            results.Add(CKANPathUtils.ToAbsolute(builtPath, gameDir));
2✔
1181
                        }
2✔
1182
                    }
2✔
1183

1184
                    return results;
2✔
1185
                })
2✔
1186
                .Where(dir => !instance.Game.IsReservedDirectory(instance, dir))
2✔
1187
                .ToHashSet();
1188
        }
2✔
1189

1190
        #endregion
1191

1192
        #region AddRemove
1193

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

1226
                long removeBytes     = remove.Sum(m => m.Module.install_size);
2✔
1227
                long removedBytes    = 0;
2✔
1228
                long downloadBytes   = toDownload.Sum(m => m.download_size);
2✔
1229
                long downloadedBytes = 0;
2✔
1230
                long installBytes    = add.Sum(m => m.install_size);
2✔
1231
                long installedBytes  = 0;
2✔
1232
                var rateCounter = new ByteRateCounter()
2✔
1233
                {
1234
                    Size      = removeBytes + downloadBytes + installBytes,
1235
                    BytesLeft = removeBytes + downloadBytes + installBytes,
1236
                };
1237
                rateCounter.Start();
2✔
1238

1239
                downloader.OverallDownloadProgress += brc =>
2✔
1240
                {
2✔
1241
                    downloadedBytes = downloadBytes - brc.BytesLeft;
2✔
1242
                    rateCounter.BytesLeft = removeBytes   - removedBytes
2✔
1243
                                          + downloadBytes - downloadedBytes
1244
                                          + installBytes  - installedBytes;
1245
                    User.RaiseProgress(rateCounter);
2✔
1246
                };
2✔
1247
                var toInstall = ModsInDependencyOrder(resolver, cached, toDownload, downloader);
2✔
1248

1249
                long modRemoveCompletedBytes = 0;
2✔
1250
                foreach (var instMod in remove)
8✔
1251
                {
2✔
1252
                    Uninstall(instMod.Module.identifier,
2✔
1253
                              ref possibleConfigOnlyDirs,
1254
                              registry_manager.registry,
1255
                              new ProgressImmediate<long>(bytes =>
1256
                              {
2✔
1257
                                  RemoveProgress?.Invoke(instMod,
2✔
1258
                                                         Math.Max(0,     instMod.Module.install_size - bytes),
1259
                                                         Math.Max(bytes, instMod.Module.install_size));
1260
                                  removedBytes = modRemoveCompletedBytes
2✔
1261
                                                 + Math.Min(bytes, instMod.Module.install_size);
1262
                                  rateCounter.BytesLeft = removeBytes   - removedBytes
2✔
1263
                                                        + downloadBytes - downloadedBytes
1264
                                                        + installBytes  - installedBytes;
1265
                                  User.RaiseProgress(rateCounter);
2✔
1266
                              }));
2✔
1267
                     modRemoveCompletedBytes += instMod.Module.install_size;
2✔
1268
                }
2✔
1269

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

1300
                registry_manager.Save(enforceConsistency);
2✔
1301
                tx.Complete();
2✔
1302
                EnforceCacheSizeLimit(registry_manager.registry, cache, config);
2✔
1303
            }
2✔
1304
        }
2✔
1305

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

1321
            var removingIdents = registry.InstalledModules.Select(im => im.identifier)
2✔
1322
                                         .Intersect(modules.Select(m => m.identifier))
2✔
1323
                                         .ToHashSet();
1324
            var autoRemoving = registry
2✔
1325
                .FindRemovableAutoInstalled(modules, removingIdents, instance)
1326
                .ToHashSet();
1327

1328
            var resolver = new RelationshipResolver(
2✔
1329
                modules,
1330
                modules.Select(m => registry.InstalledModule(m.identifier)?.Module)
2✔
1331
                       .OfType<CkanModule>()
1332
                       .Concat(autoRemoving.Select(im => im.Module)),
2✔
1333
                RelationshipResolverOptions.DependsOnlyOpts(instance.StabilityToleranceConfig),
1334
                registry,
1335
                instance.Game, instance.VersionCriteria());
1336
            var fullChangeset = resolver.ModList()
2✔
1337
                                        .ToDictionary(m => m.identifier,
2✔
1338
                                                      m => m);
2✔
1339

1340
            // Skip removing ones we still need
1341
            var keepIdents = fullChangeset.Keys.Intersect(autoRemoving.Select(im => im.Module.identifier))
2✔
1342
                                               .ToHashSet();
1343
            autoRemoving.RemoveWhere(im => keepIdents.Contains(im.Module.identifier));
2✔
1344
            foreach (var ident in keepIdents)
8✔
1345
            {
2✔
1346
                fullChangeset.Remove(ident);
2✔
1347
            }
2✔
1348

1349
            // Only install stuff that's already there if explicitly requested in param
1350
            var toInstall = fullChangeset.Values
2✔
1351
                                         .Except(registry.InstalledModules
1352
                                                         .Select(im => im.Module)
2✔
1353
                                                         .Except(modules))
1354
                                         .ToArray();
1355
            var autoInstalled = toInstall.ToDictionary(m => m, resolver.IsAutoInstalled);
2✔
1356

1357
            User.RaiseMessage(Properties.Resources.ModuleInstallerAboutToUpgrade);
2✔
1358
            User.RaiseMessage("");
2✔
1359

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

1366
            // Let's discover what we need to do with each module!
1367
            foreach (CkanModule module in toInstall)
8✔
1368
            {
2✔
1369
                var installed_mod = registry.InstalledModule(module.identifier);
2✔
1370

1371
                if (installed_mod == null)
2✔
1372
                {
2✔
1373
                    if (!cache.IsMaybeCachedZip(module)
2✔
1374
                        && cache.GetInProgressFileName(module) is FileInfo inProgressFile)
1375
                    {
2✔
1376
                        if (inProgressFile.Exists)
2✔
1377
                        {
2✔
1378
                            User.RaiseMessage(Properties.Resources.ModuleInstallerUpgradeInstallingResuming,
2✔
1379
                                              module.name, module.version,
1380
                                              string.Join(", ", PrioritizedHosts(config, module.download)),
1381
                                              CkanModule.FmtSize(module.download_size - inProgressFile.Length));
1382
                        }
2✔
1383
                        else
1384
                        {
2✔
1385
                            User.RaiseMessage(Properties.Resources.ModuleInstallerUpgradeInstallingUncached,
2✔
1386
                                              module.name, module.version,
1387
                                              string.Join(", ", PrioritizedHosts(config, module.download)),
1388
                                              CkanModule.FmtSize(module.download_size));
1389
                        }
2✔
1390
                    }
2✔
1391
                    else
1392
                    {
2✔
1393
                        User.RaiseMessage(Properties.Resources.ModuleInstallerUpgradeInstallingCached,
2✔
1394
                                          module.name, module.version);
1395
                    }
2✔
1396
                }
2✔
1397
                else
1398
                {
2✔
1399
                    // Module already installed. We'll need to remove it first.
1400
                    toRemove.Add(installed_mod);
2✔
1401

1402
                    CkanModule installed = installed_mod.Module;
2✔
1403
                    if (installed.version.Equals(module.version))
2✔
1404
                    {
×
1405
                        User.RaiseMessage(Properties.Resources.ModuleInstallerUpgradeReinstalling,
×
1406
                                          module.name, module.version);
1407
                    }
×
1408
                    else if (installed.version.IsGreaterThan(module.version))
2✔
1409
                    {
2✔
1410
                        User.RaiseMessage(Properties.Resources.ModuleInstallerUpgradeDowngrading,
2✔
1411
                                          module.name, installed.version, module.version);
1412
                    }
2✔
1413
                    else
1414
                    {
2✔
1415
                        if (!cache.IsMaybeCachedZip(module)
2✔
1416
                            && cache.GetInProgressFileName(module) is FileInfo inProgressFile)
1417
                        {
2✔
1418
                            if (inProgressFile.Exists)
2✔
1419
                            {
2✔
1420
                                User.RaiseMessage(Properties.Resources.ModuleInstallerUpgradeUpgradingResuming,
2✔
1421
                                                  module.name, installed.version, module.version,
1422
                                                  string.Join(", ", PrioritizedHosts(config, module.download)),
1423
                                                  CkanModule.FmtSize(module.download_size - inProgressFile.Length));
1424
                            }
2✔
1425
                            else
1426
                            {
2✔
1427
                                User.RaiseMessage(Properties.Resources.ModuleInstallerUpgradeUpgradingUncached,
2✔
1428
                                                  module.name, installed.version, module.version,
1429
                                                  string.Join(", ", PrioritizedHosts(config, module.download)),
1430
                                                  CkanModule.FmtSize(module.download_size));
1431
                            }
2✔
1432
                        }
2✔
1433
                        else
1434
                        {
2✔
1435
                            User.RaiseMessage(Properties.Resources.ModuleInstallerUpgradeUpgradingCached,
2✔
1436
                                              module.name, installed.version, module.version);
1437
                        }
2✔
1438
                    }
2✔
1439
                }
2✔
1440
            }
2✔
1441

1442
            if (autoRemoving.Count > 0)
2✔
1443
            {
2✔
1444
                foreach (var im in autoRemoving)
8✔
1445
                {
2✔
1446
                    User.RaiseMessage(Properties.Resources.ModuleInstallerUpgradeAutoRemoving,
2✔
1447
                                      im.Module.name, im.Module.version);
1448
                }
2✔
1449
                toRemove.AddRange(autoRemoving);
2✔
1450
            }
2✔
1451

1452
            CheckAddRemoveFreeSpace(toInstall, toRemove);
2✔
1453

1454
            if (ConfirmPrompt && !User.RaiseYesNoDialog(Properties.Resources.ModuleInstallerContinuePrompt))
2✔
1455
            {
×
1456
                throw new CancelledActionKraken(Properties.Resources.ModuleInstallerUpgradeUserDeclined);
×
1457
            }
1458

1459
            AddRemove(ref possibleConfigOnlyDirs,
2✔
1460
                      registry_manager,
1461
                      resolver,
1462
                      toInstall,
1463
                      autoInstalled,
1464
                      toRemove,
1465
                      downloader,
1466
                      enforceConsistency,
1467
                      deduper);
1468
            User.RaiseProgress(Properties.Resources.ModuleInstallerDone, 100);
2✔
1469
        }
2✔
1470

1471
        /// <summary>
1472
        /// Enacts listed Module Replacements to the specified versions for the user's KSP.
1473
        /// Will *re-install* or *downgrade* (with a warning) as well as upgrade.
1474
        /// </summary>
1475
        /// <exception cref="DependenciesNotSatisfiedKraken">Thrown if a dependency for a replacing module couldn't be satisfied.</exception>
1476
        /// <exception cref="ModuleNotFoundKraken">Thrown if a module that should be replaced is not installed.</exception>
1477
        public void Replace(IEnumerable<ModuleReplacement> replacements,
1478
                            RelationshipResolverOptions    options,
1479
                            IDownloader                    downloader,
1480
                            ref HashSet<string>?           possibleConfigOnlyDirs,
1481
                            RegistryManager                registry_manager,
1482
                            InstalledFilesDeduplicator?    deduper = null,
1483
                            bool                           enforceConsistency = true)
1484
        {
2✔
1485
            replacements = replacements.Memoize();
2✔
1486
            log.Debug("Using Replace method");
2✔
1487
            var modsToInstall = new List<CkanModule>();
2✔
1488
            var modsToRemove  = new List<InstalledModule>();
2✔
1489
            foreach (ModuleReplacement repl in replacements)
8✔
1490
            {
2✔
1491
                modsToInstall.Add(repl.ReplaceWith);
2✔
1492
                log.DebugFormat("We want to install {0} as a replacement for {1}", repl.ReplaceWith.identifier, repl.ToReplace.identifier);
2✔
1493
            }
2✔
1494

1495
            // Our replacement involves removing the currently installed mods, then
1496
            // adding everything that needs installing (which may involve new mods to
1497
            // satisfy dependencies).
1498

1499
            // Let's discover what we need to do with each module!
1500
            foreach (ModuleReplacement repl in replacements)
8✔
1501
            {
2✔
1502
                string ident = repl.ToReplace.identifier;
2✔
1503
                var installedMod = registry_manager.registry.InstalledModule(ident);
2✔
1504

1505
                if (installedMod == null)
2✔
1506
                {
×
1507
                    log.WarnFormat("Wait, {0} is not actually installed?", ident);
×
1508
                    //Maybe ModuleNotInstalled ?
1509
                    if (registry_manager.registry.IsAutodetected(ident))
×
1510
                    {
×
1511
                        throw new ModuleNotFoundKraken(ident,
×
1512
                            repl.ToReplace.version.ToString(),
1513
                            string.Format(Properties.Resources.ModuleInstallerReplaceAutodetected, ident));
1514
                    }
1515

1516
                    throw new ModuleNotFoundKraken(ident,
×
1517
                        repl.ToReplace.version.ToString(),
1518
                        string.Format(Properties.Resources.ModuleInstallerReplaceNotInstalled, ident, repl.ReplaceWith.identifier));
1519
                }
1520
                else
1521
                {
2✔
1522
                    // Obviously, we need to remove the mod we are replacing
1523
                    modsToRemove.Add(installedMod);
2✔
1524

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

1529
                    // If replacement is not installed, we've already added it to modsToInstall above
1530
                    if (installed_replacement != null)
2✔
1531
                    {
2✔
1532
                        //Module already installed. We'll need to treat it as an upgrade.
1533
                        log.DebugFormat("It turns out {0} is already installed, we'll upgrade it.", installed_replacement.identifier);
2✔
1534
                        modsToRemove.Add(installed_replacement);
2✔
1535

1536
                        CkanModule installed = installed_replacement.Module;
2✔
1537
                        if (installed.version.Equals(repl.ReplaceWith.version))
2✔
1538
                        {
2✔
1539
                            log.InfoFormat("{0} is already at the latest version, reinstalling to replace {1}", repl.ReplaceWith.identifier, repl.ToReplace.identifier);
2✔
1540
                        }
2✔
1541
                        else if (installed.version.IsGreaterThan(repl.ReplaceWith.version))
×
1542
                        {
×
1543
                            log.WarnFormat("Downgrading {0} from {1} to {2} to replace {3}", repl.ReplaceWith.identifier, repl.ReplaceWith.version, repl.ReplaceWith.version, repl.ToReplace.identifier);
×
1544
                        }
×
1545
                        else
1546
                        {
×
1547
                            log.InfoFormat("Upgrading {0} to {1} to replace {2}", repl.ReplaceWith.identifier, repl.ReplaceWith.version, repl.ToReplace.identifier);
×
1548
                        }
×
1549
                    }
2✔
1550
                    else
1551
                    {
2✔
1552
                        log.InfoFormat("Replacing {0} with {1} {2}", repl.ToReplace.identifier, repl.ReplaceWith.identifier, repl.ReplaceWith.version);
2✔
1553
                    }
2✔
1554
                }
2✔
1555
            }
2✔
1556
            var resolver = new RelationshipResolver(modsToInstall, null, options, registry_manager.registry,
2✔
1557
                                                    instance.Game, instance.VersionCriteria());
1558
            var resolvedModsToInstall = resolver.ModList().ToArray();
2✔
1559

1560
            CheckAddRemoveFreeSpace(resolvedModsToInstall, modsToRemove);
2✔
1561
            AddRemove(ref possibleConfigOnlyDirs,
2✔
1562
                      registry_manager,
1563
                      resolver,
1564
                      resolvedModsToInstall,
1565
                      resolvedModsToInstall.ToDictionary(m => m, m => false),
2✔
1566
                      modsToRemove,
1567
                      downloader,
1568
                      enforceConsistency,
1569
                      deduper);
1570
            User.RaiseProgress(Properties.Resources.ModuleInstallerDone, 100);
2✔
1571
        }
2✔
1572

1573
        #endregion
1574

1575
        public static IEnumerable<string> PrioritizedHosts(IConfiguration    config,
1576
                                                           IEnumerable<Uri>? urls)
1577
            => urls?.OrderBy(u => u, new PreferredHostUriComparer(config.PreferredHosts))
2✔
1578
                    .Select(dl => dl.Host)
2✔
1579
                    .Distinct()
1580
                   ?? Enumerable.Empty<string>();
1581

1582
        #region Recommendations
1583

1584
        /// <summary>
1585
        /// Looks for optional related modules that could be installed alongside the given modules
1586
        /// </summary>
1587
        /// <param name="instance">Game instance to use</param>
1588
        /// <param name="sourceModules">Modules to check for relationships, should contain the complete changeset including dependencies</param>
1589
        /// <param name="toInstall">Modules already being installed, to be omitted from search</param>
1590
        /// <param name="toRemove">Modules being removed, to be excluded from relationship resolution</param>
1591
        /// <param name="exclude">Modules the user has already seen and decided not to install</param>
1592
        /// <param name="registry">Registry to use</param>
1593
        /// <param name="recommendations">Modules that are recommended to install</param>
1594
        /// <param name="suggestions">Modules that are suggested to install</param>
1595
        /// <param name="supporters">Modules that support other modules we're installing</param>
1596
        /// <returns>
1597
        /// true if anything found, false otherwise
1598
        /// </returns>
1599
        public static bool FindRecommendations(GameInstance                                          instance,
1600
                                               IReadOnlyCollection<CkanModule>                       sourceModules,
1601
                                               IReadOnlyCollection<CkanModule>                       toInstall,
1602
                                               IReadOnlyCollection<CkanModule>                       toRemove,
1603
                                               IReadOnlyCollection<CkanModule>                       exclude,
1604
                                               Registry                                              registry,
1605
                                               out Dictionary<CkanModule, Tuple<bool, List<string>>> recommendations,
1606
                                               out Dictionary<CkanModule, List<string>>              suggestions,
1607
                                               out Dictionary<CkanModule, HashSet<string>>           supporters)
1608
        {
2✔
1609
            log.DebugFormat("Finding recommendations for: {0}", string.Join(", ", sourceModules));
2✔
1610
            var crit     = instance.VersionCriteria();
2✔
1611

1612
            var rmvIdents = toRemove.Select(m => m.identifier).ToHashSet();
2✔
1613
            var allRemoving = toRemove
2✔
1614
                .Concat(registry.FindRemovableAutoInstalled(sourceModules, rmvIdents, instance)
1615
                                .Select(im => im.Module))
2✔
1616
                .ToArray();
1617

1618
            var resolver = new RelationshipResolver(sourceModules.Where(m => !m.IsDLC),
2✔
1619
                                                    allRemoving,
1620
                                                    RelationshipResolverOptions.KitchenSinkOpts(instance.StabilityToleranceConfig),
1621
                                                    registry, instance.Game, crit);
1622
            var recommenders = resolver.Dependencies().ToHashSet();
2✔
1623
            log.DebugFormat("Recommenders: {0}", string.Join(", ", recommenders));
2✔
1624

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

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

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

1670
            return recommendations.Count > 0
2✔
1671
                || suggestions.Count > 0
1672
                || supporters.Count > 0;
1673
        }
2✔
1674

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

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

1739
        #endregion
1740

1741
        private static void EnforceCacheSizeLimit(Registry       registry,
1742
                                                  NetModuleCache Cache,
1743
                                                  IConfiguration config)
1744
        {
2✔
1745
            // Purge old downloads if we're over the limit
1746
            if (config.CacheSizeLimit.HasValue)
2✔
1747
            {
×
1748
                Cache.EnforceSizeLimit(config.CacheSizeLimit.Value, registry);
×
1749
            }
×
1750
        }
2✔
1751

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

1764
        private readonly GameInstance      instance;
1765
        private readonly NetModuleCache    cache;
1766
        private readonly IConfiguration    config;
1767
        private readonly CancellationToken cancelToken;
1768

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

© 2025 Coveralls, Inc