forked from Foohy/wheelofsteamgames
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSteam.cs
281 lines (207 loc) · 8.77 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Steam4NET;
using OlegEngine;
namespace WheelOfSteamGames
{
class Steam
{
public static ISteam006 steam { get; private set; }
public static ISteamClient009 SteamClient { get; private set; }
public static IClientEngine ClientEngine { get; private set; }
public static int HSteamPipe { get; private set; }
public static int HSteamUser { get; private set; }
public static IClientUser ClientUser { get; private set; }
public static IClientApps ClientApps { get; private set; }
public delegate bool ReturnCriteria(TSteamApp steamApp);
public struct App
{
public string Name { get; set; }
public uint AppID { get; set; }
public string Icon { get; set; }
}
public static void Initialize()
{
try
{
InitSteam2();
InitSteam3();
PostInitSteam2();
}
catch (Exception ex )
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ex.Message);
Console.ResetColor();
}
CallbackDispatcher.SpawnDispatchThread(HSteamPipe);
}
public static string GetAppData(uint appId, string key)
{
StringBuilder sb = new StringBuilder(255);
ClientApps.GetAppData(appId, key, sb);
return sb.ToString();
}
public static bool GetAppInternal(uint appId, out TSteamApp app)
{
TSteamError error = new TSteamError();
app = new TSteamApp()
{
uMaxNameChars = 255,
uMaxCurrentVersionLabelChars = 255,
uMaxLatestVersionLabelChars = 255,
uMaxInstallDirNameChars = 255,
szName = new string('\0', 255),
szCurrentVersionLabel = new string('\0', 255),
szLatestVersionLabel = new string('\0', 255),
szInstallDirName = new string('\0', 255),
};
if (steam.EnumerateApp(appId, ref app, ref error) == 0)
{
Console.WriteLine("ISteam006::EnumerateApp( {0} ) Failed: {1}", appId, error.szDesc);
return false;
}
return true;
}
public static bool GetAppIds(out uint[] appIds)
{
TSteamError error = new TSteamError();
TSteamAppStats stats = new TSteamAppStats();
appIds = null;
if (steam.GetAppStats(ref stats, ref error) == 0)
{
Console.WriteLine("ISteam006::GetAppStats() Failed: {0}", error.szDesc);
return false;
}
appIds = new uint[stats.uNumApps];
if (steam.GetAppIds(ref appIds, (uint)appIds.Length, ref error) == 0)
{
Console.WriteLine("ISteam006::GetAppIds( {0} ) Failed: {1}", appIds.Length, error.szDesc);
return false;
}
return true;
}
public static bool IsAppSubscribed(uint appId)
{
TSteamError error = new TSteamError();
int bSubscribed = 0;
int reserved = 0;
if (steam.IsAppSubscribed(appId, ref bSubscribed, ref reserved, ref error) == 0)
{
Console.WriteLine("ISteam006::IsAppSubscribed( {0} ) Failed: {1} ", appId, error.szDesc);
return false;
}
return bSubscribed == 1;
}
public static App[] GetSubscribedApps(ReturnCriteria rc)
{
List<App> appList = new List<App>();
uint[] appIDs;
if (!GetAppIds(out appIDs))
return null;
foreach (uint appID in appIDs)
{
if (!IsAppSubscribed(appID))
continue;
TSteamApp steamApp;
if (!GetAppInternal(appID, out steamApp))
continue;
if (rc != null)
{
if (!rc(steamApp))
continue;
}
string icon = GetAppData(appID, "logo");
App stmApp = new App()
{
Name = steamApp.szName,
AppID = appID,
Icon = icon,
};
appList.Add(stmApp);
}
return appList.ToArray();
}
public static App[] RefreshGames()
{
App[] apps = Steam.GetSubscribedApps((app) =>
{
// hide anything that isn't an installable game
if (Steam.GetAppData(app.uId, "gamedir") == "")
return false;
string state = Steam.GetAppData(app.uId, "state");
// hide sdks
if (state == "eStateTool")
return false;
// hide unavailable
if (state == "eStateUnAvailable")
return false;
// hide demos
if (Steam.GetAppData(app.uId, "DemoOfAppID") != "")
return false;
// hide dlc
if (Steam.GetAppData(app.uId, "DLCForAppID") != "")
return false;
// hide movies
if (Steam.GetAppData(app.uId, "IsMediaFile") == "1")
return false;
return true;
});
return apps;
}
static void InitSteam2()
{
Console.WriteLine("Initializing Steam2...");
if (!Steamworks.LoadSteam())
throw new InvalidOperationException("Unable to load steam.dll");
steam = Steamworks.CreateSteamInterface<ISteam006>();
if (steam == null)
throw new InvalidOperationException("Unable to get ISteam006.");
TSteamError error = new TSteamError();
if (steam.Startup(0, ref error) == 0)
throw new InvalidOperationException("Unable to startup steam interface: " + error.szDesc);
Console.WriteLine("Steam2 startup success." + Environment.NewLine);
}
static void InitSteam3()
{
Console.WriteLine("Initializing Steam3...");
if (!Steamworks.LoadSteamClient())
throw new InvalidOperationException("Unable to load steamclient.dll");
SteamClient = Steamworks.CreateInterface<ISteamClient009>();
ClientEngine = Steamworks.CreateInterface<IClientEngine>();
if (SteamClient == null || ClientEngine == null)
throw new InvalidOperationException("Unable to get required steamclient interfaces.");
HSteamPipe = SteamClient.CreateSteamPipe();
HSteamUser = SteamClient.ConnectToGlobalUser(HSteamPipe);
if (HSteamUser == 0 || HSteamPipe == 0)
throw new InvalidOperationException("Unable to connect to global user.");
ClientApps = ClientEngine.GetIClientApps<IClientApps>(HSteamUser, HSteamPipe);
ClientUser = ClientEngine.GetIClientUser<IClientUser>(HSteamUser, HSteamPipe);
if (ClientApps == null || ClientUser == null)
throw new InvalidOperationException("Unable to get required interfaces.");
Console.WriteLine("Steam3 startup success." + Environment.NewLine);
}
static void PostInitSteam2()
{
Console.WriteLine("Getting account name...");
StringBuilder accName = new StringBuilder(255);
if (!ClientUser.GetAccountName(accName))
throw new InvalidOperationException("Unable to startup steam interface.");
Console.WriteLine("Account = \"{0}\"", accName.ToString());
TSteamError error = new TSteamError();
Console.WriteLine("ISteam006::SetUser( \"{0}\" )...", accName.ToString());
int bUserSet = 0;
uint setUserHandle = steam.SetUser(accName.ToString(), ref bUserSet, ref error);
Console.WriteLine("SetUserHandle = {0}", setUserHandle);
if (setUserHandle == 0)
throw new InvalidOperationException("Unable to get SetUser call handle.");
Console.WriteLine("ISteam006::BlockingCall( {0} )...", setUserHandle);
if (steam.BlockingCall(setUserHandle, 100, ref error) == 0)
throw new InvalidOperationException("Unable to process SetUser call: " + error.szDesc);
Console.WriteLine("User set!" + Environment.NewLine);
}
}
}