forked from kugelrund/LolSceneSwitch
/
LolSceneSwitch.cpp
637 lines (550 loc) · 13.8 KB
/
LolSceneSwitch.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
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
#include "LolSceneSwitch.h"
#include <tchar.h>
#include <strsafe.h>
#include "SettingsDialog.h"
#include "PointerPath.h"
#include "Log.h"
LolSceneSwitch *plugin = nullptr;
HINSTANCE hInstDLL = nullptr;
BOOL CALLBACK DllMain(_In_ HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
if (fdwReason == DLL_PROCESS_ATTACH)
hInstDLL = hinstDLL;
return TRUE;
}
CTSTR GetPluginName()
{
return TEXT("League of Legends Scene Switch");
}
CTSTR GetPluginDescription()
{
return TEXT("Switches scenes according do the state of League of Legends.");
}
bool LoadPlugin()
{
if (plugin != nullptr)
{
return false;
}
InitLog(OBSGetPluginDataPath());
plugin = new LolSceneSwitch();
Log("INFO | Loaded LolSceneSwitch v0.2 Alpha 8");
return true;
}
void UnloadPlugin()
{
if (plugin == nullptr)
{
return;
}
delete plugin;
plugin = nullptr;
CloseLog();
}
void OnStartStream()
{
if (plugin != nullptr)
{
plugin->StartMonitoring();
}
}
void OnStopStream()
{
if (plugin != nullptr)
{
plugin->EndMonitoring();
}
}
void ConfigPlugin(_In_ HWND hWnd)
{
if (plugin != nullptr)
{
bool restart = false;
if (plugin->IsMonitoring())
{
restart = true;
plugin->EndMonitoring();
}
SettingsDialog dialog(plugin->GetSettingsRef());
dialog.Show(hWnd, hInstDLL);
if (restart)
{
plugin->StartMonitoring();
}
}
}
HANDLE GetProcessByName(_In_ TCHAR const *name, _Out_ DWORD &pid)
{
pid = 0;
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (snapshot == INVALID_HANDLE_VALUE)
{
Log("ERROR | GetProcessByName | Couldn't create process snapshot!");
LogLastError();
return nullptr;
}
PROCESSENTRY32 entry;
entry.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(snapshot, &entry))
{
Log("ERROR | GetProcessByName | Couldn't enumerate first process!");
LogLastError();
}
else
{
while (Process32Next(snapshot, &entry))
{
if (_tcscmp(entry.szExeFile, name) == 0)
{
pid = entry.th32ProcessID;
break;
}
}
}
CloseHandle(snapshot);
if (pid != 0)
{
Log("INFO | GetProcessByName | Found process", name);
HANDLE process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
if (process == nullptr)
{
Log("ERROR | GetProcessByName | Couldn't open process", name);
LogLastError();
return nullptr;
}
else
{
return process;
}
}
else
{
return nullptr;
}
}
HANDLE GetNewestLog(_In_ TCHAR const * directory, _Out_ LPFILETIME const creationTime)
{
creationTime->dwHighDateTime = 0;
creationTime->dwLowDateTime = 0;
TCHAR dirPath[MAX_PATH];
size_t pathLength;
if (FAILED(StringCchLength(directory, MAX_PATH, &pathLength)) || pathLength + 3 > MAX_PATH)
{
return INVALID_HANDLE_VALUE;
}
StringCchCopy(dirPath, MAX_PATH, directory);
StringCchCat(dirPath, MAX_PATH, TEXT("\\*"));
TCHAR newestLog[MAX_PATH];
newestLog[0] = '\0';
WIN32_FIND_DATA ffd;
size_t nameLength;
HANDLE searchHandle = FindFirstFile(dirPath, &ffd);
if (searchHandle == INVALID_HANDLE_VALUE)
{
Log("ERROR | GetNewestLog | Couldn't enumerate files in", dirPath);
LogLastError();
return nullptr;
}
do
{
if (SUCCEEDED(StringCchLength(ffd.cFileName, MAX_PATH, &nameLength)) &&
_tcscmp(ffd.cFileName + nameLength - 4, TEXT(".txt")) == 0 &&
CompareFileTime(&ffd.ftCreationTime, creationTime) == 1)
{
StringCchCopy(newestLog, MAX_PATH, ffd.cFileName);
*creationTime = ffd.ftCreationTime;
}
}
while (FindNextFile(searchHandle, &ffd) != 0);
FindClose(searchHandle);
TCHAR path[MAX_PATH];
if (FAILED(StringCchLength(newestLog, MAX_PATH, &nameLength)) || pathLength + nameLength > MAX_PATH)
{
return INVALID_HANDLE_VALUE;
}
StringCchCopy(path, MAX_PATH, directory);
StringCchCat(path, MAX_PATH, TEXT("\\"));
StringCchCat(path, MAX_PATH, newestLog);
Log("INFO | GetNewestLog | Found newest log file at", path);
return CreateFile(path,
GENERIC_READ, // we only need to read
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, // let LoL do whatever it pleases (Mundo!)
nullptr,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
nullptr);
}
HANDLE GetLogFile(String path, _In_ FILETIME * const lolStartTime)
{
FILETIME creationTime;
HANDLE file = GetNewestLog(path + TEXT("\\Logs\\Game - R3d Logs"), &creationTime);
if (file == INVALID_HANDLE_VALUE)
{
Log("ERROR | GetLogReader | Couldn't open log file!");
LogLastError();
}
else if (file == nullptr)
{
Log("INFO | GetLogReader | Couldn't find any log file, trying again");
}
else
{
if (CompareFileTime(&creationTime, lolStartTime) >= 0)
{
return file;
}
else
{
Log("INFO | GetLogReader | Found log file was to old, waiting for the current log to be created");
}
}
return nullptr;
}
HWND GetWindowById(DWORD pId)
{
HWND hwndCurrent = GetWindow(GetDesktopWindow(), GW_CHILD);
if (hwndCurrent == nullptr)
{
Log("ERROR | GetWindowById | Couldn't get desktop window handle!");
LogLastError();
return nullptr;
}
DWORD pIdCurrent = 0;
do
{
GetWindowThreadProcessId(hwndCurrent, &pIdCurrent);
// checking for GetWindowTextLength to not equal 0 is to ensure that we found
// the actual game window, not the small icon that pops up just before the game starts
if (pIdCurrent == pId && IsWindowVisible(hwndCurrent) && GetWindowTextLength(hwndCurrent) != 0)
{
Log("INFO | GetWindowById | Found window with pId", pId);
return hwndCurrent;
}
}
while (hwndCurrent = GetNextWindow(hwndCurrent, GW_HWNDNEXT));
return nullptr;
}
bool HasFocus(_In_ HWND window)
{
DWORD pid1 = 0;
DWORD pid2 = 0;
GetWindowThreadProcessId(window, &pid1);
GetWindowThreadProcessId(GetForegroundWindow(), &pid2);
return pid1 == pid2 && pid1 != 0 && pid2 != 0;
}
LolSceneSwitch::LolSceneSwitch() : settings(),
lolProcessMonitor(nullptr),
runMonitoring(false),
currentMap(Map::UNKNOWN)
{
}
LolSceneSwitch::~LolSceneSwitch()
{
}
bool LolSceneSwitch::IsMonitoring() const
{
return runMonitoring;
}
Settings & LolSceneSwitch::GetSettingsRef()
{
return settings;
}
void LolSceneSwitch::StartMonitoring()
{
if (settings.enabled && lolProcessMonitor == nullptr)
{
runMonitoring = true;
lolProcessMonitor = CreateThread(nullptr, 0, MonitorThread, this, 0, nullptr);
}
Log("INFO | LolSceneSwitch::StartMonitoring");
}
void LolSceneSwitch::EndMonitoring()
{
runMonitoring = false;
if (lolProcessMonitor != nullptr)
{
WaitForSingleObject(lolProcessMonitor, INFINITE);
CloseHandle(lolProcessMonitor);
lolProcessMonitor = nullptr;
}
Log("INFO | LolSceneSwitch::EndMonitoring");
}
void LolSceneSwitch::SetScene(String scene) const
{
if (!scene.IsEmpty() && scene != OBSGetSceneName())
{
OBSSetScene(scene, true);
}
}
void LolSceneSwitch::ChangeScene(State state)
{
StateScenes scene = settings.scenes[state];
if (scene.useMaps)
{
switch (currentMap)
{
case Map::SUMMONERS_RIFT:
SetScene(scene.mapScenes.summonersRift);
break;
case Map::CRYSTAL_SCAR:
SetScene(scene.mapScenes.crystalScar);
break;
case Map::TWISTED_TREELINE:
SetScene(scene.mapScenes.twistedTreeline);
break;
case Map::HOWLING_ABYSS:
SetScene(scene.mapScenes.howlingAbyss);
break;
}
}
else
{
SetScene(scene.single);
}
}
DWORD WINAPI LolSceneSwitch::MonitorThread(_In_ LPVOID lpParam)
{
LolSceneSwitch * instance = static_cast<LolSceneSwitch *>(lpParam);
unsigned int const INTERVALL = instance->settings.intervall;
// Handles etc. for the client
HANDLE clientProcess = nullptr;
HWND clientWindow = nullptr;
DWORD clientPid = 0;
// Handles etc. for the game
HANDLE gameProcess = nullptr;
HWND gameWindow = nullptr;
DWORD gamePid;
FILETIME gameStartTime;
// temp var for saving the exit code
DWORD exitCode;
// Used to read the log file
LogReader * reader = nullptr;
// Stuff for reading memory
PointerPath32 clienStatePointer({ -0x658, 0x78, 0x614, 0x4, 0x4C8 });
PointerPath32 map1Pointer({ -0x780, 0x20 });
PointerPath32 map2Pointer({ -0x784, 0x20 });
DWORD clientStateAddress = 0;
long clientState = 0;
DWORD mapAddress = 0;
// info variables of the state of LoL
bool ingame = false;
bool postGame = false;
State state = State::CLIENTOUT;
State oldState = State::CLIENTOUT;
bool newMapInfo = false;
// The main loop for monitoring LoL
while (instance->runMonitoring)
{
// Try to get handles for the client and game process
if (clientProcess == nullptr)
{
clientProcess = GetProcessByName(TEXT("LolClient.exe"), clientPid);
}
if (gameProcess == nullptr)
{
gameProcess = GetProcessByName(TEXT("League of Legends.exe"), gamePid);
if (gameProcess != nullptr)
{
ingame = true;
FILETIME temp;
GetProcessTimes(gameProcess, &gameStartTime, &temp, &temp, &temp);
}
}
// We have a client handle
if (clientProcess != nullptr)
{
// check if it's still running
if (GetExitCodeProcess(clientProcess, &exitCode) && exitCode == STILL_ACTIVE)
{
if (clientWindow == nullptr)
{
clientWindow = GetWindowById(clientPid);
}
if (clientStateAddress == 0)
{
clientStateAddress = PointerPath32::GetThreadAddress(clientProcess, clientPid, 0);
}
if (!ingame)
{
if (instance->settings.scenes[State::CLIENTOUT].single.IsEmpty() ||
(clientWindow != nullptr && HasFocus(clientWindow)))
{
if (clienStatePointer.Deref(clientProcess, clientStateAddress, clientState) && clientState == 1)
{
if (postGame)
{
state = State::POSTGAME;
}
else
{
state = State::CHAMPSELECT;
}
}
else
{
postGame = false;
state = State::CLIENT;
}
}
else
{
state = State::CLIENTOUT;
}
}
}
else
{
Log("INFO | LolSceneSwitch::MonitorThread | LoL client process exited!");
CloseHandle(clientProcess);
clientProcess = nullptr;
clientWindow = nullptr;
clientStateAddress = 0;
}
}
// we have a game handle
if (gameProcess != nullptr)
{
// check if it's still running
if (GetExitCodeProcess(gameProcess, &exitCode) && exitCode == STILL_ACTIVE)
{
if (gameWindow == nullptr)
{
gameWindow = GetWindowById(gamePid);
}
if (reader == nullptr)
{
HANDLE file = GetLogFile(instance->settings.lolPath, &gameStartTime);
if (file != nullptr)
{
reader = &LogReader(file);
}
}
if (mapAddress == 0)
{
mapAddress = PointerPath32::GetThreadAddress(gameProcess, gamePid, 0);
}
if (instance->currentMap == Map::UNKNOWN)
{
std::string mapString1 = map1Pointer.Deref(gameProcess, mapAddress, 5);
std::string mapString2 = map2Pointer.Deref(gameProcess, mapAddress, 5);
if (mapString1.compare("Map1") == 0 || mapString2.compare("Map1") == 0 ||
mapString1.compare("Map11") == 0 || mapString2.compare("Map11") == 0)
{
Log("INFO | LolSceneSwitch::MonitorThread | Map is Summoners Rift!");
instance->currentMap = Map::SUMMONERS_RIFT;
newMapInfo = true;
}
else if (mapString1.compare("Map8") == 0 || mapString2.compare("Map8") == 0)
{
Log("INFO | LolSceneSwitch::MonitorThread | Map is Crystal Scar!");
instance->currentMap = Map::CRYSTAL_SCAR;
newMapInfo = true;
}
else if (mapString1.compare("Map10") == 0 || mapString2.compare("Map10") == 0)
{
Log("INFO | LolSceneSwitch::MonitorThread | Map is Twisted Treeline!");
instance->currentMap = Map::TWISTED_TREELINE;
newMapInfo = true;
}
else if (mapString1.compare("Map12") == 0 || mapString2.compare("Map12") == 0)
{
Log("INFO | LolSceneSwitch::MonitorThread | Map is Howling Abyss!");
instance->currentMap = Map::HOWLING_ABYSS;
newMapInfo = true;
}
}
if (reader != nullptr && (instance->settings.scenes[State::GAMEOUT].single.IsEmpty() ||
(gameWindow != nullptr && HasFocus(gameWindow))))
{
state = reader->GetState();
}
else
{
state = State::GAMEOUT;
}
}
else
{
Log("INFO | LolSceneSwitch::MonitorThread | LoL game process exited!");
ingame = false;
postGame = true;
CloseHandle(gameProcess);
gameProcess = nullptr;
gameWindow = nullptr;
reader = nullptr;
instance->currentMap = Map::UNKNOWN;
mapAddress = 0;
}
}
if (state != oldState || newMapInfo)
{
// Something has changed!!!
Log("INFO | LolSceneSwitch::MonitorThread | New state:", static_cast<long long>(state));
oldState = state;
newMapInfo = false;
instance->ChangeScene(state);
}
Sleep(INTERVALL);
}
return 0;
}
// search strings
std::array<char const *, 3> const LogReader::searchStrings = { "Set focus to app",
"Start Main Loop",
"End game message processing!" };
LogReader::LogReader(_In_ HANDLE file) : file(file)
{
}
State LogReader::GetState()
{
if (index < searchStrings.size())
{
char buffer[BUFFER_SIZE];
char * found = nullptr;
DWORD bytesRead = 0;
while (ReadFile(file, buffer, BUFFER_SIZE - 1, &bytesRead, nullptr) && bytesRead)
{
buffer[bytesRead] = '\0';
found = strstr(buffer, searchStrings[index]);
if (found != nullptr)
{
SetFilePointer(file, -static_cast<signed long>(bytesRead) + (found - buffer + strlen(searchStrings[index])), nullptr, FILE_CURRENT);
index += 1;
if (index >= searchStrings.size())
{
// last message of interest has been read already
break;
}
}
else
{
SetFilePointer(file, -static_cast<signed long>(strlen(searchStrings[index])) + 1, nullptr, FILE_CURRENT);
if (bytesRead < BUFFER_SIZE - 1)
{
// nothing more to read here
break;
}
}
}
}
if (index == 0)
{
return State::GAMEOUT;
}
else if (index == 1)
{
return State::LOADSCREEN;
}
else if (index == 2)
{
return State::GAME;
}
else if (index == 3)
{
return State::ENDGAME;
}
else
{
return State::GAMEOUT;
}
}