示例#1
0
        public void CheckVersion()
        {
            WebClient wc = new WebClient();

            wc.Proxy                    = null;
            wc.OpenReadCompleted       += (sender, args) => Connected(this, Empty);
            wc.DownloadProgressChanged += (sender, args) => DownloadProgressChanged(this, args);
            wc.DownloadStringCompleted += (sender, args) => {
                if (args.Cancelled || args.Error != null)
                {
                    UpdateCheckFailed?.Invoke(this, Empty);
                }
                else
                {
                    try {
                        XmlDocument xd = new XmlDocument();
                        xd.LoadXml(args.Result);
                        var    versionNode  = xd["version"];
                        var    version      = Version.Parse(versionNode["version_string"].InnerText);
                        var    releaseDate  = DateTime.ParseExact(versionNode["release_date"].InnerText.Trim(), "yyyy'-'M'-'d", null);
                        string releaseNotes = versionNode["release_notes"].InnerText;
                        string url          = versionNode["url"].InnerText;

                        var myVersion = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);
                        if (version > Version.Parse(myVersion.FileVersion))
                        {
                            UpdateAvailable?.Invoke(this, new UpdateAvailableArgs {
                                DownloadUrl  = url,
                                ReleaseDate  = releaseDate,
                                ReleaseNotes = releaseNotes,
                                Version      = version,
                            });
                        }
                        else
                        {
                            AlreadyLatest?.Invoke(this, Empty);
                        }
                    }
                    catch {
                        UpdateCheckFailed?.Invoke(this, Empty);
                    }
                }
            };
            // trigger the download..
            wc.DownloadStringAsync(new Uri(UpdateCheckHost + UpdateCheckPage));
        }
        private void CheckForUpdatesInternal()
        {
            UpdaterLogger.Log("Checking for updates.");

            updateMirrors = updateMirrors.OrderBy(um => um.Rating).ToList();

            using (WebClient webClient = CreateWebClient())
            {
                webClient.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
                webClient.Encoding    = Encoding.GetEncoding(1252);

                CreateTemporaryDirectory();

                int i = 0;

                UpdateMirror updateMirror;

                string downloadDirectory = localBuildInfo.BuildPath + TEMPORARY_UPDATER_DIRECTORY + Path.DirectorySeparatorChar;

                while (i < updateMirrors.Count)
                {
                    updateMirror = updateMirrors[i];
                    UpdaterLogger.Log("Attempting to download version information from " +
                                      updateMirror.UIName + " (" + updateMirror.URL + ")");

                    try
                    {
                        webClient.DownloadFile(updateMirror.URL + REMOTE_BUILD_INFO_FILE, downloadDirectory + REMOTE_BUILD_INFO_FILE);

                        UpdaterLogger.Log("Version information downloaded, proceeding to parsing it.");

                        remoteBuildInfo = new RemoteBuildInfo();
                        remoteBuildInfo.Parse(downloadDirectory + REMOTE_BUILD_INFO_FILE);

                        lastUpdateMirrorId = i;

                        lock (locker)
                        {
                            updateCheckInProgress = false;
                        }

                        if (remoteBuildInfo.ProductVersionInfo.VersionNumber == localBuildInfo.ProductVersionInfo.VersionNumber)
                        {
                            BuildState = BuildState.UPTODATE;
                            BuildUpToDate?.Invoke(this, EventArgs.Empty);
                            return;
                        }
                        else
                        {
                            BuildState = BuildState.OUTDATED;
                            BuildOutdated?.Invoke(this, new BuildOutdatedEventArgs(
                                                      remoteBuildInfo.ProductVersionInfo.DisplayString, GetUpdateSize()));
                            return;
                        }
                    }
                    catch (WebException ex)
                    {
                        UpdaterLogger.Log("WebException when downloading version information: " + ex.Message);
                        i++;
                    }
                    catch (ParseException ex)
                    {
                        UpdaterLogger.Log("ParseException when parsing version information: " + ex.Message);
                        i++;
                    }
                    catch (FormatException ex)
                    {
                        UpdaterLogger.Log("FormatException when parsing version information: " + ex.Message);
                        i++;
                    }
                }
            }

            UpdaterLogger.Log("Failed to download version information from all update mirrors. Aborting.");

            lock (locker)
            {
                updateCheckInProgress = false;
            }

            UpdateCheckFailed?.Invoke(this, EventArgs.Empty);
        }
示例#3
0
        private void CheckForUpdateAsync(object state)
        {
            try
            {
                if (!_manualCheck & (_updateCheckedAt.AddMilliseconds(_updateCheckInterval) > DateTime.UtcNow))
                {
                    return;
                }

                using (WebClientEx wC = new WebClientEx())
                {
                    wC.Proxy   = _proxy;
                    wC.Timeout = 30000;

                    byte[] response = wC.DownloadData(_updateCheckUri);

                    using (BinaryReader bR = new BinaryReader(new MemoryStream(response, false)))
                    {
                        if (Encoding.ASCII.GetString(bR.ReadBytes(2)) != "MU") //format
                        {
                            throw new InvalidDataException("Mesh update info format is invalid.");
                        }

                        switch (bR.ReadByte()) //version
                        {
                        case 1:
                            _updateVersion = bR.ReadShortString();
                            _displayText   = bR.ReadShortString();
                            _downloadLink  = bR.ReadShortString();
                            break;

                        default:
                            throw new InvalidDataException("Mesh update info version not supported.");
                        }

                        if (IsUpdateAvailable(_currentVersion, _updateVersion))
                        {
                            _syncCxt.Send(delegate(object state2)
                            {
                                UpdateAvailable?.Invoke(this, EventArgs.Empty);
                            }, null);
                        }
                        else if (_manualCheck)
                        {
                            _syncCxt.Send(delegate(object state2)
                            {
                                NoUpdateAvailable?.Invoke(this, EventArgs.Empty);
                            }, null);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (_manualCheck)
                {
                    _syncCxt.Send(delegate(object state2)
                    {
                        UpdateCheckFailed?.Invoke(this, ex);
                    }, null);
                }
            }
            finally
            {
                _manualCheck     = false;
                _updateCheckedAt = DateTime.UtcNow;
            }
        }