106 lines
3.1 KiB
C#
106 lines
3.1 KiB
C#
using Microsoft.Win32;
|
|
using System.Runtime.Versioning;
|
|
|
|
namespace gregExtractor.Utils;
|
|
|
|
public static class SteamLocator
|
|
{
|
|
private const string DataCenterFolderName = "Data Center";
|
|
|
|
public static string? FindGamePath()
|
|
{
|
|
foreach (string libraryRoot in EnumerateSteamLibraryRoots())
|
|
{
|
|
string candidate = Path.Combine(libraryRoot, "steamapps", "common", DataCenterFolderName);
|
|
if (Directory.Exists(candidate))
|
|
return candidate;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static string? FindIl2CppAssembliesPath()
|
|
{
|
|
string? gamePath = FindGamePath();
|
|
if (string.IsNullOrWhiteSpace(gamePath))
|
|
return null;
|
|
|
|
string modernPath = Path.Combine(gamePath, "MelonLoader", "Il2CppAssemblies");
|
|
if (Directory.Exists(modernPath))
|
|
return modernPath;
|
|
|
|
string legacyPath = Path.Combine(gamePath, "MelonLoader", "Managed");
|
|
if (Directory.Exists(legacyPath))
|
|
return legacyPath;
|
|
|
|
return null;
|
|
}
|
|
|
|
private static IEnumerable<string> EnumerateSteamLibraryRoots()
|
|
{
|
|
var roots = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
foreach (string installPath in ReadSteamInstallPaths())
|
|
{
|
|
if (string.IsNullOrWhiteSpace(installPath) || !Directory.Exists(installPath))
|
|
continue;
|
|
|
|
roots.Add(installPath);
|
|
|
|
string libraryFile = Path.Combine(installPath, "steamapps", "libraryfolders.vdf");
|
|
if (!File.Exists(libraryFile))
|
|
continue;
|
|
|
|
string content = File.ReadAllText(libraryFile);
|
|
foreach (Match match in Regex.Matches(content, "\"path\"\\s+\"([^\"]+)\"", RegexOptions.IgnoreCase))
|
|
{
|
|
string parsedPath = match.Groups[1].Value.Replace("\\\\", "\\");
|
|
if (!string.IsNullOrWhiteSpace(parsedPath) && Directory.Exists(parsedPath))
|
|
roots.Add(parsedPath);
|
|
}
|
|
}
|
|
|
|
return roots;
|
|
}
|
|
|
|
private static IEnumerable<string> ReadSteamInstallPaths()
|
|
{
|
|
if (!OperatingSystem.IsWindows())
|
|
return Enumerable.Empty<string>();
|
|
|
|
var paths = new List<string>();
|
|
|
|
string? wowPath = ReadRegistryValue(@"SOFTWARE\WOW6432Node\Valve\Steam", "InstallPath");
|
|
if (!string.IsNullOrWhiteSpace(wowPath))
|
|
paths.Add(wowPath);
|
|
|
|
string? nativePath = ReadRegistryValue(@"SOFTWARE\Valve\Steam", "InstallPath");
|
|
if (!string.IsNullOrWhiteSpace(nativePath))
|
|
paths.Add(nativePath);
|
|
|
|
return paths;
|
|
}
|
|
|
|
private static string? ReadRegistryValue(string keyPath, string valueName)
|
|
{
|
|
if (!OperatingSystem.IsWindows())
|
|
return null;
|
|
|
|
return ReadRegistryValueWindows(keyPath, valueName);
|
|
}
|
|
|
|
[SupportedOSPlatform("windows")]
|
|
private static string? ReadRegistryValueWindows(string keyPath, string valueName)
|
|
{
|
|
try
|
|
{
|
|
using RegistryKey? key = Registry.LocalMachine.OpenSubKey(keyPath);
|
|
return key?.GetValue(valueName) as string;
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
}
|