9bb4f85802
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
74 lines
2.7 KiB
C#
74 lines
2.7 KiB
C#
using System.Diagnostics;
|
|
using Microsoft.Win32;
|
|
|
|
namespace CVRManager.Services;
|
|
|
|
public static class SteamIntegration
|
|
{
|
|
public static string? SteamPath
|
|
{
|
|
get
|
|
{
|
|
var p = Registry.GetValue(@"HKEY_CURRENT_USER\Software\Valve\Steam", "SteamPath", null) as string;
|
|
return p == null ? null : Path.GetFullPath(p);
|
|
}
|
|
}
|
|
|
|
public static string? SteamExe => SteamPath is { } p ? Path.Combine(p, "steam.exe") : null;
|
|
|
|
public static bool IsRunning => Process.GetProcessesByName("steam").Length > 0;
|
|
|
|
public static bool IsAppRunning =>
|
|
Registry.GetValue($@"HKEY_CURRENT_USER\Software\Valve\Steam\Apps\{Paths.AppId}", "Running", 0) is 1;
|
|
|
|
public static async Task<bool> EnsureRunningAsync(Action<string> log)
|
|
{
|
|
if (IsRunning) return true;
|
|
var exe = SteamExe;
|
|
if (exe == null || !File.Exists(exe)) return false;
|
|
log("Steam is not running; starting it in the background so playtime is counted...");
|
|
Process.Start(new ProcessStartInfo(exe, "-silent") { UseShellExecute = true });
|
|
for (var i = 0; i < 40; i++)
|
|
{
|
|
await Task.Delay(500);
|
|
var pid = Registry.GetValue(@"HKEY_CURRENT_USER\Software\Valve\Steam\ActiveProcess", "pid", 0) as int? ?? 0;
|
|
if (pid != 0 && IsRunning) { await Task.Delay(2000); return true; }
|
|
}
|
|
return IsRunning;
|
|
}
|
|
|
|
public static List<(string GameDir, string Manifest)> FindExistingInstalls()
|
|
{
|
|
var result = new List<(string, string)>();
|
|
var steam = SteamPath;
|
|
if (steam == null) return result;
|
|
var libs = new List<string> { steam };
|
|
try
|
|
{
|
|
var vdf = Path.Combine(steam, "steamapps", "libraryfolders.vdf");
|
|
if (File.Exists(vdf))
|
|
{
|
|
var doc = Vdf.ParseDocument(File.ReadAllText(vdf));
|
|
if (doc.Dict("libraryfolders") is { } lf)
|
|
foreach (var e in lf.Values.OfType<Dictionary<string, object>>())
|
|
if (e.Str("path") is { Length: > 0 } p) libs.Add(p);
|
|
}
|
|
}
|
|
catch { }
|
|
|
|
foreach (var lib in libs.Distinct(StringComparer.OrdinalIgnoreCase))
|
|
{
|
|
var manifest = Path.Combine(lib, "steamapps", $"appmanifest_{Paths.AppId}.acf");
|
|
if (!File.Exists(manifest)) continue;
|
|
try
|
|
{
|
|
var dir = Vdf.ParseDocument(File.ReadAllText(manifest)).Dict("AppState")?.Str("installdir") ?? "ChilloutVR";
|
|
var game = Path.Combine(lib, "steamapps", "common", dir);
|
|
if (Directory.Exists(game)) result.Add((game, manifest));
|
|
}
|
|
catch { }
|
|
}
|
|
return result;
|
|
}
|
|
}
|