CreamInstaller/CreamInstaller/Program.cs

122 lines
4.6 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;
2021-07-26 09:08:46 +05:00
using System.IO;
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.Steam;
using CreamInstaller.Utility;
namespace CreamInstaller;
internal static class Program
2021-07-26 09:08:46 +05:00
{
internal static readonly string ApplicationName = Application.CompanyName + " v" + Application.ProductVersion + ": " + Application.ProductName;
internal static readonly string ApplicationNameShort = Application.CompanyName + " v" + Application.ProductVersion;
2022-02-25 09:12:48 +05:00
internal static readonly Assembly EntryAssembly = Assembly.GetEntryAssembly();
internal static readonly Process CurrentProcess = Process.GetCurrentProcess();
internal static readonly string CurrentProcessFilePath = CurrentProcess.MainModule.FileName;
internal static bool BlockProtectedGames = true;
internal static readonly string[] ProtectedGameNames = { "PAYDAY 2", "Call to Arms" }; // non-functioning CreamAPI/ScreamAPI or DLL detections
internal static readonly string[] ProtectedGameDirectories = { @"\EasyAntiCheat", @"\BattlEye" }; // DLL detections
internal static readonly string[] ProtectedGameDirectoryExceptions = { "Arma 3" }; // Arma 3's BattlEye doesn't detect DLL changes?
internal static bool IsGameBlocked(string name, string directory = null)
2021-07-26 09:08:46 +05:00
{
if (!BlockProtectedGames) return false;
if (ProtectedGameNames.Contains(name)) return true;
if (directory is not null && !ProtectedGameDirectoryExceptions.Contains(name))
foreach (string path in ProtectedGameDirectories)
if (Directory.Exists(directory + path)) return true;
return false;
}
2022-02-25 10:51:11 +05:00
internal static bool IsFilePathLocked(this string filePath)
{
try { File.Open(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None).Close(); }
catch (FileNotFoundException) { return false; }
catch (IOException) { return true; }
return false;
}
2022-02-25 09:12:48 +05:00
internal static bool IsProgramRunningDialog(Form form, ProgramSelection selection)
{
if (selection.AreDllsLocked)
2022-02-25 09:12:48 +05:00
{
using DialogForm dialogForm = new(form);
if (dialogForm.Show(SystemIcons.Error,
2022-02-25 09:12:48 +05:00
$"ERROR: {selection.Name} is currently running!" +
"\n\nPlease close the program/game to continue . . . ",
"Retry", "Cancel") == DialogResult.OK)
return IsProgramRunningDialog(form, selection);
}
else return true;
return false;
}
2022-03-08 00:40:48 +05:00
internal static void GetCreamApiComponents(this string directory, out string sdk32, out string sdk32_o, out string sdk64, out string sdk64_o, out string config)
2022-02-25 10:51:11 +05:00
{
2022-03-08 00:40:48 +05:00
sdk32 = directory + @"\steam_api.dll";
sdk32_o = directory + @"\steam_api_o.dll";
sdk64 = directory + @"\steam_api64.dll";
sdk64_o = directory + @"\steam_api64_o.dll";
config = directory + @"\cream_api.ini";
2022-02-25 10:51:11 +05:00
}
2022-03-08 00:40:48 +05:00
internal static void GetScreamApiComponents(this string directory, out string sdk32, out string sdk32_o, out string sdk64, out string sdk64_o, out string config)
{
2022-03-08 00:40:48 +05:00
sdk32 = directory + @"\EOSSDK-Win32-Shipping.dll";
sdk32_o = directory + @"\EOSSDK-Win32-Shipping_o.dll";
sdk64 = directory + @"\EOSSDK-Win64-Shipping.dll";
sdk64_o = directory + @"\EOSSDK-Win64-Shipping_o.dll";
2022-03-08 00:40:48 +05:00
config = directory + @"\ScreamAPI.json";
}
[STAThread]
private static void Main()
{
using Mutex mutex = new(true, "CreamInstaller", out bool createdNew);
if (createdNew)
2021-07-26 09:08:46 +05:00
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ApplicationExit += new(OnApplicationExit);
retry:
try
{
2022-02-25 09:12:48 +05:00
HttpClientManager.Setup();
using MainForm form = new();
Application.Run(form);
}
catch (Exception e)
{
if (ExceptionHandler.OutputException(e)) goto retry;
Application.Exit();
return;
}
}
mutex.Close();
}
internal static void Invoke(this Control control, MethodInvoker methodInvoker) => control.Invoke(methodInvoker);
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();
}
}