-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.cpp
289 lines (221 loc) · 6.64 KB
/
utils.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include "StdAfx.h"
using namespace std;
/*
Steam API Interface
Developer: sk0r / Czybik
Version: v0.1 (Steam-API v015) 13th August, 2014
Contact: [email protected]
See readme.txt for more details
File: utils.cpp: Utilitiy functions
*/
//======================================================================
bool SetAppPath(char * lpszOut)
{
//Write application path to string
if (!lpszOut)
return FALSE;
//Get full file name
if (!GetModuleFileNameA(NULL, lpszOut, MAX_PATH))
return FALSE;
//Remove file name from string
for (size_t i = strlen(lpszOut); i >= 3; i--) {
if (lpszOut[i] == '\\')
break;
lpszOut[i] = 0;
}
return TRUE;
}
//======================================================================
//======================================================================
void ConsolePrint(char* lpszFmt, ...)
{
//Print text to console
char szFmtBuf[250];
//Format text
va_list vaList;
va_start(vaList, lpszFmt);
vsprintf_s(szFmtBuf, lpszFmt, vaList);
va_end(vaList);
//Pass to standard output
std::cout << szFmtBuf << std::endl;
}
//======================================================================
//======================================================================
void CNameChanger::SetInterface(ISteamFriends* pSteamFriends)
{
//Set pointer to Steam Friends interface
this->pSteamFriends = pSteamFriends;
}
//======================================================================
void CNameChanger::ReadConfig()
{
}
//======================================================================
int get_file_size(FILE *p_file)
{
fseek(p_file, 0, SEEK_END);
int size = ftell(p_file);
fseek(p_file, 0, SEEK_SET);
return size;
}
//======================================================================
//======================================================================
std::vector<std::string> split(std::string text, char sep) {
std::vector<std::string> tokens;
std::size_t start = 0, end = 0;
while ((end = text.find(sep, start)) != std::string::npos) {
tokens.push_back(text.substr(start, end - start));
start = end + 1;
}
tokens.push_back(text.substr(start));
return tokens;
}
//======================================================================
//======================================================================
void convertToLower(char *str)
{
for (int i = 0; i < strlen(str); i++)
str[i] = tolower(str[i]);
}
//======================================================================
//======================================================================
void CNameChanger::ReadNames()
{
FILE *fp = fopen("names.cfg", "rb");
int len = get_file_size(fp);
// Allocate buffer
char *fileBuffer = new char[len];
fread(fileBuffer, len, 1, fp); // Read file data to buffer
fclose(fp); // Close file
// Split buffer by lines
std::vector<std::string> lines = split(std::string(fileBuffer), '\n');
// Loop through the lines
for (auto curline : lines)
{
if (curline[0] == '#' || !strnicmp(curline.c_str(), "//", 2))
continue;
std::vector<std::string> expressions = split(curline, '=');
char *processName = strdup(expressions[0].c_str());
convertToLower(processName);
namecombinations.push_back({ processName, expressions[1] });
}
}
//======================================================================
//======================================================================
bool CNameChanger::SetStatus(bool bEnable)
{
//Set name change status
if ((bEnable) && (!pSteamFriends))
return FALSE;
//Set flag
bStatus = bEnable;
//If enabled, initialize timers
if (bStatus) {
ConsolePrint("Current Steam Friends name: %s", pSteamFriends->GetPersonaName());
dwCurTimer = dwLastTimer = GetTickCount();
}
return TRUE;
}
//======================================================================
//======================================================================
char *CNameChanger::GetName(char * game)
{
if (!strcmp(game, "<unknown>"))
return NULL;
for (auto process : namecombinations)
{
if (!strcmp(process.first.c_str(), game))
return strdup(process.second.c_str());
}
return "Failure";
}
//======================================================================
//======================================================================
bool CNameChanger::IsProcessListed(char * game)
{
for (auto process : namecombinations)
{
if (!strcmp(process.first.c_str(), game))
return true;
}
return false;
}
//======================================================================
//======================================================================
char *CNameChanger::GetCurrentName()
{
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
{
// Find default name, because this failed
return GetName("default");
}
for (int pid = 0; pid < i; pid++)
{
char *szProcessName = new char[MAX_PATH];
strcpy(szProcessName, "<unknown>");
// Get a handle to the process.
try
{
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, aProcesses[pid]);
// Get the process name.
if (NULL != hProcess)
{
HMODULE hMod;
DWORD cbNeeded;
if (EnumProcessModules(hProcess, &hMod, sizeof(hMod),
&cbNeeded))
{
GetModuleBaseNameA(hProcess, hMod, szProcessName,
MAX_PATH);
// 0 terminate string early
szProcessName[strlen(szProcessName) - 4] = '\0';
convertToLower(szProcessName);
}
}
if (IsProcessListed(szProcessName)) {
if(strcmp(szProcessName, lastGame)){
printf("Found running game: %s\n", szProcessName);
lastGame = strdup(szProcessName);
}
return GetName(szProcessName);
}
}
catch (int e){}
}
return GetName("default");
}
//======================================================================
//======================================================================
void CNameChanger::Think()
{
//Process name change
if (!bStatus)
return;
//Update timer
dwCurTimer = GetTickCount();
if (dwCurTimer > dwLastTimer + NC_TIMETOWAIT) { //If determined time has elapsed
//Update last timer
dwLastTimer = GetTickCount();
//Change name
char *pString = GetCurrentName();
char *pCurName = strdup(pSteamFriends->GetPersonaName());
if (pString) {
static bool isFirstTime = true;
if (oldName != NULL && !strcmp(oldName, pString))
return;
// Set the old name
oldName = strdup(pString);
if (!isFirstTime)
{
ConsolePrint("Changing Steam name from %s to %s.\n", oldName, pString);
pSteamFriends->SetPersonaName(pString);
}
isFirstTime = false;
}
}
}
//======================================================================