summaryrefslogtreecommitdiff
path: root/protocols/Xfire/src/Xfire_game.cpp
blob: 7d10b769c3fcfa46ae1807c9faaf732a8479143f (plain)
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#include "stdafx.h"

#include "Xfire_game.h"
#include "pwd_dlg.h" //passwort eingabe dlg

//prüft nach, ob das game das nötige extragameargs im launcherstring hat
BOOL Xfire_game::haveExtraGameArgs() {
	//kein launcher stirng, dann abbruch
	if (!this->launchparams)
		return FALSE;

	//wenn platzhalter vorhanden, dann TRUE zurück
	if (this->inString(this->launchparams, "%UA_LAUNCHER_EXTRA_ARGS%"))
		return TRUE;

	return FALSE;
}

//startes das spiel
BOOL Xfire_game::start_game(char*ip, unsigned int port, char*pw) {
	//launchparam prüfen ob gefüllt?
	if (this->launchparams == NULL)
		return FALSE;

	//ist launchparam großgenug für eibne urlprüfung?
	if (mir_strlen(this->launchparams) > 5)
	{
		//launchparams ne url? dann openurl funktion von miranda verwenden
		if (this->launchparams[0] == 'h'&&
			this->launchparams[1] == 't'&&
			this->launchparams[2] == 't'&&
			this->launchparams[3] == 'p'&&
			this->launchparams[4] == ':')
		{
			Utils_OpenUrl(this->launchparams);
			return 0;
		}
	}

	//spiel hat netwerkparameter?
	int networksize = 0;
	char* mynetworkparams = NULL;

	if (this->networkparams)
	{
		if (ip)
		{
			char portstr[6] = "";
			int pwsize = 255;

			//größe des netzwerparams berechnen
			if (this->pwparams)
				pwsize += mir_strlen(this->pwparams);

			mynetworkparams = new char[mir_strlen(this->networkparams) + pwsize];
			strcpy_s(mynetworkparams, mir_strlen(this->networkparams) + pwsize, this->networkparams);

			//port begrenzen
			port = port % 65535;
			//port in string wandeln
			mir_snprintf(portstr, _countof(portstr), "%d", port);

			str_replace(mynetworkparams, "%UA_GAME_HOST_NAME%", ip);
			str_replace(mynetworkparams, "%UA_GAME_HOST_PORT%", portstr);

			//passwort dialog, nur wenn SHIFT gehalten wird beim join, da sonst immer gefragt wird
			if (GetAsyncKeyState(VK_LSHIFT) && this->pwparams){
				char password[256] = ""; //passwort maximal 255 zeichen

				if (ShowPwdDlg(password)) {
					char* mypwargs = new char[pwsize];

					//speicher frei?
					if (mypwargs != NULL) {
						strcpy_s(mypwargs, pwsize, this->pwparams);
						str_replace(mypwargs, "%UA_GAME_HOST_PASSWORD%", password);
						str_replace(mynetworkparams, "%UA_LAUNCHER_PASSWORD_ARGS%", mypwargs);
						delete[] mypwargs;
					}
					else
						str_replace(mynetworkparams, "%UA_LAUNCHER_PASSWORD_ARGS%", "");
				}
				else
				{
					str_replace(mynetworkparams, "%UA_LAUNCHER_PASSWORD_ARGS%", "");
				}
			}
			else
				str_replace(mynetworkparams, "%UA_LAUNCHER_PASSWORD_ARGS%", "");

			//bestimmte felder erstmal leer ersetzen			
			str_replace(mynetworkparams, "%UA_LAUNCHER_RCON_ARGS%", "");
		}

		if (mynetworkparams)
			networksize = mir_strlen(mynetworkparams) + mir_strlen(this->networkparams);
	}

	//extra parameter
	int extraparamssize = 0;
	if (this->extraparams)
	{
		extraparamssize = mir_strlen(this->extraparams);
	}

	//temporäres array anlegen
	char*temp = NULL;
	temp = new char[mir_strlen(this->launchparams) + networksize + extraparamssize + 1];

	if (temp == NULL)
	{
		//wenn nwparams gesetzt, leeren
		if (mynetworkparams)
			delete[] mynetworkparams;

		return FALSE;
	}

	//launcherstring ins temporäre array
	strcpy_s(temp, mir_strlen(this->launchparams) + 1, this->launchparams);

	//netzwerkparameter ?
	if (mynetworkparams)
	{
		str_replace(temp, "%UA_LAUNCHER_NETWORK_ARGS%", mynetworkparams);
		delete[] mynetworkparams;
	}
	else
		str_replace(temp, "%UA_LAUNCHER_NETWORK_ARGS%", "");

	if (this->extraparams)
		str_replace(temp, "%UA_LAUNCHER_EXTRA_ARGS%", this->extraparams);
	else
		str_replace(temp, "%UA_LAUNCHER_EXTRA_ARGS%", "");

	//auf createprocess umgebaut
	STARTUPINFOA         si = { sizeof(si) };
	PROCESS_INFORMATION  pi;

	// MessageBoxA(NULL,temp,temp,0);

	//starten
	if (CreateProcessA(0, temp, 0, 0, FALSE, 0, 0, GetLaunchPath(temp), &si, &pi) == 0)
	{
		//schlug fehl, dann runas methode verwenden
		char*exe = strrchr(temp, '\\');
		if (exe == 0)
		{
			delete[] temp;
			return FALSE;
		}
		*exe = 0;
		exe++;
		char*params = strchr(exe, '.');
		if (params != 0)
		{
			params = strchr(params, ' ');
			if (params != 0)
			{
				*params = 0;
				params++;
			}
		}


		SHELLEXECUTEINFOA sei = { 0 };
		sei.cbSize = sizeof(sei);
		sei.hwnd = NULL;
		sei.lpVerb = "runas";
		sei.lpFile = exe;
		sei.lpParameters = params;
		sei.lpDirectory = temp;
		sei.nShow = SW_SHOWNORMAL;
		ShellExecuteExA(&sei);
	}
	delete[] temp;
	return TRUE;
}

