예제 #1
0
 public static void CloseSplashScreen()
 {
     try
     {
         SplashScreen.Close();
         SplashScreen          = null;
         SplashScreenViewModel = null;
     }
     catch (NullReferenceException ex)
     {
         Log.Debug(ex, "splashScreen was null.");
     }
 }
예제 #2
0
            public static async Task <bool> CheckForUpdates()
            {
#if DEBUG
                File.WriteAllText("../../../Installer/latest.json",
                                  JsonSerializer.Serialize(SerializableVersion.GetAppVersion())
                                  );
#endif
                if (Config.AppWasUpdated)
                {
                    Log.Information("Application was updated last time it ran, cleaning up.");
                    if (File.Exists("./PingLogger-old.exe"))
                    {
                        File.Delete("./PingLogger-old.exe");
                    }
                    if (Config.LastTempDir != string.Empty && Directory.Exists(Config.LastTempDir))
                    {
                        File.Delete(Config.LastTempDir + "/PingLogger.Setup.exe");
                        Directory.Delete(Config.LastTempDir);
                        Config.LastTempDir = string.Empty;
                    }
                    Config.AppWasUpdated = false;
                }
                else
                {
                    if (Config.UpdateLastChecked.Date >= DateTime.Today)
                    {
                        Log.Information("Application already checked for update today, skipping.");
                        return(true);
                    }
                    SplashScreenViewModel = new ViewModels.SplashScreenViewModel();
                    SplashScreen          = new Views.SplashScreen()
                    {
                        DataContext = SplashScreenViewModel
                    };
                    SplashScreen.Show();
                    SplashScreenViewModel.ProgressBarIndeterminate = true;
                    var localVersion = Assembly.GetExecutingAssembly().GetName().Version;

                    try
                    {
                        var  httpClient       = new WebClient();
                        bool downloadComplete = false;
                        httpClient.DownloadFileCompleted += (_, _) => { downloadComplete = true; };

                        string serverUrl = "https://pinglogger.lexdysia.com/";

                        await httpClient.DownloadFileTaskAsync($"{serverUrl}/latest.json", "./latest.json");

                        while (!downloadComplete)
                        {
                            await Task.Delay(100);
                        }
                        var latestJson = await File.ReadAllTextAsync("./latest.json");

                        var remoteVersion = JsonSerializer.Deserialize <SerializableVersion>(latestJson);
                        File.Delete("./latest.json");

                        Log.Information($"Remote version is {remoteVersion}, currently running {localVersion}");
                        if (remoteVersion > localVersion)
                        {
                            Log.Information("Remote contains a newer version");
                            if (true)
                            {
                                if (Config.IsInstalled)
                                {
                                    Config.LastTempDir = $"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}\\Temp\\{RandomString(8)}";
                                    Directory.CreateDirectory(Config.LastTempDir);
                                    Log.Information($"Creating temporary path {Config.LastTempDir}");
                                    Log.Information($"Downloading newest installer to {Config.LastTempDir}\\PingLogger-Setup.msi");

                                    if (remoteVersion is not null)
                                    {
                                        var downloadURL = $"{serverUrl}/{remoteVersion.Major}{remoteVersion.Minor}{remoteVersion.Build}/win/install/PingLogger.Setup.exe";
                                        Log.Information($"Downloading from {downloadURL}");
                                        using var downloader = new HttpClientDownloadWithProgress(downloadURL, Config.LastTempDir + "\\PingLogger.Setup.exe");
                                        SplashScreenViewModel.UpdateMessage = $"Downloading PingLogger setup v{remoteVersion}";
                                        downloader.ProgressChanged         += Downloader_ProgressChanged;
                                        await downloader.StartDownload();
                                    }

                                    Config.AppWasUpdated = true;
                                    Log.Information("Uninstalling current version.");
                                    Process.Start(new ProcessStartInfo
                                    {
                                        FileName        = $"{Config.LastTempDir}/PingLogger.Setup.exe",
                                        UseShellExecute = true,
                                        Arguments       = "/SILENT /CLOSEAPPLICATIONS"
                                    });

                                    Log.Information("Installer started, closing.");
                                    Environment.Exit(0);
                                }
                                else
                                {
                                    Log.Information("Renamed PingLogger.exe to PingLogger-old.exe");
                                    File.Move("./PingLogger.exe", "./PingLogger-old.exe");
                                    Log.Information("Downloading new PingLogger.exe");

                                    if (remoteVersion is not null)
                                    {
                                        string[] fileList = { "libHarfBuzzSharp.dll", "libSkiaSharp.dll", "PingLogger.exe" };
                                        SplashScreenViewModel.UpdateMessage = $"Downloading PingLogger v{remoteVersion}";
                                        foreach (var file in fileList)
                                        {
                                            var downloadUrl = $"{serverUrl}{remoteVersion.Major}{remoteVersion.Minor}{remoteVersion.Build}/win/sf/{file}";
                                            Log.Information($"Downloading from {downloadUrl}");
                                            using var downloader        = new HttpClientDownloadWithProgress(downloadUrl, $"./{file}");
                                            downloader.ProgressChanged += Downloader_ProgressChanged;
                                            await downloader.StartDownload();
                                        }
                                    }

                                    Config.AppWasUpdated = true;

                                    Process.Start(new ProcessStartInfo
                                    {
                                        FileName = "./PingLogger.exe"
                                    });

                                    Log.Information("Starting new version of PingLogger");
                                    Environment.Exit(0);
                                }
                            }
                        }
                    }
                    catch (HttpRequestException ex)
                    {
                        Log.Error("Unable to auto update: " + ex.Message);
                        return(true);
                    }
                }
                Config.UpdateLastChecked = DateTime.Now;
                CloseSplashScreen();
                return(true);
            }