コード例 #1
0
ファイル: UpdateService.cs プロジェクト: Canopus666/Anamnesis
        public override async Task Initialize()
        {
            await base.Initialize();

            string versionStr;

            if (!File.Exists(VersionFile))
            {
                throw new Exception("No version file found");
            }

            versionStr = File.ReadAllText(VersionFile);
            versionStr = versionStr.Split('\r')[0].Trim();

            versionStr = versionStr.Trim();
            Version    = DateTimeOffset.Parse(versionStr);

            if (Directory.Exists(UpdateTempDir))
            {
                Directory.Delete(UpdateTempDir, true);
            }

            if (!this.httpClient.DefaultRequestHeaders.Contains("User-Agent"))
            {
                this.httpClient.DefaultRequestHeaders.Add("User-Agent", "AutoUpdater");
            }

            try
            {
                string url    = $"https://api.github.com/repos/{Repository}/releases/latest";
                string result = await this.httpClient.GetStringAsync(url);

                this.currentRelease = JsonSerializer.Deserialize <Release>(result);

                if (this.currentRelease == null)
                {
                    throw new Exception("Failed to deserialize json response");
                }

                if (this.currentRelease.Published != null && this.currentRelease.Published > Version)
                {
                    await Dispatch.MainThread();

                    UpdateDialog dlg = new UpdateDialog();
                    dlg.Changes = this.currentRelease.Changes;
                    await ViewService.ShowDialog <UpdateDialog, bool?>("Update", dlg);
                }
            }
            catch (HttpRequestException ex)
            {
                // 404 errors just mean there are no latest releases.
                if (ex.StatusCode == HttpStatusCode.NotFound)
                {
                    return;
                }

                throw;
            }
            catch (Exception ex)
            {
                Log.Write(Severity.Error, new Exception("Unable to complete update check", ex));
                throw;
            }
        }
コード例 #2
0
        public override async Task Initialize()
        {
            await base.Initialize();

            if (!File.Exists(VersionFile))
            {
                throw new Exception("No version file found");
            }

            string[] parts = File.ReadAllText(VersionFile).Split(';');

            Version = DateTimeOffset.Parse(parts[0].Trim()).ToUniversalTime();
            SupportedGameVersion = parts[1].Trim();

            // Debug builds should not attempt to update
#if DEBUG
            Version = DateTimeOffset.UtcNow;
#endif

            DateTimeOffset lastCheck = SettingsService.Current.LastUpdateCheck;
            TimeSpan       elapsed   = DateTimeOffset.Now - lastCheck;
            if (elapsed.TotalHours < 6)
            {
                Log.Information("Last update check was less than 6 hours ago. Skipping.");
                return;
            }

            if (Directory.Exists(UpdateTempDir))
            {
                Directory.Delete(UpdateTempDir, true);
            }

            if (!this.httpClient.DefaultRequestHeaders.Contains("User-Agent"))
            {
                this.httpClient.DefaultRequestHeaders.Add("User-Agent", "AutoUpdater");
            }

            try
            {
                string url    = $"https://api.github.com/repos/{Repository}/releases/latest";
                string result = await this.httpClient.GetStringAsync(url);

                this.currentRelease = JsonSerializer.Deserialize <Release>(result);

                if (this.currentRelease == null)
                {
                    throw new Exception("Failed to deserialize json response");
                }

                if (this.currentRelease.Published == null)
                {
                    throw new Exception("No published timestamp in update json");
                }

                DateTimeOffset published = (DateTimeOffset)this.currentRelease.Published;
                published = published.ToUniversalTime();

                if (this.currentRelease.Published != null && published > Version)
                {
                    await Dispatch.MainThread();

                    UpdateDialog dlg = new UpdateDialog();
                    dlg.Changes = this.currentRelease.Changes;
                    await ViewService.ShowDialog <UpdateDialog, bool?>("Update", dlg);
                }

                SettingsService.Current.LastUpdateCheck = DateTimeOffset.Now;
                SettingsService.Save();
            }
            catch (HttpRequestException ex)
            {
                // 404 errors just mean there are no latest releases.
                if (ex.StatusCode == HttpStatusCode.NotFound)
                {
                    SettingsService.Current.LastUpdateCheck = DateTimeOffset.Now;
                    SettingsService.Save();
                    return;
                }

                throw;
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Failed to complete update check");
            }
        }
コード例 #3
0
        public async Task <bool> CheckForUpdates()
        {
            if (Directory.Exists(UpdateTempDir))
            {
                Directory.Delete(UpdateTempDir, true);
            }

            if (!this.httpClient.DefaultRequestHeaders.Contains("User-Agent"))
            {
                this.httpClient.DefaultRequestHeaders.Add("User-Agent", "AutoUpdater");
            }

            try
            {
                string url    = $"https://api.github.com/repos/{Repository}/releases/latest";
                string result = await this.httpClient.GetStringAsync(url);

                this.currentRelease = JsonSerializer.Deserialize <Release>(result);

                if (this.currentRelease == null)
                {
                    throw new Exception("Failed to deserialize json response");
                }

                if (this.currentRelease.Published == null)
                {
                    throw new Exception("No published timestamp in update json");
                }

                DateTimeOffset published = (DateTimeOffset)this.currentRelease.Published;
                published = published.ToUniversalTime();

                bool update = this.currentRelease.Published != null && published > Version;

                if (update)
                {
                    await Dispatch.MainThread();

                    UpdateDialog dlg = new UpdateDialog();
                    dlg.Changes = this.currentRelease.Changes;
                    await ViewService.ShowDialog <UpdateDialog, bool?>("Update", dlg);
                }

                SettingsService.Current.LastUpdateCheck = DateTimeOffset.Now;
                SettingsService.Save();
                return(update);
            }
            catch (HttpRequestException ex)
            {
                // 404 errors just mean there are no latest releases.
                if (ex.StatusCode == HttpStatusCode.NotFound)
                {
                    SettingsService.Current.LastUpdateCheck = DateTimeOffset.Now;
                    SettingsService.Save();
                    return(false);
                }

                throw;
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Failed to complete update check");
                return(false);
            }
        }