//prüft den übergebenen pfad, ob es dem spiel vergleichbar
BOOL Xfire_game::checkpath(PROCESSENTRY32* processInfo)
{
	//gibts net, weg mit dir
	if (this->path == NULL)
		return FALSE;

	//versuche ein processhandle des speils zubekommen
	HANDLE op = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processInfo->th32ProcessID);

	if (op)
	{
		//varaibele wohin der pfad eingelesen wird
		TCHAR fpath[MAX_PATH] = _T("");

		//lese den pfad des spiels aus
		GetModuleFileNameEx(op, NULL, fpath, _countof(fpath));

		//8.3 pfade umwandeln, nur wenn sich eine tilde im string befindet
		if (_tcschr(fpath, '~'))
			GetLongPathName(fpath, fpath, _countof(fpath));

		//alles in kelinbuchstaben umwandeln
		this->strtolowerT(fpath);

		if (this->wildcmp(_A2T(this->path), fpath))
			//if (mir_strcmp(this->path,fpath)==0)
		{
			//pfad stimmt überein, commandline prüfen
			if (checkCommandLine(op, this->mustcontain, this->notcontain))
			{
				//handle zuamachen
				CloseHandle(op);
				//positive antwort an die gamedetection
				return TRUE;
			}
		}
		else //prüfe den multipfad
		{
			int size = mpath.size();
			for (int j = 0; j < size; j++)
			{
				if (mir_tstrcmpi(_A2T(mpath.at(j)), fpath) == 0)
				{
					//pfad stimmt überein, commandline prüfen
					if (checkCommandLine(op, this->mustcontain, this->notcontain))
					{
						//handle zumachen
						CloseHandle(op);
						//positive antwort an die gamedetection
						return TRUE;
					}
				}
			}
		}
		//is nich das game, handle zumachen
		CloseHandle(op);
	}
	else //if (this->mustcontain==NULL&&this->notcontain==NULL) //spiele die was bestimmtes im pfad benötigen skippen
	{
		char* exename = strrchr(this->path, '\\') + 1;

		//kleiner fix bei fehlerhaften pfaden kann keine exe ermittelt werden also SKIP
		if ((unsigned int)exename == 0x1)
			return FALSE;

		//vergleich die exenamen
		if (_stricmp(exename, _T2A(processInfo->szExeFile)) == 0)
		{
			return TRUE;
		}
		else //anderen pfade des games noch durchprüfen
		{
			int size = mpath.size();
			for (int j = 0; j < size; j++)
			{
				//exenamen rausfischen
				char* exename = strrchr(mpath.at(j), '\\') + 1;

				//mhn keien exe, nächsten pfad prüfen
				if ((unsigned int)exename == 0x1)
					continue;

				//exe vergleichen
				if (_stricmp(exename, _T2A(processInfo->szExeFile)) == 0)
				{
					//positive antwort an die gamedetection
					return TRUE;
				}
			}
		}
	}

	//keine übereinstimmung
	return FALSE;
}

