From 2b003ef7f8f6d78d47c233ab5bae610e212ddc60 Mon Sep 17 00:00:00 2001
From: BubbaGumpShrump <joshua.p.farrell@gmail.com>
Date: Sun, 8 Dec 2024 12:48:43 -0500
Subject: [PATCH] Update fixes

---
 AutoTrackR2/UpdatePage.xaml.cs             | 62 ++++++++++------------
 AutoTrackR2_Setup/AutoTrackR2_Setup.vdproj |  4 +-
 2 files changed, 29 insertions(+), 37 deletions(-)

diff --git a/AutoTrackR2/UpdatePage.xaml.cs b/AutoTrackR2/UpdatePage.xaml.cs
index 5620c1c..c4c5483 100644
--- a/AutoTrackR2/UpdatePage.xaml.cs
+++ b/AutoTrackR2/UpdatePage.xaml.cs
@@ -96,9 +96,17 @@ namespace AutoTrackR2
                 InstallButton.IsEnabled = false;
                 InstallButton.Content = "Installing...";
 
-                // Download and install the latest version
-                string downloadUrl = await GetDownloadUrlFromGitHub();
-                await DownloadAndInstallUpdate(downloadUrl);
+                // Define the download URL
+                string downloadUrl = "https://github.com/BubbaGumpShrump/AutoTrackR2/releases/download/v2.0-beta.1/AutoTrackR2_Setup.msi";
+
+                // Download the installer before closing the app
+                string installerPath = await DownloadInstaller(downloadUrl);
+
+                // Launch the installer after the download completes
+                RunInstaller(installerPath);
+
+                // Gracefully close the app after starting the installer
+                Application.Current.Shutdown();
 
                 MessageBox.Show("Update installed successfully. Please restart the application.", "Update Installed", MessageBoxButton.OK, MessageBoxImage.Information);
             }
@@ -113,27 +121,7 @@ namespace AutoTrackR2
             }
         }
 
-        private async Task<string> GetDownloadUrlFromGitHub()
-        {
-            // Use the exact URL since we know it
-            return "https://github.com/BubbaGumpShrump/AutoTrackR2/releases/download/v2.0-beta.1/AutoTrackR2_Setup.msi";
-        }
-        private bool IsFileLocked(string filePath)
-        {
-            try
-            {
-                using (var fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite, System.IO.FileShare.None))
-                {
-                    return false; // File is not locked
-                }
-            }
-            catch (IOException)
-            {
-                return true; // File is locked
-            }
-        }
-
-        private async Task DownloadAndInstallUpdate(string url)
+        private async Task<string> DownloadInstaller(string url)
         {
             string tempFilePath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "AutoTrackR2_Setup.msi");
 
@@ -144,20 +132,24 @@ namespace AutoTrackR2
             using var fs = new System.IO.FileStream(tempFilePath, System.IO.FileMode.Create);
             await response.Content.CopyToAsync(fs);
 
-            // Wait for the system to release the file
-            await Task.Delay(2000); // Wait for 2 seconds
-
-            // Check if the file is locked
-            if (IsFileLocked(tempFilePath))
-            {
-                MessageBox.Show("The installer file is locked by another process.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
-                return;
-            }
+            return tempFilePath;
+        }
 
+        private void RunInstaller(string installerPath)
+        {
             try
             {
-                // Launch the installer
-                System.Diagnostics.Process.Start(tempFilePath);
+                // Prepare the command to run the .msi installer using msiexec
+                var processStartInfo = new System.Diagnostics.ProcessStartInfo
+                {
+                    FileName = "msiexec",
+                    Arguments = $"/i \"{installerPath}\" /quiet /norestart",
+                    UseShellExecute = false, // Ensures that the process runs in the background
+                    CreateNoWindow = true    // Hides the command prompt window
+                };
+
+                // Start the process (this will run the installer)
+                System.Diagnostics.Process.Start(processStartInfo);
             }
             catch (Exception ex)
             {
diff --git a/AutoTrackR2_Setup/AutoTrackR2_Setup.vdproj b/AutoTrackR2_Setup/AutoTrackR2_Setup.vdproj
index 4f9270c..e134145 100644
--- a/AutoTrackR2_Setup/AutoTrackR2_Setup.vdproj
+++ b/AutoTrackR2_Setup/AutoTrackR2_Setup.vdproj
@@ -198,8 +198,8 @@
         {
         "Name" = "8:Microsoft Visual Studio"
         "ProductName" = "8:AutoTrackR2_Setup"
-        "ProductCode" = "8:{772D186F-E30D-4D2B-99F2-F246555558FA}"
-        "PackageCode" = "8:{5A6337B9-AA07-4C66-8796-160B93276023}"
+        "ProductCode" = "8:{108D4428-AB25-444D-90C2-E507EA3020D8}"
+        "PackageCode" = "8:{0EBFA2DB-5EEC-48A0-9212-E8C33A05CE53}"
         "UpgradeCode" = "8:{0B78A147-D0DE-4F72-8906-A62611787CA7}"
         "AspNetVersion" = "8:"
         "RestartWWWService" = "11:FALSE"