-
-
Notifications
You must be signed in to change notification settings - Fork 407
/
CVersionUpdater.cpp
3304 lines (2876 loc) · 123 KB
/
CVersionUpdater.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
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* LICENSE: See LICENSE in the top level directory
* FILE: core/CVersionUpdater.cpp
* PURPOSE: Version update check and message dialog class
* DEVELOPERS: You're only supposed to blow the bloody doors off.
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/
#include "StdInc.h"
#include "CVersionUpdater.Util.hpp"
#include "CNewsBrowser.h"
#include "SharedUtil.Thread.h"
///////////////////////////////////////////////////////////////
//
//
// CVersionUpdater
//
//
///////////////////////////////////////////////////////////////
class CVersionUpdater : public CVersionUpdaterInterface
{
public:
ZERO_ON_NEW
// CVersionUpdaterInterface interface
CVersionUpdater ( void );
virtual ~CVersionUpdater ( void );
virtual bool SaveConfigToXML ( void );
virtual void EnableChecking ( bool bOn );
virtual void DoPulse ( void );
virtual void InitiateUpdate ( const SString& strType, const SString& strData, const SString& strHost );
virtual bool IsOptionalUpdateInfoRequired ( const SString& strHost );
virtual void InitiateDataFilesFix ( void );
virtual void InitiateManualCheck ( void );
virtual void GetAseServerList ( std::vector < SString >& outResult );
virtual void InitiateSidegradeLaunch ( const SString& strVersion, const SString& strHost, ushort usPort, const SString& strName, const SString& strPassword );
virtual void GetBrowseVersionMaps ( std::map < SString, int >& outBlockedVersionMap, std::map < SString, int >& outAllowedVersionMap );
virtual void GetNewsSettings ( SString& strOutOldestPost, uint& uiOutMaxHistoryLength );
virtual const SString& GetDebugFilterString ( void );
// CVersionUpdater functions
bool EnsureLoadedConfigFromXML ( void );
// Command lists
void RunProgram ( EUpdaterProgramType strProgramName );
void Program_VersionCheck ( void );
void Program_ManualCheck ( void );
void Program_ManualCheckSim ( void );
void Program_ServerSaysUpdate ( void );
void Program_ServerSaysRecommend ( void );
void Program_ServerSaysDataFilesWrong ( void );
void Program_MasterFetch ( void );
void Program_SendCrashDump ( void );
void Program_SendReportLog ( void );
void Program_SidegradeLaunch ( void );
void Program_NewsUpdate ( void );
// Util
CXMLNode* GetXMLConfigNode ( bool bCreateIfRequired );
bool IsBusy ( void );
void ResetEverything ( void );
CReportWrap* GetReportWrap ( void );
static std::vector < SString > MakeServerList ( const CDataInfoSet& infoMap );
void PostChangeMasterConfig ( void );
void OnPossibleConfigProblem ( void );
// Commands
void _UseMasterFetchURLs ( void );
void _UseVersionQueryURLs ( void );
void _UseProvidedURLs ( void );
void _UseReportLogURLs ( void );
void _UseCrashDumpQueryURLs ( void );
void _UseCrashDumpURLs ( void );
void _UseSidegradeURLs ( void );
void _UseNewsUpdateURLs ( void );
void _UseReportLogPostContent ( void );
void _UseCrashDumpPostContent ( void );
void _ShouldSendCrashDump ( void );
void _ResetLastCrashDump ( void );
void _ActionReconnect ( void );
void _DialogHide ( void );
void _ExitGame ( void );
void _ResetVersionCheckTimer ( void );
void _ResetNewsCheckTimer ( void );
void _ResetMasterCheckTimer ( void );
void _ResetManualCheckTimer ( void );
void _PollQuestionNoYes ( void );
void _PollAnyButton ( void );
void _DialogConnectingWait ( void );
void _DialogChecking ( void );
void _DialogNoUpdate ( void );
void _DialogDownloading ( void );
void _DialogServerSaysUpdateQuestion ( void );
void _DialogServerSaysRecommendQuestion ( void );
void _DialogUpdateQuestion ( void );
void _DialogUpdateQueryError ( void );
void _DialogUpdateResult ( void );
void _QUpdateResult ( void );
void _QUpdateNewsResult ( void );
void _DialogDataFilesQuestion ( void );
void _DialogExeFilesResult ( void );
void _StartDownload ( void );
int _PollDownload ( void );
void _StartSendPost ( void );
int _PollSendPost ( void );
void _CheckSidegradeRequirements ( void );
void _DoSidegradeLaunch ( void );
void _DialogSidegradeDownloadQuestion ( void );
void _DialogSidegradeLaunchQuestion ( void );
void _DialogSidegradeQueryError ( void );
void _ProcessCrashDumpQuery ( void );
void _ProcessMasterFetch ( void );
void _ProcessPatchFileQuery ( void );
void _ProcessPatchFileDownload ( void );
void _QuitCurrentProgram ( void );
// Doers
int DoSendDownloadRequestToNextServer ( void );
int DoPollDownload ( void );
int DoSendPostToNextServer ( void );
int DoPollPost ( void );
static void* StaticThreadProc ( void* pContext );
void* ThreadProc ( void );
void StopThread ( void );
void ProcessCommand ( EUpdaterProgramType eProgramType );
void MainStep ( void );
void UpdaterYield ( void );
SJobInfo m_JobInfo;
CHTTPClient m_HTTP;
long long m_llTimeStart;
CConditionMap m_ConditionMap;
SString m_strServerSaysType;
SString m_strServerSaysData;
SString m_strServerSaysHost;
SString m_strSidegradeVersion;
SString m_strSidegradeHost;
ushort m_usSidegradePort;
SString m_strSidegradeName;
SString m_strSidegradePassword;
SString m_strSidegradePath;
bool m_bCheckedTimeForVersionCheck;
bool m_bCheckedTimeForNewsUpdate;
bool m_bCheckedTimeForMasterFetch;
bool m_bLoadedConfig;
bool m_bSentCrashDump;
bool m_bSentReportLog;
std::map < SString, int > m_DoneOptionalMap;
long long m_llTimeLastManualCheck;
SString m_strLastManualCheckBuildType;
CReportWrap* m_pReportWrap;
SString m_strLastQueryURL;
bool m_bEnabled;
uint m_uiFrameCounter;
int m_iForceMasterConfigRefreshCount;
SUpdaterMasterConfig m_MasterConfig;
SUpdaterVarConfig m_VarConfig;
CThreadHandle* m_pProgramThreadHandle;
// Shared variables
struct
{
bool m_bTerminateThread;
bool m_bThreadTerminated;
EUpdaterProgramType m_CurrentProgram;
CComboMutex m_Mutex;
bool m_bQuitCurrentProgram;
bool m_bExitGame;
} shared;
};
///////////////////////////////////////////////////////////////
//
// Temporary home for global object
//
//
//
///////////////////////////////////////////////////////////////
static CVersionUpdater* g_pVersionUpdater = NULL;
CVersionUpdaterInterface* GetVersionUpdater ()
{
if ( !g_pVersionUpdater )
g_pVersionUpdater = new CVersionUpdater ();
return g_pVersionUpdater;
}
///////////////////////////////////////////////////////////////
//
// CVersionUpdater::CVersionUpdater
//
//
//
///////////////////////////////////////////////////////////////
CVersionUpdater::CVersionUpdater ( void )
{
shared.m_Mutex.Lock ();
m_pProgramThreadHandle = new CThreadHandle( CVersionUpdater::StaticThreadProc, this );
}
///////////////////////////////////////////////////////////////
//
// CVersionUpdater::~CVersionUpdater
//
//
//
///////////////////////////////////////////////////////////////
CVersionUpdater::~CVersionUpdater ( void )
{
StopThread();
SAFE_DELETE ( m_pProgramThreadHandle );
SAFE_DELETE ( m_pReportWrap );
}
///////////////////////////////////////////////////////////////
//
// CVersionUpdater::StopThread
//
// Stop the updater processing thread
//
///////////////////////////////////////////////////////////////
void CVersionUpdater::StopThread( void )
{
shared.m_bTerminateThread = true;
shared.m_Mutex.Signal();
shared.m_Mutex.Unlock();
for ( uint i = 0 ; i < 5000 ; i += 15 )
{
if ( shared.m_bThreadTerminated )
return;
Sleep ( 15 );
}
// If thread not stopped, (async) cancel it
m_pProgramThreadHandle->Cancel();
}
///////////////////////////////////////////////////////////////
//
// CVersionUpdater::GetXMLConfigNode
//
// Get the main node for load/saving settings
//
///////////////////////////////////////////////////////////////
CXMLNode* CVersionUpdater::GetXMLConfigNode ( bool bCreateIfRequired )
{
// Get the root node
CXMLNode *pRoot = CCore::GetSingleton ().GetConfig ();
if ( !pRoot )
return NULL;
// Get the top config node
CXMLNode* pSectionNode = pRoot->FindSubNode ( CONFIG_NODE_UPDATER );
if ( !pSectionNode )
{
if ( !bCreateIfRequired )
return NULL;
// Non-existent, create a new node
pSectionNode = pRoot->CreateSubNode ( CONFIG_NODE_UPDATER );
}
return pSectionNode;
}
///////////////////////////////////////////////////////////////
//
// CVersionUpdater::EnsureLoadedConfigFromXML
//
// Load settings
//
///////////////////////////////////////////////////////////////
bool CVersionUpdater::EnsureLoadedConfigFromXML ( void )
{
if ( m_bLoadedConfig )
return false;
m_bLoadedConfig = true;
// Reset
m_MasterConfig = SUpdaterMasterConfig ();
// Try load
CXMLNode* pMainNode = GetXMLConfigNode ( false );
if ( !pMainNode )
return false;
if ( CXMLNode* pVar = pMainNode->FindSubNode ( "var" ) )
{
CXMLAccess XMLAccess ( pVar );
// Stuff for here
XMLAccess.GetSubNodeValue ( "version_lastchecktime", m_VarConfig.version_lastCheckTime );
XMLAccess.GetSubNodeValue ( "master_lastchecktime", m_VarConfig.master_lastCheckTime );
XMLAccess.GetSubNodeValue ( "master_highestnotifyrevision", m_VarConfig.master_highestNotifyRevision );
XMLAccess.GetSubNodeValue ( "news_lastchecktime", m_VarConfig.news_lastCheckTime );
XMLAccess.GetSubNodeValue ( "news_lastnewsdate", m_VarConfig.news_lastNewsDate );
XMLAccess.GetSubNodeValue ( "crashdump_historylist", m_VarConfig.crashdump_history );
}
if ( CXMLNode* pMaster = pMainNode->FindSubNode ( "mastercache" ) )
{
CXMLAccess XMLAccess ( pMaster );
// Stuff from master
XMLAccess.GetSubNodeValue ( "master.revision", m_MasterConfig.master.strRevision );
XMLAccess.GetSubNodeValue ( "master.serverlist", m_MasterConfig.master.serverInfoMap );
XMLAccess.GetSubNodeValue ( "master.interval", m_MasterConfig.master.interval );
XMLAccess.GetSubNodeValue ( "version.serverlist", m_MasterConfig.version.serverInfoMap );
XMLAccess.GetSubNodeValue ( "version.interval", m_MasterConfig.version.interval );
XMLAccess.GetSubNodeValue ( "report.serverlist", m_MasterConfig.report.serverInfoMap );
XMLAccess.GetSubNodeValue ( "report.interval", m_MasterConfig.report.interval );
XMLAccess.GetSubNodeValue ( "report.filter2", m_MasterConfig.report.strFilter );
XMLAccess.GetSubNodeValue ( "report.minsize", m_MasterConfig.report.iMinSize );
XMLAccess.GetSubNodeValue ( "report.maxsize", m_MasterConfig.report.iMaxSize );
XMLAccess.GetSubNodeValue ( "crashdump.serverlist", m_MasterConfig.crashdump.serverInfoMap );
XMLAccess.GetSubNodeValue ( "crashdump.duplicates", m_MasterConfig.crashdump.iDuplicates );
XMLAccess.GetSubNodeValue ( "crashdump.maxhistorylength", m_MasterConfig.crashdump.iMaxHistoryLength );
XMLAccess.GetSubNodeValue ( "trouble.serverlist", m_MasterConfig.trouble.serverInfoMap );
XMLAccess.GetSubNodeValue ( "ase.serverlist", m_MasterConfig.ase.serverInfoMap );
XMLAccess.GetSubNodeValue ( "sidegrade.serverlist", m_MasterConfig.sidegrade.serverInfoMap );
XMLAccess.GetSubNodeValue ( "sidegrade.nobrowselist", m_MasterConfig.sidegrade.nobrowseInfoMap );
XMLAccess.GetSubNodeValue ( "sidegrade.onlybrowselist", m_MasterConfig.sidegrade.onlybrowseInfoMap );
XMLAccess.GetSubNodeValue ( "news.serverlist", m_MasterConfig.news.serverInfoMap );
XMLAccess.GetSubNodeValue ( "news.interval", m_MasterConfig.news.interval );
XMLAccess.GetSubNodeValue ( "news.oldestpost", m_MasterConfig.news.strOldestPost );
XMLAccess.GetSubNodeValue ( "news.maxhistorylength", m_MasterConfig.news.iMaxHistoryLength );
XMLAccess.GetSubNodeValue ( "misc.debug.filter2", m_MasterConfig.misc.debug.strFilter );
PostChangeMasterConfig ();
}
return true;
}
///////////////////////////////////////////////////////////////
//
// CVersionUpdater::SaveConfigToXML
//
// Save setting
//
///////////////////////////////////////////////////////////////
bool CVersionUpdater::SaveConfigToXML ( void )
{
if ( !m_bLoadedConfig )
return false;
CXMLNode* pMainNode = GetXMLConfigNode ( true );
if ( !pMainNode )
return false;
pMainNode->DeleteAllSubNodes ();
if ( CXMLNode* pVar = pMainNode->CreateSubNode ( "var" ) )
{
CXMLAccess XMLAccess ( pVar );
// Stuff for here
XMLAccess.SetSubNodeValue ( "version_lastchecktime", m_VarConfig.version_lastCheckTime );
XMLAccess.SetSubNodeValue ( "master_lastchecktime", m_VarConfig.master_lastCheckTime );
XMLAccess.SetSubNodeValue ( "master_highestnotifyrevision", m_VarConfig.master_highestNotifyRevision );
XMLAccess.SetSubNodeValue ( "news_lastchecktime", m_VarConfig.news_lastCheckTime );
XMLAccess.SetSubNodeValue ( "news_lastnewsdate", m_VarConfig.news_lastNewsDate );
XMLAccess.SetSubNodeValue ( "crashdump_historylist", m_VarConfig.crashdump_history );
}
if ( CXMLNode* pMaster = pMainNode->CreateSubNode ( "mastercache" ) )
{
CXMLAccess XMLAccess ( pMaster );
// Stuff from master
XMLAccess.SetSubNodeValue ( "master.revision", m_MasterConfig.master.strRevision );
XMLAccess.SetSubNodeValue ( "master.serverlist", m_MasterConfig.master.serverInfoMap );
XMLAccess.SetSubNodeValue ( "master.interval", m_MasterConfig.master.interval );
XMLAccess.SetSubNodeValue ( "version.serverlist", m_MasterConfig.version.serverInfoMap );
XMLAccess.SetSubNodeValue ( "version.interval", m_MasterConfig.version.interval );
XMLAccess.SetSubNodeValue ( "report.serverlist", m_MasterConfig.report.serverInfoMap );
XMLAccess.SetSubNodeValue ( "report.interval", m_MasterConfig.report.interval );
XMLAccess.SetSubNodeValue ( "report.filter2", m_MasterConfig.report.strFilter );
XMLAccess.SetSubNodeValue ( "report.minsize", m_MasterConfig.report.iMinSize );
XMLAccess.SetSubNodeValue ( "report.maxsize", m_MasterConfig.report.iMaxSize );
XMLAccess.SetSubNodeValue ( "crashdump.serverlist", m_MasterConfig.crashdump.serverInfoMap );
XMLAccess.SetSubNodeValue ( "crashdump.duplicates", m_MasterConfig.crashdump.iDuplicates );
XMLAccess.SetSubNodeValue ( "crashdump.maxhistorylength", m_MasterConfig.crashdump.iMaxHistoryLength );
XMLAccess.SetSubNodeValue ( "trouble.serverlist", m_MasterConfig.trouble.serverInfoMap );
XMLAccess.SetSubNodeValue ( "ase.serverlist", m_MasterConfig.ase.serverInfoMap );
XMLAccess.SetSubNodeValue ( "sidegrade.serverlist", m_MasterConfig.sidegrade.serverInfoMap );
XMLAccess.SetSubNodeValue ( "sidegrade.nobrowselist", m_MasterConfig.sidegrade.nobrowseInfoMap );
XMLAccess.SetSubNodeValue ( "sidegrade.onlybrowselist", m_MasterConfig.sidegrade.onlybrowseInfoMap );
XMLAccess.SetSubNodeValue ( "news.serverlist", m_MasterConfig.news.serverInfoMap );
XMLAccess.SetSubNodeValue ( "news.interval", m_MasterConfig.news.interval );
XMLAccess.SetSubNodeValue ( "news.oldestpost", m_MasterConfig.news.strOldestPost );
XMLAccess.SetSubNodeValue ( "news.maxhistorylength", m_MasterConfig.news.iMaxHistoryLength );
XMLAccess.SetSubNodeValue ( "misc.debug.filter2", m_MasterConfig.misc.debug.strFilter );
}
return true;
}
///////////////////////////////////////////////////////////////
//
// CVersionUpdater::EnableChecking
//
//
//
///////////////////////////////////////////////////////////////
void CVersionUpdater::EnableChecking( bool bOn )
{
m_bEnabled = bOn;
EnsureLoadedConfigFromXML ();
}
///////////////////////////////////////////////////////////////
//
// CVersionUpdater::GetReportWrap
//
//
//
///////////////////////////////////////////////////////////////
CReportWrap* CVersionUpdater::GetReportWrap()
{
if ( !m_pReportWrap )
m_pReportWrap = new CReportWrap();
return m_pReportWrap;
}
///////////////////////////////////////////////////////////////
//
// CVersionUpdater::DoPulse
//
//
//
///////////////////////////////////////////////////////////////
void CVersionUpdater::DoPulse ( void )
{
if ( !m_bEnabled )
return;
TIMING_CHECKPOINT( "+VersionUpdaterPulse" );
EnsureLoadedConfigFromXML ();
//
// Time for master check?
//
if ( !m_bCheckedTimeForMasterFetch && !IsBusy () )
{
m_bCheckedTimeForMasterFetch = true;
// Do update from master now if our config is wrong
if ( !m_MasterConfig.IsValid () )
m_VarConfig.master_lastCheckTime.SetFromSeconds ( 0 );
// Master update interval maximum is 7 days
if ( m_MasterConfig.master.interval.ToSeconds () > CTimeSpan( "7d" ).ToSeconds () )
m_MasterConfig.master.interval.SetFromString ( "7d" );
time_t secondsSinceCheck = CDateTime::Now ().ToSeconds () - m_VarConfig.master_lastCheckTime.ToSeconds ();
OutputDebugLine ( SString ( "[Updater] master timeSinceCheck: %d time till next check: %d", (int)(secondsSinceCheck), (int)(m_MasterConfig.master.interval.ToSeconds () - secondsSinceCheck) ) );
// Only check once a week
if ( secondsSinceCheck > m_MasterConfig.master.interval.ToSeconds () || secondsSinceCheck < 0 )
{
RunProgram ( EUpdaterProgramType::MasterFetch );
}
}
//
// Time for periodic check?
//
if ( !m_bCheckedTimeForVersionCheck && !IsBusy () && !CCore::GetSingleton().WasLaunchedWithConnectURI () )
{
m_bCheckedTimeForVersionCheck = true;
// Do update check now if game did not stop cleanly last time
if ( WatchDogWasUncleanStop () )
m_VarConfig.version_lastCheckTime.SetFromSeconds ( 0 );
time_t secondsSinceCheck = CDateTime::Now ().ToSeconds () - m_VarConfig.version_lastCheckTime.ToSeconds ();
OutputDebugLine ( SString ( "[Updater] version timeSinceCheck: %d time till next check: %d", (int)(secondsSinceCheck), (int)(m_MasterConfig.version.interval.ToSeconds () - secondsSinceCheck) ) );
// Only check once a day
if ( secondsSinceCheck > m_MasterConfig.version.interval.ToSeconds () || secondsSinceCheck < 0 )
{
RunProgram ( EUpdaterProgramType::VersionCheck );
}
}
//
// Time for news update?
//
if ( !m_bCheckedTimeForNewsUpdate && !IsBusy () )
{
m_bCheckedTimeForNewsUpdate = true;
time_t secondsSinceCheck = CDateTime::Now ().ToSeconds () - m_VarConfig.news_lastCheckTime.ToSeconds ();
OutputDebugLine ( SString ( "[Updater] news timeSinceCheck: %d time till next check: %d", (int)(secondsSinceCheck), (int)(m_MasterConfig.news.interval.ToSeconds () - secondsSinceCheck) ) );
// Only check once an interval
if ( secondsSinceCheck > m_MasterConfig.news.interval.ToSeconds () || secondsSinceCheck < 0 )
{
RunProgram ( EUpdaterProgramType::NewsUpdate );
}
}
//
// Should send previous crash dump?
//
if ( !m_bSentCrashDump && !IsBusy () )
{
m_bSentCrashDump = true;
RunProgram ( EUpdaterProgramType::SendCrashDump );
}
//
// Should send report on how new features are functioning?
//
if ( !m_bSentReportLog && !IsBusy () )
{
m_bSentReportLog = true;
RunProgram ( EUpdaterProgramType::SendReportLog );
}
//
// Give updater thread some time if it is doing something
//
if ( IsBusy() )
{
MainStep();
}
TIMING_CHECKPOINT( "-VersionUpdaterPulse" );
}
///////////////////////////////////////////////////////////////
//
// CVersionUpdater::InitiateUpdate
//
//
//
///////////////////////////////////////////////////////////////
void CVersionUpdater::InitiateUpdate ( const SString& strType, const SString& strData, const SString& strHost )
{
if ( strType == "Mandatory" )
{
CCore::GetSingleton ().RemoveMessageBox ();
RunProgram ( EUpdaterProgramType::ServerSaysUpdate );
}
else
if ( strType == "Optional" || strType == "Recommended" )
{
CCore::GetSingleton ().RemoveMessageBox ();
MapSet ( m_DoneOptionalMap, strHost, 1 );
RunProgram ( EUpdaterProgramType::ServerSaysRecommend );
}
m_strServerSaysType = strType;
m_strServerSaysData = strData;
m_strServerSaysHost = strHost;
}
///////////////////////////////////////////////////////////////
//
// CVersionUpdater::IsOptionalUpdateInfoRequired
//
//
//
///////////////////////////////////////////////////////////////
bool CVersionUpdater::IsOptionalUpdateInfoRequired ( const SString& strHost )
{
// Should this server tell us about recommended updates?
return !MapContains ( m_DoneOptionalMap, strHost );
}
///////////////////////////////////////////////////////////////
//
// CVersionUpdater::InitiateDataFilesFix
//
//
//
///////////////////////////////////////////////////////////////
void CVersionUpdater::InitiateDataFilesFix ( void )
{
RunProgram ( EUpdaterProgramType::ServerSaysDataFilesWrong );
}
///////////////////////////////////////////////////////////////
//
// CVersionUpdater::InitiateManualCheck
//
//
//
///////////////////////////////////////////////////////////////
void CVersionUpdater::InitiateManualCheck ( void )
{
if ( GetQuestionBox ().IsVisible () )
{
// Bring to the front
GetQuestionBox ().Show ();
return;
}
if ( IsBusy () )
{
CCore::GetSingleton ().ShowMessageBox ( _("Busy"), _("Can't check for updates right now"), MB_BUTTON_OK | MB_ICON_ERROR );
return;
}
SString strUpdateBuildType;
CVARS_GET ( "update_build_type", strUpdateBuildType );
if ( GetTickCount64_ () - m_llTimeLastManualCheck > 10000 || strUpdateBuildType != m_strLastManualCheckBuildType )
{
RunProgram ( EUpdaterProgramType::ManualCheck );
}
else
{
// Pretend to do a check if less than a minute since last check
RunProgram ( EUpdaterProgramType::ManualCheckSim );
}
}
///////////////////////////////////////////////////////////////
//
// CVersionUpdater::InitiateSidegradeLaunch
//
//
//
///////////////////////////////////////////////////////////////
void CVersionUpdater::InitiateSidegradeLaunch ( const SString& strVersion, const SString& strHost, ushort usPort, const SString& strName, const SString& strPassword )
{
m_strSidegradeVersion = strVersion;
m_strSidegradeHost = strHost;
m_usSidegradePort = usPort;
m_strSidegradeName = strName;
m_strSidegradePassword = strPassword;
RunProgram ( EUpdaterProgramType::SidegradeLaunch );
m_strServerSaysHost = SString ( "%s:%d", *strHost, usPort );
}
///////////////////////////////////////////////////////////////
//
// CVersionUpdater::GetAseServerList
//
//
//
///////////////////////////////////////////////////////////////
void CVersionUpdater::GetAseServerList ( std::vector < SString >& outResult )
{
EnsureLoadedConfigFromXML ();
outResult = MakeServerList ( m_MasterConfig.ase.serverInfoMap );
// Backup plan if list is empty
if ( outResult.empty () )
outResult.push_back ( "http://1mgg.com/affil/mta" );
}
///////////////////////////////////////////////////////////////
//
// CVersionUpdater::GetBrowseVersionMaps
//
//
//
///////////////////////////////////////////////////////////////
void CVersionUpdater::GetBrowseVersionMaps ( std::map < SString, int >& outBlockedVersionMap, std::map < SString, int >& outAllowedVersionMap )
{
EnsureLoadedConfigFromXML ();
outBlockedVersionMap.clear ();
outAllowedVersionMap.clear ();
//
// Blocked - Find list for our version
//
{
const CDataInfoSet& dataInfoSet = m_MasterConfig.sidegrade.nobrowseInfoMap;
for ( uint i = 0 ; i < dataInfoSet.size () ; i++ )
{
const SDataInfoItem& line = dataInfoSet[i];
const SString* pstrVersion = MapFind ( line.attributeMap, "version" );
if ( pstrVersion && *pstrVersion == MTA_DM_ASE_VERSION )
{
// Found list for our version
std::vector < SString > blockedVersionList;
line.strValue.Split ( ";", blockedVersionList );
for ( uint i = 0 ; i < blockedVersionList.size () ; i++ )
{
outBlockedVersionMap[ blockedVersionList[i] ] = 1;
}
break;
}
}
}
//
// Allowed - Find list for our version
//
{
const CDataInfoSet& dataInfoSet = m_MasterConfig.sidegrade.onlybrowseInfoMap;
for ( uint i = 0 ; i < dataInfoSet.size () ; i++ )
{
const SDataInfoItem& line = dataInfoSet[i];
const SString* pstrVersion = MapFind ( line.attributeMap, "version" );
if ( pstrVersion && *pstrVersion == MTA_DM_ASE_VERSION )
{
// Found list for our version
std::vector < SString > allowedVersionList;
line.strValue.Split ( ";", allowedVersionList );
for ( uint i = 0 ; i < allowedVersionList.size () ; i++ )
{
outAllowedVersionMap[ allowedVersionList[i] ] = 1;
}
break;
}
}
}
}
///////////////////////////////////////////////////////////////
//
// CVersionUpdater::GetDebugFilterString
//
//
//
///////////////////////////////////////////////////////////////
const SString& CVersionUpdater::GetDebugFilterString ( void )
{
EnsureLoadedConfigFromXML ();
return m_MasterConfig.misc.debug.strFilter;
}
///////////////////////////////////////////////////////////////
//
// CVersionUpdater::ResetEverything
//
//
//
///////////////////////////////////////////////////////////////
void CVersionUpdater::ResetEverything ()
{
m_JobInfo = SJobInfo();
m_HTTP.Get ("");
m_llTimeStart = 0;
m_ConditionMap.clear ();
m_strServerSaysType = "";
m_strServerSaysData = "";
m_strServerSaysHost = "";
GetQuestionBox ().Reset ();
shared.m_bQuitCurrentProgram = false;
}
///////////////////////////////////////////////////////////////
//
// CVersionUpdater::IsBusy
//
//
//
///////////////////////////////////////////////////////////////
bool CVersionUpdater::IsBusy ( void )
{
if ( shared.m_CurrentProgram != EUpdaterProgramType::None )
return true;
if ( GetQuestionBox ().IsVisible () )
return true;
if ( CCore::GetSingleton ().WillRequestNewNickOnStart() )
return true;
return false;
}
///////////////////////////////////////////////////////////////
//
// CVersionUpdater::MakeServerList
//
// Make ordered server list from config map
//
///////////////////////////////////////////////////////////////
std::vector < SString > CVersionUpdater::MakeServerList ( const CDataInfoSet& dataInfoSetIn )
{
std::vector < SString > serverList;
RandomizeRandomSeed ();
//
// Sort out server order
//
CDataInfoSet dataInfoSet = dataInfoSetIn;
if ( int iSize = dataInfoSet.size () )
{
// randomize list
for ( int i = 0 ; i < iSize * 10 ; i++ )
{
int iThis = i % iSize;
int iOther = ( rand () + 1 ) % iSize;
std::swap ( dataInfoSet[iThis], dataInfoSet[iOther] );
}
// bubble sort based on the priority attribute
for ( int i = 0 ; i < iSize - 1 ; i++ )
{
SDataInfoItem& a = dataInfoSet[i];
SDataInfoItem& b = dataInfoSet[i + 1];
int iPriorityA = MapFind ( a.attributeMap, "priority" ) ? atoi ( *MapFind ( a.attributeMap, "priority" ) ) : 5;
int iPriorityB = MapFind ( b.attributeMap, "priority" ) ? atoi ( *MapFind ( b.attributeMap, "priority" ) ) : 5;
if ( iPriorityA > iPriorityB )
{
std::swap ( a, b );
i = Max ( i - 2, -1 );
}
}
// If servers have the same priority, keep one and put the other(s) at the back of the list
CDataInfoSet keepList;
CDataInfoSet tailList;
for ( int iFirst = 0 ; iFirst < iSize ; iFirst++ )
{
// First item in range
const SDataInfoItem& a = dataInfoSet[iFirst];
int iPriorityA = MapFind ( a.attributeMap, "priority" ) ? atoi ( *MapFind ( a.attributeMap, "priority" ) ) : 5;
// Find last item in range
int iLast;
for ( iLast = iFirst ; iLast < iSize - 1 ; iLast++ )
{
const SDataInfoItem& b = dataInfoSet[ iLast + 1 ];
int iPriorityB = MapFind ( b.attributeMap, "priority" ) ? atoi ( *MapFind ( b.attributeMap, "priority" ) ) : 5;
if ( iPriorityA != iPriorityB )
break;
}
// Choose random server to keep in range, put the others at the back
int iRangeSize = iLast - iFirst + 1;
int iKeepIdx = rand () % ( iRangeSize );
for ( int i = 0 ; i < iRangeSize ; i++ )
{
if ( i == iKeepIdx )
keepList.push_back ( dataInfoSet[ iFirst + i ] );
else
tailList.push_back ( dataInfoSet[ iFirst + i ] );
}
// continue with first item in the next priority range
iFirst = iLast;
}
// Output values into the result list
for ( uint i = 0 ; i < keepList.size () ; i++ )
serverList.push_back ( keepList[i].strValue );
for ( uint i = 0 ; i < tailList.size () ; i++ )
serverList.push_back ( tailList[i].strValue );
}
return serverList;
}
///////////////////////////////////////////////////////////////
//
// CVersionUpdater::OnPossibleConfigProblem
//
//
//
///////////////////////////////////////////////////////////////
void CVersionUpdater::OnPossibleConfigProblem ( void )
{
// Limit number of times this can be done
if ( m_iForceMasterConfigRefreshCount < 1 )
{
m_iForceMasterConfigRefreshCount++;
// Queue master config refresh
m_VarConfig.master_lastCheckTime.SetFromSeconds ( 0 );
m_bCheckedTimeForMasterFetch = false;
}
}
///////////////////////////////////////////////////////////////
//
// CVersionUpdater::RunProgram
//
//
//
///////////////////////////////////////////////////////////////
void CVersionUpdater::RunProgram ( EUpdaterProgramType strProgramName )
{
assert( IsMainThread() );
OutputDebugLine ( SString ( "[Updater] RunProgram %d", strProgramName ) );
// Stop any running program
while( shared.m_CurrentProgram != EUpdaterProgramType::None )
{
shared.m_bQuitCurrentProgram = true;
MainStep();
}
ResetEverything();
shared.m_CurrentProgram = strProgramName;
}
///////////////////////////////////////////////////////////////
//
// CVersionUpdater::MainStep
//
// Let the updater thread have some time
//
///////////////////////////////////////////////////////////////
void CVersionUpdater::MainStep( void )
{
assert( IsMainThread() );
shared.m_Mutex.Signal();
shared.m_Mutex.Wait( -1 );
if ( shared.m_bExitGame )
CCore::GetSingleton().Quit();
}
///////////////////////////////////////////////////////////////
//
// CVersionUpdater::UpdaterYield
//
// Let the main thread continue
//
///////////////////////////////////////////////////////////////
void CVersionUpdater::UpdaterYield( void )
{
assert( !IsMainThread() );
shared.m_Mutex.Signal();
shared.m_Mutex.Wait( -1 );
if ( shared.m_bQuitCurrentProgram )
_QuitCurrentProgram();
}
//
//
//
// Job servicing thread
//
//
//
///////////////////////////////////////////////////////////////
//
// CVersionUpdater::StaticThreadProc
//
// static function
// Thread entry point
//
///////////////////////////////////////////////////////////////
void* CVersionUpdater::StaticThreadProc ( void* pContext )
{
CThreadHandle::AllowASyncCancel();