mirror of
https://github.com/FroggMaster/CreamInstaller.git
synced 2026-06-12 11:01:23 -07:00
Replace GoTo Statement with While Loops
- Pretty basic code adjustment, changes GOTO statements to while loops.
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -46,82 +46,84 @@ internal static partial class SteamCMD
|
||||
private static async Task<string> 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<bool> Setup(IProgress<int> 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())
|
||||
|
||||
+21
-15
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user