Files
CreamInstaller/CreamInstaller/Program.cs
T
Frog 71d17031b9 WIP Start to Settings Menu / UI Adjustments
- Remove the MainForm cancel button > it doesn't really serve a purpose and it breaks the UI cause I suck.
- Add the start to a settings dialog, relocated several settings from the top bar into the new setttings dialog
- Add a custom Toggle Switch, change the CheckBox for SmokeAPI/CreamAPI to a Toggle Switch
- Relocate Uninstall All button to main form (Currently disabled)
2026-07-20 01:41:55 -07:00

196 lines
6.8 KiB
C#

#nullable enable
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using CreamInstaller.Forms;
using CreamInstaller.Platforms.Steam;
using CreamInstaller.Utility;
namespace CreamInstaller;
internal static class Program
{
internal static readonly string Name = Application.CompanyName!;
private static readonly string Description = Application.ProductName!;
internal static readonly string Version = Application.ProductVersion[
..(Application.ProductVersion.IndexOf('+') is var index && index != -1
? index
: Application.ProductVersion.Length)];
internal const string RepositoryOwner = "FroggMaster";
internal static readonly string RepositoryName = Name;
internal static readonly string RepositoryPackage = Name + ".zip";
internal static readonly string RepositoryExecutable = Name + ".exe";
#if DEBUG
internal static readonly string ApplicationName = Name + " v" + Version + "-debug: " + Description;
internal static readonly string ApplicationNameShort = Name + " v" + Version + "-debug";
#else
internal static readonly string ApplicationName = Name + " v" + Version + ": " + Description;
internal static readonly string ApplicationNameShort = Name + " v" + Version;
#endif
private static readonly Process CurrentProcess = Process.GetCurrentProcess();
internal static readonly string CurrentProcessFilePath = CurrentProcess.MainModule?.FileName ?? "";
internal static readonly int CurrentProcessId = CurrentProcess.Id;
// Settings loaded from ProgramData on startup — persisted across sessions
internal static SettingsModel AppSettings { get; private set; } = new();
internal static bool UseSmokeAPI
{
get => AppSettings.UseSmokeAPI;
set => AppSettings.UseSmokeAPI = value;
}
internal static bool BlockProtectedGames
{
get => AppSettings.BlockProtectedGames;
set => AppSettings.BlockProtectedGames = value;
}
internal static bool DarkModeEnabled
{
get => AppSettings.DarkModeEnabled;
set => AppSettings.DarkModeEnabled = value;
}
internal static bool SortByName
{
get => AppSettings.SortByName;
set => AppSettings.SortByName = value;
}
internal static readonly string[] ProtectedGames = ["PAYDAY 2"];
internal static readonly string[] ProtectedGameDirectories = [@"\EasyAntiCheat", @"\BattlEye"];
internal static readonly string[] ProtectedGameDirectoryExceptions = [];
internal static bool IsGameBlocked(string name, string? directory = null)
=> GetGameBlockedReason(name, directory) is not null;
internal static string? GetGameBlockedReason(string name, string? directory = null)
{
if (!BlockProtectedGames) return null;
if (ProtectedGames.Contains(name)) return "on protected games list";
if (directory is null) return null;
if (ProtectedGameDirectoryExceptions.Contains(name)) return null;
string? foundAntiCheat = ProtectedGameDirectories.FirstOrDefault(path => (directory + path).DirectoryExists());
return foundAntiCheat is not null
? $"{foundAntiCheat[1..]} directory found"
: null;
}
[STAThread]
private static void Main()
{
using Mutex mutex = new(true, Name, out bool createdNew);
if (createdNew)
{
_ = Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ApplicationExit += OnApplicationExit;
Application.ThreadException += (_, e) => e.Exception.HandleFatalException();
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException +=
(_, e) => (e.ExceptionObject as Exception)?.HandleFatalException();
bool retry = true;
while (retry)
{
try
{
HttpClientManager.Setup();
AppSettings = ProgramData.LoadSettings(); // load persisted settings
using UpdateForm form = new();
#if DEBUG
DebugForm.Current.Attach(form);
#endif
// 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;
}
}
}
}
mutex.Close();
}
internal static bool Canceled;
/// <summary>
/// Initiates application cleanup asynchronously. Use this when you can await the result.
/// </summary>
/// <param name="cancel">Whether to set the Canceled flag</param>
/// <returns>Task that completes when cleanup is finished</returns>
internal static async Task CleanupAsync(bool cancel = true)
{
if (cancel)
Canceled = true;
await SteamCMD.Cleanup();
}
/// <summary>
/// Synchronous cleanup wrapper for event handlers and other synchronous contexts.
/// Initiates cleanup without blocking but does not wait for completion.
/// </summary>
/// <param name="cancel">Whether to set the Canceled flag</param>
internal static void Cleanup(bool cancel = true)
{
if (cancel)
Canceled = true;
// Fire and forget - don't block synchronous callers
// Any exceptions will be logged but won't crash the app
_ = Task.Run(async () =>
{
try
{
await SteamCMD.Cleanup();
}
catch (Exception ex)
{
ProgramData.Log.Warn($"Cleanup failed: {ex.Message}");
}
});
}
private static void OnApplicationExit(object? s, EventArgs e)
{
Canceled = true;
// For application exit, we should try to wait briefly for cleanup
try
{
Task cleanupTask = SteamCMD.Cleanup();
// Wait up to 5 seconds for graceful cleanup
if (!cleanupTask.Wait(TimeSpan.FromSeconds(5)))
{
ProgramData.Log.Warn("Cleanup timed out during application exit");
}
}
catch (Exception ex)
{
ProgramData.Log.Warn($"Cleanup exception during exit: {ex.Message}");
}
finally
{
ProgramData.SaveSettings(AppSettings);
HttpClientManager.Dispose();
}
}
}