-
Notifications
You must be signed in to change notification settings - Fork 5
/
steamapi.cpp
273 lines (212 loc) · 7.29 KB
/
steamapi.cpp
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
#include "StdAfx.h"
/*
Steam API Interface
Developer: sk0r / Czybik
Version: v0.1 (Steam-API v015) 13th August, 2014
Contact: Czybik_Stylez@gmx.de
See readme.txt for more details
File: steamapi.cpp: SteamAPI access implementation
*/
//======================================================================
InitResult CAccessSteamAPI::Initialize(LPCSTR lpszAppPath)
{
//Initialize Steam API
if (irStatus == IR_SUCCESS)
return IR_SUCCESS;
if (!lpszAppPath)
return IR_PARAM;
char szAPIModule[MAX_PATH];
char szAppTxt[MAX_PATH];
//Format strings
sprintf_s(szAPIModule, "%s" STEAMAPI_MODULE, lpszAppPath);
sprintf_s(szAppTxt, "%s" STEAMAPPID_NAME, lpszAppPath);
//Check for a steam_appid.txt. Create the file with a default AppId if not exists
if (!FileExists(szAppTxt)) {
if (!CreateSteamAppIdFile(lpszAppPath)) {
return IR_APPID;
}
}
//Load module
hSteamAPIDll = LoadLibraryA(szAPIModule);
if (!hSteamAPIDll)
return IR_LOADLIB;
//Initialize access functions
#define RETRIEVE_ACCADDRESS(fnc, n) sAccTable.pfn##fnc = (Tpfn##fnc)GetProcAddress(hSteamAPIDll, n); \
if (!sAccTable.pfn##fnc) { FreeLibrary(hSteamAPIDll); return IR_SACC; }
RETRIEVE_ACCADDRESS(SteamAPI_Init, "SteamAPI_Init");
RETRIEVE_ACCADDRESS(SteamAPI_IsSteamRunning, "SteamAPI_IsSteamRunning");
RETRIEVE_ACCADDRESS(SteamAPI_Shutdown, "SteamAPI_Shutdown");
//Call function to let SteamAPI initialize and connect with Steam
if (!sAccTable.pfnSteamAPI_Init()) {
FreeLibrary(hSteamAPIDll);
return IR_INIT;
}
//Initialize API functions
#define RETRIEVE_APIADDRESS(cls, n) sApiTable.p##cls = (Tpfn##cls)GetProcAddress(hSteamAPIDll, n); \
if (!sApiTable.p##cls) { FreeLibrary(hSteamAPIDll); return IR_SAPI; }
RETRIEVE_APIADDRESS(ISteamUser, "SteamUser");
RETRIEVE_APIADDRESS(ISteamFriends, "SteamFriends");
RETRIEVE_APIADDRESS(ISteamUtils, "SteamUtils");
RETRIEVE_APIADDRESS(ISteamMatchmaking, "SteamMatchmaking");
RETRIEVE_APIADDRESS(ISteamUserStats, "SteamUserStats");
RETRIEVE_APIADDRESS(ISteamApps, "SteamApps");
RETRIEVE_APIADDRESS(ISteamMatchmakingServers, "SteamMatchmakingServers");
//Success
return irStatus = IR_SUCCESS;
}
//======================================================================
//======================================================================
BOOL CAccessSteamAPI::CreateSteamAppIdFile(LPCSTR lpszAppPath)
{
//Create a valid steam_appid.txt
char szFileName[MAX_PATH];
char szAppId[25];
DWORD dwBytesWritten;
//Format strings
sprintf_s(szFileName, "%s" STEAMAPPID_NAME, lpszAppPath);
sprintf_s(szAppId, "%d", STEAMAPPID_DEFAULT);
//Create new file
HANDLE hFile = CreateFileA(szFileName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile == INVALID_HANDLE_VALUE)
return FALSE;
//Write buffer to file
if ((!WriteFile(hFile, szAppId, strlen(szAppId), &dwBytesWritten, NULL)) || (dwBytesWritten != strlen(szAppId))) {
CloseHandle(hFile);
return FALSE;
}
//Close handle
CloseHandle(hFile);
return TRUE;
}
//======================================================================
//======================================================================
BOOL CAccessSteamAPI::Shutdown(VOID)
{
//Shutdown Steam API
if (irStatus != IR_SUCCESS)
return FALSE;
//Call Steam API shutdown function
sAccTable.pfnSteamAPI_Shutdown();
//Clear variable
irStatus = IR_NULL;
//Free library
return FreeLibrary(hSteamAPIDll);
}
//======================================================================
//======================================================================
BOOL CAccessSteamAPI::IsSteamRunning(VOID)
{
//Check if Steam is currently running
if (irStatus != IR_SUCCESS)
return FALSE;
return sAccTable.pfnSteamAPI_IsSteamRunning() == true;
}
//======================================================================
//======================================================================
ISteamUser* CAccessSteamAPI::GetSteamUserInterface(VOID)
{
//Return address of 'ISteamUser' class instance
if (irStatus != IR_SUCCESS)
return NULL;
return sApiTable.pISteamUser();
}
//======================================================================
//======================================================================
ISteamFriends* CAccessSteamAPI::SteamFriendsInterface(VOID)
{
//Return address of 'ISteamFriends' class instance
if (irStatus != IR_SUCCESS)
return NULL;
return sApiTable.pISteamFriends();
}
//======================================================================
//======================================================================
ISteamUtils* CAccessSteamAPI::SteamUtilsInterface(VOID)
{
//Return address of 'ISteamUtils' class instance
if (irStatus != IR_SUCCESS)
return NULL;
return sApiTable.pISteamUtils();
}
//======================================================================
//======================================================================
ISteamMatchmaking* CAccessSteamAPI::SteamMatchmakingInterface(VOID)
{
//Return address of 'ISteamMatchmaking' class instance
if (irStatus != IR_SUCCESS)
return NULL;
return sApiTable.pISteamMatchmaking();
}
//======================================================================
//======================================================================
ISteamUserStats* CAccessSteamAPI::SteamUserStatsInterface(VOID)
{
//Return address of 'ISteamUserStats' class instance
if (irStatus != IR_SUCCESS)
return NULL;
return sApiTable.pISteamUserStats();
}
//======================================================================
//======================================================================
ISteamApps* CAccessSteamAPI::SteamAppsInterface(VOID)
{
//Return address of 'ISteamApps' class instance
if (irStatus != IR_SUCCESS)
return NULL;
return sApiTable.pISteamApps();
}
//======================================================================
//======================================================================
ISteamMatchmakingServers* CAccessSteamAPI::SteamMatchmakingServers(VOID)
{
//Return address of 'ISteamMatchmakingServers' class instance
if (irStatus != IR_SUCCESS)
return NULL;
return sApiTable.pISteamMatchmakingServers();
}
//======================================================================
//======================================================================
LPCSTR CAccessSteamAPI::InitResultToString(InitResult irValue)
{
//Get description of InitResult value
switch (irValue) {
case IR_SUCCESS:
return "IR_SUCCESS";
break;
case IR_PARAM:
return "IR_PARAM";
break;
case IR_APPID:
return "IR_APPID";
break;
case IR_LOADLIB:
return "IR_LOADLIB";
break;
case IR_SACC:
return "IR_SACC";
break;
case IR_INIT:
return "IR_INIT";
break;
case IR_SAPI:
return "IR_SAPI";
break;
default:
return "<undefined>";
break;
}
return "IR_NULL";
}
//======================================================================
//======================================================================
BOOL CAccessSteamAPI::FileExists(LPCSTR lpszFileName)
{
//Check if a specified file exists
HANDLE hFile = CreateFileA(lpszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile != INVALID_HANDLE_VALUE) {
CloseHandle(hFile);
return TRUE;
}
return FALSE;
}
//======================================================================