CreamInstaller/CreamInstaller/Program.cs

119 lines
4.5 KiB
C#
Raw Normal View History

2021-07-26 09:08:46 +05:00
using System;
2021-07-30 03:19:59 +05:00
using System.Diagnostics;
using System.Drawing;
using System.Linq;
2021-07-30 03:19:59 +05:00
using System.Reflection;
2021-07-26 09:08:46 +05:00
using System.Threading;
using System.Windows.Forms;
using CreamInstaller.Forms;
using CreamInstaller.Platforms.Steam;
using CreamInstaller.Utility;
2021-07-26 09:08:46 +05:00
namespace CreamInstaller;
internal static class Program
2021-07-26 09:08:46 +05:00
{
2022-09-20 19:38:27 +05:00
internal static readonly string Name = Application.CompanyName;
private static readonly string Description = Application.ProductName;
2022-09-20 19:38:27 +05:00
internal static readonly string Version = Application.ProductVersion;
internal const string RepositoryOwner = "pointfeev";
internal static readonly string RepositoryName = Name;
internal static readonly string RepositoryPackage = Name + ".zip";
2022-06-12 08:37:32 +05:00
#if DEBUG
2022-09-20 19:38:27 +05:00
internal static readonly string ApplicationName = Name + " v" + Version + "-debug: " + Description;
internal static readonly string ApplicationNameShort = Name + " v" + Version + "-debug";
internal static readonly string ApplicationExecutable = Name + "-debug.exe"; // should be the same as in .csproj
2022-06-12 08:37:32 +05:00
#else
2022-09-20 19:38:27 +05:00
internal static readonly string ApplicationName = Name + " v" + Version + ": " + Description;
internal static readonly string ApplicationNameShort = Name + " v" + Version;
internal static readonly string ApplicationExecutable = Name + ".exe"; // should be the same as in .csproj
2022-06-12 08:37:32 +05:00
#endif
2022-02-25 09:12:48 +05:00
internal static readonly Assembly EntryAssembly = Assembly.GetEntryAssembly();
private static readonly Process CurrentProcess = Process.GetCurrentProcess();
internal static readonly string CurrentProcessFilePath = CurrentProcess.MainModule?.FileName;
internal static bool BlockProtectedGames = true;
2022-08-22 00:48:38 +05:00
internal static readonly string[] ProtectedGames = { "PAYDAY 2" };
internal static readonly string[] ProtectedGameDirectories = { @"\EasyAntiCheat", @"\BattlEye" };
internal static readonly string[] ProtectedGameDirectoryExceptions = Array.Empty<string>();
internal static bool IsGameBlocked(string name, string directory = null)
2021-07-26 09:08:46 +05:00
{
2023-01-05 23:22:44 +05:00
if (!BlockProtectedGames)
return false;
if (ProtectedGames.Contains(name))
return true;
if (directory is null || ProtectedGameDirectoryExceptions.Contains(name))
return false;
2023-01-30 07:37:39 +05:00
return ProtectedGameDirectories.Any(path => (directory + path).DirectoryExists());
}
2023-03-28 09:36:37 +05:00
internal static bool AreDllsLockedDialog(Form form, ProgramSelection selection)
2022-02-25 09:12:48 +05:00
{
while (true)
2022-02-25 09:12:48 +05:00
{
if (selection.AreDllsLocked)
{
using DialogForm dialogForm = new(form);
if (dialogForm.Show(SystemIcons.Error,
$"ERROR: One or more DLLs crucial to unlocker installation are locked for {selection.Name}!"
+ "\n\nThis is commonly caused by the program/game being active or an anti-virus blocking access."
+ "\n\nPlease close the program/game or resolve your anti-virus to continue . . . ", "Retry", "Cancel") == DialogResult.OK)
continue;
}
else
return true;
return false;
2022-02-25 09:12:48 +05:00
}
}
[STAThread]
private static void Main()
{
2022-09-20 19:38:27 +05:00
using Mutex mutex = new(true, Name, out bool createdNew);
if (createdNew)
2021-07-26 09:08:46 +05:00
{
_ = Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ApplicationExit += OnApplicationExit;
2023-01-14 02:48:34 +05:00
Application.ThreadException += (_, e) => e.Exception.HandleFatalException();
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
2023-01-14 02:48:34 +05:00
AppDomain.CurrentDomain.UnhandledException += (_, e) => (e.ExceptionObject as Exception)?.HandleFatalException();
retry:
try
{
2022-02-25 09:12:48 +05:00
HttpClientManager.Setup();
using MainForm form = new();
#if DEBUG
DebugForm.Current.Attach(form);
#endif
Application.Run(form);
}
catch (Exception e)
{
2023-01-05 23:22:44 +05:00
if (e.HandleException())
goto retry;
Application.Exit();
return;
}
}
mutex.Close();
}
internal static bool Canceled;
internal static async void Cleanup(bool cancel = true)
{
Canceled = cancel;
await SteamCMD.Cleanup();
}
2021-07-26 09:08:46 +05:00
2022-02-25 10:51:11 +05:00
private static void OnApplicationExit(object s, EventArgs e)
{
Cleanup();
HttpClientManager.Dispose();
}
}