Files
AiQ_GUI/GUIUpdate.cs
2025-09-02 15:32:24 +01:00

115 lines
4.9 KiB
C#

using System.Diagnostics;
using System.Reflection;
using File = System.IO.File;
namespace AiQ_GUI
{
internal class GUIUpdate
{
public static string GUIVerShort = "";
public static string FindGUIVersion()
{
string GUIVersion = Convert.ToString(Assembly.GetExecutingAssembly().GetName().Version);
int dotLocation = GUIVersion.IndexOf('.'); // Find the first dot location
// If there is no dot in the version string, return the version as is
if (dotLocation < 0)
{
return GUIVersion;
}
// Check if the next character after the dot is "0"
if (dotLocation + 1 < GUIVersion.Length && GUIVersion[dotLocation + 1] == '0')
{
return GUIVersion.Substring(0, dotLocation); // If it's "0", remove everything after the first dot
}
else
{
// Otherwise, trim the version to the second dot (if it exists)
int secondDotLocation = GUIVersion.IndexOf('.', dotLocation + 1);
if (secondDotLocation >= 0)
{
return GUIVersion.Substring(0, secondDotLocation);
}
else
{
// If there's no second dot, return the version up to the first dot
return GUIVersion.Substring(0, dotLocation);
}
}
}
// Checks if the version in the model info spreadsheet is newer than the one installed and goes to update path if so and opens file
// Because it has the same application name Windows treats it as an update and installs it as the same program, therefore no need for acrhive folder
// It will install as 1 program as not to fill up the PC with lots of different apps
public static void UpdateGUI()
{
if (ComapreVersions()) // Checks if the current version is older than the latest version
{
string GUIPath = $"{GoogleAPI.DrivePath}AiQ\\GUI's\\AiQ_Final_Test\\AiQ_GUI.application";
// Check if path is real
if (!File.Exists(GUIPath))
{
MainForm.Instance.AddToActionsList("Error finding new app version in Google Drive.");
return;
}
// Brings up messagebox to ask user if they want to update
DialogResult result = MessageBox.Show(
$"Do you want to update to version {UniversalData.LatestVersion}?",
"Update Available",
MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly
);
if (result == DialogResult.Yes)
{
ProcessStartInfo psi = new()
{
FileName = GUIPath,
UseShellExecute = true // Lets the OS decide how to open it
};
Logging.LogMessage($"Updating to {UniversalData.LatestVersion} at {GUIPath}");
Process.Start(psi);
Application.Exit();
}
else
{
Logging.LogWarningMessage($"Refused Update to {UniversalData.LatestVersion} at {GUIPath}");
}
}
}
private static bool ComapreVersions()
{
// Handles missing dots and patch numbers
string[] currentParts = GUIVerShort.Split('.');
string[] latestParts = UniversalData.LatestVersion.Split('.');
int currentMajor = currentParts.Length > 0 && int.TryParse(currentParts[0], out int cm) ? cm : 0;
int latestMajor = latestParts.Length > 0 && int.TryParse(latestParts[0], out int lm) ? lm : 0;
int currentMinor = currentParts.Length > 1 && int.TryParse(currentParts[1], out int cmi) ? cmi : 0;
int latestMinor = latestParts.Length > 1 && int.TryParse(latestParts[1], out int lmi) ? lmi : 0;
int currentPatch = currentParts.Length > 2 && int.TryParse(currentParts[2], out int cp) ? cp : 0;
int latestPatch = latestParts.Length > 2 && int.TryParse(latestParts[2], out int lp) ? lp : 0;
if (latestMajor > currentMajor)
return true; // Newer major version
else if (latestMajor == currentMajor)
{
if (latestMinor > currentMinor)
return true; // Newer minor version
else if (latestMinor == currentMinor)
{
if (latestPatch > currentPatch)
return true; // Newer patch version
}
}
return false; // There is not a newer version
}
}
}