/
SteamSourceResolver.cs
159 lines (123 loc) · 4.32 KB
/
SteamSourceResolver.cs
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
#region Copyright & License Information
/*
* Copyright (c) The OpenRA Developers and Contributors
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using Microsoft.Win32;
namespace OpenRA.Mods.Common.Installer
{
public class SteamSourceResolver : ISourceResolver
{
public string FindSourcePath(ModContent.ModSource modSource)
{
var appId = modSource.Type.NodeWithKeyOrDefault("AppId");
if (appId == null)
return null;
foreach (var steamDirectory in SteamDirectory())
{
var manifestPath = Path.Combine(steamDirectory, "steamapps", $"appmanifest_{appId.Value.Value}.acf");
if (!File.Exists(manifestPath))
continue;
var data = ParseGameManifest(manifestPath);
if (!data.TryGetValue("StateFlags", out var stateFlags) || stateFlags != "4")
continue;
if (!data.TryGetValue("installdir", out var installDir))
continue;
if (installDir == null)
continue;
var path = Path.Combine(steamDirectory, "steamapps", "common", installDir);
if (InstallerUtils.IsValidSourcePath(path, modSource))
return path;
}
return null;
}
public Availability GetAvailability()
{
return Availability.DigitalInstall;
}
static IEnumerable<string> SteamDirectory()
{
var candidatePaths = new List<string>();
switch (Platform.CurrentPlatform)
{
case PlatformType.Windows:
{
// We need an extra check for the platform here to silence a warning when the registry is accessed
// TODO: Remove this once our platform checks use the same method
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
break;
var prefixes = new[] { "HKEY_LOCAL_MACHINE\\Software\\", "HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\" };
foreach (var prefix in prefixes)
{
if (Registry.GetValue($"{prefix}Valve\\Steam", "InstallPath", null) is string path)
candidatePaths.Add(path);
}
break;
}
case PlatformType.OSX:
candidatePaths.Add(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Library", "Application Support", "Steam"));
break;
case PlatformType.Linux:
// Direct distro install
candidatePaths.Add(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".steam", "root"));
// Flatpak installed via Flathub
candidatePaths.Add(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".var", "app", "com.valvesoftware.Steam", ".steam", "root"));
break;
}
foreach (var libraryPath in candidatePaths.Where(Directory.Exists))
{
yield return libraryPath;
var libraryFoldersPath = Path.Combine(libraryPath, "steamapps", "libraryfolders.vdf");
if (!File.Exists(libraryFoldersPath))
continue;
foreach (var e in ParseLibraryManifest(libraryFoldersPath).Where(e => e.Item1 == "path"))
yield return Unescape(e.Item2);
}
}
static string Unescape(string path)
{
return path.Replace(@"\\", @"\");
}
static Dictionary<string, string> ParseGameManifest(string path)
{
var regex = new Regex("^\\s*\"(?<key>[^\"]*)\"\\s*\"(?<value>[^\"]*)\"\\s*$");
var result = new Dictionary<string, string>();
using (var s = new FileStream(path, FileMode.Open))
{
foreach (var line in s.ReadAllLines())
{
var match = regex.Match(line);
if (match.Success)
result[match.Groups["key"].Value] = match.Groups["value"].Value;
}
}
return result;
}
static List<Tuple<string, string>> ParseLibraryManifest(string path)
{
var regex = new Regex("^\\s*\"(?<key>[^\"]*)\"\\s*\"(?<value>[^\"]*)\"\\s*$");
var result = new List<Tuple<string, string>>();
using (var s = new FileStream(path, FileMode.Open))
{
foreach (var line in s.ReadAllLines())
{
var match = regex.Match(line);
if (match.Success)
result.Add(new Tuple<string, string>(match.Groups["key"].Value, match.Groups["value"].Value));
}
}
return result;
}
}
}