示例#1
0
        /// <summary>
        /// Checks the version
        /// </summary>
        /// <exception cref="System.NotImplementedException"></exception>
        public async Task CheckVersions(Type targetSubscriber = null)
        {
            // If 3 mins passed, update info
            if (targetSubscriber == typeof(UpdateMenuViewModel) && Properties.Settings.Default.LastVersionCheck.AddSeconds(30) <= DateTime.Now ||
                Properties.Settings.Default.LastVersionCheck.AddMinutes(3) <= DateTime.Now)
            {
                // Initialize an HttpWebRequest for the current URL.
                var webReq = (HttpWebRequest)WebRequest.Create(ConstantsBase.ApiUrl + "Version");
                webReq.Timeout = 1000;
                webReq.Headers.Add("Client-Version", _currentVersion.ToString());

                // Send request
                using (WebResponse response = await webReq.GetResponseAsync())
                {
                    // Get response
                    var respStream = response.GetResponseStream();

                    // The downloaded resource ends up in the variable named content.
                    var content = new MemoryStream();

                    // Read response if stream is not null
                    if (respStream != null)
                    {
                        await respStream.CopyToAsync(content);
                    }

                    // Deserialize string
                    string str = Encoding.Default.GetString(content.ToArray());
                    VersionList.Clear();
                    VersionList = JsonConvert.DeserializeObject <List <VersionModel> >(str);

                    // Update AppUpdater hash
                    this._appUpdaterHash = response.Headers["App-Checksum"];

                    content.Dispose();
                }

                // Update last check time
                Properties.Settings.Default.LastVersionCheck = DateTime.Now;
                Properties.Settings.Default.Save();
            }

            if (VersionList == null || !VersionList.Any())
            {
                return;
            }

            // Get latest version
            LatestVersion = VersionList.MaxBy(x => x.VersionNumber);

            if (CheckVersionsCompleted != null)
            {
                bool update = LatestVersion.VersionNumber > _currentVersion;
                IsUpdateAvailable = update;
                CheckVersionsCompleted(this, new CheckVersionEventArgs(update, targetSubscriber));
            }
        }
示例#2
0
        /// <summary>
        /// Downloads the version package.
        /// </summary>
        /// <exception cref="System.NotImplementedException"></exception>
        public async Task DownloadLatestVersion()
        {
            if (!VersionList.Any())
            {
                await CheckVersions();
            }

            // Get latest version
            LatestVersion = VersionList.MaxBy(x => x.VersionNumber);

            if (LatestVersion == null)
            {
                throw new InvalidOperationException(
                          "A version wasn't found after successfully reaching the server, this should not happen!");
            }

            // Check if package already is downloaded
            if (File.Exists(ConstantsBase.UpdatePath) &&
                String.Equals(CrcHelper.GetCrc32HashToString(ConstantsBase.UpdatePath), LatestVersion.CrcHash,
                              StringComparison.CurrentCultureIgnoreCase))
            {
                // Used to raise DownloadCompleted
                _webClient.CancelAsync();
                return;
            }

            // Iterate URLs
            foreach (var url in LatestVersion.DownloadUrls)
            {
                Directory.CreateDirectory("tmp");

                // Delete file if existent
                if (File.Exists(ConstantsBase.UpdatePath))
                {
                    File.Delete(ConstantsBase.UpdatePath);
                }

                // Download file
                await _webClient.DownloadFileTaskAsync(new Uri(url), ConstantsBase.UpdatePath);

                if (CrcHelper.GetCrc32HashToString(ConstantsBase.UpdatePath) != LatestVersion.CrcHash)
                {
                    continue;
                }
            }

            if (!String.Equals(CrcHelper.GetCrc32HashToString(ConstantsBase.UpdatePath), LatestVersion.CrcHash, StringComparison.CurrentCultureIgnoreCase))
            {
                throw new IOException("Could not successfully download the update package! Data corruption occurred! ");
            }
        }