forked from danarcher/XCom2ModTool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSteam.cs
101 lines (92 loc) · 3.63 KB
/
Steam.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
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using Microsoft.Win32;
namespace XCom2ModTool
{
internal static class Steam
{
private static readonly string AppsFolderName = "steamapps";
private static readonly string CommonFolderName = "common";
private static readonly string LibraryFoldersFileName = "libraryfolders.vdf";
private static readonly string WorkshopFolderName = "workshop";
private static readonly string WorkshopContentFolderName = "content";
private static readonly string PathDelimeter = "\"path\"";
public static string InstallPath { get; } = FindInstallPath();
public static string[] LibraryPaths { get; } = FindLibraryPaths();
private static string FindInstallPath()
{
// If we we had any "Any CPU" or x64 build, this could/would be under HKLM\Software\Wow6432Node\Valve\Steam.
using (var key = Registry.LocalMachine.OpenSubKey(@"Software\Valve\Steam"))
{
return (string)key.GetValue("InstallPath");
}
}
private static string[] FindLibraryPaths()
{
var path = Path.Combine(InstallPath, AppsFolderName, LibraryFoldersFileName);
var lines = File.ReadAllLines(path);
var paths = new List<string>();
foreach (var line in lines)
{
var pathIndex = line.IndexOf(PathDelimeter);
if (pathIndex >= 0)
{
var text = line.Substring(pathIndex + PathDelimeter.Length + 1);
text = text.Trim().Trim('\"').Replace("\\\\", "\\");
if (!string.IsNullOrEmpty(text))
{
paths.Add(text);
}
}
}
return paths.ToArray();
}
public static bool TryFindApp(string appName, out string path)
{
foreach (var libraryPath in LibraryPaths)
{
path = Path.Combine(libraryPath, AppsFolderName, CommonFolderName, appName);
if (Directory.Exists(path))
{
return true;
}
}
path = null;
return false;
}
public static string FindApp(string appName)
{
if (!TryFindApp(appName, out string path))
{
throw new DirectoryNotFoundException($"Steam install of {appName} not found");
}
return path;
}
public static IEnumerable<string> FindAppWorkshopPaths(int appId)
{
var pathsFound = new List<string>();
foreach (var libraryPath in LibraryPaths)
{
var path = Path.Combine(libraryPath, AppsFolderName, WorkshopFolderName, WorkshopContentFolderName, appId.ToString(CultureInfo.InvariantCulture));
if (Directory.Exists(path))
{
yield return path;
}
}
}
public static IEnumerable<string> FindAppWorkshopItemPaths(int appId)
{
foreach (var workshopPath in FindAppWorkshopPaths(appId))
{
foreach (var itemPath in Directory.EnumerateDirectories(workshopPath, "*", SearchOption.TopDirectoryOnly))
{
if (int.TryParse(Path.GetFileName(itemPath), NumberStyles.None, CultureInfo.InvariantCulture, out int itemNumber))
{
yield return itemPath;
}
}
}
}
}
}