//icondaten setzen
void Xfire_game::setIcon(HICON hicon, HANDLE handle) {
	this->hicon = hicon;
	this->iconhandl = handle;
}

//liest gamewerte aus der db
void Xfire_game::readFromDB(unsigned dbid)
{
	//lese alle string werte aus der db und befülle die passenden variablen damit
	this->readStringfromDB("gamepath", dbid, &this->path);

	//8.3 fix, prüfe auf ~ pfad, wenn ja pfad var umwalnd in longname
	if (this->path)
	{
		BOOL found = FALSE;
		for (unsigned int i = 0; i < mir_strlen(this->path); i++)
		{
			if (this->path[i] == '~')
			{
				found = TRUE;
				break;
			}
		}
		//gefunden? dann stirng wandeln und in pfad speichern
		if (found) {
			char ctemp[MAX_PATH] = "";
			strcpy_s(ctemp, MAX_PATH, this->path);
			GetLongPathNameA(ctemp, ctemp, sizeof(ctemp));
			this->strtolower(ctemp);
			this->setString(ctemp, &this->path);
		}
	}

	this->readStringfromDB("gamelaunch", dbid, &this->launchparams);
	this->readStringfromDB("gamenetargs", dbid, &this->networkparams);
	this->readStringfromDB("gamepwargs", dbid, &this->pwparams);
	this->readStringfromDB("gameuserargs", dbid, &this->userparams);
	this->readStringfromDB("gamecmdline", dbid, &this->mustcontain);
	this->readStringfromDB("gamencmdline", dbid, &this->notcontain);

	//alle sonstigen werte
	this->id = this->readWordfromDB("gameid", dbid);
	this->send_gameid = this->readWordfromDB("gamesendid", dbid);
	if (this->send_gameid == 0)
		this->send_gameid = this->id;
	this->setstatusmsg = this->readWordfromDB("gamesetsmsg", dbid, 0);
	this->custom = this->readBytefromDB("gamecustom", dbid, 0);
	this->skip = this->readBytefromDB("gameskip", this->id, 0);
	this->notinstartmenu = this->readBytefromDB("notinstartmenu", this->id, 0);
	this->noicqstatus = this->readBytefromDB("gamenostatus", this->id, 0);
	this->readStringfromDB("gameextraparams", this->id, &this->extraparams);
	this->readStringfromDB("customgamename", this->id, &this->customgamename);
	this->readUtf8StringfromDB("statusmsg", this->id, &this->statusmsg);

	//mehrere pfade
	int size = this->readWordfromDB("gamemulti", dbid, 0);
	for (int j = 0; j < size; j++)
	{
		char* tpath = NULL;
		this->readStringfromDB("gamepath", dbid, j, &tpath);

		if (tpath)
		{
			mpath.push_back(tpath);
		}
	}

	//spielname auslesen, wird später für die sortierung gebraucht
	setNameandIcon();
}

//läd spielnamen aus, sowie icon
void Xfire_game::setNameandIcon()
{
	if (this->customgamename)
	{
		this->setString(this->customgamename, &this->name);
	}
	else
	{
		//std::string game=GetGame(this->id,0,&this->iconhandl,&this->hicon,TRUE);
		//zielbuffer für den namen
		char buf[XFIRE_MAXSIZEOFGAMENAME] = "Unknown Game";
		//name des spiels auslesen
		this->getGamename(this->id, buf, XFIRE_MAXSIZEOFGAMENAME);
		//namen setzen
		this->setString(buf, &this->name);
	}
}

