-
Notifications
You must be signed in to change notification settings - Fork 6
/
ghost.cpp
executable file
·2231 lines (1812 loc) · 67.1 KB
/
ghost.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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
ent-ghost
Copyright [2011-2013] [Jack Lu]
This file is part of the ent-ghost source code.
ent-ghost is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ent-ghost source code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ent-ghost source code. If not, see <http://www.gnu.org/licenses/>.
ent-ghost is modified from GHost++ (http://ghostplusplus.googlecode.com/)
GHost++ is Copyright [2008] [Trevor Hogan]
*/
#include "ghost.h"
#include "util.h"
#include "crc32.h"
#include "sha1.h"
#include "csvparser.h"
#include "config.h"
#include "language.h"
#include "socket.h"
#include "ghostdb.h"
#include "ghostdbmysql.h"
#include "bnet.h"
#include "map.h"
#include "packed.h"
#include "savegame.h"
#include "gameplayer.h"
#include "gameprotocol.h"
#include "gcbiprotocol.h"
#include "amhprotocol.h"
#include "gpsprotocol.h"
#include "game_base.h"
#include "game.h"
#include "streamplayer.h"
#include "stageplayer.h"
#include <signal.h>
#include <execinfo.h> //to generate stack trace-like thing on exception
#include <stdlib.h>
#ifdef WIN32
#include <ws2tcpip.h> // for WSAIoctl
#endif
#define __STORMLIB_SELF__
#include <stormlib/StormLib.h>
#ifdef WIN32
#include <windows.h>
#include <winsock.h>
#endif
#include <time.h>
#ifndef WIN32
#include <sys/time.h>
#endif
#ifdef __APPLE__
#include <mach/mach_time.h>
#endif
string gCFGFile;
string gLogFile;
uint32_t gLogMethod;
ofstream *gLog = NULL;
CGHost *gGHost = NULL;
vector<string> gLogQueue;
uint32_t gLogLastTicks;
boost::mutex PrintMutex;
uint32_t GetTime( )
{
return GetTicks( ) / 1000;
}
uint32_t GetTicks( )
{
#ifdef WIN32
// don't use GetTickCount anymore because it's not accurate enough (~16ms resolution)
// don't use QueryPerformanceCounter anymore because it isn't guaranteed to be strictly increasing on some systems and thus requires "smoothing" code
// use timeGetTime instead, which typically has a high resolution (5ms or more) but we request a lower resolution on startup
return timeGetTime( );
#elif __APPLE__
uint64_t current = mach_absolute_time( );
static mach_timebase_info_data_t info = { 0, 0 };
// get timebase info
if( info.denom == 0 )
mach_timebase_info( &info );
uint64_t elapsednano = current * ( info.numer / info.denom );
// convert ns to ms
return elapsednano / 1e6;
#else
uint32_t ticks;
struct timespec t;
clock_gettime( CLOCK_MONOTONIC, &t );
ticks = t.tv_sec * 1000;
ticks += t.tv_nsec / 1000000;
return ticks;
#endif
}
void SignalCatcher2( int s )
{
CONSOLE_Print( "[!!!] caught signal " + UTIL_ToString( s ) + ", exiting NOW" );
if( gGHost )
{
if( gGHost->m_Exiting )
exit( 1 );
else
gGHost->m_Exiting = true;
}
else
exit( 1 );
}
void SignalCatcher( int s )
{
// signal( SIGABRT, SignalCatcher2 );
signal( SIGINT, SignalCatcher2 );
CONSOLE_Print( "[!!!] caught signal " + UTIL_ToString( s ) + ", exiting nicely" );
if( gGHost )
gGHost->m_ExitingNice = true;
else
exit( 1 );
}
void handler()
{
void *trace_elems[20];
int trace_elem_count(backtrace( trace_elems, 20 ));
char **stack_syms(backtrace_symbols( trace_elems, trace_elem_count ));
for ( int i = 0 ; i < trace_elem_count ; ++i )
{
std::cout << stack_syms[i] << "\n";
}
free( stack_syms );
exit(1);
}
void CONSOLE_Print( string message )
{
boost::mutex::scoped_lock printLock( PrintMutex );
gLogQueue.push_back( message );
printLock.unlock( );
}
void CONSOLE_Flush( )
{
vector<string> logQueue;
boost::mutex::scoped_lock printLock( PrintMutex );
gLogQueue.swap( logQueue );
printLock.unlock( );
if( logQueue.empty( ) )
return;
for( vector<string>::iterator it = logQueue.begin( ); it != logQueue.end( ); it++) {
cout << *it << endl;
}
// logging
if( !gLogFile.empty( ) )
{
time_t Now = time( NULL );
string Time = asctime( localtime( &Now ) );
// erase the newline
Time.erase( Time.size( ) - 1 );
if( gLogMethod == 1 )
{
ofstream Log;
Log.open( gLogFile.c_str( ), ios :: app );
if( !Log.fail( ) )
{
for( vector<string>::iterator it = logQueue.begin( ); it != logQueue.end( ); it++) {
Log << "[" << Time << "] " << *it << endl;
}
Log.close( );
}
}
else if( gLogMethod == 2 )
{
if( gLog && !gLog->fail( ) )
{
for( vector<string>::iterator it = logQueue.begin( ); it != logQueue.end( ); it++) {
*gLog << "[" << Time << "] " << *it << endl;
}
gLog->flush( );
}
}
}
}
void DEBUG_Print( string message )
{
cout << message << endl;
}
void DEBUG_Print( BYTEARRAY b )
{
cout << "{ ";
for( unsigned int i = 0; i < b.size( ); ++i )
cout << hex << (int)b[i] << " ";
cout << "}" << endl;
}
//
// main
//
int main( int argc, char **argv )
{
CONSOLE_Print("*******************************************************************");
CONSOLE_Print("*** ****");
CONSOLE_Print("*** GriefNetwork-Bot v1.0 ****");
CONSOLE_Print("*** https://github.com/Grief-Code/GriefNetwork-Bot ****");
CONSOLE_Print("*** ****");
CONSOLE_Print("*******************************************************************");
srand( time( NULL ) );
CConfig CFG;
CFG.Read( "default.cfg" );
gLogFile = CFG.GetString( "bot_log", string( ) );
gLogMethod = CFG.GetInt( "bot_logmethod", 1 );
if( !gLogFile.empty( ) )
{
if( gLogMethod == 1 )
{
// log method 1: open, append, and close the log for every message
// this works well on Linux but poorly on Windows, particularly as the log file grows in size
// the log file can be edited/moved/deleted while GHost++ is running
}
else if( gLogMethod == 2 )
{
// log method 2: open the log on startup, flush the log for every message, close the log on shutdown
// the log file CANNOT be edited/moved/deleted while GHost++ is running
gLog = new ofstream( );
gLog->open( gLogFile.c_str( ), ios :: app );
}
}
CONSOLE_Print( "[GHOST] starting up" );
if( !gLogFile.empty( ) )
{
if( gLogMethod == 1 )
CONSOLE_Print( "[GHOST] using log method 1, logging is enabled and [" + gLogFile + "] will not be locked" );
else if( gLogMethod == 2 )
{
if( gLog->fail( ) )
CONSOLE_Print( "[GHOST] using log method 2 but unable to open [" + gLogFile + "] for appending, logging is disabled" );
else
CONSOLE_Print( "[GHOST] using log method 2, logging is enabled and [" + gLogFile + "] is now locked" );
}
}
else
CONSOLE_Print( "[GHOST] no log file specified, logging is disabled" );
// catch SIGABRT and SIGINT
// signal( SIGABRT, SignalCatcher );
signal( SIGINT, SignalCatcher );
std::set_terminate( handler ); //to generate stack trace on fail
#ifndef WIN32
// disable SIGPIPE since some systems like OS X don't define MSG_NOSIGNAL
signal( SIGPIPE, SIG_IGN );
#endif
#ifdef WIN32
// initialize timer resolution
// attempt to set the resolution as low as possible from 1ms to 5ms
unsigned int TimerResolution = 0;
for( unsigned int i = 1; i <= 5; ++i )
{
if( timeBeginPeriod( i ) == TIMERR_NOERROR )
{
TimerResolution = i;
break;
}
else if( i < 5 )
CONSOLE_Print( "[GHOST] error setting Windows timer resolution to " + UTIL_ToString( i ) + " milliseconds, trying a higher resolution" );
else
{
CONSOLE_Print( "[GHOST] error setting Windows timer resolution" );
return 1;
}
}
CONSOLE_Print( "[GHOST] using Windows timer with resolution " + UTIL_ToString( TimerResolution ) + " milliseconds" );
#elif __APPLE__
// not sure how to get the resolution
#else
// print the timer resolution
struct timespec Resolution;
if( clock_getres( CLOCK_MONOTONIC, &Resolution ) == -1 )
CONSOLE_Print( "[GHOST] error getting monotonic timer resolution" );
else
CONSOLE_Print( "[GHOST] using monotonic timer with resolution " + UTIL_ToString( (double)( Resolution.tv_nsec / 1000 ), 2 ) + " microseconds" );
#endif
#ifdef WIN32
// initialize winsock
CONSOLE_Print( "[GHOST] starting winsock" );
WSADATA wsadata;
if( WSAStartup( MAKEWORD( 2, 2 ), &wsadata ) != 0 )
{
CONSOLE_Print( "[GHOST] error starting winsock" );
return 1;
}
// increase process priority
CONSOLE_Print( "[GHOST] setting process priority to \"above normal\"" );
SetPriorityClass( GetCurrentProcess( ), ABOVE_NORMAL_PRIORITY_CLASS );
#endif
// initialize ghost
gGHost = new CGHost( &CFG );
while( 1 )
{
// block for 50ms on all sockets - if you intend to perform any timed actions more frequently you should change this
// that said it's likely we'll loop more often than this due to there being data waiting on one of the sockets but there aren't any guarantees
if( gGHost->Update( 50000 ) )
break;
}
// shutdown ghost
CONSOLE_Print( "[GHOST] shutting down" );
CONSOLE_Flush();
delete gGHost;
gGHost = NULL;
#ifdef WIN32
// shutdown winsock
CONSOLE_Print( "[GHOST] shutting down winsock" );
WSACleanup( );
// shutdown timer
timeEndPeriod( TimerResolution );
#endif
if( gLog )
{
if( !gLog->fail( ) )
gLog->close( );
delete gLog;
}
return 0;
}
//
// CGHost
//
CGHost :: CGHost( CConfig *CFG )
{
for( int i = 0; i < 10; i++)
{
string Key = "udp_broadcasttarget";
if( i != 0 )
Key += UTIL_ToString( i );
string Target = CFG->GetString( Key, string( ) );
if( Target.empty( ) )
continue;
CUDPSocket *UDPSocket = new CUDPSocket( );
UDPSocket->SetBroadcastTarget( Target );
UDPSocket->SetDontRoute( CFG->GetInt( "udp_dontroute", 0 ) == 0 ? false : true );
m_UDPSockets.push_back( UDPSocket );
}
m_LocalSocket = new CUDPSocket( );
m_LocalSocket->SetBroadcastTarget( "localhost" );
m_LocalSocket->SetDontRoute( CFG->GetInt( "udp_dontroute", 0 ) == 0 ? false : true );
m_ReconnectSocket = NULL;
m_StreamSocket = NULL;
m_GPSProtocol = new CGPSProtocol( );
m_GCBIProtocol = new CGCBIProtocol( );
m_AMHProtocol = new CAMHProtocol( );
m_GameProtocol = new CGameProtocol( this );
m_CRC = new CCRC32( );
m_CRC->Initialize( );
m_SHA = new CSHA1( );
m_CurrentGame = NULL;
m_CallableCommandList = NULL;
m_LastDenyCleanTime = 0;
m_CallableSpoofList = NULL;
m_LastSpoofRefreshTime = 0;
CONSOLE_Print( "[GHOST] opening primary database" );
m_DB = new CGHostDBMySQL( CFG );
// get a list of local IP addresses
// this list is used elsewhere to determine if a player connecting to the bot is local or not
CONSOLE_Print( "[GHOST] attempting to find local IP addresses" );
#ifdef WIN32
// use a more reliable Windows specific method since the portable method doesn't always work properly on Windows
// code stolen from: http://tangentsoft.net/wskfaq/examples/getifaces.html
SOCKET sd = WSASocket( AF_INET, SOCK_DGRAM, 0, 0, 0, 0 );
if( sd == SOCKET_ERROR )
CONSOLE_Print( "[GHOST] error finding local IP addresses - failed to create socket (error code " + UTIL_ToString( WSAGetLastError( ) ) + ")" );
else
{
INTERFACE_INFO InterfaceList[20];
unsigned long nBytesReturned;
if( WSAIoctl( sd, SIO_GET_INTERFACE_LIST, 0, 0, &InterfaceList, sizeof(InterfaceList), &nBytesReturned, 0, 0 ) == SOCKET_ERROR )
CONSOLE_Print( "[GHOST] error finding local IP addresses - WSAIoctl failed (error code " + UTIL_ToString( WSAGetLastError( ) ) + ")" );
else
{
int nNumInterfaces = nBytesReturned / sizeof(INTERFACE_INFO);
for( int i = 0; i < nNumInterfaces; ++i )
{
sockaddr_in *pAddress;
pAddress = (sockaddr_in *)&(InterfaceList[i].iiAddress);
CONSOLE_Print( "[GHOST] local IP address #" + UTIL_ToString( i + 1 ) + " is [" + string( inet_ntoa( pAddress->sin_addr ) ) + "]" );
m_LocalAddresses.push_back( UTIL_CreateByteArray( (uint32_t)pAddress->sin_addr.s_addr, false ) );
}
}
closesocket( sd );
}
#else
// use a portable method
char HostName[255];
if( gethostname( HostName, 255 ) == SOCKET_ERROR )
CONSOLE_Print( "[GHOST] error finding local IP addresses - failed to get local hostname" );
else
{
CONSOLE_Print( "[GHOST] local hostname is [" + string( HostName ) + "]" );
struct hostent *HostEnt = gethostbyname( HostName );
if( !HostEnt )
CONSOLE_Print( "[GHOST] error finding local IP addresses - gethostbyname failed" );
else
{
for( int i = 0; HostEnt->h_addr_list[i] != NULL; ++i )
{
struct in_addr Address;
memcpy( &Address, HostEnt->h_addr_list[i], sizeof(struct in_addr) );
CONSOLE_Print( "[GHOST] local IP address #" + UTIL_ToString( i + 1 ) + " is [" + string( inet_ntoa( Address ) ) + "]" );
m_LocalAddresses.push_back( UTIL_CreateByteArray( (uint32_t)Address.s_addr, false ) );
}
}
}
#endif
m_Language = NULL;
m_Exiting = false;
m_ExitingNice = false;
m_Enabled = true;
m_Version = "17.1";
m_HostCounter = CFG->GetInt( "bot_hostcounter", 1 );
m_AutoHostMaximumGames = CFG->GetInt( "autohost_maxgames", 0 );
m_AutoHostAutoStartPlayers = CFG->GetInt( "autohost_startplayers", 0 );
m_AutoHostGameName = CFG->GetString( "autohost_gamename", string( ) );
m_AutoHostOwner = CFG->GetString( "autohost_owner", string( ) );
m_LocalIPs = CFG->GetString( "bot_local", "127.0.0.1 127.0.1.1" );
m_LastAutoHostTime = GetTime( );
m_AutoHostMatchMaking = CFG->GetInt( "autohost_matchmaking", 0 );
m_LastCommandListTime = GetTime( );
m_AutoHostMinimumScore = CFG->GetInt( "autohost_minscore", 0 );
m_AutoHostMaximumScore = CFG->GetInt( "autohost_maxscore", 0 );
m_AllGamesFinished = false;
m_AllGamesFinishedTime = 0;
m_TFT = CFG->GetInt( "bot_tft", 1 ) == 0 ? false : true;
if( m_TFT )
CONSOLE_Print( "[GHOST] acting as Warcraft III: The Frozen Throne" );
else
CONSOLE_Print( "[GHOST] acting as Warcraft III: Reign of Chaos" );
m_HostPort = CFG->GetInt( "bot_hostport", 6112 );
m_Reconnect = CFG->GetInt( "bot_reconnect", 1 ) == 0 ? false : true;
m_ReconnectPort = CFG->GetInt( "bot_reconnectport", 6114 );
m_DefaultMap = CFG->GetString( "bot_defaultmap", "map" );
m_LANWar3Version = CFG->GetInt( "lan_war3version", 27 );
m_ReplayWar3Version = CFG->GetInt( "replay_war3version", 27 );
m_ReplayBuildNumber = CFG->GetInt( "replay_buildnumber", 6059 );
bool IPToCountry = CFG->GetInt( "bot_iptocountry", 0 ) == 0 ? false : true; //
SetConfigs( CFG );
// load the battle.net connections
// we're just loading the config data and creating the CBNET classes here, the connections are established later (in the Update function)
for( uint32_t i = 0; i < 15; ++i )
{
string Prefix;
if( i == 0 )
Prefix = "bnet_";
else
Prefix = "bnet" + UTIL_ToString( i ) + "_";
string Server = CFG->GetString( Prefix + "server", string( ) );
string ServerAlias = CFG->GetString( Prefix + "serveralias", string( ) );
string CDKeyROC = CFG->GetString( Prefix + "cdkeyroc", string( ) );
string CDKeyTFT = CFG->GetString( Prefix + "cdkeytft", string( ) );
string CountryAbbrev = CFG->GetString( Prefix + "countryabbrev", "USA" );
string Country = CFG->GetString( Prefix + "country", "United States" );
string Locale = CFG->GetString( Prefix + "locale", "system" );
uint32_t LocaleID;
if( Locale == "system" )
{
#ifdef WIN32
LocaleID = GetUserDefaultLangID( );
#else
LocaleID = 1033;
#endif
}
else
LocaleID = UTIL_ToUInt32( Locale );
string UserName = CFG->GetString( Prefix + "username", string( ) );
string UserPassword = CFG->GetString( Prefix + "password", string( ) );
string KeyOwnerName = CFG->GetString( Prefix + "keyownername", "GHost" );
string FirstChannel = CFG->GetString( Prefix + "firstchannel", "The Void" );
string RootAdmin = CFG->GetString( Prefix + "rootadmin", string( ) );
string BNETCommandTrigger = CFG->GetString( Prefix + "commandtrigger", "!" );
if( BNETCommandTrigger.empty( ) )
BNETCommandTrigger = "!";
bool HoldFriends = CFG->GetInt( Prefix + "holdfriends", 1 ) == 0 ? false : true;
bool HoldClan = CFG->GetInt( Prefix + "holdclan", 1 ) == 0 ? false : true;
bool PublicCommands = CFG->GetInt( Prefix + "publiccommands", 1 ) == 0 ? false : true;
string BNLSServer = CFG->GetString( Prefix + "bnlsserver", string( ) );
int BNLSPort = CFG->GetInt( Prefix + "bnlsport", 9367 );
int BNLSWardenCookie = CFG->GetInt( Prefix + "bnlswardencookie", 0 );
unsigned char War3Version = CFG->GetInt( Prefix + "custom_war3version", 27 );
BYTEARRAY EXEVersion = UTIL_ExtractNumbers( CFG->GetString( Prefix + "custom_exeversion", string( ) ), 4 );
BYTEARRAY EXEVersionHash = UTIL_ExtractNumbers( CFG->GetString( Prefix + "custom_exeversionhash", string( ) ), 4 );
string PasswordHashType = CFG->GetString( Prefix + "custom_passwordhashtype", string( ) );
string PVPGNRealmName = CFG->GetString( Prefix + "custom_pvpgnrealmname", "PvPGN Realm" );
uint32_t MaxMessageLength = CFG->GetInt( Prefix + "custom_maxmessagelength", 200 );
if( Server.empty( ) )
break;
if( CDKeyROC.empty( ) )
{
CONSOLE_Print( "[GHOST] missing " + Prefix + "cdkeyroc, skipping this battle.net connection" );
continue;
}
if( m_TFT && CDKeyTFT.empty( ) )
{
CONSOLE_Print( "[GHOST] missing " + Prefix + "cdkeytft, skipping this battle.net connection" );
continue;
}
if( UserName.empty( ) )
{
CONSOLE_Print( "[GHOST] missing " + Prefix + "username, skipping this battle.net connection" );
continue;
}
if( UserPassword.empty( ) )
{
CONSOLE_Print( "[GHOST] missing " + Prefix + "password, skipping this battle.net connection" );
continue;
}
CONSOLE_Print( "[GHOST] found battle.net connection #" + UTIL_ToString( i ) + " for server [" + Server + "]" );
if( Locale == "system" )
{
#ifdef WIN32
CONSOLE_Print( "[GHOST] using system locale of " + UTIL_ToString( LocaleID ) );
#else
CONSOLE_Print( "[GHOST] unable to get system locale, using default locale of 1033" );
#endif
}
m_BNETs.push_back( new CBNET( this, Server, ServerAlias, BNLSServer, (uint16_t)BNLSPort, (uint32_t)BNLSWardenCookie, CDKeyROC, CDKeyTFT, CountryAbbrev, Country, LocaleID, UserName, UserPassword, KeyOwnerName, FirstChannel, RootAdmin, BNETCommandTrigger[0], HoldFriends, HoldClan, PublicCommands, War3Version, EXEVersion, EXEVersionHash, PasswordHashType, PVPGNRealmName, MaxMessageLength, i ) );
}
if( m_BNETs.empty( ) )
CONSOLE_Print( "[GHOST] warning - no battle.net connections found in config file" );
// extract common.j and blizzard.j from War3Patch.mpq if we can
// these two files are necessary for calculating "map_crc" when loading maps so we make sure to do it before loading the default map
// see CMap :: Load for more information
ExtractScripts( );
// load the default maps (note: make sure to run ExtractScripts first)
if( m_DefaultMap.size( ) < 4 || m_DefaultMap.substr( m_DefaultMap.size( ) - 4 ) != ".cfg" )
{
m_DefaultMap += ".cfg";
CONSOLE_Print( "[GHOST] adding \".cfg\" to default map -> new default is [" + m_DefaultMap + "]" );
}
CConfig MapCFG;
MapCFG.Read( m_MapCFGPath + m_DefaultMap );
m_Map = new CMap( this, &MapCFG, m_MapCFGPath + m_DefaultMap );
m_MapGameCreateRequest = NULL;
for( int i = 0; i < 100; i++)
{
string AMKey = "autohost_map" + UTIL_ToString( i );
if( i == 0 )
AMKey = "bot_defaultmap";
string AutoHostMapCFGString = CFG->GetString( AMKey , string( ) );
if( AutoHostMapCFGString.empty( ) )
{
continue;
}
if( AutoHostMapCFGString.size( ) < 4 || AutoHostMapCFGString.substr( AutoHostMapCFGString.size( ) - 4 ) != ".cfg" )
{
AutoHostMapCFGString += ".cfg";
CONSOLE_Print( "[GHOST] adding \".cfg\" to autohost game map -> new name is [" + AutoHostMapCFGString + "]" );
}
CConfig AutoHostMapCFG;
AutoHostMapCFG.Read( m_MapCFGPath + AutoHostMapCFGString );
CMap *AutoHostMap = new CMap( this, &AutoHostMapCFG, m_MapCFGPath + AutoHostMapCFGString );
m_AutoHostMap.push_back( new CMap( *AutoHostMap ) );
}
m_SaveGame = new CSaveGame( );
if( m_BNETs.empty( ) )
CONSOLE_Print( "[GHOST] warning - no battle.net connections found" );
CONSOLE_Print( "[GHOST] GHost++ Version " + m_Version + " (with MySQL support)" );
CONSOLE_Print("[GHOST] Loading slap phrases...");
ifstream phrasein;
phrasein.open( "phrase.txt" );
if( phrasein.fail( ) )
{
CONSOLE_Print("[GHOST] Slap phrase load failed!");
perror("Fail");
}
else
{
string Line;
while( !phrasein.eof( ) )
{
getline( phrasein, Line );
if( !Line.empty( ) )
m_SlapPhrases.push_back(Line);
}
phrasein.close( );
}
m_FlameTriggers.push_back("cunt");
m_FlameTriggers.push_back("bitch");
m_FlameTriggers.push_back("whore");
m_FlameTriggers.push_back("retard");
m_FlameTriggers.push_back("nigger");
m_FlameTriggers.push_back("dumb");
m_FlameTriggers.push_back("fuck you");
m_FlameTriggers.push_back("fuck u");
m_FlameTriggers.push_back("you suck");
m_FlameTriggers.push_back("u suck");
m_FlameTriggers.push_back("fucking noob");
m_FlameTriggers.push_back("fuck off");
m_FlameTriggers.push_back("stupid");
m_FlameTriggers.push_back("noob as fuck");
m_FlameTriggers.push_back("idiot");
m_FlameTriggers.push_back("moron");
m_FlameTriggers.push_back("shithead");
m_FlameTriggers.push_back("assfuck");
m_FlameTriggers.push_back("asshole");
m_FlameTriggers.push_back("is shit");
m_FlameTriggers.push_back("are shit");
m_FlameTriggers.push_back("shitty");
m_FlameTriggers.push_back("pussy");
m_FlameTriggers.push_back("loser");
m_FlameTriggers.push_back("fucking bad");
m_FlameTriggers.push_back("faggot");
m_FlameTriggers.push_back("dick");
m_FlameTriggers.push_back("raizen");
CONSOLE_Print( "[GHOST] Loading GeoIP data" );
m_GeoIP = GeoIP_open( m_GeoIPFile.c_str( ), GEOIP_STANDARD | GEOIP_CHECK_CACHE );
if( m_GeoIP == NULL )
CONSOLE_Print( "[GHOST] GeoIP: error opening database" );
// clear the gamelist for this bot, in case there's residual entries
m_Callables.push_back( m_DB->ThreadedGameUpdate(0, "", "", "", "", 0, "", 0, 0, false, false) );
}
CGHost :: ~CGHost( )
{
for( vector<CUDPSocket *> :: iterator i = m_UDPSockets.begin( ); i != m_UDPSockets.end( ); ++i )
delete *i;
delete m_LocalSocket;
delete m_ReconnectSocket;
delete m_StreamSocket;
for( vector<CTCPSocket *> :: iterator i = m_ReconnectSockets.begin( ); i != m_ReconnectSockets.end( ); ++i )
delete *i;
for( vector<CStreamPlayer *> :: iterator i = m_StreamPlayers.begin( ); i != m_StreamPlayers.end( ); ++i )
delete *i;
delete m_GPSProtocol;
delete m_GCBIProtocol;
delete m_GameProtocol;
delete m_AMHProtocol;
delete m_CRC;
delete m_SHA;
for( vector<CBNET *> :: iterator i = m_BNETs.begin( ); i != m_BNETs.end( ); ++i )
delete *i;
if( m_CurrentGame )
m_CurrentGame->doDelete();
boost::mutex::scoped_lock lock( m_GamesMutex );
for( vector<CBaseGame *> :: iterator i = m_Games.begin( ); i != m_Games.end( ); ++i )
(*i)->doDelete();
lock.unlock( );
delete m_DB;
// warning: we don't delete any entries of m_Callables here because we can't be guaranteed that the associated threads have terminated
// this is fine if the program is currently exiting because the OS will clean up after us
// but if you try to recreate the CGHost object within a single session you will probably leak resources!
if( !m_Callables.empty( ) )
CONSOLE_Print( "[GHOST] warning - " + UTIL_ToString( m_Callables.size( ) ) + " orphaned callables were leaked (this is not an error)" );
delete m_Language;
delete m_Map;
delete m_AdminMap;
ClearAutoHostMap( );
delete m_SaveGame;
}
bool CGHost :: Update( long usecBlock )
{
// todotodo: do we really want to shutdown if there's a database error? is there any way to recover from this?
if( m_DB->HasError( ) )
{
CONSOLE_Print( "[GHOST] database error - " + m_DB->GetError( ) );
return true;
}
boost::mutex::scoped_lock gamesLock( m_GamesMutex );
// get rid of any deleted games
for( vector<CBaseGame *> :: iterator i = m_Games.begin( ); i != m_Games.end( ); )
{
if( (*i)->readyDelete( ) )
{
delete *i;
m_Games.erase( i );
} else {
++i;
}
}
if( m_CurrentGame && m_CurrentGame->readyDelete( ) )
{
for( vector<CBNET *> :: iterator i = m_BNETs.begin( ); i != m_BNETs.end( ); ++i )
{
(*i)->QueueGameUncreate( );
(*i)->QueueEnterChat( );
}
delete m_CurrentGame;
m_CurrentGame = NULL;
}
gamesLock.unlock( );
// try to exit nicely if requested to do so
if( m_ExitingNice )
{
if( !m_BNETs.empty( ) )
{
CONSOLE_Print( "[GHOST] deleting all battle.net connections in preparation for exiting nicely" );
for( vector<CBNET *> :: iterator i = m_BNETs.begin( ); i != m_BNETs.end( ); ++i )
delete *i;
m_BNETs.clear( );
}
if( m_CurrentGame )
{
CONSOLE_Print( "[GHOST] deleting current game in preparation for exiting nicely" );
m_CurrentGame->doDelete( );
m_CurrentGame = NULL;
}
if( !m_StagePlayers.empty( ) )
{
for( vector<CStagePlayer *> :: iterator i = m_StagePlayers.begin( ); i != m_StagePlayers.end( ); i++ )
delete *i;
m_StagePlayers.clear( );
}
if( m_Games.empty( ) )
{
if( !m_AllGamesFinished )
{
CONSOLE_Print( "[GHOST] all games finished, waiting 60 seconds for threads to finish" );
CONSOLE_Print( "[GHOST] there are " + UTIL_ToString( m_Callables.size( ) ) + " threads in progress" );
m_AllGamesFinished = true;
m_AllGamesFinishedTime = GetTime( );
}
else
{
if( m_Callables.empty( ) )
{
CONSOLE_Print( "[GHOST] all threads finished, exiting nicely" );
m_Exiting = true;
}
else if( GetTime( ) - m_AllGamesFinishedTime >= 60 )
{
CONSOLE_Print( "[GHOST] waited 60 seconds for threads to finish, exiting anyway" );
CONSOLE_Print( "[GHOST] there are " + UTIL_ToString( m_Callables.size( ) ) + " threads still in progress which will be terminated" );
m_Exiting = true;
}
}
}
}
// update callables
boost::mutex::scoped_lock callablesLock( m_CallablesMutex );
for( vector<CBaseCallable *> :: iterator i = m_Callables.begin( ); i != m_Callables.end( ); )
{
if( !(*i) )
{
// NULL presumably because we're using SQLite database with unimplemented database call
// so just remove it from callables
i = m_Callables.erase( i );
}
else if( (*i)->GetReady( ) )
{
m_DB->RecoverCallable( *i );
delete *i;
i = m_Callables.erase( i );
}
else
++i;
}
callablesLock.unlock( );
// create the GProxy++ reconnect listener
if( m_Reconnect )
{
if( !m_ReconnectSocket )
{
bool Success = false;
for( unsigned int i = 0; i < 50; i++ )
{
m_ReconnectSocket = new CTCPServer( );
if( m_ReconnectSocket->Listen( m_BindAddress, m_ReconnectPort ) )
{
CONSOLE_Print( "[GHOST] listening for GProxy++ reconnects on port " + UTIL_ToString( m_ReconnectPort ) );
Success = true;
break;
}
else
{
CONSOLE_Print( "[GHOST] error listening for GProxy++ reconnects on port " + UTIL_ToString( m_ReconnectPort ) );
delete m_ReconnectSocket;
m_ReconnectSocket = NULL;
m_ReconnectPort++;
}
}
if( !Success )
{
CONSOLE_Print( "[GHOST] failed to listen for GProxy++ reconnects too many times, giving up" );
m_Reconnect = false;
}
}
else if( m_ReconnectSocket->HasError( ) )
{
CONSOLE_Print( "[GHOST] GProxy++ reconnect listener error (" + m_ReconnectSocket->GetErrorString( ) + ")" );
delete m_ReconnectSocket;
m_ReconnectSocket = NULL;
m_Reconnect = false;
}
}
// create the stream listener
if( m_Stream )
{
if( !m_StreamSocket )
{
bool Success = false;
for( unsigned int i = 0; i < 50; i++ )
{
m_StreamSocket = new CTCPServer( );
if( m_StreamSocket->Listen( string( ), m_StreamPort ) )
{
CONSOLE_Print( "[GHOST] listening for streamers on port " + UTIL_ToString( m_StreamPort ) );
Success = true;
break;
}
else
{
CONSOLE_Print( "[GHOST] error listening for streamers on port " + UTIL_ToString( m_StreamPort ) );
delete m_StreamSocket;
m_StreamSocket = NULL;
m_StreamPort++;
}
}
if( !Success )
{
CONSOLE_Print( "[GHOST] failed to listen for streamers too many times, giving up" );
m_Stream = false;
}
}
else if( m_StreamSocket->HasError( ) )
{
CONSOLE_Print( "[GHOST] streamer listener error (" + m_StreamSocket->GetErrorString( ) + ")" );
delete m_StreamSocket;
m_StreamSocket = NULL;