This repository has been archived by the owner on Nov 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSteamClient.cs
217 lines (184 loc) · 7.95 KB
/
SteamClient.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using Microsoft.Win32;
using Microsoft.Win32.SafeHandles;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;
using System.IO;
using System.Linq.Expressions;
/// <summary>
/// Provides methods for interacting with a Steam Client instance.
/// </summary>
public class SteamClient
{
[DllImport("User32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint dwProcessId);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
static extern bool SetWindowText(IntPtr hWnd, string lpString);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("Kernel32.dll")]
static extern uint SuspendThread(IntPtr hThread);
[DllImport("Kernel32.dll")]
static extern uint ResumeThread(IntPtr hThread);
[DllImport("Kernel32.dll")]
static extern IntPtr OpenThread(uint dwDesiredAccess, bool bInheritHandle, uint dwThreadId);
[DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
static extern IntPtr CreateEvent(IntPtr lpEventAttributes, bool bManualReset, bool bInitialState, IntPtr lpName);
[DllImport("Kernel32.dll")]
static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);
[DllImport("Kernel32.dll")]
static extern bool CloseHandle(IntPtr hObject);
[DllImport("Advapi32.dll")]
static extern long RegNotifyChangeKeyValue(SafeRegistryHandle hKey, bool bWatchSubtree, uint dwNotifyFilter, IntPtr hEvent, bool fAsynchronous);
/// <summary>
/// Obtain a running Steam client instance.
/// </summary>
/// <returns>Any currently running Steam Client instance, null if no instance is running.</returns>
public static Process GetInstance()
{
if (GetWindowThreadProcessId(FindWindow("vguiPopupWindow", "SteamClient"), out uint dwProcessId) != 0)
return Process.GetProcessById((int)dwProcessId);
return null;
}
/// <summary>
/// Obtains installed Steam applications with their App ID and name.
/// </summary>
/// <returns>
/// A dictionary of installed Steam applications.
/// </returns>
public static Dictionary<string, string> GetApps()
{
Dictionary<string, string> apps = [];
using RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Valve\\Steam\\Apps");
if (registryKey != null)
{
string[] subKeyNames = registryKey.GetSubKeyNames();
for (int i = 0; i < subKeyNames.Length; i++)
{
using RegistryKey subKey = registryKey.OpenSubKey(subKeyNames[i]);
if ((int)subKey.GetValue("Installed", 0) == 1 &&
subKey.GetValueNames().Contains("Name"))
apps[subKey.GetValue("Name").ToString()] = subKeyNames[i];
}
}
return apps;
}
/// <summary>
/// Initializes a new Steam Client instance for the class.
/// </summary>
/// <returns>
/// An instance of the launched Steam Client instance or null if a launched instance already exists or Steam isn't installed.
/// </returns>
public static Process Launch()
{
using RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Valve\\Steam");
string steamExe = registryKey.GetValue("SteamExe").ToString();
if (FindWindow("vguiPopupWindow", "SteamClient") != IntPtr.Zero || !File.Exists(steamExe))
return null;
Process process;
if (GetWindowThreadProcessId(FindWindow("vguiPopupWindow", "Untitled"), out uint dwProcessId) != 0)
using (process = Process.GetProcessById((int)dwProcessId))
{
Process.Start(steamExe, "-shutdown").Dispose();
process.WaitForExit();
}
process = Process.Start(steamExe, $"-silent -cef-single-process -cef-in-process-gpu -cef-disable-d3d11 -cef-disable-breakpad");
IntPtr hWnd;
while ((hWnd = FindWindow("vguiPopupWindow", "Untitled")) == IntPtr.Zero)
Thread.Sleep(1);
SetWindowText(hWnd, "SteamClient");
WebHelper(false);
return process;
}
/// <summary>
/// Uninitializes the current running Steam Client instance.
/// </summary>
/// <returns>If the operation was successful true is returned else false.</returns>
public static bool Shutdown()
{
if (FindWindow("vguiPopupWindow", "SteamClient") != IntPtr.Zero)
{
WebHelper(true);
using RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Valve\\Steam");
Process.Start(registryKey.GetValue("SteamExe").ToString(), "-shutdown").Dispose();
}
return true;
}
/// <summary>
/// Disables or enables the Steam WebHelper.
/// </summary>
/// <param name="enable">Pass true to enable or false to disable the Steam WebHelper.</param>
/// <returns>If the operation was successful true is returned else false.</returns>
public static bool WebHelper(bool enable)
{
IntPtr
hWnd = FindWindow("vguiPopupWindow", "SteamClient"),
hThread = OpenThread(0x0002, false, GetWindowThreadProcessId(hWnd, out uint _));
if (hWnd != IntPtr.Zero)
{
if (enable) ResumeThread(hThread);
else SuspendThread(hThread);
Process[] processes = Process.GetProcessesByName("steamwebhelper");
for (int i = 0; i < processes.Length; i++)
{
processes[i].Kill();
processes[i].Dispose();
}
}
CloseHandle(hThread);
return hWnd != IntPtr.Zero;
}
/// <summary>
/// Runs the specified App ID.
/// </summary>
/// <param name="gameId">App ID of the app to run.</param>
/// <returns>
/// If the operation was successful true is returned else false.
/// </returns>
public static bool RunGameId(string gameId)
{
if (FindWindow("vguiPopupWindow", "SteamClient") == IntPtr.Zero)
return false;
using RegistryKey registryKey = Registry.CurrentUser.OpenSubKey($"SOFTWARE\\Valve\\Steam\\Apps\\{gameId}");
IntPtr hEvent = CreateEvent(IntPtr.Zero, true, false, IntPtr.Zero);
WebHelper(true);
Process.Start("explorer.exe", $"steam://rungameid/{gameId}").Close();
RegNotifyChangeKeyValue(registryKey.Handle, true, 0x00000004, hEvent, true);
WaitForSingleObject(hEvent, 0xffffffff);
WebHelper(false);
RegNotifyChangeKeyValue(registryKey.Handle, true, 0x00000004, hEvent, true);
WaitForSingleObject(hEvent, 0xffffffff);
CloseHandle(hEvent);
return true;
}
/// <summary>
/// Obtains installed Steam applications with their App ID and name for the currently signed in user.
/// </summary>
/// <returns>
/// A dictionary of installed Steam applications for the currently signed in user.
/// </returns>
public static Dictionary<string, string> GetAppsForUser()
{
Dictionary<string, string> apps = GetApps(), userApps = []; ;
using RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Valve\\Steam");
using RegistryKey subKey = registryKey.OpenSubKey("ActiveProcess");
string[] lines = File.ReadAllLines($"{registryKey.GetValue("SteamPath")}/userdata/{subKey.GetValue("ActiveUser")}/config/localconfig.vdf");
for (int i = 0; i < lines.Length; i++)
{
try
{
KeyValuePair<string, string> keyValuePair = new();
string line = lines[i].Trim().Trim('"');
Convert.ToUInt32(line);
keyValuePair = apps.First(source => source.Value == line);
userApps.Add(keyValuePair.Key, keyValuePair.Value);
}
catch (FormatException) { }
catch (InvalidOperationException) { }
}
return userApps;
}
}