//liest gamewerte aus der db
void Xfire_game::writeToDB(unsigned dbid)
{
	//alle stringwerte schreiben
	this->writeStringtoDB("gamepath", dbid, this->path);
	this->writeStringtoDB("gamelaunch", dbid, this->launchparams);
	this->writeStringtoDB("gamenetargs", dbid, this->networkparams);
	this->writeStringtoDB("gamepwargs", dbid, this->pwparams);
	this->writeStringtoDB("gameuserargs", dbid, this->userparams);
	this->writeStringtoDB("gamecmdline", dbid, this->mustcontain);
	this->writeStringtoDB("gamencmdline", dbid, this->notcontain);

	//alle sonstigen werte
	this->writeWordtoDB("gameid", dbid, this->id);
	if (this->send_gameid != 0 && this->send_gameid != this->id) this->writeWordtoDB("gamesendid", dbid, this->send_gameid);
	if (this->setstatusmsg != 0) this->writeWordtoDB("gamesetsmsg", dbid, this->setstatusmsg);
	if (this->custom != 0) this->writeBytetoDB("gamecustom", dbid, this->custom);
	//wenn gesetzt, dann eintrag machen
	if (this->skip != 0)
		this->writeBytetoDB("gameskip", this->id, this->skip);
	else //wenn nicht eintrag aus db löschen
		this->removeDBEntry("gameskip", this->id);
	if (this->notinstartmenu != 0)
		this->writeBytetoDB("notinstartmenu", this->id, this->notinstartmenu);
	else //wenn nicht eintrag aus db löschen
		this->removeDBEntry("notinstartmenu", this->id);
	if (this->noicqstatus != 0)
		this->writeBytetoDB("gamenostatus", this->id, this->noicqstatus);
	else //wenn nicht eintrag aus db löschen
		this->removeDBEntry("gamenostatus", this->id);
	if (this->extraparams != 0)
		this->writeStringtoDB("gameextraparams", this->id, this->extraparams);
	else //wenn nicht eintrag aus db löschen
		this->removeDBEntry("gameextraparams", this->id);
	if (this->customgamename != 0)
		this->writeStringtoDB("customgamename", this->id, this->customgamename);
	else //wenn nicht eintrag aus db löschen
		this->removeDBEntry("customgamename", this->id);
	if (this->statusmsg != 0)
		this->writeUtf8StringtoDB("statusmsg", this->id, this->statusmsg);
	else //wenn nicht eintrag aus db löschen
		this->removeDBEntry("statusmsg", this->id);



	//mehrere pfade
	int size = mpath.size();
	if (size > 0)
	{
		this->writeWordtoDB("gamemulti", dbid, mpath.size());
		for (int j = 0; j < size; j++)
		{
			this->writeStringtoDB("gamepath", dbid, j, mpath.at(j));
		}
	}

	//sendid 0 dann standard id reinladen
	if (this->send_gameid == 0)
		this->send_gameid = this->id;
}

//erzeugt ein menüpunkt
void Xfire_game::createMenuitem(unsigned int pos, int dbid)
{
	char servicefunction[100];
	mir_strcpy(servicefunction, protocolname);
	mir_strcat(servicefunction, "StartGame%d");

	if (dbid < 0)
		dbid = pos;

	CLISTMENUITEM mi = { 0 };
	mi.popupPosition = 500084000;
	mi.pszPopupName = Translate("Start game");
	mi.pszContactOwner = protocolname;

	mir_snprintf(temp, _countof(temp), servicefunction, this->id);
	//wenn die servicefunktion schon exisitert vernichten, hehe
	if (ServiceExists(temp))
		DestroyServiceFunction(temp);
	CreateServiceFunctionParam(temp, StartGame, this->id);
	mi.pszService = temp;
	mi.position = 500090002 + pos;
	mi.hIcon = this->hicon ? this->hicon : LoadIcon(hinstance, MAKEINTRESOURCE(ID_OP));
	mi.pszName = menuitemtext(this->name);
	this->menuhandle = Menu_AddMainMenuItem(&mi);

	//menu aktualisieren ob hidden
	this->refreshMenuitem();
}

//entfernt menüpunkt
void Xfire_game::remoteMenuitem()
{
	if (menuhandle != NULL)
	{
		Menu_RemoveItem(menuhandle);
		menuhandle = NULL;
	}
}

//aktualisiert menüpunkt ob hidden
void Xfire_game::refreshMenuitem()
{
	if (menuhandle != NULL)
		Menu_ShowItem(menuhandle, !this->notinstartmenu);
}