Example #1
0
        private async void StartUpdate()
        {
            ResetProgressBars();

            string currentDirectory = Directory.GetCurrentDirectory();
            string installDirectory = Path.GetDirectoryName(currentDirectory);
            string tempDirectory    = string.Format("{0}\\{1}\\", currentDirectory, TempDirectoryName);
            string latestVersion    = LatestVersion;

            try {
                _log.Info("Preparing temp directory...");
                if (Directory.Exists(tempDirectory))
                {
                    Directory.Delete(tempDirectory, true);
                }
                Directory.CreateDirectory(tempDirectory);

                _log.Info("Sending download request...");
                string fileName = GetFullFileName(
                    MainConfig["FileName"],
                    latestVersion,
                    CleanUpdateCheckBox.IsChecked.GetValueOrDefault()
                    );
                string uri = _githubHelper.GetDownloadUri(latestVersion, fileName);

                _cancelTokenSource = new CancellationTokenSource();
                SetCurrentStage(OperationStage.Cancel);

                string file = await _httpClient.DownloadFileAsync(
                    uri,
                    tempDirectory,
                    _cancelTokenSource.Token,
                    OnDownloadProgressCallback
                    );

                ToggleMainButton(false);

                _log.Info("Preparing files for installing...");
                App.ObsoleteSelf();

                _log.Info("Installing update files...");
                await FileUtilities.ExtractToDirectoryAsync(file, installDirectory, OnInstallProgressCallback, true);

                App.PruneSelf();

                _log.Info("Update installed successfully.");
                SetCurrentStage(OperationStage.Done);
                CurrentVersion = latestVersion;
                CurrentVersionTextBox.Background = new SolidColorBrush(UpdatedVersionColor);
            }
            catch (Exception e) {
                bool handled = false;
                if (e is TaskCanceledException)
                {
                    _log.Info("Update canceled.");
                    handled = true;
                }
                else if (e is HttpRequestException)
                {
                    if (e.Message.Contains("404"))
                    {
                        if (CleanUpdateCheckBox.IsChecked.GetValueOrDefault())
                        {
                            _log.Error("Update file not found, please contact the developer!");
                        }
                        else
                        {
                            _log.Error("Patch file not found, you should try again but with \"Clean update\" enabled.");
                        }
                        handled = true;
                    }
                }

                if (!handled)
                {
                    _log.Error("Failed to update: " + e.Message);
                }

                ResetProgressBars();
                SetCurrentStage(OperationStage.Update);
            }
            finally {
                _log.Info("Cleaning up temp files...");
                if (Directory.Exists(tempDirectory))
                {
                    Directory.Delete(tempDirectory, true);
                }
            }

            if (CurrentStage == OperationStage.Update)
            {
                return;
            }

            try {
                _log.Info("Updating config...");
                // Reload the ini file in case there was an update to the ini itself.
                ConfigFile = await IniFile.LoadAsync(App.ExecutableName + ".ini");

                MainConfig = ConfigFile[App.ExecutableName];
                string currentVersion = MainConfig["CurrentVersion"];
                if (string.Compare(currentVersion, latestVersion, true) != 0)
                {
                    MainConfig["CurrentVersion"] = latestVersion;

                    ConfigFile.Save();
                }
                _log.Info("Config updated.");
            }
            catch (Exception e) {
                _log.Error("Failed to update config even though update was sucessful: " + e.Message);
            }

            _log.Info("All done, you are now on the latest version.");
        }