Beispiel #1
0
        public static void BeginUpdate()
        {
            var directDlUrl = LatestReply.GetDonwnloadLink();

            if (directDlUrl != null)
            {
                RunDirectDownloadUpdate(directDlUrl);
            }
            else
            {
                var dlPageUrl = LatestReply.GetDownloadPageLink();
                if (dlPageUrl != null)
                {
                    Forms.Tools.PremadeDialogs.StartProcessSafely(dlPageUrl.ToString());
                }
            }
        }
Beispiel #2
0
        private static void RunDirectDownloadUpdate(Uri directDlUrl)
        {
            var dir   = new DirectoryInfo(ExtractedUpdatePath);
            var error = LoadingDialog.ShowDialog(null, Localisation.UpdateSystem_DownloadingTitle, iface =>
            {
                try
                {
                    using (var wc = new WebClient())
                    {
                        iface.SetMaximum(100);
                        wc.DownloadFileAsync(directDlUrl, DownloadFilename);

                        wc.DownloadProgressChanged +=
                            (sender, args) => { iface.SetProgress(args.ProgressPercentage); };

                        while (wc.IsBusy)
                        {
                            Thread.Sleep(100);
                        }
                        //iface.SetProgress(-1);
                    }

                    var remoteHash = LatestReply.GetHash();
                    if (remoteHash.IsNotEmpty())
                    {
                        using (var hasher = MD5.Create())
                            using (var fileStream = File.OpenRead(DownloadFilename))
                            {
                                var localHash = hasher.ComputeHash(fileStream);
                                if (!localHash.ToHexString().Equals(remoteHash, StringComparison.OrdinalIgnoreCase))
                                {
                                    throw new CryptographicException(Localisation.UpdateSystem_FailedHash);
                                }
                            }
                    }

                    iface.SetTitle(Localisation.UpdateSystem_ExtractingTitle);

                    using (var zip = new ZipFile(DownloadFilename))
                    {
                        zip.ZipErrorAction = ZipErrorAction.Throw;

                        zip.ExtractProgress += (sender, args) =>
                        {
                            iface.SetMaximum(args.EntriesTotal);
                            iface.SetProgress(args.EntriesExtracted);
                        };

                        if (dir.Exists)
                        {
                            dir.Delete(true);
                        }
                        zip.ExtractAll(dir.FullName);

                        if (dir.GetFiles().Length != zip.Entries.Count(x => !x.FileName.ContainsAny(new[] { '\\', '/' })))
                        {
                            throw new IOException(Localisation.UpdateSystem_FailedExtractDetails);
                        }
                    }
                }
                catch
                {
                    try
                    {
                        dir.Delete(true);
                    }
                    catch
                    {
                        /* Ignore errors to avoid losing the original exception */
                    }

                    throw;
                }
                finally
                {
                    // Delete the update archive, it is no longer necessary
                    try
                    {
                        File.Delete(DownloadFilename);
                    }
                    catch
                    {
                        /* Ignore errors to avoid losing the original exception */
                    }
                }
            });

            try
            {
                if (error != null)
                {
                    throw error;
                }

                // update success, launch the helper
                if (!File.Exists(UpdateHelperPath))
                {
                    throw new IOException(Localisation.Error_UpdateHelperMissing);
                }

                // Use a magic keyword to bypass the default user-frienly message
                Process.Start(UpdateHelperPath, "ItsMagic");

                Application.Exit();
            }
            catch (Exception ex)
            {
                CustomMessageBox.ShowDialog(null,
                                            new CmbBasicSettings(Localisation.UpdateSystem_FailedTitle, ex.Message,
                                                                 Localisation.UpdateSystem_Failed_Details, SystemIcons.Error, "OK")
                {
                    StartPosition = FormStartPosition.CenterScreen
                });
            }
        }