Skip to content

Commit

Permalink
Merge pull request #102 from gwdevhub/dev
Browse files Browse the repository at this point in the history
download framework dependent version if runtime is installed
  • Loading branch information
DubbleClick authored Sep 25, 2024
2 parents 0b5ae42 + fa1dd42 commit a702bbe
Showing 1 changed file with 68 additions and 11 deletions.
79 changes: 68 additions & 11 deletions GW Launcher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ static bool IsProcessOpen(string name)
if (clsProcess.ProcessName.Contains(name))
return true;
}

return false;
}

Expand All @@ -57,6 +58,7 @@ static bool WaitFor(Func<bool> test_func, uint timeout_ms = 10000)
elapsed += 200;
ok = test_func();
}

return ok;
}

Expand All @@ -67,10 +69,12 @@ static bool WaitFor(Func<bool> test_func, uint timeout_ms = 10000)
return "Failed to find account for " + account_name;
return LaunchAccount(found);
}

static Account? GetAccountByIndex(int account_index)
{
return accounts[account_index];
}

static string GetAccountName(int account_index)
{
var account = GetAccountByIndex(account_index);
Expand All @@ -94,11 +98,13 @@ static string GetAccountName(int account_index)
if (res != null)
return res;
}

if (memory == null)
return "Failed to launch account.";

uint timeout = 10000;
bool ok = WaitFor(() => {
bool ok = WaitFor(() =>
{
memory.process.Refresh();
return memory.process.MainWindowHandle != IntPtr.Zero;
}, timeout);
Expand All @@ -107,7 +113,9 @@ static string GetAccountName(int account_index)
memory.process.Kill();
return "Failed to wait for MainWindowHandle after " + (timeout / 1000) + " seconds.";
}
ok = WaitFor(() => {

ok = WaitFor(() =>
{
memory.process.Refresh();
return memory.process.WaitForInputIdle(1000);
}, timeout);
Expand All @@ -133,8 +141,10 @@ static string GetAccountName(int account_index)
{
SetWindowText(memory.process.MainWindowHandle, account.Name);
}

return null;
}

[STAThread]
internal static void Main()
{
Expand All @@ -146,6 +156,7 @@ internal static void Main()
Exit();
return; // Error message already displayed
}

if (!LoadAccountsJson())
{
Exit();
Expand All @@ -158,6 +169,7 @@ internal static void Main()
Task.Run(CheckGitHubGModVersion);
Task.Run(CheckForGwExeUpdates);
}

settings.Save();

if (command_arg_launch_account_name.Length > 0)
Expand All @@ -167,6 +179,7 @@ internal static void Main()
{
MessageBox.Show(@"Failed to launch account " + command_arg_launch_account_name + "\n" + res);
}

Exit();
return;
}
Expand All @@ -181,7 +194,6 @@ internal static void Main()
mainThreadRunning = true;
mainthread = new Thread(() =>
{

while (!shouldClose)
{
UnlockMutex();
Expand All @@ -208,10 +220,12 @@ internal static void Main()
{
state = "Active";
}

if (state != "Active" && accounts[i].process != null)
{
accounts[i].process = null;
}

if (accounts[i].state != state)
{
mainForm?.SetAccountState(i, state);
Expand All @@ -222,6 +236,7 @@ internal static void Main()

Thread.Sleep(1000);
}

mainThreadRunning = false;
});

Expand All @@ -230,6 +245,7 @@ internal static void Main()
mainForm.FormClosed += (_, _) => { Exit(); };
Application.Run(mainForm);
}

public static bool QueueLaunch(int index)
{
if (index < 0 || accounts.Length <= index)
Expand All @@ -251,6 +267,7 @@ public static void Exit()
gwlMutex = null;
}
}

private static bool ParseCommandLineArgs()
{
var args = Environment.GetCommandLineArgs();
Expand All @@ -265,12 +282,15 @@ private static bool ParseCommandLineArgs()
MessageBox.Show(@"No value for command line argument -launch");
return false;
}

command_arg_launch_account_name = args[i];
break;
}
}

return true;
}

private static bool InitialiseGWLauncherMutex()
{
// Check to see if another instance is running
Expand All @@ -279,9 +299,11 @@ private static bool InitialiseGWLauncherMutex()
//MessageBox.Show(@"GW Launcher already running. GW Launcher will close.");
return false;
}

gwlMutex = new Mutex(true, GwlMutexName);
return true;
}

