protected override async Task InitializeAsync()
        {
            SafeAsync.Run(() => Task.Run(async() =>
            {
                IsLoading.Value = true;

                await Task.WhenAll(
                    SafeAsync.RunAsync(FetchNewsAsync),
                    SafeAsync.RunAsync(FetchLatestYouTubeAsync));

                IsLoading.Value = false;

                await CheckForUpdatesAsync();
            }), Tracer.Error);

            await base.InitializeAsync();
        }
        protected override async Task InitializeAsync()
        {
            IsVREnabled.Value = _settingsService.GetValue(SettingsCategories.LaunchOptions, SettingsKeys.IsVREnabled, false);

            SafeAsync.Run(() => Task.Run(async() =>
            {
                IsLoading.Value = true;

                await Task.WhenAll(
                    SafeAsync.RunAsync(FetchNewsAsync),
                    SafeAsync.RunAsync(FetchLatestYouTubeAsync));

                IsLoading.Value = false;

                await CheckForUpdatesAsync();
            }), Tracer.Error);

            await base.InitializeAsync();
        }
Esempio n. 3
0
        public Task <bool> CheckAsync()
        {
            return(Task.Run(async() =>
            {
                if (!await _semaphore.WaitAsync(1))
                {
                    return false;
                }

                try
                {
                    Tracer.Info("Checking for application updates.");

                    var extractionPath = Path.Combine(ApplicationPaths.ApplicationPath, "_update");
                    var downloadDirectory = Path.Combine(ApplicationPaths.ApplicationPath, "_downloads");
                    var versionPath = Path.Combine(downloadDirectory, "version.json");

                    if (Directory.Exists(extractionPath) && Directory.GetFileSystemEntries(extractionPath).Length > 0)
                    {
                        var c = File.ReadAllText(versionPath);
                        var v = JsonConvert.DeserializeObject <AutoUpdateVersionInfo>(c);

                        Tracer.Info($"Update {v.Version} is currently waiting to be installed.");
                        return true;
                    }

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

                    Directory.CreateDirectory(downloadDirectory);

                    Tracer.Info("Downloading version file.");

                    var file = await SafeAsync.RunAsync(
                        () => DownloadFileAsync("https://drive.google.com/open?id=1Njnp1Zy_Ed4hzQ5DVStTJG88BjvMLbpm", versionPath),
                        e => { Tracer.Error("An error occured while trying to download the version file.", e); });

                    if (!(file?.Exists ?? false))
                    {
                        Tracer.Warn("Unable to download version info.  File was not found.");
                        return false;
                    }

                    var contents = File.ReadAllText(file.FullName);
                    var versionInfo = JsonConvert.DeserializeObject <AutoUpdateVersionInfo>(contents);
                    var assembly = Assembly.GetExecutingAssembly();
                    var version = assembly.GetName().Version;

                    if (version >= versionInfo.ConcreteVersion)
                    {
                        Tracer.Info("Application is up to date.");
                        return false;
                    }

                    var zipPath = Path.Combine(downloadDirectory, "update.zip");

                    Tracer.Info("Downloading update.");

                    file = await SafeAsync.RunAsync(
                        () => DownloadFileAsync(versionInfo.Url, zipPath),
                        e => { Tracer.Error("An error occured while trying to download the the update.", e); });

                    if (!(file?.Exists ?? false))
                    {
                        Tracer.Warn("Unable to download update archive.");
                        return false;
                    }

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

                    Directory.CreateDirectory(extractionPath);

                    try
                    {
                        Tracer.Info("Extracting update.");

                        ExtractZipFile(zipPath, extractionPath);

                        try
                        {
                            Tracer.Info("Removing downloaded archive file.");

                            File.Delete(zipPath);

                            MoveFile(extractionPath, ApplicationPaths.ApplicationPath, "AutoUpdate.exe");
                            MoveFile(extractionPath, ApplicationPaths.ApplicationPath, "AutoUpdate.exe.config");
                            MoveFile(extractionPath, ApplicationPaths.ApplicationPath, "AutoUpdate.pdb");
                        }
                        catch (Exception e)
                        {
                            Tracer.Error("Unable to update AutoUpdate.exe", e);
                        }
                    }
                    catch (Exception e)
                    {
                        Tracer.Error(e);
                        Directory.Delete(extractionPath, true);
                        return false;
                    }

                    return true;
                }
                catch (Exception e)
                {
                    Tracer.Error(e);
                    return false;
                }
                finally
                {
                    _semaphore.Release(1);
                }
            }));
        }