diff --git a/CreamInstaller/Forms/InstallForm.cs b/CreamInstaller/Forms/InstallForm.cs index e2374e4..794fa40 100644 --- a/CreamInstaller/Forms/InstallForm.cs +++ b/CreamInstaller/Forms/InstallForm.cs @@ -426,25 +426,29 @@ internal sealed partial class InstallForm : CustomForm private void OnLoad(object sender, EventArgs a) { - retry: - try + bool retry = true; + while (retry) { - userInfoLabel.Text = "Loading . . . "; - logTextBox.Text = string.Empty; - selectionCount = 0; - foreach (Selection selection in Selection.AllEnabled) + try { - selectionCount++; - _ = activeSelections.Add(selection); - } + userInfoLabel.Text = "Loading . . . "; + logTextBox.Text = string.Empty; + selectionCount = 0; + foreach (Selection selection in Selection.AllEnabled) + { + selectionCount++; + _ = activeSelections.Add(selection); + } - Start(); - } - catch (Exception e) - { - if (e.HandleException(this)) - goto retry; - Close(); + Start(); + retry = false; + } + catch (Exception e) + { + retry = e.HandleException(this); + if (!retry) + Close(); + } } } diff --git a/CreamInstaller/Forms/SelectForm.cs b/CreamInstaller/Forms/SelectForm.cs index 21873ae..938428d 100644 --- a/CreamInstaller/Forms/SelectForm.cs +++ b/CreamInstaller/Forms/SelectForm.cs @@ -1075,18 +1075,22 @@ internal sealed partial class SelectForm : CustomForm private void OnLoad(object sender, EventArgs _) { - retry: - try + bool retry = true; + while (retry) { - HideProgressBar(); - selectionTreeView.AfterCheck += OnTreeViewNodeCheckedChanged; - OnLoad(forceProvideChoices: true); - } - catch (Exception e) - { - if (e.HandleException(this)) - goto retry; - Close(); + try + { + HideProgressBar(); + selectionTreeView.AfterCheck += OnTreeViewNodeCheckedChanged; + OnLoad(forceProvideChoices: true); + retry = false; + } + catch (Exception e) + { + retry = e.HandleException(this); + if (!retry) + Close(); + } } } diff --git a/CreamInstaller/Forms/UpdateForm.cs b/CreamInstaller/Forms/UpdateForm.cs index 6a474e6..c2a172a 100644 --- a/CreamInstaller/Forms/UpdateForm.cs +++ b/CreamInstaller/Forms/UpdateForm.cs @@ -119,17 +119,21 @@ internal sealed partial class UpdateForm : CustomForm private void OnLoad(object sender, EventArgs _) { - retry: - try + bool retry = true; + while (retry) { - UpdaterPath.DeleteFile(); - OnLoad(); - } - catch (Exception e) - { - if (e.HandleException(this)) - goto retry; - Close(); + try + { + UpdaterPath.DeleteFile(); + OnLoad(); + retry = false; + } + catch (Exception e) + { + retry = e.HandleException(this); + if (!retry) + Close(); + } } } diff --git a/CreamInstaller/Platforms/Steam/SteamCMD.cs b/CreamInstaller/Platforms/Steam/SteamCMD.cs index 740ab8b..d74e376 100644 --- a/CreamInstaller/Platforms/Steam/SteamCMD.cs +++ b/CreamInstaller/Platforms/Steam/SteamCMD.cs @@ -46,82 +46,84 @@ internal static partial class SteamCMD private static async Task Run(string appId) => await Task.Run(() => { - wait_for_lock: - if (Program.Canceled) - return ""; - for (int i = 0; i < Locks.Length; i++) + while (true) { if (Program.Canceled) return ""; - if (Interlocked.CompareExchange(ref Locks[i], 1, 0) != 0) - continue; - if (appId != null) - { - _ = AttemptCount.TryGetValue(appId, out int count); - AttemptCount[appId] = ++count; - } - if (Program.Canceled) - return ""; - ProcessStartInfo processStartInfo = new() - { - FileName = FilePath, RedirectStandardOutput = true, RedirectStandardInput = true, - RedirectStandardError = true, - UseShellExecute = false, Arguments = appId is null ? "+quit" : GetArguments(appId), - CreateNoWindow = true, - StandardInputEncoding = Encoding.UTF8, StandardOutputEncoding = Encoding.UTF8, - StandardErrorEncoding = Encoding.UTF8 - }; - Process process = Process.Start(processStartInfo); - StringBuilder output = new(); - StringBuilder appInfo = new(); - bool appInfoStarted = false; - DateTime lastOutput = DateTime.UtcNow; - while (process != null) + for (int i = 0; i < Locks.Length; i++) { if (Program.Canceled) + return ""; + if (Interlocked.CompareExchange(ref Locks[i], 1, 0) != 0) + continue; + if (appId != null) { + _ = AttemptCount.TryGetValue(appId, out int count); + AttemptCount[appId] = ++count; + } + + if (Program.Canceled) + return ""; + ProcessStartInfo processStartInfo = new() + { + FileName = FilePath, RedirectStandardOutput = true, RedirectStandardInput = true, + RedirectStandardError = true, + UseShellExecute = false, Arguments = appId is null ? "+quit" : GetArguments(appId), + CreateNoWindow = true, + StandardInputEncoding = Encoding.UTF8, StandardOutputEncoding = Encoding.UTF8, + StandardErrorEncoding = Encoding.UTF8 + }; + Process process = Process.Start(processStartInfo); + StringBuilder output = new(); + StringBuilder appInfo = new(); + bool appInfoStarted = false; + DateTime lastOutput = DateTime.UtcNow; + while (process != null) + { + if (Program.Canceled) + { + process.Kill(true); + process.Close(); + break; + } + + int c = process.StandardOutput.Read(); + if (c != -1) + { + lastOutput = DateTime.UtcNow; + char ch = (char)c; + if (ch == '{') + appInfoStarted = true; + _ = appInfoStarted ? appInfo.Append(ch) : output.Append(ch); + } + + DateTime now = DateTime.UtcNow; + TimeSpan timeDiff = now - lastOutput; + if (!(timeDiff.TotalSeconds > 0.1)) + continue; process.Kill(true); process.Close(); - break; + if (appId != null && + output.ToString().Contains($"No app info for AppID {appId} found, requesting...")) + { + AttemptCount[appId]++; + processStartInfo.Arguments = GetArguments(appId); + process = Process.Start(processStartInfo); + appInfoStarted = false; + _ = output.Clear(); + _ = appInfo.Clear(); + } + else + break; } - int c = process.StandardOutput.Read(); - if (c != -1) - { - lastOutput = DateTime.UtcNow; - char ch = (char)c; - if (ch == '{') - appInfoStarted = true; - _ = appInfoStarted ? appInfo.Append(ch) : output.Append(ch); - } - - DateTime now = DateTime.UtcNow; - TimeSpan timeDiff = now - lastOutput; - if (!(timeDiff.TotalSeconds > 0.1)) - continue; - process.Kill(true); - process.Close(); - if (appId != null && - output.ToString().Contains($"No app info for AppID {appId} found, requesting...")) - { - AttemptCount[appId]++; - processStartInfo.Arguments = GetArguments(appId); - process = Process.Start(processStartInfo); - appInfoStarted = false; - _ = output.Clear(); - _ = appInfo.Clear(); - } - else - break; + _ = Interlocked.Decrement(ref Locks[i]); + return appInfo.ToString(); } - _ = Interlocked.Decrement(ref Locks[i]); - return appInfo.ToString(); + Thread.Sleep(200); } - - Thread.Sleep(200); - goto wait_for_lock; }); internal static async Task Setup(IProgress progress) @@ -129,27 +131,39 @@ internal static partial class SteamCMD await Cleanup(); if (!FilePath.FileExists()) { - retryDownload: - HttpClient httpClient = HttpClientManager.HttpClient; - if (httpClient is null) - return false; - while (!Program.Canceled) - try - { - byte[] file = - await httpClient.GetByteArrayAsync( - new Uri("https://steamcdn-a.akamaihd.net/client/installer/steamcmd.zip")); - _ = file.WriteResource(ArchivePath); - ArchivePath.ExtractZip(DirectoryPath); - ArchivePath.DeleteFile(); - break; - } - catch (Exception e) - { - if (e.HandleException(caption: Program.Name + " failed to download SteamCMD")) - goto retryDownload; + bool retryDownload = true; + while (retryDownload) + { + HttpClient httpClient = HttpClientManager.HttpClient; + if (httpClient is null) return false; + + bool downloadSuccess = false; + while (!Program.Canceled && !downloadSuccess) + { + try + { + byte[] file = + await httpClient.GetByteArrayAsync( + new Uri("https://steamcdn-a.akamaihd.net/client/installer/steamcmd.zip")); + _ = file.WriteResource(ArchivePath); + ArchivePath.ExtractZip(DirectoryPath); + ArchivePath.DeleteFile(); + downloadSuccess = true; + retryDownload = false; + } + catch (Exception e) + { + retryDownload = e.HandleException(caption: Program.Name + " failed to download SteamCMD"); + if (!retryDownload) + return false; + break; + } } + + if (downloadSuccess) + break; + } } if (DllPath.FileExists()) diff --git a/CreamInstaller/Program.cs b/CreamInstaller/Program.cs index f93303c..2f79e22 100644 --- a/CreamInstaller/Program.cs +++ b/CreamInstaller/Program.cs @@ -66,24 +66,30 @@ internal static class Program Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); AppDomain.CurrentDomain.UnhandledException += (_, e) => (e.ExceptionObject as Exception)?.HandleFatalException(); - retry: - try + bool retry = true; + while (retry) { - HttpClientManager.Setup(); - using UpdateForm form = new(); + try + { + HttpClientManager.Setup(); + using UpdateForm form = new(); #if DEBUG - DebugForm.Current.Attach(form); + DebugForm.Current.Attach(form); #endif - // Apply initial theme (dark by default) - Utility.ThemeManager.Apply(form); - Application.Run(form); - } - catch (Exception e) - { - if (e.HandleException()) - goto retry; - Application.Exit(); - return; + // Apply initial theme (dark by default) + Utility.ThemeManager.Apply(form); + Application.Run(form); + retry = false; + } + catch (Exception e) + { + retry = e.HandleException(); + if (!retry) + { + Application.Exit(); + return; + } + } } }