Beispiel #1
0
        private async Task AddInfoAsync(Dictionary <string, ExtendedModInfo> infos, string modName)
        {
            if (!infos.TryGetValue(modName, out var info))
            {
                try
                {
                    info = await ModWebsite.GetExtendedInfoAsync(modName);

                    infos.Add(modName, info);
                }
                catch (WebException ex)
                {
                    if (ex.Status == WebExceptionStatus.ProtocolError)
                    {
                        var response = ex.Response as HttpWebResponse;
                        if ((response != null) && (response.StatusCode == HttpStatusCode.NotFound))
                        {
                            return;
                        }
                    }

                    throw;
                }
            }
        }
        private async Task UpdateModAsyncInner(Mod oldMod, ModRelease newestRelease, string token, IProgress <double> progress, CancellationToken cancellationToken)
        {
            FileInfo modFile = await ModWebsite.UpdateReleaseAsync(newestRelease, GlobalCredentials.Instance.Username, token, progress, cancellationToken);

            Mod newMod;

            if (App.Instance.Settings.AlwaysUpdateZipped || (oldMod is ZippedMod))
            {
                newMod = new ZippedMod(oldMod.Name, newestRelease.Version, newestRelease.FactorioVersion, modFile, InstalledMods, InstalledModpacks);
            }
            else
            {
                DirectoryInfo modDirectory = await Task.Run(() =>
                {
                    progress.Report(2);
                    DirectoryInfo modsDirectory = App.Instance.Settings.GetModDirectory(newestRelease.FactorioVersion);
                    ZipFile.ExtractToDirectory(modFile.FullName, modsDirectory.FullName);
                    modFile.Delete();

                    return(new DirectoryInfo(Path.Combine(modsDirectory.FullName, modFile.NameWithoutExtension())));
                });

                newMod = new ExtractedMod(oldMod.Name, newestRelease.Version, newestRelease.FactorioVersion, modDirectory, InstalledMods, InstalledModpacks);
            }

            InstalledMods.Add(newMod);
            if (oldMod.Update(newMod))
            {
                InstalledModpacks.ExchangeMods(oldMod, newMod);
            }

            ModpackTemplateList.Instance.Update(InstalledModpacks);
            ModpackTemplateList.Instance.Save();
        }
