forked from LavaGang/MelonLoader.Installer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSteamHandler.cs
More file actions
115 lines (110 loc) · 4.61 KB
/
SteamHandler.cs
File metadata and controls
115 lines (110 loc) · 4.61 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
using Microsoft.Win32;
using System;
using System.IO;
using System.Text.RegularExpressions;
namespace MelonLoader
{
internal static class SteamHandler
{
internal static string GetFilePathFromAppId(string appid)
{
if (string.IsNullOrEmpty(appid))
return null;
string steaminstallpath = GetSteamInstallPath();
if (string.IsNullOrEmpty(steaminstallpath) || !Directory.Exists(steaminstallpath))
return null;
string steamappspath = Path.Combine(steaminstallpath, "steamapps");
if (!Directory.Exists(steamappspath))
return null;
string appmanifestfilename = ("appmanifest_" + appid + ".acf");
string appmanifestpath = Path.Combine(steamappspath, appmanifestfilename);
string installdir = ReadAppManifestInstallDir(appmanifestpath);
if (string.IsNullOrEmpty(installdir))
{
installdir = ReadLibraryFolders(appmanifestfilename, ref steamappspath);
if (string.IsNullOrEmpty(installdir))
return null;
}
if (!ScanForExe(steamappspath, installdir, out string filepath))
return null;
return filepath;
}
private static string ReadAppManifestInstallDir(string appmanifestpath)
{
if (!File.Exists(appmanifestpath))
return null;
string[] file_lines = File.ReadAllLines(appmanifestpath);
if (file_lines.Length <= 0)
return null;
string output = null;
foreach (string line in file_lines)
{
Match match = new Regex(@"""installdir""\s+""(.+)""").Match(line);
if (!match.Success)
continue;
output = match.Groups[1].Value;
break;
}
return output;
}
private static string ReadLibraryFolders(string appmanifestfilename, ref string steamappspath)
{
string libraryfoldersfilepath = Path.Combine(steamappspath, "libraryfolders.vdf");
if (!File.Exists(libraryfoldersfilepath))
return null;
string[] file_lines = File.ReadAllLines(libraryfoldersfilepath);
if (file_lines.Length <= 0)
return null;
string output = null;
foreach (string line in file_lines)
{
Match match = new Regex(@"""\d+""\s+""(.+)""").Match(line);
if (!match.Success)
continue;
string steamappspath2 = Path.Combine(match.Groups[1].Value.Replace(":\\\\", ":\\"), "steamapps");
if (!Directory.Exists(steamappspath2))
continue;
string installdir = ReadAppManifestInstallDir(Path.Combine(steamappspath2, appmanifestfilename));
if (string.IsNullOrEmpty(installdir))
continue;
steamappspath = steamappspath2;
output = installdir;
}
return output;
}
private static bool ScanForExe(string steamappspath, string installdir, out string filepath)
{
filepath = null;
string installpath = Path.Combine(steamappspath, "common", installdir);
if (!Directory.Exists(installpath))
return false;
string newfilepath = Path.Combine(installpath, (installdir + ".exe"));
if (File.Exists(newfilepath))
{
filepath = newfilepath;
return true;
}
newfilepath = Path.Combine(installpath, (installdir.Replace(" ", "") + ".exe"));
if (File.Exists(newfilepath))
{
filepath = newfilepath;
return true;
}
newfilepath = Path.Combine(installpath, installdir, (installdir + ".exe"));
if (File.Exists(newfilepath))
{
filepath = newfilepath;
return true;
}
newfilepath = Path.Combine(installpath, installdir, (installdir.Replace(" ", "") + ".exe"));
if (File.Exists(newfilepath))
{
filepath = newfilepath;
return true;
}
// Improve Exe Scanning
return false;
}
private static string GetSteamInstallPath() => Registry.LocalMachine.OpenSubKey(!Environment.Is64BitOperatingSystem ? "SOFTWARE\\Valve\\Steam" : "SOFTWARE\\Wow6432Node\\Valve\\Steam")?.GetValue("InstallPath")?.ToString();
}
}