-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameStateIntegration.cpp
619 lines (526 loc) · 15.8 KB
/
GameStateIntegration.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
#include "pch.hpp"
#include "GameStateIntegration.hpp"
void GameStateIntegration::sendVibrate(int strength)
{
std::wstring cmd = L"Vibrate:" + std::to_wstring(strength) + L";";
#ifndef NO_BLE
if (!bLEController.GattWrite(cmd))
{
std::cerr << "Failed sending a command to the device" << std::endl;
}
#endif
#ifdef _DEBUG
std::wcout << DBG_START << L"Sent: " << cmd << DBG_END << std::endl;
#endif
}
void GameStateIntegration::handleJSON(web::json::value msg)
{
if (msg.has_object_field(L"previously") && msg.has_object_field(L"player"))
{
web::json::value player = msg.at(L"player");
web::json::value previously = msg.at(L"previously");
if (previously.has_object_field(L"player"))
{
web::json::value previousPlayer = previously.at(L"player");
if (previously.has_boolean_field(L"map"))
{
if (previously.at(L"map").as_bool())
{
#ifdef EVENTS
std::cout << EVT("Map unload") << std::endl;
#endif
currentStrength = config.baseVibration;
isMVP = false;
isDead = false;
sendVibrate(config.baseVibration);
return;
}
}
if (previously.has_object_field(L"round"))
{
web::json::value round = previously.at(L"round");
if (round.has_string_field(L"phase") && round.at(L"phase").as_string() == L"over")
{
#ifdef EVENTS
std::cout << EVT("Round reset") << std::endl;
#endif
currentStrength = config.baseVibration;
isMVP = false;
isDead = false;
sendVibrate(config.baseVibration);
return;
}
}
if (isDead || (isMVP && config.burstOnMVP))
{
return;
}
if (previousPlayer.has_object_field(L"match_stats"))
{
web::json::value match_stats = previousPlayer.at(L"match_stats");
if (config.burstOnMVP)
{
if (match_stats.has_integer_field(L"mvps"))
{
#ifdef EVENTS
std::cout << EVT("MVP") << std::endl;
#endif
isMVP = true;
sendVibrate(20);
return;
}
}
if (match_stats.has_integer_field(L"deaths"))
{
#ifdef EVENTS
std::cout << EVT("Dead") << std::endl;
#endif
isDead = true;
currentStrength = config.baseVibration;
sendVibrate(config.baseVibration);
return;
}
}
if (config.increaseOnKill)
{
if (previousPlayer.has_object_field(L"state") && player.has_object_field(L"state"))
{
web::json::value state = player.at(L"state");
if (previousPlayer.at(L"state").has_integer_field(L"round_kills") && state.has_integer_field(L"round_kills"))
{
#ifdef EVENTS
std::cout << EVT("Kill") << std::endl;
#endif
web::json::value round_kills = state.at(L"round_kills");
int strength = static_cast<int>(config.baseVibration + round_kills.as_integer() * config.increaseAmount);
currentStrength = strength;
sendVibrate(std::clamp(strength, 0, 20));
}
}
}
if (config.stopOnKnife || config.vibrateOnShoot)
{
if (previousPlayer.has_object_field(L"weapons") && player.has_object_field(L"weapons"))
{
web::json::value previousWeapons = previousPlayer.at(L"weapons");
web::json::object weapons = player.at(L"weapons").as_object();
for (auto it = weapons.begin(); it != weapons.end(); ++it)
{
std::wstring name = it->first;
web::json::value weapon = it->second;
if (weapon.has_string_field(L"state") && weapon.at(L"state").as_string() == L"active")
{
if (previousWeapons.has_object_field(name))
{
web::json::value previousWeapon = previousWeapons.at(name);
if (config.vibrateOnShoot)
{
if (previousWeapon.has_integer_field(L"ammo_clip") && weapon.has_integer_field(L"ammo_clip") &&
!previousWeapon.has_string_field(L"name") &&
previousWeapon.at(L"ammo_clip").as_integer() - 1 == weapon.at(L"ammo_clip").as_integer())
{
#ifdef EVENTS
std::cout << EVT("Shot") << std::endl;
#endif
lastShot = std::chrono::steady_clock::now();
sendVibrate(currentStrength);
pplx::wait(100);
auto timeElapsed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - lastShot).count();
if (timeElapsed > 100 && !isMVP && config.vibrateOnShoot)
{
#ifdef _DEBUG
std::cout << DBG("Reset") << std::endl;
#endif
sendVibrate(config.baseVibration);
}
}
}
else if (config.stopOnKnife)
{
if (previousWeapon.has_string_field(L"state") && previousWeapon.at(L"state").as_string() == L"holstered")
{
if (weapon.has_string_field(L"type") && weapon.at(L"type").as_string() == L"Knife")
{
#ifdef EVENTS
std::cout << EVT("Knife active") << std::endl;
#endif
sendVibrate(config.baseVibration);
return;
}
#ifdef EVENTS
std::cout << EVT("Knife holstered") << std::endl;
#endif
sendVibrate(currentStrength);
}
}
}
}
}
}
}
}
}
}
void GameStateIntegration::handlePost(http_request message)
{
#ifdef _DEBUG
std::cout << DBG("Received post") << std::endl;
#endif
message.reply(status_codes::OK);
web::json::value msg = message.extract_json().get();
handleJSON(msg);
}
void GameStateIntegration::setupListener(int port)
{
mListener = http_listener(uri_builder(L"http://localhost:" + std::to_wstring(port)).to_string());
mListener.support(methods::POST, std::bind(&GameStateIntegration::handlePost, this, std::placeholders::_1));
}
std::optional<std::string> GameStateIntegration::getCSGOPath()
{
winreg::RegKey key{ HKEY_CURRENT_USER, L"SOFTWARE\\Valve\\Steam" };
if (std::optional<std::wstring> steamPathO = key.TryGetStringValue(L"SteamPath"))
{
std::wstring steamPath = *steamPathO + std::wstring(L"/config/config.vdf"); // using steamapps/libraryfolders.vdf would be possible too
#ifdef _DEBUG
std::wcout << DBG_START << L"Trying to open steam config at: " << steamPath << DBG_END << std::endl;
#endif
std::ifstream steamConfig;
steamConfig.open(steamPath);
if (steamConfig.good())
{
std::regex baseInstallRegex("\"BaseInstallFolder_[0-9]+\"\t+\"(.+)\"");
std::string line;
while (getline(steamConfig, line))
{
std::smatch matches;
if (std::regex_search(line, matches, baseInstallRegex))
{
#ifdef _DEBUG
std::cout << DBG_START << "Found install folder at: " << matches[1] << DBG_END << std::endl;
#endif
std::string csgoPath = std::string(matches[1]) + "\\\\steamapps\\\\common\\\\Counter-Strike Global Offensive\\\\csgo\\\\cfg";
DWORD fileAttributes = GetFileAttributesA(csgoPath.c_str());
if (fileAttributes != INVALID_FILE_ATTRIBUTES && fileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
return csgoPath;
}
}
}
}
else
{
#ifdef _DEBUG
std::cout << DBG("Could not read config.vdf") << std::endl;
#endif
return {};
}
}
return {};
}
int GameStateIntegration::createConfig()
{
int port;
std::string input;
std::cout << "Enter the port to be used (default: 6969): ";
std::cin.clear();
std::cin.sync();
std::getline(std::cin, input);
if (!input.empty())
{
std::istringstream stream(input);
stream >> port;
if (port == 0)
{
port = 6969;
}
if (port < 1025)
{
std::cout << "You entered a well-known port. This potentially requires admin permissions." << std::endl;
}
}
else
{
port = 6969;
}
#ifdef _DEBUG
std::cout << DBG_START << "Selected port is: " << port << DBG_END << std::endl;
#endif
std::string csgoPath;
if (std::optional<std::string> csgoPathO = getCSGOPath())
{
csgoPath = *csgoPathO;
#ifdef _DEBUG
std::cout << DBG_START << "Found CSGO directory at: " << csgoPath << DBG_END << std::endl;
#endif
}
else
{
std::cout << "Could not determine CSGO's path automatically.\nPlease enter its installation location: ";
std::cin.clear();
std::cin.sync();
std::getline(std::cin, csgoPath);
while (true)
{
csgoPath = std::regex_replace(csgoPath, std::regex("\\\\"), "\\\\");
csgoPath += std::string("\\\\csgo\\\\cfg");
DWORD fileAttributes = GetFileAttributesA(csgoPath.c_str());
if (fileAttributes != INVALID_FILE_ATTRIBUTES && fileAttributes & FILE_ATTRIBUTE_DIRECTORY) // fix
{
break;
}
std::cerr << "The path you have entered is not valid\n";
std::cout << "Please enter a valid csgo path: ";
std::cin.clear();
std::cin.sync();
std::getline(std::cin, csgoPath);
}
}
std::string gameStateConfig(R"("CSSense"
{
"uri" "http://localhost:)" + std::to_string(port) + R"("
"timeout" "12.0"
"buffer" "0.0"
"throttle" "0.0"
"heartbeat" "120"
"output"
{
"precision_time" "3"
"precision_position" "1"
"precision_vector" "3"
}
"data"
{
"provider" "1"
"map" "1"
"round" "1"
"player_id" "1"
"player_state" "1"
"player_weapons" "1"
"player_match_stats" "1"
}
})");
#ifdef _DEBUG
std::cout << DBG_START << "Writing gamestate file to: " << csgoPath << DBG_END << std::endl;
#endif
std::ofstream gameStateFile(csgoPath + "\\gamestate_integration_cssense.cfg");
gameStateFile << gameStateConfig;
gameStateFile.close();
std::cout << "\nNow let's set up the config.\nEnter the minimum amount of strength the device should vibrate at at all times (0-20, integer) (default: 0): ";
std::cin.clear();
std::cin.sync();
std::getline(std::cin, input);
if (!input.empty())
{
std::istringstream stream(input);
stream >> config.baseVibration;
config.baseVibration = std::clamp(0, 20, config.baseVibration);
}
else
{
config.baseVibration = 0;
}
std::cout << "Should the strength increase after a kill (T/F) (default: T)? ";
std::cin.clear();
std::cin.sync();
std::getline(std::cin, input);
if (!input.empty() && (input.starts_with('f') || input.starts_with('F')))
{
config.increaseOnKill = false;
config.increaseAmount = 0;
config.vibrateOnShoot = false;
config.stopOnKnife = false;
}
else
{
config.increaseOnKill = true;
std::cout << "Enter the amount by which the strength should get increased (or decreased) on kill (-20-20, double) (default: 3): ";
std::cin.clear();
std::cin.sync();
std::getline(std::cin, input);
if (!input.empty())
{
std::istringstream stream(input);
stream >> config.increaseAmount;
config.increaseAmount = std::clamp(config.increaseAmount, -20., 20.);
}
else
{
config.increaseAmount = 3;
}
std::cout << "Should the increased strength only get applied when shooting (T/F) (default: T)? ";
std::cin.clear();
std::cin.sync();
std::getline(std::cin, input);
if (!input.empty() && (input.starts_with('f') || input.starts_with('F')))
{
config.vibrateOnShoot = false;
std::cout << "Should the device return to base vibration when holding a knife (T/F) (default: T)? ";
std::cin.clear();
std::cin.sync();
std::getline(std::cin, input);
if (!input.empty() && (input.starts_with('f') || input.starts_with('F')))
{
config.stopOnKnife = false;
}
else
{
config.stopOnKnife = true;
}
}
else
{
config.vibrateOnShoot = true;
config.stopOnKnife = false;
}
}
std::cout << "Should the device vibrate at maximum strength on MVP (T/F) (default: T)? ";
std::cin.clear();
std::cin.sync();
std::getline(std::cin, input);
if (!input.empty() && (input.starts_with('f') || input.starts_with('F')))
{
config.burstOnMVP = false;
}
else
{
config.burstOnMVP = true;
}
std::ostringstream configStream;
configStream << std::boolalpha << "{\n\t\"baseVibration\": " << config.baseVibration <<
",\n\t\"increaseOnKill\": " << config.increaseOnKill <<
",\n\t\"increaseAmount\": " << config.increaseAmount <<
",\n\t\"vibrateOnShoot\": " << config.vibrateOnShoot <<
",\n\t\"stopOnKnife\": " << config.stopOnKnife <<
",\n\t\"burstOnMVP\": " << config.burstOnMVP <<
",\n\t\"gamestateConfigPath\": \"" << csgoPath << "\"\n}";
#ifdef _DEBUG
std::cout << DBG("Writing default config") << std::endl;
std::cout << DBG_START << configStream.str() << DBG_END << std::endl;
#endif
std::ofstream configFile("config.json");
configFile << configStream.str();
configFile.close();
std::cout << "\nEverything has been initialized.\n"
"You can configure the config to your liking by either editing 'config.json' or by running 'CSSense -i' again.\n"
"You can find an explanation on https://github.com/Acurisu/CSSense-CLI.\n"
"Restart CSGO if you had it running.\nEnjoy =^w^=\n" << std::endl;
return port;
}
int GameStateIntegration::parseConfig()
{
std::ifstream configFile;
configFile.open("config.json");
if (configFile.good())
{
std::stringstream buffer;
buffer << configFile.rdbuf();
try
{
web::json::value cfg = web::json::value::parse(buffer);
config.increaseOnKill = cfg.at(L"increaseOnKill").as_bool();
config.vibrateOnShoot = cfg.at(L"vibrateOnShoot").as_bool();
config.stopOnKnife = cfg.at(L"stopOnKnife").as_bool();
config.burstOnMVP = cfg.at(L"burstOnMVP").as_bool();
config.increaseAmount = cfg.at(L"increaseAmount").as_number().to_double();
config.baseVibration = cfg.at(L"baseVibration").as_integer();
std::wstring gamestateConfigPath = cfg.at(L"gamestateConfigPath").as_string();
#ifdef _DEBUG
std::wcout << std::boolalpha << DBG_START << L"baseVibration:\t" << config.baseVibration
<< L"\tincreaseOnKill:\t" << config.increaseOnKill
<< L"\n[DBG] increaseAmount:\t" << config.increaseAmount
<< L"\tvibrateOnShoot:\t" << config.vibrateOnShoot
<< L"\n[DBG] stopOnKnife:\t" << config.stopOnKnife
<< L"\tburstOnMVP:\t" << config.burstOnMVP
<< L"\n[DBG] gamestateConfigPath:\t" << gamestateConfigPath << DBG_END << std::endl;
#endif
std::ifstream gamestateFile;
gamestateFile.open(gamestateConfigPath + L"\\gamestate_integration_cssense.cfg");
if (gamestateFile.good())
{
std::regex portRegex("http:\\/\\/localhost:([0-9]+)");
std::string line;
while (std::getline(gamestateFile, line))
{
std::smatch matches;
if (std::regex_search(line, matches, portRegex))
{
#ifdef _DEBUG
std::cout << DBG_START << "Found port: " << matches[1] << DBG_END << std::endl;
#endif
return std::stoi(matches[1]);
}
}
}
else
{
std::cerr << "Something went wrong trying to retrieve the port.\n"
"Please run 'CSSense -i' again." << std::endl;
}
}
catch (web::json::json_exception e)
{
std::cerr << "Something went wrong trying to parse the config.\n"
"Please run 'CSSense -i' again or fix the error shown below:\n"
<< e.what() << std::endl;
}
}
else
{
std::cerr << "Something went wrong trying to read the config.\nDid you forget to run 'CSSense -i'?" << std::endl;
}
return -1;
}
GameStateIntegration::GameStateIntegration(bool init)
{
int port;
if (init)
{
port = createConfig();
}
else
{
port = parseConfig();
}
if (port == -1)
{
exit(-1);
}
#ifndef NO_BLE
std::wregex serviceGUIDRegex(L"^\\{..300001-002.-4bd4-bbd5-a6920e4c5653\\}");
std::cout << "Searching for a Lovesense device..." << std::endl;
if (!bLEController.ConnectBLEDeviceByService(serviceGUIDRegex))
{
std::cerr << "Could not connect to the BLE device" << std::endl;
exit(-1);
}
if (!bLEController.SelectCharacteristics([](
winrt::Windows::Devices::Bluetooth::GenericAttributeProfile::
GattCharacteristic sender,
winrt::Windows::Devices::Bluetooth::GenericAttributeProfile::
GattValueChangedEventArgs eventArgs)
{
#ifdef _DEBUG
std::cout << DBG_START << "Received: " << eventArgs.CharacteristicValue().data() << DBG_END << std::endl;
#endif
}))
{
std::cerr << "Could not select the specified characteristics" << std::endl;
exit(-1);
}
std::cout << "Found and connected to the device." << std::endl;
#endif
currentStrength = config.baseVibration;
sendVibrate(config.baseVibration);
setupListener(port);
}
GameStateIntegration::~GameStateIntegration()
{
sendVibrate(0);
#ifndef NO_BLE
if (!bLEController.DisconnectBLEDevice())
{
std::cerr << "Failed trying to disconnect the BLE device" << std::endl;
exit(-1);
}
#endif
}