示例#1
0
        async Task CheckForUpdatesAsync()
        {
            try
            {
                #region check version
                var latestVersion = await VersionProvider.GetLatestVersionAsync();

                if (latestVersion == PendingUpdate)
                {
                    return; // already loaded and ready to install
                }
                var currentVersion = ProductProvider.GetInstalledVersion();
                if (currentVersion >= latestVersion)
                {
                    return; // already up to date or newer
                }
                #endregion

                Log.Warning($"Newer version {latestVersion} found, current version {currentVersion}");

                #region load binaries
                if (!File.Exists(InstallerPath) ||
                    !ProductProvider.VerifyPackage(InstallerPath) ||
                    !ProductProvider.VerifyPackageVersion(InstallerPath, latestVersion))
                {
                    Log.Debug("Load binaries");

                    if (!Directory.Exists(WorkingDirectory))
                    {
                        Directory.CreateDirectory(WorkingDirectory);
                    }

                    using (var binariesStream = await BinariesProvider.GetLatestBinariesAsync())
                        using (var fileStream = File.Open(InstallerPath, FileMode.Create))
                        {
                            await binariesStream.CopyToAsync(fileStream);
                        }
                }
                #endregion

                Log.Debug($"Binaries loaded");

                #region verify binaries
                if (!ProductProvider.VerifyPackage(InstallerPath))
                {
                    Log.Warning($"Loaded binaries are untrusted");
                    return;
                }
                if (!ProductProvider.VerifyPackageVersion(InstallerPath, latestVersion))
                {
                    Log.Warning($"Loaded binaries are not the latest version");
                    return;
                }
                #endregion

                Log.Debug("Binaries verified");

                PendingUpdate = latestVersion;
                UpdatesReady?.Invoke(this, new ReadyEventArgs(latestVersion, InstallerPath));
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Failed to check updates");
            }
        }