Пример #1
0
        private bool ShowUpdateWindow(List <UpdateTarget> targets, out UpdateTarget target)
        {
            var updateListWindow = new UpdateListWindow()
            {
                Owner = Window
            };
            var updateListViewModel = (UpdateListViewModel)updateListWindow.ViewModel;

            updateListViewModel.UpdateTargets = targets;
            bool?result = updateListWindow.ShowDialog();

            if (result.HasValue && result.Value)
            {
                target = updateListViewModel.SelectedTarget;
                return(target != null);
            }
            else
            {
                target = null;
                return(false);
            }
        }
Пример #2
0
        private async Task UpdateSelectedVersion()
        {
            string token;

            if (GlobalCredentials.Instance.LogIn(Window, out token))
            {
                UpdateInfo        updateInfo  = UpdateWebsite.GetUpdateInfo(GlobalCredentials.Instance.Username, token);
                List <UpdateStep> updateSteps = updateInfo.Package.Where(step => step.From >= SelectedVersion.Version).ToList();

                if (updateSteps.Count > 0)
                {
                    List <UpdateTarget> targets = FactorioUpdater.GetUpdateTargets(SelectedVersion, FactorioVersions, updateSteps);

                    var updateListWindow = new UpdateListWindow()
                    {
                        Owner = Window
                    };
                    var updateListViewModel = (UpdateListViewModel)updateListWindow.ViewModel;
                    updateListViewModel.UpdateTargets = targets;
                    bool?result = updateListWindow.ShowDialog();
                    if (result.HasValue && result.Value)
                    {
                        UpdateTarget target = updateListViewModel.SelectedTarget;

                        var progressWindow = new ProgressWindow {
                            Owner = Window
                        };
                        var progressViewModel = (ProgressViewModel)progressWindow.ViewModel;
                        progressViewModel.ActionName = App.Instance.GetLocalizedResourceString("UpdatingFactorioAction");

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

                        var progress      = new Progress <double>(value => progressViewModel.Progress = value);
                        var stageProgress = new Progress <UpdaterStageInfo>(value =>
                        {
                            progressViewModel.CanCancel           = value.CanCancel;
                            progressViewModel.ProgressDescription = value.Description;
                        });

                        try
                        {
                            Task closeWindowTask = null;
                            try
                            {
                                Task updateTask = FactorioUpdater.ApplyUpdateAsync(SelectedVersion,
                                                                                   GlobalCredentials.Instance.Username, token, target,
                                                                                   progress, stageProgress, cancellationSource.Token);

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

                                await updateTask;
                            }
                            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);
                        }
                        catch (CriticalUpdaterException)
                        {
                            MessageBox.Show(Window,
                                            App.Instance.GetLocalizedMessage("FactorioUpdaterCritical", MessageType.Error),
                                            App.Instance.GetLocalizedMessageTitle("FactorioUpdaterCritical", MessageType.Error),
                                            MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }
                else
                {
                    MessageBox.Show(Window,
                                    App.Instance.GetLocalizedMessage("NoFactorioUpdate", MessageType.Information),
                                    App.Instance.GetLocalizedMessageTitle("NoFactorioUpdate", MessageType.Information),
                                    MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
        }