/// <summary> /// install mods as an asynchronous operation. /// </summary> /// <returns>Task<System.Boolean>.</returns> public virtual async Task <bool> InstallModsAsync() { var game = GameService.GetSelected(); if (game == null) { return(false); } var mods = GetInstalledModsInternal(game, false); var descriptors = new List <IMod>(); var userDirectoryMods = GetAllModDescriptors(Path.Combine(game.UserDirectory, Shared.Constants.ModDirectory), ModSource.Local); if (userDirectoryMods?.Count() > 0) { descriptors.AddRange(userDirectoryMods); } var workshopDirectoryMods = GetAllModDescriptors(game.WorkshopDirectory, ModSource.Steam); if (workshopDirectoryMods?.Count() > 0) { descriptors.AddRange(workshopDirectoryMods); } var diffs = descriptors.Where(p => !mods.Any(m => m.DescriptorFile.Equals(p.DescriptorFile, StringComparison.OrdinalIgnoreCase))).ToList(); if (diffs.Count > 0) { await ModWriter.CreateModDirectoryAsync(new ModWriterParameters() { RootDirectory = game.UserDirectory, Path = Shared.Constants.ModDirectory }); var tasks = new List <Task>(); foreach (var diff in diffs) { if (IsPatchModInternal(diff)) { continue; } tasks.Add(ModWriter.WriteDescriptorAsync(new ModWriterParameters() { Mod = diff, RootDirectory = game.UserDirectory, Path = diff.DescriptorFile }, IsPatchModInternal(diff))); } if (tasks.Count > 0) { await Task.WhenAll(tasks); Cache.Invalidate(ModsCachePrefix, ConstructModsCacheKey(game, true), ConstructModsCacheKey(game, false)); } return(true); } return(false); }
/// <summary> /// Exports the mods asynchronous. /// </summary> /// <param name="enabledMods">The mods.</param> /// <param name="regularMods">The regular mods.</param> /// <param name="modCollection">The mod collection.</param> /// <returns>Task<System.Boolean>.</returns> public virtual async Task <bool> ExportModsAsync(IReadOnlyCollection <IMod> enabledMods, IReadOnlyCollection <IMod> regularMods, IModCollection modCollection) { var game = GameService.GetSelected(); if (game == null || enabledMods == null || regularMods == null || modCollection == null) { return(false); } var allMods = GetInstalledModsInternal(game, false); var mod = GeneratePatchModDescriptor(allMods, game, GenerateCollectionPatchName(modCollection.Name)); var applyModParams = new ModWriterParameters() { OtherMods = regularMods.Where(p => !enabledMods.Any(m => m.DescriptorFile.Equals(p.DescriptorFile))).ToList(), EnabledMods = enabledMods, RootDirectory = game.UserDirectory }; if (await ModWriter.ModDirectoryExistsAsync(new ModWriterParameters() { RootDirectory = mod.FullPath })) { if (modCollection.PatchModEnabled && enabledMods.Any()) { if (await ModWriter.WriteDescriptorAsync(new ModWriterParameters() { Mod = mod, RootDirectory = game.UserDirectory, Path = mod.DescriptorFile, LockDescriptor = CheckIfModShouldBeLocked(game, mod) }, IsPatchModInternal(mod))) { applyModParams.TopPriorityMods = new List <IMod>() { mod }; Cache.Invalidate(new CacheInvalidateParameters() { Region = ModsCacheRegion, Prefix = game.Type, Keys = new List <string> { GetModsCacheKey(true), GetModsCacheKey(false) } }); } } } else { // Remove left over descriptor if (allMods.Any(p => p.Name.Equals(mod.Name))) { await DeleteDescriptorsInternalAsync(new List <IMod>() { mod }); } } return(await ModWriter.ApplyModsAsync(applyModParams)); }
/// <summary> /// Exports the mods asynchronous. /// </summary> /// <param name="enabledMods">The mods.</param> /// <param name="regularMods">The regular mods.</param> /// <param name="collectionName">Name of the collection.</param> /// <returns>Task<System.Boolean>.</returns> public virtual async Task <bool> ExportModsAsync(IReadOnlyCollection <IMod> enabledMods, IReadOnlyCollection <IMod> regularMods, string collectionName) { var game = GameService.GetSelected(); if (game == null || enabledMods == null || regularMods == null) { return(false); } var allMods = GetInstalledModsInternal(game, false); var mod = GeneratePatchModDescriptor(allMods, game, GenerateCollectionPatchName(collectionName)); var applyModParams = new ModWriterParameters() { OtherMods = regularMods.Where(p => !enabledMods.Any(m => m.DescriptorFile.Equals(p.DescriptorFile))).ToList(), EnabledMods = enabledMods, RootDirectory = game.UserDirectory }; if (await ModWriter.ModDirectoryExistsAsync(new ModWriterParameters() { RootDirectory = game.UserDirectory, Path = mod.FileName })) { if (await ModWriter.WriteDescriptorAsync(new ModWriterParameters() { Mod = mod, RootDirectory = game.UserDirectory, Path = mod.DescriptorFile }, IsPatchModInternal(mod))) { applyModParams.TopPriorityMods = new List <IMod>() { mod }; Cache.Invalidate(ModsCachePrefix, ConstructModsCacheKey(game, true), ConstructModsCacheKey(game, false)); } } return(await ModWriter.ApplyModsAsync(applyModParams)); }
/// <summary> /// install mods as an asynchronous operation. /// </summary> /// <param name="statusToRetain">The status to retain.</param> /// <returns>Task<System.Boolean>.</returns> public virtual async Task <bool> InstallModsAsync(IEnumerable <IMod> statusToRetain) { var game = GameService.GetSelected(); if (game == null) { return(false); } var mods = GetInstalledModsInternal(game, false); var descriptors = new List <IMod>(); var userDirectoryMods = GetAllModDescriptors(Path.Combine(game.UserDirectory, Shared.Constants.ModDirectory), ModSource.Local); if (userDirectoryMods?.Count() > 0) { descriptors.AddRange(userDirectoryMods); } var workshopDirectoryMods = GetAllModDescriptors(game.WorkshopDirectory, ModSource.Steam); if (workshopDirectoryMods?.Count() > 0) { descriptors.AddRange(workshopDirectoryMods); } var diffs = descriptors.Where(p => !mods.Any(m => m.DescriptorFile.Equals(p.DescriptorFile, StringComparison.OrdinalIgnoreCase))).ToList(); if (diffs.Count > 0) { await ModWriter.CreateModDirectoryAsync(new ModWriterParameters() { RootDirectory = game.UserDirectory, Path = Shared.Constants.ModDirectory }); var tasks = new List <Task>(); foreach (var diff in diffs.GroupBy(p => p.DescriptorFile)) { var localDiff = diff.FirstOrDefault(); if (IsPatchModInternal(localDiff)) { continue; } tasks.Add(Task.Run(async() => { bool shouldLock = CheckIfModShouldBeLocked(game, localDiff); if (statusToRetain != null && !shouldLock) { var mod = statusToRetain.FirstOrDefault(p => p.DescriptorFile.Equals(localDiff.DescriptorFile, StringComparison.OrdinalIgnoreCase)); if (mod != null) { shouldLock = mod.IsLocked; } } await ModWriter.WriteDescriptorAsync(new ModWriterParameters() { Mod = localDiff, RootDirectory = game.UserDirectory, Path = localDiff.DescriptorFile, LockDescriptor = shouldLock }, IsPatchModInternal(localDiff));; })); } if (tasks.Count > 0) { await Task.WhenAll(tasks); Cache.Invalidate(ModsCachePrefix, ConstructModsCacheKey(game, true), ConstructModsCacheKey(game, false)); } return(true); } return(false); }
/// <summary> /// install mods as an asynchronous operation. /// </summary> /// <param name="statusToRetain">The status to retain.</param> /// <returns>Task<System.Boolean>.</returns> public virtual async Task <IReadOnlyCollection <IModInstallationResult> > InstallModsAsync(IEnumerable <IMod> statusToRetain) { var game = GameService.GetSelected(); if (game == null) { return(null); } var mods = GetInstalledModsInternal(game, false); var descriptors = new List <IModInstallationResult>(); var userDirectoryMods = GetAllModDescriptors(Path.Combine(game.UserDirectory, Shared.Constants.ModDirectory), ModSource.Local); if (userDirectoryMods?.Count() > 0) { descriptors.AddRange(userDirectoryMods); } if (!string.IsNullOrWhiteSpace(game.CustomModDirectory)) { var customMods = GetAllModDescriptors(GetModDirectoryRootPath(game), ModSource.Local); if (customMods != null && customMods.Any()) { descriptors.AddRange(customMods); } } var workshopDirectoryMods = game.WorkshopDirectory.SelectMany(p => GetAllModDescriptors(p, ModSource.Steam)); if (workshopDirectoryMods.Any()) { descriptors.AddRange(workshopDirectoryMods); } var diffs = descriptors.Where(p => p.Mod != null && !mods.Any(m => m.DescriptorFile.Equals(p.Mod.DescriptorFile, StringComparison.OrdinalIgnoreCase))).ToList(); if (diffs.Count > 0) { var result = new List <IModInstallationResult>(); await ModWriter.CreateModDirectoryAsync(new ModWriterParameters() { RootDirectory = game.UserDirectory, Path = Shared.Constants.ModDirectory }); var tasks = new List <Task>(); foreach (var diff in diffs.GroupBy(p => p.Mod.DescriptorFile)) { var installResult = diff.FirstOrDefault(); var localDiff = diff.FirstOrDefault().Mod; if (IsPatchModInternal(localDiff)) { continue; } tasks.Add(Task.Run(async() => { bool shouldLock = CheckIfModShouldBeLocked(game, localDiff); if (statusToRetain != null && !shouldLock) { var mod = statusToRetain.FirstOrDefault(p => p.DescriptorFile.Equals(localDiff.DescriptorFile, StringComparison.OrdinalIgnoreCase)); if (mod != null) { shouldLock = mod.IsLocked; } } await ModWriter.WriteDescriptorAsync(new ModWriterParameters() { Mod = localDiff, RootDirectory = game.UserDirectory, Path = localDiff.DescriptorFile, LockDescriptor = shouldLock }, IsPatchModInternal(localDiff));; })); installResult.Installed = true; result.Add(installResult); } if (tasks.Count > 0) { await Task.WhenAll(tasks); Cache.Invalidate(new CacheInvalidateParameters() { Region = ModsCacheRegion, Prefix = game.Type, Keys = new List <string> { GetModsCacheKey(true), GetModsCacheKey(false) } }); } if (descriptors.Any(p => p.Invalid)) { result.AddRange(descriptors.Where(p => p.Invalid)); } return(result); } if (descriptors.Any(p => p.Invalid)) { return(descriptors.Where(p => p.Invalid).ToList()); } return(null); }