GetInstallDirectory(appId) for any SteamApp

This commit is contained in:
glax 2024-01-13 20:58:54 +01:00
parent bc25d7d384
commit 5e4d9599ba

32
OpenCS2hock/Installer.cs Normal file
View File

@ -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;
}
}