From 5e4d9599ba500580016b894a909fca723c756a9b Mon Sep 17 00:00:00 2001 From: glax Date: Sat, 13 Jan 2024 20:58:54 +0100 Subject: [PATCH] GetInstallDirectory(appId) for any SteamApp --- OpenCS2hock/Installer.cs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 OpenCS2hock/Installer.cs diff --git a/OpenCS2hock/Installer.cs b/OpenCS2hock/Installer.cs new file mode 100644 index 0000000..d52925d --- /dev/null +++ b/OpenCS2hock/Installer.cs @@ -0,0 +1,32 @@ +using Microsoft.Win32; + +namespace OpenCS2hock; + +public static class Installer +{ + public static string? GetInstallDirectory(int appId = 730) + { + string steamInstallation = +#pragma warning disable CA1416 //Registry only available on Windows + (string)(Registry.GetValue(@"HKEY_CURRENT_USER\SOFTWARE\Valve\Steam", "SteamPath", null) ?? +#pragma warning restore CA1416 + throw new DirectoryNotFoundException("No Steam Installation found.")); + string libraryFolderFilepath = Path.Combine(steamInstallation, "steamapps\\libraryfolders.vdf"); + string? libraryPath = null; + string? appManifestFolderPath = null; + foreach (string line in File.ReadAllLines(libraryFolderFilepath)) + if (line.Contains("path")) + libraryPath = line.Split("\"").Last(split => split.Length > 0); + else if (line.Contains($"\"{appId}\"")) + appManifestFolderPath = Path.Combine(libraryPath!, $"steamapps\\appmanifest_{appId}.acf"); + + string installationPath = ""; + if (appManifestFolderPath is null) + throw new DirectoryNotFoundException($"No {appId} Installation found."); + foreach(string line in File.ReadAllLines(appManifestFolderPath)) + if (line.Contains("installdir")) + installationPath = Path.Combine(libraryPath!, "steamapps\\common", line.Split("\"").Last(split => split.Length > 0)); + + return installationPath; + } +} \ No newline at end of file