-
-
Notifications
You must be signed in to change notification settings - Fork 452
/
Copy pathCCore.cpp
2420 lines (2045 loc) · 68.2 KB
/
CCore.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/CCore.cpp
* PURPOSE: Base core class
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/
#include "StdInc.h"
#include <game/CGame.h>
#include <game/CSettings.h>
#include <Accctrl.h>
#include <Aclapi.h>
#include <filesystem>
#include "Userenv.h" // This will enable SharedUtil::ExpandEnvString
#define ALLOC_STATS_MODULE_NAME "core"
#include "SharedUtil.hpp"
#include <clocale>
#include "DXHook/CDirect3DHook9.h"
#include "DXHook/CDirect3DHookManager.h"
#include "CTimingCheckpoints.hpp"
#include "CModelCacheManager.h"
#include <SharedUtil.Detours.h>
#include <ServerBrowser/CServerCache.h>
#include "CDiscordRichPresence.h"
using SharedUtil::CalcMTASAPath;
using namespace std;
namespace fs = std::filesystem;
static float fTest = 1;
extern CCore* g_pCore;
bool g_bBoundsChecker = false;
SString g_strJingleBells;
extern fs::path g_gtaDirectory;
template <>
CCore* CSingleton<CCore>::m_pSingleton = NULL;
static auto Win32LoadLibraryA = static_cast<decltype(&LoadLibraryA)>(nullptr);
static constexpr long long TIME_DISCORD_UPDATE_RICH_PRESENCE_RATE = 10000;
static HMODULE WINAPI SkipDirectPlay_LoadLibraryA(LPCSTR fileName)
{
// GTA:SA expects a valid module handle for DirectPlay. We return a handle for an already loaded library.
if (!StrCmpIA("dpnhpast.dll", fileName))
return Win32LoadLibraryA("d3d8.dll");
if (!StrCmpIA("enbseries\\enbhelper.dll", fileName))
{
std::error_code ec;
// Try to load enbhelper.dll from our custom launch directory first.
const fs::path inLaunchDir = fs::path{FromUTF8(GetLaunchPath())} / "enbseries" / "enbhelper.dll";
if (fs::is_regular_file(inLaunchDir, ec))
return Win32LoadLibraryA(inLaunchDir.u8string().c_str());
// Try to load enbhelper.dll from the GTA install directory second.
const fs::path inGTADir = g_gtaDirectory / "enbseries" / "enbhelper.dll";
if (fs::is_regular_file(inGTADir, ec))
return Win32LoadLibraryA(inGTADir.u8string().c_str());
return nullptr;
}
return Win32LoadLibraryA(fileName);
}
CCore::CCore()
{
// Initialize the global pointer
g_pCore = this;
m_pConfigFile = NULL;
// Set our locale to the C locale, except for character handling which is the system's default
std::setlocale(LC_ALL, "C");
std::setlocale(LC_CTYPE, "");
// check LC_COLLATE is the old-time raw ASCII sort order
assert(strcoll("a", "B") > 0);
// Parse the command line
const char* pszNoValOptions[] = {"window", NULL};
ParseCommandLine(m_CommandLineOptions, m_szCommandLineArgs, pszNoValOptions);
// Load our settings and localization as early as possible
CreateXML();
ApplyCoreInitSettings();
g_pLocalization = new CLocalization;
// Create a logger instance.
m_pConsoleLogger = new CConsoleLogger();
// Create interaction objects.
m_pCommands = new CCommands;
m_pConnectManager = new CConnectManager;
// Create the GUI manager and the graphics lib wrapper
m_pLocalGUI = new CLocalGUI;
m_pGraphics = new CGraphics(m_pLocalGUI);
g_pGraphics = m_pGraphics;
m_pGUI = NULL;
// Create the mod manager
m_pModManager = new CModManager;
CCrashDumpWriter::SetHandlers();
m_pfnMessageProcessor = NULL;
m_pMessageBox = NULL;
m_bFirstFrame = true;
m_bIsOfflineMod = false;
m_bQuitOnPulse = false;
m_bDestroyMessageBox = false;
m_bCursorToggleControls = false;
m_bLastFocused = true;
m_bWaitToSetNick = false;
m_DiagnosticDebug = EDiagnosticDebug::NONE;
// Create our Direct3DData handler.
m_pDirect3DData = new CDirect3DData;
WriteDebugEvent("CCore::CCore");
m_pKeyBinds = new CKeyBinds(this);
m_pMouseControl = new CMouseControl();
// Create our hook objects.
// m_pFileSystemHook = new CFileSystemHook ( );
m_pDirect3DHookManager = new CDirect3DHookManager();
m_pDirectInputHookManager = new CDirectInputHookManager();
m_pMessageLoopHook = new CMessageLoopHook();
m_pSetCursorPosHook = new CSetCursorPosHook();
// Register internal commands.
RegisterCommands();
// Setup our hooks.
ApplyHooks();
// No initial fps limit
m_bDoneFrameRateLimit = false;
m_uiFrameRateLimit = 0;
m_uiServerFrameRateLimit = 0;
m_uiNewNickWaitFrames = 0;
m_iUnminimizeFrameCounter = 0;
m_bDidRecreateRenderTargets = false;
m_fMinStreamingMemory = 0;
m_fMaxStreamingMemory = 0;
m_bGettingIdleCallsFromMultiplayer = false;
m_bWindowsTimerEnabled = false;
m_timeDiscordAppLastUpdate = 0;
// Create tray icon
m_pTrayIcon = new CTrayIcon();
// Create discord rich presence
m_pDiscordRichPresence = std::shared_ptr<CDiscordRichPresence>(new CDiscordRichPresence());
}
CCore::~CCore()
{
WriteDebugEvent("CCore::~CCore");
// Reset Discord rich presence
if (m_pDiscordRichPresence)
m_pDiscordRichPresence.reset();
// Destroy tray icon
delete m_pTrayIcon;
// This will set the GTA volume to the GTA volume value in the settings,
// and is not affected by the master volume setting.
m_pLocalGUI->GetMainMenu()->GetSettingsWindow()->ResetGTAVolume();
// Remove input hook
CMessageLoopHook::GetSingleton().RemoveHook();
// Delete the mod manager
delete m_pModManager;
SAFE_DELETE(m_pMessageBox);
// Destroy early subsystems
m_bModulesLoaded = false;
DestroyNetwork();
DestroyMultiplayer();
DestroyGame();
// Remove global events
g_pCore->m_pGUI->ClearInputHandlers(INPUT_CORE);
// Store core variables to cvars
CVARS_SET("console_pos", m_pLocalGUI->GetConsole()->GetPosition());
CVARS_SET("console_size", m_pLocalGUI->GetConsole()->GetSize());
// Delete interaction objects.
delete m_pCommands;
delete m_pConnectManager;
delete m_pDirect3DData;
// Delete hooks.
delete m_pSetCursorPosHook;
// delete m_pFileSystemHook;
delete m_pDirect3DHookManager;
delete m_pDirectInputHookManager;
// Delete the GUI manager
delete m_pLocalGUI;
delete m_pGraphics;
// Delete the web
DestroyWeb();
// Delete lazy subsystems
DestroyGUI();
DestroyXML();
// Delete keybinds
delete m_pKeyBinds;
// Delete Mouse Control
delete m_pMouseControl;
// Delete the logger
delete m_pConsoleLogger;
// Delete last so calls to GetHookedWindowHandle do not crash
delete m_pMessageLoopHook;
}
eCoreVersion CCore::GetVersion()
{
return MTACORE_20;
}
CConsoleInterface* CCore::GetConsole()
{
return m_pLocalGUI->GetConsole();
}
CCommandsInterface* CCore::GetCommands()
{
return m_pCommands;
}
CGame* CCore::GetGame()
{
return m_pGame;
}
CGraphicsInterface* CCore::GetGraphics()
{
return m_pGraphics;
}
CModManagerInterface* CCore::GetModManager()
{
return m_pModManager;
}
CMultiplayer* CCore::GetMultiplayer()
{
return m_pMultiplayer;
}
CXMLNode* CCore::GetConfig()
{
if (!m_pConfigFile)
return NULL;
CXMLNode* pRoot = m_pConfigFile->GetRootNode();
if (!pRoot)
pRoot = m_pConfigFile->CreateRootNode(CONFIG_ROOT);
return pRoot;
}
CGUI* CCore::GetGUI()
{
return m_pGUI;
}
CNet* CCore::GetNetwork()
{
return m_pNet;
}
CKeyBindsInterface* CCore::GetKeyBinds()
{
return m_pKeyBinds;
}
CLocalGUI* CCore::GetLocalGUI()
{
return m_pLocalGUI;
}
void CCore::SaveConfig(bool bWaitUntilFinished)
{
if (m_pConfigFile)
{
CXMLNode* pBindsNode = GetConfig()->FindSubNode(CONFIG_NODE_KEYBINDS);
if (!pBindsNode)
pBindsNode = GetConfig()->CreateSubNode(CONFIG_NODE_KEYBINDS);
m_pKeyBinds->SaveToXML(pBindsNode);
GetVersionUpdater()->SaveConfigToXML();
m_pConfigFile->Write();
GetServerCache()->SaveServerCache(bWaitUntilFinished);
}
}
void CCore::ChatEcho(const char* szText, bool bColorCoded)
{
CChat* pChat = m_pLocalGUI->GetChat();
if (pChat)
{
CColor color(255, 255, 255, 255);
pChat->SetTextColor(color);
}
// Echo it to the console and chat
m_pLocalGUI->EchoChat(szText, bColorCoded);
if (bColorCoded)
{
m_pLocalGUI->EchoConsole(RemoveColorCodes(szText));
}
else
m_pLocalGUI->EchoConsole(szText);
}
void CCore::DebugEcho(const char* szText)
{
CDebugView* pDebugView = m_pLocalGUI->GetDebugView();
if (pDebugView)
{
CColor color(255, 255, 255, 255);
pDebugView->SetTextColor(color);
}
m_pLocalGUI->EchoDebug(szText);
}
void CCore::DebugPrintf(const char* szFormat, ...)
{
// Convert it to a string buffer
char szBuffer[1024];
va_list ap;
va_start(ap, szFormat);
VSNPRINTF(szBuffer, 1024, szFormat, ap);
va_end(ap);
DebugEcho(szBuffer);
}
void CCore::SetDebugVisible(bool bVisible)
{
if (m_pLocalGUI)
{
m_pLocalGUI->SetDebugViewVisible(bVisible);
}
}
bool CCore::IsDebugVisible()
{
if (m_pLocalGUI)
return m_pLocalGUI->IsDebugViewVisible();
else
return false;
}
void CCore::DebugEchoColor(const char* szText, unsigned char R, unsigned char G, unsigned char B)
{
// Set the color
CDebugView* pDebugView = m_pLocalGUI->GetDebugView();
if (pDebugView)
{
CColor color(R, G, B, 255);
pDebugView->SetTextColor(color);
}
m_pLocalGUI->EchoDebug(szText);
}
void CCore::DebugPrintfColor(const char* szFormat, unsigned char R, unsigned char G, unsigned char B, ...)
{
// Set the color
if (szFormat)
{
// Convert it to a string buffer
char szBuffer[1024];
va_list ap;
va_start(ap, B);
VSNPRINTF(szBuffer, 1024, szFormat, ap);
va_end(ap);
// Echo it to the console and chat
DebugEchoColor(szBuffer, R, G, B);
}
}
void CCore::DebugClear()
{
CDebugView* pDebugView = m_pLocalGUI->GetDebugView();
if (pDebugView)
{
pDebugView->Clear();
}
}
void CCore::ChatEchoColor(const char* szText, unsigned char R, unsigned char G, unsigned char B, bool bColorCoded)
{
// Set the color
CChat* pChat = m_pLocalGUI->GetChat();
if (pChat)
{
CColor color(R, G, B, 255);
pChat->SetTextColor(color);
}
// Echo it to the console and chat
m_pLocalGUI->EchoChat(szText, bColorCoded);
if (bColorCoded)
{
m_pLocalGUI->EchoConsole(RemoveColorCodes(szText));
}
else
m_pLocalGUI->EchoConsole(szText);
}
void CCore::ChatPrintf(const char* szFormat, bool bColorCoded, ...)
{
// Convert it to a string buffer
char szBuffer[1024];
va_list ap;
va_start(ap, bColorCoded);
VSNPRINTF(szBuffer, 1024, szFormat, ap);
va_end(ap);
// Echo it to the console and chat
ChatEcho(szBuffer, bColorCoded);
}
void CCore::ChatPrintfColor(const char* szFormat, bool bColorCoded, unsigned char R, unsigned char G, unsigned char B, ...)
{
// Set the color
if (szFormat)
{
if (m_pLocalGUI)
{
// Convert it to a string buffer
char szBuffer[1024];
va_list ap;
va_start(ap, B);
VSNPRINTF(szBuffer, 1024, szFormat, ap);
va_end(ap);
// Echo it to the console and chat
ChatEchoColor(szBuffer, R, G, B, bColorCoded);
}
}
}
void CCore::SetChatVisible(bool bVisible, bool bInputBlocked)
{
if (m_pLocalGUI)
{
m_pLocalGUI->SetChatBoxVisible(bVisible, bInputBlocked);
}
}
bool CCore::IsChatVisible()
{
if (m_pLocalGUI)
{
return m_pLocalGUI->IsChatBoxVisible();
}
return false;
}
bool CCore::IsChatInputBlocked()
{
if (m_pLocalGUI)
{
return m_pLocalGUI->IsChatBoxInputBlocked();
}
return false;
}
bool CCore::ClearChat()
{
if (m_pLocalGUI)
{
CChat* pChat = m_pLocalGUI->GetChat();
if (pChat)
{
pChat->Clear();
return true;
}
}
return false;
}
void CCore::InitiateScreenShot(bool bIsCameraShot)
{
CScreenShot::InitiateScreenShot(bIsCameraShot);
}
void CCore::EnableChatInput(char* szCommand, DWORD dwColor)
{
if (m_pLocalGUI)
{
if (m_pGame->GetSystemState() == 9 /* GS_PLAYING_GAME */ && m_pModManager->GetCurrentMod() != NULL && !IsOfflineMod() && !m_pGame->IsAtMenu() &&
!m_pLocalGUI->GetMainMenu()->IsVisible() && !m_pLocalGUI->GetConsole()->IsVisible() && !m_pLocalGUI->IsChatBoxInputEnabled())
{
CChat* pChat = m_pLocalGUI->GetChat();
pChat->SetCommand(szCommand);
m_pLocalGUI->SetChatBoxInputEnabled(true);
}
}
}
bool CCore::IsChatInputEnabled()
{
if (m_pLocalGUI)
{
return (m_pLocalGUI->IsChatBoxInputEnabled());
}
return false;
}
bool CCore::SetChatboxCharacterLimit(int charLimit)
{
CChat* pChat = m_pLocalGUI->GetChat();
if (!pChat)
return false;
pChat->SetCharacterLimit(charLimit);
return true;
}
void CCore::ResetChatboxCharacterLimit()
{
CChat* pChat = m_pLocalGUI->GetChat();
if (!pChat)
return;
pChat->SetCharacterLimit(pChat->GetDefaultCharacterLimit());
}
int CCore::GetChatboxCharacterLimit()
{
CChat* pChat = m_pLocalGUI->GetChat();
if (!pChat)
return 0;
return pChat->GetCharacterLimit();
}
int CCore::GetChatboxMaxCharacterLimit()
{
CChat* pChat = m_pLocalGUI->GetChat();
if (!pChat)
return 0;
return pChat->GetMaxCharacterLimit();
}
bool CCore::IsSettingsVisible()
{
if (m_pLocalGUI)
{
return (m_pLocalGUI->GetMainMenu()->GetSettingsWindow()->IsVisible());
}
return false;
}
bool CCore::IsMenuVisible() const noexcept
{
if (m_pLocalGUI)
{
return (m_pLocalGUI->GetMainMenu()->IsVisible());
}
return false;
}
bool CCore::IsCursorForcedVisible()
{
if (m_pLocalGUI)
{
return (m_pLocalGUI->IsCursorForcedVisible());
}
return false;
}
void CCore::ApplyConsoleSettings()
{
CVector2D vec;
CConsole* pConsole = m_pLocalGUI->GetConsole();
CVARS_GET("console_pos", vec);
pConsole->SetPosition(vec);
CVARS_GET("console_size", vec);
pConsole->SetSize(vec);
}
void CCore::ApplyGameSettings()
{
bool bVal;
int iVal;
float fVal;
CControllerConfigManager* pController = m_pGame->GetControllerConfigManager();
CGameSettings* pGameSettings = m_pGame->GetSettings();
CVARS_GET("invert_mouse", bVal);
pController->SetMouseInverted(bVal);
CVARS_GET("fly_with_mouse", bVal);
pController->SetFlyWithMouse(bVal);
CVARS_GET("steer_with_mouse", bVal);
pController->SetSteerWithMouse(bVal);
CVARS_GET("classic_controls", bVal);
pController->SetClassicControls(bVal);
CVARS_GET("volumetric_shadows", bVal);
pGameSettings->SetVolumetricShadowsEnabled(bVal);
CVARS_GET("aspect_ratio", iVal);
pGameSettings->SetAspectRatio((eAspectRatio)iVal, CVARS_GET_VALUE<bool>("hud_match_aspect_ratio"));
CVARS_GET("grass", bVal);
pGameSettings->SetGrassEnabled(bVal);
CVARS_GET("heat_haze", bVal);
m_pMultiplayer->SetHeatHazeEnabled(bVal);
CVARS_GET("fast_clothes_loading", iVal);
m_pMultiplayer->SetFastClothesLoading((CMultiplayer::EFastClothesLoading)iVal);
CVARS_GET("tyre_smoke_enabled", bVal);
m_pMultiplayer->SetTyreSmokeEnabled(bVal);
pGameSettings->UpdateFieldOfViewFromSettings();
pGameSettings->ResetBlurEnabled();
pGameSettings->ResetVehiclesLODDistance();
pGameSettings->ResetPedsLODDistance();
pGameSettings->ResetCoronaReflectionsEnabled();
CVARS_GET("dynamic_ped_shadows", bVal);
pGameSettings->SetDynamicPedShadowsEnabled(bVal);
pController->SetVerticalAimSensitivityRawValue(CVARS_GET_VALUE<float>("vertical_aim_sensitivity"));
CVARS_GET("mastervolume", fVal);
pGameSettings->SetRadioVolume(pGameSettings->GetRadioVolume() * fVal);
pGameSettings->SetSFXVolume(pGameSettings->GetSFXVolume() * fVal);
}
void CCore::SetConnected(bool bConnected)
{
m_pLocalGUI->GetMainMenu()->SetIsIngame(bConnected);
UpdateIsWindowMinimized(); // Force update of stuff
if (g_pCore->GetCVars()->GetValue("allow_discord_rpc", false))
{
const auto discord = g_pCore->GetDiscord();
if (!discord->IsDiscordRPCEnabled())
discord->SetDiscordRPCEnabled(true);
discord->SetPresenceState(bConnected ? _("In-game") : _("Main menu"), false);
discord->SetPresenceStartTimestamp(0);
discord->SetPresenceDetails("", false);
if (bConnected)
discord->SetPresenceStartTimestamp(time(nullptr));
}
}
bool CCore::IsConnected()
{
return m_pLocalGUI->GetMainMenu() && m_pLocalGUI->GetMainMenu()->GetIsIngame();
}
bool CCore::Reconnect(const char* szHost, unsigned short usPort, const char* szPassword, bool bSave)
{
return m_pConnectManager->Reconnect(szHost, usPort, szPassword, bSave);
}
void CCore::SetOfflineMod(bool bOffline)
{
m_bIsOfflineMod = bOffline;
}
const char* CCore::GetModInstallRoot(const char* szModName)
{
m_strModInstallRoot = CalcMTASAPath(PathJoin("mods", szModName));
return m_strModInstallRoot;
}
void CCore::ForceCursorVisible(bool bVisible, bool bToggleControls)
{
m_bCursorToggleControls = bToggleControls;
m_pLocalGUI->ForceCursorVisible(bVisible);
}
void CCore::SetMessageProcessor(pfnProcessMessage pfnMessageProcessor)
{
m_pfnMessageProcessor = pfnMessageProcessor;
}
void CCore::ShowMessageBox(const char* szTitle, const char* szText, unsigned int uiFlags, GUI_CALLBACK* ResponseHandler)
{
RemoveMessageBox();
// Create the message box
m_pMessageBox = m_pGUI->CreateMessageBox(szTitle, szText, uiFlags);
if (ResponseHandler)
m_pMessageBox->SetClickHandler(*ResponseHandler);
// Make sure it doesn't auto-destroy, or we'll crash if the msgbox had buttons and the user clicks OK
m_pMessageBox->SetAutoDestroy(false);
}
void CCore::RemoveMessageBox(bool bNextFrame)
{
if (bNextFrame)
{
m_bDestroyMessageBox = true;
}
else
{
if (m_pMessageBox)
{
delete m_pMessageBox;
m_pMessageBox = NULL;
}
}
}
//
// Show message box with possibility of on-line help
//
void CCore::ShowErrorMessageBox(const SString& strTitle, SString strMessage, const SString& strTroubleLink)
{
if (strTroubleLink.empty())
{
CCore::GetSingleton().ShowMessageBox(strTitle, strMessage, MB_BUTTON_OK | MB_ICON_ERROR);
}
else
{
CQuestionBox* pQuestionBox = CCore::GetSingleton().GetLocalGUI()->GetMainMenu()->GetQuestionWindow();
pQuestionBox->Reset();
pQuestionBox->SetTitle(strTitle);
pQuestionBox->SetMessage(strMessage);
pQuestionBox->SetOnLineHelpOption(strTroubleLink);
pQuestionBox->Show();
}
}
//
// Show message box with possibility of on-line help
// + with net error code appended to message and trouble link
//
void CCore::ShowNetErrorMessageBox(const SString& strTitle, SString strMessage, SString strTroubleLink, bool bLinkRequiresErrorCode)
{
uint uiErrorCode = CCore::GetSingleton().GetNetwork()->GetExtendedErrorCode();
if (uiErrorCode != 0)
{
// Do anti-virus check soon
SetApplicationSettingInt("noav-user-says-skip", 1);
strMessage += SString(" \nCode: %08X", uiErrorCode);
if (!strTroubleLink.empty())
strTroubleLink += SString("&neterrorcode=%08X", uiErrorCode);
}
else if (bLinkRequiresErrorCode)
strTroubleLink = ""; // No link if no error code
AddReportLog(7100, SString("Core - NetError (%s) (%s)", *strTitle, *strMessage));
ShowErrorMessageBox(strTitle, strMessage, strTroubleLink);
}
//
// Callback used in CCore::ShowErrorMessageBox
//
void CCore::ErrorMessageBoxCallBack(void* pData, uint uiButton)
{
CCore::GetSingleton().GetLocalGUI()->GetMainMenu()->GetQuestionWindow()->Reset();
SString* pstrTroubleLink = (SString*)pData;
if (uiButton == 1)
{
uint uiErrorCode = (uint)pData;
BrowseToSolution(*pstrTroubleLink, EXIT_GAME_FIRST);
}
delete pstrTroubleLink;
}
//
// Check for disk space problems
// Returns false if low disk space, and dialog is being shown
//
bool CCore::CheckDiskSpace(uint uiResourcesPathMinMB, uint uiDataPathMinMB)
{
SString strDriveWithNoSpace = GetDriveNameWithNotEnoughSpace(uiResourcesPathMinMB, uiDataPathMinMB);
if (!strDriveWithNoSpace.empty())
{
SString strMessage(_("MTA:SA cannot continue because drive %s does not have enough space."), *strDriveWithNoSpace);
SString strTroubleLink(SString("low-disk-space&drive=%s", *strDriveWithNoSpace.Left(1)));
g_pCore->ShowErrorMessageBox(_("Fatal error") + _E("CC43"), strMessage, strTroubleLink);
return false;
}
return true;
}
HWND CCore::GetHookedWindow()
{
return CMessageLoopHook::GetSingleton().GetHookedWindowHandle();
}
void CCore::HideMainMenu()
{
m_pLocalGUI->GetMainMenu()->SetVisible(false);
}
void CCore::ShowServerInfo(unsigned int WindowType)
{
RemoveMessageBox();
CServerInfo::GetSingletonPtr()->Show((eWindowType)WindowType);
}
void CCore::ApplyHooks()
{
WriteDebugEvent("CCore::ApplyHooks");
// Create our hooks.
m_pDirectInputHookManager->ApplyHook();
// m_pDirect3DHookManager->ApplyHook ( );
// m_pFileSystemHook->ApplyHook ( );
m_pSetCursorPosHook->ApplyHook();
// Redirect basic files.
// m_pFileSystemHook->RedirectFile ( "main.scm", "../../mta/gtafiles/main.scm" );
// Remove useless DirectPlay dependency (dpnhpast.dll) @ 0x745701
// We have to patch here as multiplayer_sa and game_sa are loaded too late
DetourLibraryFunction("kernel32.dll", "LoadLibraryA", Win32LoadLibraryA, SkipDirectPlay_LoadLibraryA);
}
bool UsingAltD3DSetup()
{
static bool bAltStartup = GetApplicationSettingInt("nvhacks", "optimus-alt-startup") ? true : false;
return bAltStartup;
}
void CCore::ApplyHooks2()
{
WriteDebugEvent("CCore::ApplyHooks2");
// Try this one a little later
if (!UsingAltD3DSetup())
m_pDirect3DHookManager->ApplyHook();
else
{
// Done a little later to get past the loading time required to decrypt the gta
// executable into memory...
if (!CCore::GetSingleton().AreModulesLoaded())
{
CCore::GetSingleton().SetModulesLoaded(true);
CCore::GetSingleton().CreateNetwork();
CCore::GetSingleton().CreateGame();
CCore::GetSingleton().CreateMultiplayer();
CCore::GetSingleton().CreateXML();
CCore::GetSingleton().CreateGUI();
}
}
}
void CCore::ApplyHooks3(bool bEnable)
{
if (bEnable)
CDirect3DHook9::GetSingletonPtr()->ApplyHook();
else
CDirect3DHook9::GetSingletonPtr()->RemoveHook();
}
void CCore::SetCenterCursor(bool bEnabled)
{
if (bEnabled)
m_pSetCursorPosHook->EnableSetCursorPos();
else
m_pSetCursorPosHook->DisableSetCursorPos();
}
////////////////////////////////////////////////////////////////////////
//
// LoadModule
//
// Attempt to load a module. Returns if successful.
// On failure, displays message box and terminates the current process.
//
////////////////////////////////////////////////////////////////////////
void LoadModule(CModuleLoader& m_Loader, const SString& strName, const SString& strModuleName)
{
WriteDebugEvent("Loading " + strName.ToLower());
// Ensure DllDirectory has not been changed
SString strDllDirectory = GetSystemDllDirectory();
if (CalcMTASAPath("mta").CompareI(strDllDirectory) == false)
{
AddReportLog(3119, SString("DllDirectory wrong: DllDirectory:'%s' Path:'%s'", *strDllDirectory, *CalcMTASAPath("mta")));
SetDllDirectory(CalcMTASAPath("mta"));
}
// Save current directory (shouldn't change anyway)
SString strSavedCwd = GetSystemCurrentDirectory();
// Load approrpiate compilation-specific library.
#ifdef MTA_DEBUG
SString strModuleFileName = strModuleName + "_d.dll";
#else
SString strModuleFileName = strModuleName + ".dll";
#endif
m_Loader.LoadModule(CalcMTASAPath(PathJoin("mta", strModuleFileName)));
if (m_Loader.IsOk() == false)
{
SString strMessage("Error loading '%s' module!\n%s", *strName, *m_Loader.GetLastErrorMessage());
SString strType = "module-not-loadable&name=" + strModuleName;
// Extra message if d3d9.dll exists
SString strD3dModuleFilename = PathJoin(GetLaunchPath(), "d3d9.dll");
if (FileExists(strD3dModuleFilename))
{
strMessage += "\n\n";
strMessage += _("TO FIX, REMOVE THIS FILE:") + "\n";
strMessage += strD3dModuleFilename;
strType += "&d3d9=1";
}
BrowseToSolution(strType, ASK_GO_ONLINE | EXIT_GAME_FIRST, strMessage);
}
// Restore current directory
SetCurrentDirectory(strSavedCwd);
WriteDebugEvent(strName + " loaded.");
}
////////////////////////////////////////////////////////////////////////
//
// InitModule
//
// Attempt to initialize a loaded module. Returns if successful.
// On failure, displays message box and terminates the current process.
//
////////////////////////////////////////////////////////////////////////
template <class T, class U>
T* InitModule(CModuleLoader& m_Loader, const SString& strName, const SString& strInitializer, U* pObj)
{
// Save current directory (shouldn't change anyway)
SString strSavedCwd = GetSystemCurrentDirectory();
// Get initializer function from DLL.
typedef T* (*PFNINITIALIZER)(U*);
PFNINITIALIZER pfnInit = static_cast<PFNINITIALIZER>(m_Loader.GetFunctionPointer(strInitializer));
if (pfnInit == NULL)
{
MessageBoxUTF8(0, SString(_("%s module is incorrect!"), *strName), "Error" + _E("CC40"), MB_OK | MB_ICONERROR | MB_TOPMOST);
TerminateProcess(GetCurrentProcess(), 1);
}
// If we have a valid initializer, call it.
T* pResult = pfnInit(pObj);
// Restore current directory
SetCurrentDirectory(strSavedCwd);
WriteDebugEvent(strName + " initialized.");
return pResult;
}
////////////////////////////////////////////////////////////////////////
//
// CreateModule
//
// Attempt to load and initialize a module. Returns if successful.
// On failure, displays message box and terminates the current process.
//
////////////////////////////////////////////////////////////////////////
template <class T, class U>
T* CreateModule(CModuleLoader& m_Loader, const SString& strName, const SString& strModuleName, const SString& strInitializer, U* pObj)
{
LoadModule(m_Loader, strName, strModuleName);
return InitModule<T>(m_Loader, strName, strInitializer, pObj);
}
void CCore::CreateGame()
{