diff --git a/CreamInstaller/Classes/Diagnostics.cs b/CreamInstaller/Classes/Diagnostics.cs new file mode 100644 index 0000000..895023e --- /dev/null +++ b/CreamInstaller/Classes/Diagnostics.cs @@ -0,0 +1,24 @@ +using System.Diagnostics; + +namespace CreamInstaller.Classes; + +internal static class Diagnostics +{ + internal static void OpenFileInNotepad(string path) => Process.Start(new ProcessStartInfo + { + FileName = "notepad.exe", + Arguments = path + }); + + internal static void OpenDirectoryInFileExplorer(string path) => Process.Start(new ProcessStartInfo + { + FileName = "explorer.exe", + Arguments = path + }); + + internal static void OpenUrlInInternetBrowser(string url) => Process.Start(new ProcessStartInfo + { + FileName = url, + UseShellExecute = true + }); +} diff --git a/CreamInstaller/Classes/FileGrabber.cs b/CreamInstaller/Classes/FileGrabber.cs deleted file mode 100644 index e8d3989..0000000 --- a/CreamInstaller/Classes/FileGrabber.cs +++ /dev/null @@ -1,65 +0,0 @@ -using System.Diagnostics; -using System.IO; - -using Microsoft.Win32; - -namespace CreamInstaller.Classes; - -internal static class FileGrabber -{ - internal static string steamInstallPath = null; - internal static string SteamInstallPath - { - get - { - steamInstallPath ??= Registry.GetValue(@"HKEY_LOCAL_MACHINE\Software\Valve\Steam", "InstallPath", null) as string; - steamInstallPath ??= Registry.GetValue(@"HKEY_LOCAL_MACHINE\Software\Wow6432Node\Valve\Steam", "InstallPath", null) as string; - return steamInstallPath; - } - } - - internal static string paradoxLauncherInstallPath = null; - internal static string ParadoxLauncherInstallPath - { - get - { - paradoxLauncherInstallPath ??= Registry.GetValue(@"HKEY_CURRENT_USER\Software\Paradox Interactive\Paradox Launcher v2", "LauncherInstallation", null) as string; - return paradoxLauncherInstallPath; - } - } - - internal static void GetApiComponents(this string directory, out string api, out string api_o, out string api64, out string api64_o, out string cApi) - { - api = directory + @"\steam_api.dll"; - api_o = directory + @"\steam_api_o.dll"; - api64 = directory + @"\steam_api64.dll"; - api64_o = directory + @"\steam_api64_o.dll"; - cApi = directory + @"\cream_api.ini"; - } - - 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; - } - - internal static void OpenFileInNotepad(string path) => Process.Start(new ProcessStartInfo - { - FileName = "notepad.exe", - Arguments = path - }); - - internal static void OpenDirectoryInFileExplorer(string path) => Process.Start(new ProcessStartInfo - { - FileName = "explorer.exe", - Arguments = path - }); - - internal static void OpenUrlInInternetBrowser(string url) => Process.Start(new ProcessStartInfo - { - FileName = url, - UseShellExecute = true - }); -} diff --git a/CreamInstaller/Classes/HttpClientManager.cs b/CreamInstaller/Classes/HttpClientManager.cs index e3439ca..85906b3 100644 --- a/CreamInstaller/Classes/HttpClientManager.cs +++ b/CreamInstaller/Classes/HttpClientManager.cs @@ -54,9 +54,9 @@ internal static class HttpClientManager "//div[@class='recommendation']/div/a"); if (nodes is not null) foreach (HtmlNode node in nodes) - if (int.TryParse(node.Attributes["data-ds-appid"]?.Value, out int dlcAppId) && !dlcIds.Contains(dlcAppId)) + if (int.TryParse(node.Attributes?["data-ds-appid"]?.Value, out int dlcAppId) && !dlcIds.Contains(dlcAppId)) dlcIds.Add(dlcAppId); } - internal static void Cleanup() => httpClient.Dispose(); + internal static void Dispose() => httpClient.Dispose(); } diff --git a/CreamInstaller/Classes/SteamLibrary.cs b/CreamInstaller/Classes/SteamLibrary.cs index e53bd7b..403ef8f 100644 --- a/CreamInstaller/Classes/SteamLibrary.cs +++ b/CreamInstaller/Classes/SteamLibrary.cs @@ -7,15 +7,38 @@ using System.Threading.Tasks; using Gameloop.Vdf.Linq; +using Microsoft.Win32; + namespace CreamInstaller.Classes; internal static class SteamLibrary { + internal static string paradoxLauncherInstallPath = null; + internal static string ParadoxLauncherInstallPath + { + get + { + paradoxLauncherInstallPath ??= Registry.GetValue(@"HKEY_CURRENT_USER\Software\Paradox Interactive\Paradox Launcher v2", "LauncherInstallation", null) as string; + return paradoxLauncherInstallPath; + } + } + + internal static string steamInstallPath = null; + internal static string SteamInstallPath + { + get + { + steamInstallPath ??= Registry.GetValue(@"HKEY_LOCAL_MACHINE\Software\Valve\Steam", "InstallPath", null) as string; + steamInstallPath ??= Registry.GetValue(@"HKEY_LOCAL_MACHINE\Software\Wow6432Node\Valve\Steam", "InstallPath", null) as string; + return steamInstallPath; + } + } + internal static async Task> GetLibraryDirectories() => await Task.Run(() => { List gameDirectories = new(); if (Program.Canceled) return gameDirectories; - string steamInstallPath = FileGrabber.SteamInstallPath; + string steamInstallPath = SteamInstallPath; if (steamInstallPath != null && Directory.Exists(steamInstallPath)) { string libraryFolder = steamInstallPath + @"\steamapps"; diff --git a/CreamInstaller/Forms/Components/CustomForm.cs b/CreamInstaller/Forms/Components/CustomForm.cs index 1072c6e..fddf86b 100644 --- a/CreamInstaller/Forms/Components/CustomForm.cs +++ b/CreamInstaller/Forms/Components/CustomForm.cs @@ -17,4 +17,11 @@ internal class CustomForm : Form return handleParam; } } + + internal void InheritLocation(Form fromForm) + { + int X = fromForm.Location.X + fromForm.Size.Width / 2 - Size.Width / 2; + int Y = fromForm.Location.Y + fromForm.Size.Height / 2 - Size.Height / 2; + Location = new(X, Y); + } } diff --git a/CreamInstaller/Forms/SelectForm.cs b/CreamInstaller/Forms/SelectForm.cs index 6633b6c..9d3c9e2 100644 --- a/CreamInstaller/Forms/SelectForm.cs +++ b/CreamInstaller/Forms/SelectForm.cs @@ -83,8 +83,8 @@ internal partial class SelectForm : CustomForm { if (Program.Canceled) return; List> applicablePrograms = new(); - if (Directory.Exists(FileGrabber.ParadoxLauncherInstallPath)) - applicablePrograms.Add(new(0, "Paradox Launcher", "", 0, FileGrabber.ParadoxLauncherInstallPath)); + if (Directory.Exists(SteamLibrary.ParadoxLauncherInstallPath)) + applicablePrograms.Add(new(0, "Paradox Launcher", "", 0, SteamLibrary.ParadoxLauncherInstallPath)); List gameLibraryDirectories = await SteamLibrary.GetLibraryDirectories(); foreach (string libraryDirectory in gameLibraryDirectories) { @@ -393,9 +393,9 @@ internal partial class SelectForm : CustomForm Dictionary images = new(); Task.Run(async () => { - if (Directory.Exists(FileGrabber.ParadoxLauncherInstallPath)) + if (Directory.Exists(SteamLibrary.ParadoxLauncherInstallPath)) { - foreach (string file in Directory.GetFiles(FileGrabber.ParadoxLauncherInstallPath, "*.exe")) + foreach (string file in Directory.GetFiles(SteamLibrary.ParadoxLauncherInstallPath, "*.exe")) { images["Paradox Launcher"] = IconGrabber.GetFileIconImage(file); break; @@ -449,7 +449,7 @@ internal partial class SelectForm : CustomForm { nodeContextMenu.Items.Add(new ToolStripSeparator()); nodeContextMenu.Items.Add(new ToolStripMenuItem("Open AppInfo", Image("Notepad"), - new EventHandler((sender, e) => FileGrabber.OpenFileInNotepad(appInfo)))); + new EventHandler((sender, e) => Diagnostics.OpenFileInNotepad(appInfo)))); nodeContextMenu.Items.Add(new ToolStripMenuItem("Refresh AppInfo", Image("Command Prompt"), new EventHandler((sender, e) => { @@ -519,25 +519,25 @@ internal partial class SelectForm : CustomForm } nodeContextMenu.Items.Add(new ToolStripSeparator()); nodeContextMenu.Items.Add(new ToolStripMenuItem("Open Root Directory", Image("File Explorer"), - new EventHandler((sender, e) => FileGrabber.OpenDirectoryInFileExplorer(selection.RootDirectory)))); + new EventHandler((sender, e) => Diagnostics.OpenDirectoryInFileExplorer(selection.RootDirectory)))); for (int i = 0; i < selection.SteamApiDllDirectories.Count; i++) { string directory = selection.SteamApiDllDirectories[i]; nodeContextMenu.Items.Add(new ToolStripMenuItem($"Open Steamworks Directory ({i + 1})", Image("File Explorer"), - new EventHandler((sender, e) => FileGrabber.OpenDirectoryInFileExplorer(directory)))); + new EventHandler((sender, e) => Diagnostics.OpenDirectoryInFileExplorer(directory)))); } } if (appId != 0) { nodeContextMenu.Items.Add(new ToolStripSeparator()); nodeContextMenu.Items.Add(new ToolStripMenuItem("Open SteamDB", Image("SteamDB"), - new EventHandler((sender, e) => FileGrabber.OpenUrlInInternetBrowser("https://steamdb.info/app/" + appId)))); + new EventHandler((sender, e) => Diagnostics.OpenUrlInInternetBrowser("https://steamdb.info/app/" + appId)))); nodeContextMenu.Items.Add(new ToolStripMenuItem("Open Steam Store", Image("Steam Store"), - new EventHandler((sender, e) => FileGrabber.OpenUrlInInternetBrowser("https://store.steampowered.com/app/" + appId)))); + new EventHandler((sender, e) => Diagnostics.OpenUrlInInternetBrowser("https://store.steampowered.com/app/" + appId)))); if (selection is not null) { ToolStripMenuItem steamCommunity = new("Open Steam Community", Image("ClientIcon_" + node.Name), - new EventHandler((sender, e) => FileGrabber.OpenUrlInInternetBrowser("https://steamcommunity.com/app/" + appId))); + new EventHandler((sender, e) => Diagnostics.OpenUrlInInternetBrowser("https://steamcommunity.com/app/" + appId))); nodeContextMenu.Items.Add(steamCommunity); if (steamCommunity.Image is null) { @@ -603,7 +603,7 @@ internal partial class SelectForm : CustomForm installForm.ShowDialog(); if (installForm.Reselecting) { - this.InheritLocation(installForm); + InheritLocation(installForm); Show(); OnLoad(); } diff --git a/CreamInstaller/Program.cs b/CreamInstaller/Program.cs index 50972ba..8847f29 100644 --- a/CreamInstaller/Program.cs +++ b/CreamInstaller/Program.cs @@ -34,6 +34,14 @@ internal static class Program return false; } + 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; + } + internal static bool IsProgramRunningDialog(Form form, ProgramSelection selection) { if (selection.AreSteamApiDllsLocked) @@ -48,6 +56,15 @@ internal static class Program return false; } + internal static void GetApiComponents(this string directory, out string api, out string api_o, out string api64, out string api64_o, out string cApi) + { + api = directory + @"\steam_api.dll"; + api_o = directory + @"\steam_api_o.dll"; + api64 = directory + @"\steam_api64.dll"; + api64_o = directory + @"\steam_api64_o.dll"; + cApi = directory + @"\cream_api.ini"; + } + [STAThread] private static void Main() { @@ -76,20 +93,16 @@ internal static class Program internal static void Invoke(this Control control, MethodInvoker methodInvoker) => control.Invoke(methodInvoker); - internal static void InheritLocation(this Form form, Form fromForm) - { - int X = fromForm.Location.X + fromForm.Size.Width / 2 - form.Size.Width / 2; - int Y = fromForm.Location.Y + fromForm.Size.Height / 2 - form.Size.Height / 2; - form.Location = new(X, Y); - } - internal static bool Canceled = false; internal static async void Cleanup(bool cancel = true) { Canceled = cancel; - HttpClientManager.Cleanup(); await SteamCMD.Cleanup(); } - private static void OnApplicationExit(object s, EventArgs e) => Cleanup(); + private static void OnApplicationExit(object s, EventArgs e) + { + Cleanup(); + HttpClientManager.Dispose(); + } }