-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathSteamLocator.cs
More file actions
197 lines (171 loc) · 6.02 KB
/
Copy pathSteamLocator.cs
File metadata and controls
197 lines (171 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
namespace HedgeModManager.Steam;
using System.Runtime.Versioning;
using Microsoft.Win32;
using Foundation;
using System.IO;
using System.Collections.Generic;
public class SteamLocator : IGameLocator
{
public string? SteamInstallPath { get; protected set; }
[SupportedOSPlatform("windows")]
private string? FindSteamLibraryWindows()
{
var key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default).OpenSubKey("SOFTWARE\\Wow6432Node\\Valve\\Steam") ??
RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default).OpenSubKey("SOFTWARE\\Valve\\Steam");
if (key != null)
{
var path = key.GetValue("InstallPath")?.ToString();
if (!string.IsNullOrEmpty(path))
{
if (Directory.Exists(path))
{
return path;
}
Logger.Warning($"Steam path found, but \"{path}\" does not exist!");
}
}
return null;
}
[SupportedOSPlatform("macos")]
private string? FindSteamLibraryMacOS()
{
// TODO: Open a folder selection window
return "";
}
private string? FindSteamLibraryUnix()
{
var pathList = new[]
{
Path.Combine(".steam", "steam"),
Path.Combine(".local", "share", "Steam"),
Path.Combine(".var", "app", "com.valvesoftware.Steam", ".steam", "steam"),
Path.Combine("snap", "steam", "common", ".steam", "steam"),
};
foreach (string path in pathList)
{
string steamPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), path);
// Due to lack of permissions with flatpak, we will check for a file we need
if (File.Exists(Path.Combine(steamPath, "steamapps", "libraryfolders.vdf")))
return steamPath;
}
return null;
}
public string? FindDefaultSteamLibrary()
{
if (SteamInstallPath == null)
{
if (OperatingSystem.IsWindows())
{
SteamInstallPath = FindSteamLibraryWindows();
return SteamInstallPath;
}
if (OperatingSystem.IsMacOS())
{
SteamInstallPath = FindSteamLibraryMacOS();
return SteamInstallPath;
}
SteamInstallPath = FindSteamLibraryUnix();
return SteamInstallPath;
}
return SteamInstallPath;
}
public static string? LocateSteamPrefix(string appid, IReadOnlyList<string> compatDirs)
{
if (!OperatingSystem.IsLinux())
return null;
foreach (var compatDir in compatDirs)
{
var protonDir = Path.Combine(compatDir, appid);
if (Directory.Exists(protonDir))
{
return Path.Combine(protonDir, "pfx");
}
}
if (compatDirs.Count > 0)
{
Logger.Debug($"Using default prefix for appid {appid}");
return Path.Combine(compatDirs[0], appid, "pfx");
}
Logger.Warning($"Could not locate Steam prefix for appid {appid}");
return null;
}
public List<SteamGame> Locate()
{
var games = new List<SteamGame>();
var library = FindDefaultSteamLibrary();
if (string.IsNullOrEmpty(library))
{
return games;
}
var folders = ValveDataFile.FromFile(Path.Combine(library, "steamapps", "libraryfolders.vdf"));
var compatDirs = new List<string>();
if (OperatingSystem.IsLinux())
{
var compatDataDir = Path.Combine(library, "steamapps", "compatdata");
if (Directory.Exists(compatDataDir))
{
compatDirs.Add(compatDataDir);
}
}
foreach (var folder in folders)
{
var path = folder.GetCaseInsensitive("path").GetString();
if (string.IsNullOrEmpty(path))
{
continue;
}
var apps = folder.GetCaseInsensitive("apps");
if (apps == null)
{
continue;
}
// Paths will be encoded by Windows Steam and will need to be adapted for macOS paths.
// C:\ is equivalent to the Wine prefix.
// Any other drive letter should be removed and treated as raw Unix path.
if (OperatingSystem.IsMacOS())
{
if (path.StartsWith(@"C:\"))
{
path = SteamInstallPath;
}
else
{
// Remove drive letter and replace slashes
path = path[2..].Replace(@"\", "/");
}
}
var libPath = Path.Combine(path, "steamapps");
foreach (var app in apps)
{
var appid = app.Name;
if (string.IsNullOrEmpty(appid))
{
continue;
}
try
{
var manifest = ValveDataFile.FromFile(Path.Combine(libPath, $"appmanifest_{appid}.acf"));
var name = manifest.GetCaseInsensitive("name").GetString();
var installDir = manifest.GetCaseInsensitive("installdir").GetString();
var root = Path.Combine(libPath, "common", installDir);
if (Directory.Exists(root))
{
games.Add(new SteamGame
{
ID = appid,
Name = name,
Root = root,
PrefixRoot = LocateSteamPrefix(appid, compatDirs)
});
}
}
catch
{
// ignore
}
}
}
return games;
}
IReadOnlyList<IGame> IGameLocator.Locate() => Locate();
}