| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices;
- namespace SlamCross.Services;
- public static class GameLocatorService
- {
- public static string? FindSteamAppsPath(string exeName)
- {
- if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
- return FindSteamAppsLinux(exeName);
- if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
- return FindSteamAppsWindows(exeName);
- return null;
- }
- private static string? FindSteamAppsLinux(string exeName)
- {
- try
- {
- var proc = Process.GetProcessesByName(exeName).FirstOrDefault();
- if (proc?.MainModule?.FileName is string exe)
- {
- var dir = Path.GetDirectoryName(exe);
- while (dir != null)
- {
- if (Path.GetFileName(dir) == "steamapps")
- return dir;
- dir = Path.GetDirectoryName(dir);
- }
- }
- }
- catch { }
- var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
- string[] candidates =
- [
- Path.Combine(home, ".local/share/Steam/steamapps"),
- Path.Combine(home, ".steam/steam/steamapps"),
- "/usr/local/games/steamapps",
- ];
- return candidates.FirstOrDefault(Directory.Exists);
- }
- private static string? FindSteamAppsWindows(string exeName)
- {
- try
- {
- var proc = Process.GetProcessesByName(exeName).FirstOrDefault();
- if (proc?.MainModule?.FileName is string exe)
- {
- var gameDir = "common\\" + GetGameFolderName(exeName) + "\\";
- var idx = exe.IndexOf(gameDir, StringComparison.OrdinalIgnoreCase);
- if (idx >= 0) return exe[..idx];
- }
- }
- catch { }
- return null;
- }
- private static string GetGameFolderName(string exeName) => exeName switch
- {
- "cs2" => "Counter-Strike Global Offensive",
- _ => exeName
- };
- }
|