-
Notifications
You must be signed in to change notification settings - Fork 0
/
dllmain.cpp
2788 lines (2414 loc) · 80.7 KB
/
dllmain.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
// NFS Underground - Nuzlocke Challenge
// by Xan/Tenjoin
// TODO: add a points system of some sort -- done?? using style points for now... but I think we should track the acculumated points during the runtime of Nuzlocke
// TODO: add an "extra life" system maybe
#include "NFSU_nuzlocke.h"
#include <windows.h>
#include <stdlib.h>
#include <math.h>
#include <timeapi.h>
#include "includes\injector\injector.hpp"
#include "inireader\IniReader.h"
//////////////////////////////////////////////////////////////////
// Dear ImGui declarations
//////////////////////////////////////////////////////////////////
#include "imgui.h"
#include "imgui_impl_dx9.h"
#include "imgui_impl_win32.h"
#include <d3d9.h>
#include <tchar.h>
#include <iostream>
#define GAME_HWND_ADDR 0x00736380
#define GAME_D3DDEVICE_ADDR 0x0073636C
static LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
bool bInitedImgui = false;
bool bBlockedGameInput = false;
// Forward declare message handler from imgui_impl_win32.cpp
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
// DIALOG BOOLS
bool bShowIntroMessage = false;
bool bShowAlreadyStartedWarning = false;
bool bShowStatsWindow = false;
bool bShowGameOverScreen = false;
bool bShowCarLifeHint = false;
bool bShowDifficultySelector = false;
bool bShowLoadMessage = false;
bool bShowErrorMessage = false;
// Show-once bools
bool bShownGameOverOnce = false;
bool bShownLifeOverOnce = false;
// permanent ones -- CONFIG
bool bSkipIntroMessage = false;
bool bSkipAlreadyStartedWarning = false;
// HUD hide stuff -- CONFIG
bool bHideFEHUD = false;
bool bHideIGHUD = false;
ImFont* lcd_font;
char hud_disp_string[1024];
// Win32 mouse API stuff
RECT windowRect;
bool bMousePressedDown = false;
bool bMousePressedDownOldState = false;
bool bConfineMouse = false;
int TimeSinceLastMouseMovement = 0;
#define MOUSEHIDE_TIME 5000
#define FEMOUSECURSOR_X_ADDR 0x0070649C
#define FEMOUSECURSOR_Y_ADDR 0x007064A0
#define FEMOUSECURSOR_BUTTONPRESS_ADDR 0x007064B0
#define FEMOUSECURSOR_BUTTONPRESS2_ADDR 0x007064B1
#define FEMOUSECURSOR_BUTTONPRESS3_ADDR 0x007064B2
#define FEMOUSECURSOR_CARORBIT_X_ADDR 0x007064A4
#define FEMOUSECURSOR_CARORBIT_Y_ADDR 0x007064A8
#define FEMOUSECURSOR_CARORBIT_Z_ADDR 0x007064AC
#define FEHUD_FONT_SCALE 1.5
#define IGHUD_FONT_SCALE 1.5
// error string buffer
std::stringstream errbuffer;
std::string err_text;
//////////////////////////////////////////////////////////////////
// Dear ImGui declarations end
//////////////////////////////////////////////////////////////////
// Game-related addresses
#define CAREER_CAR_TYPE_HASH_POINTER 0x75EF00
#define FECAREERMANAGER_POINTER 0x75EEF8
#define THEGARAGEMANAGER_ADDR 0x00758AD4
#define CARTYPEINFOARRAY_ADDRESS 0x00734588
#define GAMEFLOWMANAGER_STATUS_ADDR 0x0077A920
#define GAMEFLOWMANAGER_OBJ_ADDR 0x0077B240
#define CURRENTWORLD_OBJ_ADDR 0x007361F8
#define GAMEMODE_ADDR 0x00777B4C
#define RACETYPE_ADDR 0x00777CC8
#define FEDATABASE_ADDR 0x00748F70
#define PLAYERNAME_ADDR 0x007588C4
#define CFENG_PINSTANCE_ADDR 0x0073578C
#define PLAYERMONEY_ADDR 0x0076026C
#define GAMEPAUSED_ADDR 0x007041C4
// currentrace + 0x14 = timer
#define CURRENTRACE_POINTER_ADDR 0x0073619C
#define PLAYER_POINTER 0x007361BC
#define CAREER_STATUS_ADDR 0x0075F2B5
char* TradeCarScreen = "MU_UG_TradeCareerCar.fng";
unsigned int NumberOfCars = 35; // this is a fixed value in the executable, change only if you manage to increase the car count in the game
// difficulty-related stuff
unsigned int NuzlockeDifficulty = 0; // 0 = easy, 1 = medium, 2 = hard/nuzlocke, 3 = ultra hard/ultra nuzlocke, 4 = custom
unsigned int NumberOfLives = 1; // starting number of lives
bool bAllowTradingCarMidGame = false; // option to allow/disallow car changing mid-game
unsigned int LockedGameDifficulty = 0; // 0 = unlocked, 1 = medium, 2 = hard
// VARIABLE FOR FUN - force traffic cars into race paths (independent of difficulty)
bool bTrafficRacers = false;
// miscellaneous
#define MINIWORLD_NUMVISIBLESEGMENTS_MAIN_ADDR 0x006FB048
#define MINIWORLD_NUMVISIBLESEGMENTS_REF_ADDR 0x006FB04C
#define MINIWORLD_NUMVISIBLESEGMENTS_ENV_ADDR 0x006FB050
bool bIncreaseMiniworldSegments = true;
int CustomNumberOfLives = 2;
unsigned int CustomLockedGameDifficulty = 0;
bool bCustomAllowTrading = false;
const char CarTradingStatusNames[2][16] = { NUZLOCKE_UI_CARTRADING_DISALLOWED, NUZLOCKE_UI_CARTRADING_ALLOWED };
const char NuzDifficultyNames[NUZLOCKED_NUZ_DIFF_COUNT][16] = { NUZLOCKE_UI_NUZ_DIFF_EASY, NUZLOCKE_UI_NUZ_DIFF_MEDIUM, NUZLOCKE_UI_NUZ_DIFF_HARD, NUZLOCKE_UI_NUZ_DIFF_ULTRAHARD , NUZLOCKE_UI_NUZ_DIFF_CUSTOM };
const char GameDifficultyNames[3][16] = { NUZLOCKE_UI_GAME_DIFF_EASY, NUZLOCKE_UI_GAME_DIFF_MEDIUM, NUZLOCKE_UI_GAME_DIFF_HARD};
unsigned int CareerCarHash = 0; // currently selected car in career mode (not initialized until DDay is completed)
unsigned int GameFlowStatus = 0;
unsigned int Money = 0;
unsigned int StylePoints = 0;
unsigned int TimeSinceModeSwitch = 0;
unsigned int TimeBaseSinceModeSwitch = 0;
unsigned int OldGameMode = 0;
unsigned int LastCarTime = 0;
unsigned int LastTotalTime = 0;
unsigned int LastTotalPlayTime = 0;
unsigned int GameMode = 0; // 1 = career mode, others are quickrace or online modes
unsigned int RaceType = 0; // quick race - race type, currently unused
bool bGameIsOver = false;
bool bCantAffordAnyCar = false; // if player lost lives on current car and can't afford any = game over
bool bAllCarsLost = false; // if player got all their cars on 0 lives
bool bGameComplete = false; // if player beats UG mode with Nuzlocke
bool bGameStarted = false; // should start right after entering career mode
bool bProfileStartedCareer = false;
bool bRaceFinished = false;
bool bShowingRaceOver = false;
bool bMarkedStatusAlready = false;
unsigned int UnlockedCarCount = 0; // we MUST keep track of this -- if the player has 1 car unlocked, trade menu CANNOT open
unsigned int GameUnlockedCarCount = 0; // unlocked cars by the game progression itself
// some extra stats
unsigned int TotalLivesLost = 0;
unsigned int TotalLosses = 0;
unsigned int TotalWins = 0;
unsigned int TotalTimeSpentRacing = 0;
unsigned int TotalTimePlaying = 0;
unsigned int TotalEventsPlayed = 0;
enum CarUsageType
{
CAR_USAGE_TYPE_RACING = 0,
CAR_USAGE_TYPE_COP = 1,
CAR_USAGE_TYPE_TRAFFIC = 2,
CAR_USAGE_TYPE_WHEELS = 3,
CAR_USAGE_TYPE_UNIVERSAL = 4,
};
struct NuzlockeStruct
{
char* CarName;
unsigned int CarNameHash;
CarUsageType UsageType;
int Lives;
unsigned int Wins;
unsigned int Losses;
unsigned int TimeSpentRacing;
unsigned int TimeOfDeathRT;
unsigned int TimeOfDeathPT;
bool bUnlocked;
}*NuzCars, DDayCar;
// DDay car name
char* DDayCarName = "D-Day";
struct CarTypeInfo
{
char CarTypeName[32];
char BaseModelName[32];
char GeometryFilename[32];
char GeometryFilenameComp[32];
unsigned char padding[64];
char ManufacturerName[16];
unsigned int CarTypeNameHash;
unsigned char RestOfData[0xB7C];
int Type;
CarUsageType UsageType;
unsigned char RestOfData2[0x38];
}*CarTypeInfoArray;
struct NuzlockeSave
{
unsigned int Magic; // NUZL -- just a simple file signature, nothing special
unsigned int NuzlockeDifficulty;
unsigned int LockedGameDifficulty;
unsigned int NumberOfLives;
unsigned int TotalLivesLost;
unsigned int TotalLosses;
unsigned int TotalWins;
unsigned int TotalTimeSpentRacing;
unsigned int TotalTimePlaying;
unsigned int TotalEventsPlayed;
unsigned int dummy1;
bool bAllowTradingCarMidGame;
bool bTrafficRacers;
bool bGameIsOver;
bool bCantAffordAnyCar;
bool bAllCarsLost;
bool bGameComplete;
bool bGameStarted;
bool bProfileStartedCareer;
bool bRaceFinished;
unsigned int dummy2;
// ... then store the NuzCars below
};
char SaveFileName[64];
//////////////////////////////////////////////////////////////////
// Function pointers
//////////////////////////////////////////////////////////////////
//bool(__fastcall* IsCarUnlocked)(unsigned int arg1, unsigned int arg2) = (bool(__fastcall*)(unsigned int, unsigned int))0x005A1630;
// FERaceEvent namespace
bool(__stdcall* HasEventBeenWon)(unsigned int arg1, unsigned int arg2) = (bool(__stdcall*)(unsigned int, unsigned int))0x005A2F10;
bool(__thiscall* IsThereANextRace)(void* dis) = (bool(__thiscall*)(void*))0x005A2C00;
// PauseMenu namespace
void(__stdcall* DoQuitRace)(int unk) = (void(__stdcall*)(int unk))0x004CDA50;
// UndergroundTradeCarScreen namespace
void(__stdcall*BuildTradeableList)(int unk) = (void(__stdcall*)(int))0x004C3240;
// PostRaceMenuScreen namespace
unsigned int(__stdcall*PostRaceMenuScreen_Setup)(unsigned int PostRaceMenuScreen, unsigned int PostRaceMenuSetupParams) = (unsigned int(__stdcall*)(unsigned int, unsigned int))0x004969E0;
// FE stuff
void(__thiscall* _FEngSetColor)(void* FEObject, unsigned int color) = (void(__thiscall*)(void*, unsigned int))0x004F75B0;
unsigned int(*FEngGetCurrentButton)(char* pkgname) = (unsigned int(*)(char*))0x004F64D0;
// UndergroundMenuScreen
void(__thiscall* UndergroundMenuScreen_NotificationMessage)(void* UndergroundMenuScreen, unsigned int msg, void* FEObject, unsigned int unk2, unsigned int unk3) = (void(__thiscall*)(void*, unsigned int, void*, unsigned int, unsigned int))0x004EB1E0;
// UndergroundTradeCarScreen
unsigned int(__stdcall*UndergroundTradeCarScreen_Constructor)(void* Object, void* ScreenConstructorData) = (unsigned int(__stdcall*)(void*, void*))0x004C2ED0;
//cFEng
unsigned int(__stdcall* cFEng_GetMouseInfo)(unsigned int FEMouseInfo) = (unsigned int(__stdcall*)(unsigned int))0x004F6080;
// GameFlowManager
bool(__thiscall* GameFlowManager_IsPaused)(void* dis) = (bool(__thiscall*)(void*))0x0043A2E0;
// UndergroundBriefScreen
void(__thiscall* UndergroundBriefScreen_NotificationMessage)(void* UndergroundBriefScreen, unsigned int msg, void* FEObject, unsigned int unk2, unsigned int unk3) = (void(__thiscall*)(void*, unsigned int, void*, unsigned int, unsigned int))0x004C5FA0;
void(__thiscall* UndergroundBriefScreen_LaunchCurrentEvent)(void* UndergroundBriefScreen, void* FEObject) = (void(__thiscall*)(void*, void*))0x004C5DF0;
// save game
void(__thiscall* SaveGameFunction)(void* TheThis, unsigned int unk1, char* unk2) = (void(__thiscall*)(void*, unsigned int, char*))0x41D8A0;
void(*sub_546780)() = (void(*)())0x546780;
void(*sub_4DFD70)() = (void(*)())0x4DFD70;
void(*sub_40A4E0)() = (void(*)())0x0040A4E0;
void(*GenerateJoyEvents)() = (void(*)())0x00573CB0;
void(*game_crt_free)(void* block) = (void(*)(void*))0x00671102;
unsigned int GameWndProcAddr = 0;
LRESULT(WINAPI* GameWndProc)(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
//////////////////////////////////////////////////////////////////
// Function pointers end
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
// Inireader
//////////////////////////////////////////////////////////////////
void UpdateIniFile()
{
CIniReader inireader("");
inireader.WriteInteger("Nuzlocke", "SkipIntroMessage", bSkipIntroMessage);
inireader.WriteInteger("Nuzlocke", "SkipAlreadyStartedWarning", bSkipAlreadyStartedWarning);
inireader.WriteInteger("Nuzlocke", "HideFEHUD", bHideFEHUD);
inireader.WriteInteger("Nuzlocke", "HideIGHUD", bHideIGHUD);
inireader.WriteInteger("CustomGame", "NumberOfLives", CustomNumberOfLives);
inireader.WriteInteger("CustomGame", "AllowTrading", bCustomAllowTrading);
inireader.WriteInteger("CustomGame", "LockedGameDifficulty", CustomLockedGameDifficulty);
}
void ReadIniFile()
{
CIniReader inireader("");
bSkipIntroMessage = inireader.ReadInteger("Nuzlocke", "SkipIntroMessage", 0);
bSkipAlreadyStartedWarning = inireader.ReadInteger("Nuzlocke", "SkipAlreadyStartedWarning", 0);
bHideFEHUD = inireader.ReadInteger("Nuzlocke", "HideFEHUD", 0);
bHideIGHUD = inireader.ReadInteger("Nuzlocke", "HideIGHUD", 0);
NumberOfCars = inireader.ReadInteger("Nuzlocke", "NumberOfCars", 35);
bConfineMouse = inireader.ReadInteger("Nuzlocke", "ConfineMouse", 0);
CustomNumberOfLives = inireader.ReadInteger("CustomGame", "NumberOfLives", 2);
bCustomAllowTrading = inireader.ReadInteger("CustomGame", "AllowTrading", 1);
CustomLockedGameDifficulty = inireader.ReadInteger("CustomGame", "LockedGameDifficulty", 0);
bIncreaseMiniworldSegments = inireader.ReadInteger("Misc", "IncreaseMiniworldSegments", 1);
}
//////////////////////////////////////////////////////////////////
// Inireader end
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
// Custom FE functions
//////////////////////////////////////////////////////////////////
// because the game uses __fastcall convention, we gotta do some asm magic/wrapping to make it work with __stdcall/__cdecl
unsigned int FEHashUpper_Addr = 0x004FD230;
unsigned int FEPkgMgr_FindPackage_Addr = 0x004F65D0;
unsigned int FEPackageManager_FindPackage_Addr = 0x004F3F90;
unsigned int FEngFindObject_Addr = 0x004FFB70;
unsigned int FEngSetButtonState_Addr = 0x004F5F80;
unsigned int FEngSwitchPackages_Addr = 0x004F6360;
unsigned int FEngSendMessageToPackage_Addr = 0x004C96C0;
unsigned int LaunchPartsBrowser_Addr = 0x00504990;
unsigned int uUserStartedCareer_Addr = 0x004B2AA0;
unsigned int __stdcall FEHashUpper(const char* str)
{
unsigned int result = 0;
_asm
{
mov edx, str
call FEHashUpper_Addr
mov result, eax
}
return result;
}
unsigned int __stdcall FEPkgMgr_FindPackage(const char* pkgname)
{
unsigned int result = 0;
_asm
{
mov eax, pkgname
call FEPkgMgr_FindPackage_Addr
mov result, eax
}
return result;
}
unsigned int __stdcall FEPackageManager_FindPackage(const char* pkgname)
{
unsigned int result = 0;
_asm
{
push 0x00746104
mov eax, pkgname
call FEPackageManager_FindPackage_Addr
mov result, eax
//add esp, 4
}
return result;
}
unsigned int __stdcall FEngFindObject(const char* pkg, int hash)
{
unsigned int result = 0;
_asm
{
mov ecx, hash
mov edx, pkg
call FEngFindObject_Addr
mov result, eax
}
return result;
}
int FEngFindString(const char* pkgname, int objhash)
{
int result; // r3
result = FEngFindObject((char*)FEPkgMgr_FindPackage(pkgname), objhash);
if (!result || *(unsigned int*)(result + 24) != 2)
result = 0;
return result;
}
void __stdcall FEngSetButtonState(const char* pkgname, unsigned int hash, unsigned int state)
{
int cfeng_instance = *(int*)CFENG_PINSTANCE_ADDR;
_asm
{
push state
mov edi, hash
mov eax, pkgname
push cfeng_instance
call FEngSetButtonState_Addr
//add esp, 4
}
}
unsigned int FEngSetInvisible_Addr = 0x000495F70;
void __stdcall FEngSetInvisible(const char* pkgname, unsigned int hash)
{
_asm
{
mov edi, hash
mov esi, pkgname
call FEngSetInvisible_Addr
}
}
unsigned int FEngSetVisible_Addr = 0x00495FC0;
void __stdcall FEngSetVisible(const char* pkgname, unsigned int hash)
{
_asm
{
mov edi, hash
mov esi, pkgname
call FEngSetVisible_Addr
}
}
#pragma runtime_checks( "", off )
unsigned int FEngSetCurrentButton_Addr = 0x004F5ED0;
void __stdcall FEngSetCurrentButton(const char* pkgname, unsigned int button_hash)
{
_asm
{
mov eax, ds:CFENG_PINSTANCE_ADDR
push eax
mov ecx, button_hash
mov eax, pkgname
call FEngSetCurrentButton_Addr
}
}
#pragma runtime_checks( "", restore )
void __stdcall FEngSwitchPackages(char* src, char* dest)
{
_asm
{
push src
mov edi, dest
call FEngSwitchPackages_Addr
add esp, 4
}
}
void __stdcall FEngSendMessageToPackage(unsigned int msg, char* dest)
{
_asm
{
push msg
mov eax, dest
call FEngSendMessageToPackage_Addr
add esp, 4
}
}
void __stdcall LaunchPartsBrowser(int eBrowserModes, void* FECarConfig, const char* src_pkg, const char* dest_pkg, int somebool)
{
_asm
{
push somebool
push dest_pkg
mov ebx, src_pkg
mov eax, FECarConfig
push eBrowserModes
call LaunchPartsBrowser_Addr
add esp, 0xC
}
}
bool __stdcall bUserStartedCareer()
{
bool result = false;
_asm
{
mov edx, 0x75EEF8
xor ecx, ecx
call uUserStartedCareer_Addr
mov result, al
}
return result;
}
unsigned int sub_4C2D20 = 0x4C2D20;
void __stdcall UpdateCarPricing(unsigned int unk1, unsigned int unk2)
{
_asm
{
mov ebx, unk1
push unk2
call sub_4C2D20
}
}
unsigned int IsCarUnlocked_Addr = 0x005A1630;
bool __stdcall IsCarUnlocked(unsigned int hash, unsigned int careermanager)
{
bool result_checker = false;
_asm
{
mov edi, hash
mov ebx, careermanager
call IsCarUnlocked_Addr
mov result_checker, al
}
return result_checker;
}
unsigned int UndergroundBriefScreen_Redraw_Addr = 0x004C5680;
void __stdcall UndergroundBriefScreen_Redraw(void* UndergroundBriefScreen)
{
_asm
{
mov eax, UndergroundBriefScreen
call UndergroundBriefScreen_Redraw_Addr
}
}
// super hacky way of handling this, because the game doesn't actually use a __thiscall for this, but by sheer luck it passes the object through the ECX register
// RTC disabled for this function to stop the runtime from complaining about misaligned stack after the function call
#pragma runtime_checks( "", off )
void __stdcall FEngSetColor(void* FEObject, unsigned int color)
{
_FEngSetColor(FEObject, color);
_asm add esp, 4
}
#pragma runtime_checks( "", restore )
void __stdcall FE_SetColor_Str(const char* option_name, const char* pkgname, unsigned int color)
{
FEngSetColor((void*)FEngFindObject((char*)FEPkgMgr_FindPackage(pkgname), FEHashUpper(option_name)), color);
}
void __stdcall FE_SetColor_Hash(unsigned int FEHash, const char* pkgname, unsigned int color)
{
FEngSetColor((void*)FEngFindObject((char*)FEPkgMgr_FindPackage(pkgname), FEHash), color);
}
void __stdcall FE_SetButtonState_Str(const char* option_name, const char* pkgname, bool state)
{
FEngSetButtonState(pkgname, FEHashUpper(option_name), state);
}
//////////////////////////////////////////////////////////////////
// Custom FE functions end
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
// Nuzlocke logic
//////////////////////////////////////////////////////////////////
unsigned int FindCarIndexByHash(unsigned int CarTypeNameHash)
{
for (int i = 0; i < NumberOfCars; i++)
{
if (CarTypeNameHash == NuzCars[i].CarNameHash)
return i;
}
return 0;
}
void UpdateCarUnlockStatus()
{
CareerCarHash = *(int*)CAREER_CAR_TYPE_HASH_POINTER;
unsigned int carcount = 0;
unsigned int gamecarcount = 0;
for (int i = 0; i < NumberOfCars; i++)
{
unsigned int Arg1_checker = NuzCars[i].CarNameHash;
unsigned int Arg2_checker = FECAREERMANAGER_POINTER;
NuzCars[i].bUnlocked = IsCarUnlocked(NuzCars[i].CarNameHash, FECAREERMANAGER_POINTER);
if (NuzCars[i].bUnlocked && (NuzCars[i].Lives > 0) && (NuzCars[i].UsageType == CAR_USAGE_TYPE_RACING))
carcount++;
if (NuzCars[i].bUnlocked && (NuzCars[i].UsageType == CAR_USAGE_TYPE_RACING))
gamecarcount++;
}
UnlockedCarCount = carcount;
GameUnlockedCarCount = gamecarcount;
}
bool bCheckIfAllCarsLocked()
{
if (!bProfileStartedCareer)
{
if (DDayCar.Lives > 0)
return false;
return true;
}
for (int i = 0; i < NumberOfCars; i++)
{
NuzCars[i].bUnlocked = IsCarUnlocked(NuzCars[i].CarNameHash, FECAREERMANAGER_POINTER);
if (NuzCars[i].bUnlocked && (NuzCars[i].Lives > 0) && (NuzCars[i].UsageType == CAR_USAGE_TYPE_RACING))
return false;
}
return true;
}
unsigned int Arg1 = 0;
unsigned int Arg2 = 0;
bool PrevResult = false;
bool __stdcall IsCarUnlockedHook_Code()
{
if (bGameIsOver) // if the game is over, unlock it
return PrevResult;
if (UnlockedCarCount <= 1) // if we have only 1 car unlocked, we MUST keep the current one unlocked so we can access the trade menu
{
if (Arg1 == CareerCarHash)
return true;
}
if (NuzCars[FindCarIndexByHash(Arg1)].Lives <= 0)
return false;
return PrevResult;
}
void __declspec(naked) IsCarUnlockedHook()
{
_asm
{
// take args and pass on the code normally
mov Arg1, edi
mov Arg2, ebx
call IsCarUnlocked_Addr
// save the result
mov PrevResult, al
// modify the result
jmp IsCarUnlockedHook_Code
}
}
void SaveGameForCurrentProfile()
{
struct stat st = { 0 };
if (stat("NuzlockeSave", &st))
_wmkdir(L"NuzlockeSave");
NuzlockeSave SaveGame = { 0 };
// copy the values to the savegame structure
SaveGame.Magic = 0x4C5A554E; // NUZL -- just a simple file signature, nothing special
SaveGame.dummy1 = 0x4C4F4F42; // BOOL -- bools start marker
SaveGame.dummy2 = 0x53524143; // CARS -- cars start marker
SaveGame.NuzlockeDifficulty = NuzlockeDifficulty;
SaveGame.LockedGameDifficulty = LockedGameDifficulty;
SaveGame.NumberOfLives = NumberOfLives;
SaveGame.TotalLivesLost = TotalLivesLost;
SaveGame.TotalLosses = TotalLosses;
SaveGame.TotalWins = TotalWins;
SaveGame.TotalTimeSpentRacing = TotalTimeSpentRacing;
SaveGame.TotalTimePlaying = TotalTimePlaying;
SaveGame.TotalEventsPlayed = TotalEventsPlayed;
SaveGame.bAllowTradingCarMidGame = bAllowTradingCarMidGame;
SaveGame.bTrafficRacers = bTrafficRacers;
SaveGame.bGameIsOver = bGameIsOver;
SaveGame.bCantAffordAnyCar = bCantAffordAnyCar;
SaveGame.bAllCarsLost = bAllCarsLost;
SaveGame.bGameComplete = bGameComplete;
SaveGame.bGameStarted = bGameStarted;
SaveGame.bProfileStartedCareer = bProfileStartedCareer;
SaveGame.bRaceFinished = bRaceFinished;
// open the file and save it and the car statuses
sprintf(SaveFileName, "NuzlockeSave\\%s.nuz", (char*)(PLAYERNAME_ADDR));
FILE* fout = fopen(SaveFileName, "wb");
if (!fout)
{
errbuffer.str("");
errbuffer << "Can't open file " << SaveFileName << " for writing: " << strerror(errno);
err_text = errbuffer.str();
ImGui::OpenPopup(NUZLOCKE_HEADER_ERROR);
bShowErrorMessage = true;
return;
}
fwrite(&SaveGame, sizeof(NuzlockeSave), 1, fout);
fwrite(NuzCars, sizeof(NuzlockeStruct), NumberOfCars, fout);
fwrite(&DDayCar, sizeof(NuzlockeStruct), 1, fout);
fclose(fout);
}
#pragma runtime_checks( "", off )
void __stdcall SaveGameFunction_Hook(unsigned int unk1, char* unk2)
{
unsigned int thethis = 0;
_asm mov thethis, ecx
SaveGameForCurrentProfile();
return SaveGameFunction((void*)thethis, unk1, unk2);
}
#pragma runtime_checks( "", restore )
// triggers game autosave and Nuzlocke save together
void __stdcall TriggerTotalSaveGame()
{
SaveGameForCurrentProfile();
SaveGameFunction((void*)0xFFFFFFFF, 0x78A478, "auto");
}
void LoadGameForCurrentProfile()
{
sprintf(SaveFileName, "NuzlockeSave\\%s.nuz", (char*)(PLAYERNAME_ADDR));
FILE* fin = fopen(SaveFileName, "rb");
if (!fin)
{
errbuffer.str("");
errbuffer << "Can't open file " << SaveFileName << " for reading: " << strerror(errno);
err_text = errbuffer.str();
ImGui::OpenPopup(NUZLOCKE_HEADER_ERROR);
bShowErrorMessage = true;
return;
}
NuzlockeSave LoadGame = { 0 };
fread(&LoadGame, sizeof(NuzlockeSave), 1, fin);
if (LoadGame.Magic != 0x4C5A554E)
{
errbuffer.str("");
errbuffer << "Wrong file magic! Expected: 0x4C5A554E Read: 0x" << std::uppercase << std::hex << LoadGame.Magic << std::endl;
err_text = errbuffer.str();
ImGui::OpenPopup(NUZLOCKE_HEADER_ERROR);
bShowErrorMessage = true;
return;
}
// assign the vars from the save file
NuzlockeDifficulty = LoadGame.NuzlockeDifficulty;
LockedGameDifficulty = LoadGame.LockedGameDifficulty;
NumberOfLives = LoadGame.NumberOfLives;
TotalLivesLost = LoadGame.TotalLivesLost;
TotalLosses = LoadGame.TotalLosses;
TotalWins = LoadGame.TotalWins;
TotalTimeSpentRacing = LoadGame.TotalTimeSpentRacing;
TotalTimePlaying = LoadGame.TotalTimePlaying;
TotalEventsPlayed = LoadGame.TotalEventsPlayed;
bAllowTradingCarMidGame = LoadGame.bAllowTradingCarMidGame;
bTrafficRacers = LoadGame.bTrafficRacers;
bGameIsOver = LoadGame.bGameIsOver;
bCantAffordAnyCar = LoadGame.bCantAffordAnyCar;
bAllCarsLost = LoadGame.bAllCarsLost;
bGameComplete = LoadGame.bGameComplete;
bGameStarted = LoadGame.bGameStarted;
bProfileStartedCareer = LoadGame.bProfileStartedCareer;
bRaceFinished = LoadGame.bRaceFinished;
// load the cars
fread(NuzCars, sizeof(NuzlockeStruct), NumberOfCars, fin);
fread(&DDayCar, sizeof(NuzlockeStruct), 1, fin);
// re-init car name pointers
for (int i = 0; i < NumberOfCars; i++)
NuzCars[i].CarName = CarTypeInfoArray[i].CarTypeName;
DDayCar.CarName = DDayCarName;
fclose(fin);
// shown once bools
bShownGameOverOnce = false;
bShownLifeOverOnce = false;
UpdateCarUnlockStatus();
}
bool bIsThereASaveFile()
{
sprintf(SaveFileName, "NuzlockeSave\\%s.nuz", (char*)(PLAYERNAME_ADDR));
FILE* test = fopen(SaveFileName, "rb");
if (test)
{
fclose(test);
return true;
}
return false;
}
void __stdcall BuildTradeableList_Hook(int unk)
{
UpdateCarUnlockStatus();
return BuildTradeableList(unk);
}
int DisableTradeMenu_Exit_True = 0x00504EBB;
int DisableTradeMenu_Exit_False = 0x00504E8C;
void __declspec(naked) DisableTradeMenu_Cave()
{
if ((UnlockedCarCount <= 1 || !bAllowTradingCarMidGame) && !bGameIsOver) // disable the "Trade" menu option if there's 1 or less cars unlocked
_asm jmp DisableTradeMenu_Exit_True
_asm
{
mov edx, 0x006C1614
or ecx, 0xFFFFFFFF
jmp DisableTradeMenu_Exit_False
}
}
void CheckAllCarPrices(unsigned int TradeCarObj)
{
bool bLastOne = false;
bool bCantAfford = true;
float PriceDiff = 0.0;
int IntPriceDiff = 0;
unsigned int CarArg1 = *(unsigned int*)(TradeCarObj + 0x40);
unsigned int CarArg2 = TradeCarObj + 0x90;
unsigned int NextCarArg1 = 0;
unsigned int LastCarArg1 = *(unsigned int*)(TradeCarObj + 0x44);
do
{
UpdateCarPricing(CarArg1, CarArg2);
PriceDiff = *(float*)(CarArg1 + 0xE64);
NextCarArg1 = *(unsigned int*)(CarArg1);
// if any price difference is a rebate (larger than 0) then the player obviously CAN afford a car and we immediately stop checking
if (PriceDiff > 0)
{
bCantAfford = false;
break;
}
// if any price difference is a cost, check if the player can afford it, and if so, stop checking immediately
if (PriceDiff < 0)
{
IntPriceDiff = (int)floor(fabs(PriceDiff)); // game floors the value, so we do it too
if (IntPriceDiff < Money)
{
bCantAfford = false;
break;
}
}
if (CarArg1 == LastCarArg1)
bLastOne = true;
CarArg1 = NextCarArg1;
} while (!bLastOne);
if (bCantAfford)
bCantAffordAnyCar = true;
}
unsigned int __stdcall UndergroundTradeCarScreen_Constructor_Hook(void* Object, void* ScreenConstructorData)
{
unsigned int result = UndergroundTradeCarScreen_Constructor(Object, ScreenConstructorData);
// in case accessing this screen is allowed, we don't want to trigger the game over condition
if (NuzCars[FindCarIndexByHash(CareerCarHash)].Lives <= 0)
CheckAllCarPrices((unsigned int)Object);
return result;
}
bool __stdcall HasEventBeenWon_hook(unsigned int arg1, unsigned int arg2)
{
bool result = HasEventBeenWon(arg1, arg2);
NuzlockeStruct* car;
if (bProfileStartedCareer)
{
unsigned int ci = FindCarIndexByHash(CareerCarHash);
car = &NuzCars[ci];
}
else
car = &DDayCar;
if ((GameMode == 1) && !IsThereANextRace((void*)arg1))
{
bMarkedStatusAlready = true;
if (!result)
{
(*car).Lives--;
(*car).Losses++;
TotalLivesLost++;
TotalLosses++;
if ((*car).Lives <= 0)
{
// mark the time of death
(*car).TimeOfDeathRT = TotalTimeSpentRacing;
(*car).TimeOfDeathPT = TotalTimePlaying;
}
}
if (result)
{
(*car).Wins++;
TotalWins++;
}
TotalEventsPlayed++;
// save the nuzlocke game
TriggerTotalSaveGame();
}
return result;
}
// any type of restarting is considered a loss EXCEPT if player had already won the event
bool __stdcall NotifyRestart_hook(unsigned int arg1, unsigned int arg2)
{
bool result = HasEventBeenWon(arg1, arg2);
NuzlockeStruct* car;
if (bProfileStartedCareer)
{
unsigned int ci = FindCarIndexByHash(CareerCarHash);
car = &NuzCars[ci];
}
else
car = &DDayCar;
if (GameMode == 1 && !result)
{
(*car).Lives--;
(*car).Losses++;
TotalLivesLost++;
TotalLosses++;
bMarkedStatusAlready = false;
TotalEventsPlayed++;
if ((*car).Lives <= 0)
{
// mark the time of death
(*car).TimeOfDeathRT = TotalTimeSpentRacing;
(*car).TimeOfDeathPT = TotalTimePlaying;
}
// save the nuzlocke game
TriggerTotalSaveGame();
}
bRaceFinished = false;
return result;
}
// race quitting from PauseMenu = loss (EXCEPT if player had already won the event)
void __stdcall PauseMenu_DoQuitRace_Hook(int unk)
{
int FERaceEvent = *(int*)(FEDATABASE_ADDR + 0x1E838);
bool event_won = false;
NuzlockeStruct* car;
if (bProfileStartedCareer)
{
unsigned int ci = FindCarIndexByHash(CareerCarHash);
car = &NuzCars[ci];
}
else
car = &DDayCar;
if (FERaceEvent)
event_won = HasEventBeenWon(FERaceEvent, 0);
if (GameMode == 1 && !event_won)
{
(*car).Lives--;
(*car).Losses++;
TotalLivesLost++;
TotalLosses++;
bMarkedStatusAlready = false;
TotalEventsPlayed++;
if ((*car).Lives <= 0)
{
// mark the time of death
(*car).TimeOfDeathRT = TotalTimeSpentRacing;
(*car).TimeOfDeathPT = TotalTimePlaying;
}
// save the nuzlocke game
TriggerTotalSaveGame();
}
bRaceFinished = false;
return DoQuitRace(unk);
}
bool __stdcall PostRace_DoQuitRace_hook(unsigned int arg1, unsigned int arg2)
{
bool result = HasEventBeenWon(arg1, arg2);
NuzlockeStruct* car;
if (bProfileStartedCareer)
{
unsigned int ci = FindCarIndexByHash(CareerCarHash);
car = &NuzCars[ci];
}