Exemple #1
0
        private void LauncherUpdate(object sender, DoWorkEventArgs e)
        {
            SetProgressBar(0);
            BackgroundWorker worker = (BackgroundWorker)sender;

            Version currentVersion = Versioning.LauncherVersion;
            Version latestVersion;

            SetTextStatus(App.resourceManager.GetString("lc_checking_new_version"));

            if (worker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
            try
            {
                String versionURI = $"{Settings.LauncherPatchesURL}{Settings.LauncherVersionFN}";
                using (WebClientAuth wc = new WebClientAuth())
                {
                    latestVersion = Version.Parse(wc.DownloadString(versionURI));
                }
            }
            catch (FormatException ex)
            {
                e.Cancel = true;
                MBError(String.Format("Server has invalid response for url `{0}` . {1}", $"{Settings.LauncherPatchesURL}{Settings.LauncherVersionFN}", ex.Message));
                return;
            }
            catch (WebException ex)
            {
                if (ex.Message.Contains("timed out"))
                {
                    MBError("Unable to retrieve latest version, request timed out.");
                }
                HttpWebResponse response = (HttpWebResponse)ex.Response;
                if (response == null)
                {
                    MBError("Unable to retrieve latest version, no response.");
                }
                else
                {
                    MBError(String.Format(
                                "{0} \nResponse: {1} {2}",
                                "Unable to retrieve latest version.",
                                (int)response.StatusCode,
                                response.StatusDescription
                                ));
                }

                e.Cancel = true;
                return;
            }
            catch (KeyNotFoundException ex)
            {
                e.Cancel = true;
                MBError(ex.Message);
                MBError(ex.StackTrace);
                return;
            }


            if (worker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
            int versionDifference = currentVersion.CompareTo(latestVersion);

            if (versionDifference.Equals(-1))
            {
                SetTextStatus(App.resourceManager.GetString("start_downloading"));
                WebClientAuth webClient = new WebClientAuth();
                webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(LauncherWebClient_DownloadProgressChanged);
                webClient.DownloadFileCompleted   += new AsyncCompletedEventHandler(LauncherWebClient_DownloadFileCompleted);
                string tempLauncherFilename = Path.GetTempFileName();
                Debug.WriteLine(tempLauncherFilename);
                string launcher_filename = "AyalaLauncher.exe";
                string launcher_uri      = Settings.LauncherPatchesURL + launcher_filename;

                launcherDownloadEvent.Reset();
                try
                {
                    webClient.DownloadFileAsync(new Uri(launcher_uri), tempLauncherFilename, launcher_filename);
                }
                catch (Exception ex)
                {
                    e.Cancel = true;

                    MBError(ex.Message + "\n" + ex.StackTrace);
                    return;
                }
                launcherDownloadEvent.WaitOne();

                if (launcherDownloadingException != null)
                {
                    WebException    webex    = (WebException)launcherDownloadingException;
                    HttpWebResponse response = (HttpWebResponse)webex.Response;
                    e.Cancel = true;
                    MBError(String.Format(
                                "{0}\nMessage: {1}\nRequest: {2}\nResponse: {3}",
                                App.resourceManager.GetString("cant_get_patch"),
                                webex.Message,
                                (response != null ? response.ResponseUri.ToString() : "null"),
                                (response != null ? response.StatusDescription : "null")
                                ));
                    return;
                }

                try
                {
                    LauncherUpdater.RenameRunningLauncher();
                    LauncherUpdater.CopyNewLauncher(tempLauncherFilename);
                }
                catch (Exception ex)
                {
                    MBError(String.Format(App.resourceManager.GetString("unknown_exception"), ex.Message, ex.StackTrace));
                    e.Cancel = true;
                    return;
                }
                finally
                {
                    LauncherUpdater.RestartToNewLauncher();
                }
                File.Delete(tempLauncherFilename);
            }
        }
Exemple #2
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);
            }
        }