Beispiel #3
0
        public static async Task <List <ModInfo> > FetchMods(Window progressOwner)
        {
            var progressWindow = new ProgressWindow()
            {
                Owner = progressOwner
            };
            var progressViewModel = (ProgressViewModel)progressWindow.ViewModel;

            progressViewModel.ActionName = App.Instance.GetLocalizedResourceString("FetchingModsAction");
            progressViewModel.CanCancel  = true;

            var progress = new Progress <Tuple <double, string> >(value =>
            {
                progressViewModel.Progress            = value.Item1;
                progressViewModel.ProgressDescription = value.Item2;
            });
            var cancellationSource = new CancellationTokenSource();

            progressViewModel.CancelRequested += (sender, e) => cancellationSource.Cancel();

            Task <List <ModInfo> > fetchModsTask = ModWebsite.GetModsAsync(progress, cancellationSource.Token);

            Task closeWindowTask = fetchModsTask.ContinueWith(t => progressWindow.Dispatcher.Invoke(progressWindow.Close));

            progressWindow.ShowDialog();

            List <ModInfo> modInfos = await fetchModsTask;
            await          closeWindowTask;

            return(cancellationSource.IsCancellationRequested ? null : modInfos);
        }
        private async Task DownloadModRelease(ModExportTemplate modTemplate, ModRelease release, DirectoryInfo fileLocation, IProgress <double> progress, CancellationToken cancellationToken)
        {
            if (Mods.TryGetMod(modTemplate.Name, release.Version, out Mod installedMod))
            {
                modTemplate.Mod = installedMod;
                return;
            }

            string token;

            GlobalCredentials.Instance.LogIn(Window, out token);

            string fileName = Path.Combine(fileLocation.FullName, $"{modTemplate.Uid}+{release.FileName}");
            await ModWebsite.DownloadReleaseToFileAsync(release, GlobalCredentials.Instance.Username, token, fileName, progress, cancellationToken);
        }
        private async Task DownloadDependency(ModDependencyInfo dependency, IProgress <double> progress, CancellationToken cancellationToken, string token)
        {
            var info = await ModWebsite.GetExtendedInfoAsync(dependency.Name);

            var latestRelease = info.GetLatestRelease(dependency.FactorioVersion);

            if ((dependency.Version != null) && (latestRelease.Version < dependency.Version))
            {
                MessageBox.Show(Window,
                                string.Format(App.Instance.GetLocalizedMessage("DependencyUnavailable", MessageType.Information), dependency.Name, dependency.Version),
                                App.Instance.GetLocalizedMessageTitle("DependencyUnavailable", MessageType.Information),
                                MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else
            {
                await ModWebsite.DownloadReleaseAsync(latestRelease, GlobalCredentials.Instance.Username, token, progress, cancellationToken, Mods, Modpacks);
            }
        }
        private async Task <ExtendedModInfo> GetModInfo(ModExportTemplate modTemplate)
        {
            ExtendedModInfo info = null;

            try
            {
                info = await ModWebsite.GetExtendedInfoAsync(modTemplate.Name);
            }
            catch (WebException ex)
            {
                if (ex.Status != WebExceptionStatus.ProtocolError)
                {
                    throw;
                }
            }

            return(info);
        }
Beispiel #7
0
        private async Task <ExtendedModInfo> GetModInfoAsync(Dictionary <string, ExtendedModInfo> infos, string modName)
        {
            ExtendedModInfo info;

            if (!infos.TryGetValue(modName, out info))
            {
                try
                {
                    info = await ModWebsite.GetExtendedInfoAsync(modName);
                }
                catch (WebException ex) when(ex.Status == WebExceptionStatus.ProtocolError)
                {
                    info = null;
                }

                infos.Add(modName, info);
            }

            return(info);
        }
Beispiel #8
0
        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 LoadExtendedModInfoAsync(ModInfo mod, int operationIndex)
        {
            ExtendedModInfo extendedInfo;

            try
            {
                extendedInfo = await ModWebsite.GetExtendedInfoAsync(mod);
            }
            catch (WebException)
            {
                MessageBox.Show(Window,
                                App.Instance.GetLocalizedMessage("InternetConnection", MessageType.Error),
                                App.Instance.GetLocalizedMessageTitle("InternetConnection", MessageType.Error),
                                MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (operationIndex == asyncFetchExtendedInfoIndex)
            {
                ExtendedInfo = extendedInfo;
            }
        }
Beispiel #10
0
        public static async Task <List <ModInfo> > FetchMods(Window progressOwner, ModCollection installedMods)
        {
            var progressWindow = new ProgressWindow()
            {
                Owner = progressOwner
            };
            var progressViewModel = (ProgressViewModel)progressWindow.ViewModel;

            progressViewModel.ActionName      = App.Instance.GetLocalizedResourceString("FetchingModsAction");
            progressViewModel.CanCancel       = false;
            progressViewModel.IsIndeterminate = true;

            Task <List <ModInfo> > fetchModsTask = ModWebsite.GetModsAsync(installedMods);

            Task closeWindowTask = fetchModsTask.ContinueWith(t => progressWindow.Dispatcher.Invoke(progressWindow.Close));

            progressWindow.ShowDialog();

            List <ModInfo> modInfos = await fetchModsTask;
            await          closeWindowTask;

            return(modInfos);
        }
        private async Task DownloadSelectedModRelease()
        {
            string token;

            if (GlobalCredentials.Instance.LogIn(Window, out token))
            {
                var progressWindow = new ProgressWindow {
                    Owner = Window
                };
                var progressViewModel = (ProgressViewModel)progressWindow.ViewModel;
                progressViewModel.ActionName          = App.Instance.GetLocalizedResourceString("DownloadingAction");
                progressViewModel.ProgressDescription = string.Format(App.Instance.GetLocalizedResourceString("DownloadingDescription"), selectedRelease.FileName);

                progressViewModel.CanCancel = true;
                var cancellationSource = new CancellationTokenSource();
                progressViewModel.CancelRequested += (sender, e) => cancellationSource.Cancel();

                var progress = new Progress <double>(p => progressViewModel.Progress = p);

                Mod newMod;
                try
                {
                    Task closeWindowTask = null;
                    try
                    {
                        Task <Mod> downloadTask = ModWebsite.DownloadReleaseAsync(selectedRelease,
                                                                                  GlobalCredentials.Instance.Username, token,
                                                                                  progress, cancellationSource.Token, InstalledMods, MainViewModel.Instance.Modpacks);

                        closeWindowTask = downloadTask.ContinueWith(t => progressWindow.Dispatcher.Invoke(progressWindow.Close));
                        progressWindow.ShowDialog();

                        newMod = await downloadTask;
                    }
                    finally
                    {
                        if (closeWindowTask != null)
                        {
                            await closeWindowTask;
                        }
                    }
                }
                catch (HttpRequestException)
                {
                    MessageBox.Show(Window,
                                    App.Instance.GetLocalizedMessage("InternetConnection", MessageType.Error),
                                    App.Instance.GetLocalizedMessageTitle("InternetConnection", MessageType.Error),
                                    MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                if (!cancellationSource.IsCancellationRequested)
                {
                    if (newMod != null)
                    {
                        InstalledMods.Add(newMod);
                    }
                    UpdateSelectedReleases();
                }
            }
        }
Beispiel #12
0
 private async Task DownloadDependency(ModDependencyInfo dependency, IProgress <double> progress, CancellationToken cancellationToken, string token)
 {
     var release = dependency.Release;
     await ModWebsite.DownloadReleaseAsync(release, GlobalCredentials.Instance.Username, token, progress, cancellationToken, Mods, Modpacks);
 }
Beispiel #13
0
        private async Task UpdateSelectedModRelease()
        {
            string token;

            if (GlobalCredentials.Instance.LogIn(Window, out token))
            {
                ModRelease newestRelease = GetNewestRelease(ExtendedInfo, SelectedRelease);
                Mod        mod           = InstalledMods.FindByFactorioVersion(SelectedMod.Name, newestRelease.FactorioVersion);
                var        zippedMod     = mod as ZippedMod;
                var        extractedMod  = mod as ExtractedMod;

                var cancellationSource = new CancellationTokenSource();
                var progressWindow     = new ProgressWindow {
                    Owner = Window
                };
                var progressViewModel = (ProgressViewModel)progressWindow.ViewModel;
                progressViewModel.ActionName          = App.Instance.GetLocalizedResourceString("UpdatingAction");
                progressViewModel.ProgressDescription = string.Format(App.Instance.GetLocalizedResourceString("DownloadingDescription"), newestRelease.FileName);
                progressViewModel.CanCancel           = true;
                progressViewModel.CancelRequested    += (sender, e) => cancellationSource.Cancel();

                IProgress <double> progress = new Progress <double>(p =>
                {
                    if (p > 1)
                    {
                        progressViewModel.ProgressDescription = App.Instance.GetLocalizedResourceString("ExtractingDescription");
                        progressViewModel.IsIndeterminate     = true;
                        progressViewModel.CanCancel           = false;
                    }
                    else
                    {
                        progressViewModel.Progress = p;
                    }
                });

                try
                {
                    Task closeWindowTask = null;
                    try
                    {
                        Task downloadTask = ModWebsite.UpdateReleaseAsync(newestRelease, GlobalCredentials.Instance.Username, token, progress, cancellationSource.Token);

                        if (extractedMod != null)
                        {
                            downloadTask = downloadTask.ContinueWith(t =>
                            {
                                progress.Report(2);

                                FileInfo modFile           = ((Task <FileInfo>)t).Result;
                                DirectoryInfo modDirectory = App.Instance.Settings.GetModDirectory(newestRelease.FactorioVersion);
                                ZipFile.ExtractToDirectory(modFile.FullName, modDirectory.FullName);
                                modFile.Delete();

                                return(new DirectoryInfo(Path.Combine(modDirectory.FullName, modFile.NameWithoutExtension())));
                            }, TaskContinuationOptions.NotOnFaulted | TaskContinuationOptions.NotOnCanceled);
                        }

                        closeWindowTask = downloadTask.ContinueWith(t => progressWindow.Dispatcher.Invoke(progressWindow.Close));
                        progressWindow.ShowDialog();

                        if (zippedMod != null)
                        {
                            FileInfo newModFile = await(Task <FileInfo>) downloadTask;
                            if (zippedMod.FactorioVersion == newestRelease.FactorioVersion)
                            {
                                zippedMod.Update(newModFile, newestRelease.Version);
                            }
                            else
                            {
                                var newMod = new ZippedMod(zippedMod.Name, newestRelease.Version, newestRelease.FactorioVersion, newModFile,
                                                           InstalledMods, MainViewModel.Instance.Modpacks);
                                InstalledMods.Add(newMod);
                                foreach (var modpack in MainViewModel.Instance.Modpacks)
                                {
                                    ModReference reference;
                                    if (modpack.Contains(zippedMod, out reference))
                                    {
                                        modpack.Mods.Remove(reference);
                                        modpack.Mods.Add(new ModReference(newMod, modpack));
                                    }
                                }
                                zippedMod.File.Delete();
                                InstalledMods.Remove(extractedMod);
                            }
                        }
                        if (extractedMod != null)
                        {
                            DirectoryInfo newModDirectory = await(Task <DirectoryInfo>) downloadTask;
                            if (extractedMod.FactorioVersion == newestRelease.FactorioVersion)
                            {
                                extractedMod.Update(newModDirectory, newestRelease.Version);
                            }
                            else
                            {
                                var newMod = new ExtractedMod(extractedMod.Name, newestRelease.Version, newestRelease.FactorioVersion, newModDirectory,
                                                              InstalledMods, MainViewModel.Instance.Modpacks);
                                InstalledMods.Add(newMod);
                                foreach (var modpack in MainViewModel.Instance.Modpacks)
                                {
                                    ModReference reference;
                                    if (modpack.Contains(extractedMod, out reference))
                                    {
                                        modpack.Mods.Remove(reference);
                                        modpack.Mods.Add(new ModReference(newMod, modpack));
                                    }
                                }
                                extractedMod.Directory.Delete(true);
                                InstalledMods.Remove(extractedMod);

                                ModpackTemplateList.Instance.Update(MainViewModel.Instance.Modpacks);
                                ModpackTemplateList.Instance.Save();
                            }
                        }
                    }
                    finally
                    {
                        if (closeWindowTask != null)
                        {
                            await closeWindowTask;
                        }
                    }
                }
                catch (HttpRequestException)
                {
                    MessageBox.Show(Window,
                                    App.Instance.GetLocalizedMessage("InternetConnection", MessageType.Error),
                                    App.Instance.GetLocalizedMessageTitle("InternetConnection", MessageType.Error),
                                    MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                SelectedRelease = null;
                foreach (var release in SelectedReleases)
                {
                    release.IsInstalled        = InstalledMods.Contains(selectedMod.Name, release.Version);
                    release.IsVersionInstalled = !release.IsInstalled && InstalledMods.ContainsByFactorioVersion(selectedMod.Name, release.FactorioVersion);
                }
            }
        }
Beispiel #14
0
        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);
        }