Esempio n. 1
0
        private void GameUpdate(object sender, DoWorkEventArgs e)
        {
            SetProgressBar(0);
            SetTextStatus(App.resourceManager.GetString("checking_version"));
            BackgroundWorker worker = (BackgroundWorker)sender;

            int currentVersion = 0;
            int lastVersion    = 0;

            try
            {
                currentVersion = Versioning.CurrentGameVersion;
            }
            catch
            {
                e.Cancel = true;
                MBError(App.resourceManager.GetString("cant_get_current_version"));
                return;
            }

            SetTextStatus(App.resourceManager.GetString("checking_new_version"));
            if (worker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }

            try
            {
                String versionURI = $"{Settings.GamePatchesURL}{Settings.GameVersionFN}";
                using (WebClientAuth wc = new WebClientAuth())
                {
                    lastVersion = int.Parse(wc.DownloadString(versionURI));
                }
            } catch (FormatException)
            {
                e.Cancel = true;
                MBError(string.Format("For url `{0}` server has invalid response. Integer is expected.", $"{Settings.GamePatchesURL}{Settings.GameVersionFN}"));
                return;
            } catch (WebException ex)
            {
                e.Cancel = true;
                HttpWebResponse response = (HttpWebResponse)ex.Response;
                MBError($"{App.resourceManager.GetString("cant_get_new_version")} \nResponse: {(int)response.StatusCode} {response.StatusDescription}");
                return;
            } catch (KeyNotFoundException ex)
            {
                e.Cancel = true;
                MBError(ex.Message);
                MBError(ex.StackTrace);
                return;
            }


            if (worker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }

            for (int i = currentVersion; i < lastVersion; i++)
            {
                SetTextStatus(App.resourceManager.GetString("start_downloading"));
                WebClientAuth webClient = new WebClientAuth();
                webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(GameWebClient_DownloadProgressChanged);
                webClient.DownloadFileCompleted   += new AsyncCompletedEventHandler(GameWebClient_DownloadFileCompleted);
                string tempPatchFilename = Path.GetTempFileName();
                string patch_filename    = string.Format("{0}_{1}.patch", i, i + 1);
                string patch_uri         = Settings.GamePatchesURL + patch_filename;

                patchDownloadEvent.Reset();
                try
                {
                    webClient.DownloadFileAsync(new Uri(patch_uri), tempPatchFilename, patch_filename);
                }
                catch (UriFormatException)
                {
                    e.Cancel = true;
                    MBError("Configuration.patches_directory is incorrect. Nothing can be downloaded from `" + patch_uri + "`. Url expected.");
                    return;
                }
                patchDownloadEvent.WaitOne();

                if (patchDownloadingException != null)
                {
                    WebException    webex    = (WebException)patchDownloadingException;
                    HttpWebResponse response = (HttpWebResponse)webex.Response;
                    e.Cancel = true;
                    MBError(App.resourceManager.GetString("cant_get_patch") + "\nMessage: " + webex.Message + "\nRequest: " + (response != null ? response.ResponseUri.ToString() : "null") + "\nResponse: " + (response != null ? response.StatusDescription : "null"));
                    return;
                }

                try
                {
                    Updater.ApplyPatch(tempPatchFilename, new Action <string, int>(delegate(string text, int percent)
                    {
                        SetTextStatus(text);
                        SetProgressBar(percent);
                    }));
                }
                catch (PatcherException ex)
                {
                    MBError($"Error patching game:\n{ex.Message}", "Patcher error");
                    e.Cancel = true;
                    return;
                }
                catch (Exception ex)
                {
                    MBError(string.Format(App.resourceManager.GetString("unknown_exception"), ex.Message, ex.StackTrace));
                    e.Cancel = true;
                    return;
                }
                UpdateVersionAfterPatch(i + 1);
                File.Delete(tempPatchFilename);
            }
        }