private async Task PreserveMods(DirectoryInfo localModDirectory) { foreach (var file in localModDirectory.GetFiles("*.zip")) { if (ModFile.TryLoadFromFile(file, out var modFile)) { await Mod.Add(modFile, Mods, MainViewModel.Instance.Modpacks, true, true); } } foreach (var directory in localModDirectory.GetDirectories()) { if (ModFile.TryLoadFromDirectory(directory, out var modFile)) { await Mod.Add(modFile, Mods, MainViewModel.Instance.Modpacks, true, true); } } }
/// <summary> /// Downloads a mod. /// </summary> /// <param name="release">The mods release to be downloaded.</param> /// <param name="username">The username.</param> /// <param name="token">The login token.</param> /// <param name="progress">A progress object used to report the progress of the operation.</param> /// <param name="cancellationToken">A cancelation token that can be used to cancel the operation.</param> /// <param name="parentCollection">The collection to contain the mods.</param> /// <param name="modpackCollection">The collection containing all modpacks.</param> public static async Task <Mod> DownloadReleaseAsync(ModRelease release, string username, string token, IProgress <double> progress, CancellationToken cancellationToken, ModCollection parentCollection, ModpackCollection modpackCollection) { DirectoryInfo modDirectory = App.Instance.Settings.GetModDirectory(release.InfoFile.FactorioVersion); if (!modDirectory.Exists) { modDirectory.Create(); } var downloadUrl = BuildUrl(release, username, token); var file = new FileInfo(Path.Combine(modDirectory.FullName, release.FileName)); try { await WebHelper.DownloadFileAsync(downloadUrl, null, file, progress, cancellationToken); if (!cancellationToken.IsCancellationRequested) { if (ModFile.TryLoadFromFile(file, out ModFile modFile)) { if (modFile.InfoFile.FactorioVersion == release.InfoFile.FactorioVersion) { return(await Mod.Add(modFile, parentCollection, modpackCollection, false)); } } throw new InvalidOperationException("The server sent an invalid mod file."); } } catch (Exception) { if (file.Exists) { file.Delete(); } throw; } return(null); }
private async Task AddMod(ModExportTemplate modTemplate, DirectoryInfo fileLocation) { var fsInfo = GetIncludedFileOrDirectory(modTemplate, fileLocation); FileInfo file = fsInfo as FileInfo; if (file != null) { if (ModFile.TryLoadFromFile(file, out var modFile, true)) { Mod mod = await Mod.Add(modFile, Mods, Modpacks, false, true); modTemplate.Mod = mod; return; } else { throw new InvalidOperationException("Invalid mod file."); } }
private async Task UpdateModAsyncInner(ModUpdateInfo modUpdate, string token, IProgress <double> progress, CancellationToken cancellationToken) { var updateFile = await ModWebsite.DownloadUpdateAsync(modUpdate.Update, GlobalCredentials.Instance.Username, token, progress, cancellationToken); if (modUpdate.Extract) { updateFile = await updateFile.ExtractAsync(); } Mod newMod = await Mod.Add(updateFile, Mods, Modpacks, false, true); foreach (var version in modUpdate.ModVersions) { if (version.IsSelected) { bool deleteAfter = !App.Instance.Settings.KeepOldModVersions; foreach (var modpack in Modpacks) { if (modpack.Contains(version.Mod, out var reference)) { if (modpack.IsLocked) { deleteAfter = false; } else { reference.RemoveFromParentCommand.Execute(); modpack.Mods.Add(new ModReference(newMod, modpack)); } } } if (deleteAfter) { version.Mod.Delete(false); } } } }
private async Task UpdateModAsyncInner(ModUpdateInfo modUpdate, string token, IProgress <double> progress, CancellationToken cancellationToken) { var updateFile = await ModWebsite.DownloadUpdateAsync(modUpdate.Update, GlobalCredentials.Instance.Username, token, progress, cancellationToken); await Mod.Add(updateFile, Mods, Modpacks, false, true); }