private static bool LoadAccountsJson()
{
// Load accounts
Expand All @@ -307,6 +329,7 @@ private static bool LockMutex()
gotMutex = gotMutex || mutex.WaitOne(1000);
return gotMutex;
}

private static void UnlockMutex()
{
if (gotMutex)
Expand All @@ -326,7 +349,7 @@ private static async Task CheckGitHubNewerVersion()
File.Delete(newName);
}

//Get all releases from GitHub
// Get all releases from GitHub
var client = new GitHubClient(new ProductHeaderValue("GWLauncher"));
var releases = await client.Repository.Release.GetAll("gwdevhub", "gwlauncher");

Expand Down Expand Up @@ -362,11 +385,12 @@ private static async Task CheckGitHubNewerVersion()
}

var latest = releases[0];
var runtimeInstalled = IsDotNet8DesktopInstalled();

if (!settings.AutoUpdate)
{
var msgBoxResult = MessageBox.Show(
$@"New version {tagName} available online. Visit page?",
$@"New version {tagName} available online. Visit page?{(runtimeInstalled ? "\nYou can download the Framework Dependent version." : "")}",
@"GW Launcher",
MessageBoxButtons.YesNo,
MessageBoxIcon.Information,
Expand All @@ -385,7 +409,11 @@ private static async Task CheckGitHubNewerVersion()
return;
}

var asset = latest.Assets.First(a => a.Name == "GW_Launcher.exe");
var assetName = runtimeInstalled
? "GW_Launcher_Framework_Dependent.exe"
: "GW_Launcher.exe";

var asset = latest.Assets.FirstOrDefault(a => a.Name == assetName);
if (asset == null)
{
return;
Expand All @@ -401,8 +429,7 @@ private static async Task CheckGitHubNewerVersion()

mutex.WaitOne();
shouldClose = true;
if (mainthread.ThreadState == ThreadState.Running &&
!mainthread.Join(5000))
if (mainthread.ThreadState == ThreadState.Running && !mainthread.Join(5000))
{
return;
}
Expand All @@ -411,7 +438,6 @@ private static async Task CheckGitHubNewerVersion()
gwlMutex?.Close();

File.Move(currentName, oldName);

File.Move(newName, currentName);

var fileName = Environment.ProcessPath;
Expand All @@ -427,6 +453,34 @@ private static async Task CheckGitHubNewerVersion()
Environment.Exit(0);
}

private static bool IsDotNet8DesktopInstalled()
{
try
{
var processInfo = new ProcessStartInfo
{
FileName = "dotnet",
Arguments = "--list-runtimes",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};

using var process = Process.Start(processInfo);
using var reader = process.StandardOutput;
var output = reader.ReadToEnd();
process.WaitForExit();

// Check if .NET 8 is listed in the installed runtimes
return output.Contains("Microsoft.WindowsDesktop.App 8");
}
catch (Exception)
{
// If the check fails, assume .NET 8 is not installed
return false;
}
}

private static async Task CheckGitHubGModVersion()
{
var location = Path.GetDirectoryName(AppContext.BaseDirectory);
Expand Down Expand Up @@ -459,6 +513,7 @@ private static async Task CheckGitHubGModVersion()
{
strVersion = "1.0.0";
}

var localVersion = new Version(strVersion);

var versionComparison = localVersion.CompareTo(latestVersion);
Expand Down Expand Up @@ -508,7 +563,8 @@ private static async Task CheckForGwExeUpdates()

if (accsToUpdate.Count == 0) return;
var accNames = string.Join(',', accsToUpdate.Select(acc => acc.Name));
var ok = MessageBox.Show($"Accounts {accNames} are out of date. Update now?", "GW Update", MessageBoxButtons.YesNo);
var ok = MessageBox.Show($"Accounts {accNames} are out of date. Update now?", "GW Update",
MessageBoxButtons.YesNo);
if (ok == DialogResult.Yes)
{
AdminAccess.RestartAsAdminPrompt(true);
Expand All @@ -533,7 +589,8 @@ await mainForm.Invoke(async () =>
{
// Log the exception
Console.WriteLine($"Error checking for Gw.exe updates: {ex.Message}");
MessageBox.Show($"Error checking for updates: {ex.Message}", "Update Check Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show($"Error checking for updates: {ex.Message}", "Update Check Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}

0 comments on commit a702bbe

Please sign in to comment.