-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMain.h
4275 lines (3729 loc) · 101 KB
/
Main.h
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
#pragma once
#pragma comment(lib, "libdeflate/libdeflatestatic.lib")
#pragma comment(lib, "shlwapi.lib")
#include "GECKUtility.h"
#include "Editor.h"
#include "resource.h"
#include <filesystem>
#include <unordered_set>
#include <functional>
#include <shlwapi.h>
#include "NiNodes.h"
#include "NiObjects.h"
#include "GameScript.h"
#include "GameSettings.h"
#include "FormSearch.h"
#include "Settings.h"
#include "NavMeshPickPreventer.h"
#include "libs/stb_sprintf.h"
#define GH_NAME "ZeGaryHax" // this is the string for IsPluginInstalled and GetPluginVersion (also shows in nvse.log)
#define GH_VERSION 0.49
HMODULE ZeGaryHaxHandle;
extern HWND g_trackBarHwnd;
extern HWND g_timeOfDayTextHwnd;
extern HWND g_allowCellWindowLoadsButtonHwnd;
IDebugLog gLog;
BOOL __stdcall HavokPreviewCallback(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam);
extern "C" IMAGE_DOS_HEADER __ImageBase;
struct z_stream_s
{
const void* next_in;
uint32_t avail_in;
uint32_t total_in;
void* next_out;
uint32_t avail_out;
uint32_t total_out;
const char* msg;
struct internal_state* state;
};
struct DialogOverrideData
{
DLGPROC DialogFunc; // Original function pointer
LPARAM Param; // Original parameter
bool IsDialog; // True if it requires EndDialog()
};
std::recursive_mutex g_DialogMutex;
std::unordered_map<HWND, DialogOverrideData> g_DialogOverrides;
__declspec(thread) DialogOverrideData* DlgData;
const char* nvseMSG[20] =
{
"PostLoad",
"ExitGame",
"ExitToMainMenu",
"LoadGame",
"SaveGame",
"Precompile",
"PreLoadGame",
"QQQ",
"PostLoadGame",
"PostPostLoad",
"ScriptError",
"DeleteGame",
"RenameGame",
"RenameNewGame",
"NewGame",
"DeleteGameName",
"RenameGameName",
"RenameNewGameName",
};
static const char* geckwikiurl = "https://geckwiki.com/index.php/";
static const char* geckwikiscriptingurl = "https://geckwiki.com/index.php/Category:Scripting";
static const char* geckwikicommandsurl = "https://geckwiki.com/index.php/Category:Commands";
static const char* geckwikifunctionsurl = "https://geckwiki.com/index.php/Category:Functions";
#define ID_CMB_IDLE_SPEAKER 2170
#define ID_CMB_IDLE_LISTENER 2173
#define ID_CMB_SPEAKER 2564
int cb_ids[3] = { ID_CMB_IDLE_SPEAKER, ID_CMB_IDLE_LISTENER, ID_CMB_SPEAKER };
bool cb_filled[_countof(cb_ids)] = { };
bool cb_reset[_countof(cb_ids)] = { };
// From NVSE Hooks_Editor.cpp
// Patch script window font
static HANDLE fontHandle;
static LOGFONT fontInfo;
static LOGFONT editorFont =
{
13, // lfHeight
0, // lfWidth
0, // lfEscapement
0, // lfOrientation
FW_MEDIUM, // lfWeight
0, // lfItalic
0, // lfUnderline
0, // lfStrikeOut
0, // lfCharSet
3, // lfOutPrecision
2, // lfClipPrecision
1, // lfQuality
49, // lfPitchAndFamily
"Consolas" // lfFaceName[LF_FACESIZE]
};
LPLOGFONT GetEditorFont()
{
return &editorFont;
}
void MoveDlgItem(HWND hWnd, int ID, int deltaX, int deltaY)
{
RECT rect;
POINT point;
HWND element = GetDlgItem(hWnd, ID);
GetWindowRect(element, &rect);
point.x = rect.left;
point.y = rect.top;
ScreenToClient(hWnd, &point);
SetWindowPos(element, NULL, point.x + deltaX, point.y + deltaY, NULL, NULL, SWP_NOSIZE);
}
static void DoModScriptWindow(HWND wnd)
{
SendMessage(wnd, EM_EXLIMITTEXT, 0, 0x00FFFFFF);
GetPrivateProfileStringA("Script", "Font", "Consolas", editorFont.lfFaceName, 31, IniPath);
editorFont.lfHeight = GetOrCreateINIValue("Script", "FontSize", 13, IniPath);
editorFont.lfWeight = GetOrCreateINIValue("Script", "FontWeight", FW_MEDIUM, IniPath);
// try something nice, otherwise fall back on SYSTEM_FIXED_FONT
fontHandle = CreateFontIndirect(&editorFont);
if (fontHandle)
{
fontInfo = editorFont;
}
else
{
fontHandle = GetStockObject(SYSTEM_FIXED_FONT);
GetObject(fontHandle, sizeof(fontInfo), &fontInfo);
}
SendMessage(wnd, EM_SETTEXTMODE, TM_PLAINTEXT, 0);
SendMessage(wnd, WM_SETFONT, (WPARAM)fontHandle, 1);
SendMessage(wnd, EM_SETMODIFY, 1, 0);
// one tab stop every 16 dialog units
UInt32 tabStopSize = 16;
SendMessage(wnd, EM_SETTABSTOPS, 1, (LPARAM)& tabStopSize);
DeleteObject(fontHandle);
}
static __HOOK ModScriptWindow()
{
__asm
{
call CreateWindowExA
pushad
push eax
call DoModScriptWindow
add esp, 4
popad
push 0x0B0001
push 0x005C4336
ret
}
}
static void FixEditorFont(void)
{
// Right after call d:CreateWindowA after aRichedit20a
UInt32 basePatchAddr = 0x005C432B;
WriteRelJump(basePatchAddr, UInt32(ModScriptWindow));
SafeWrite8(basePatchAddr + 5, 0x90);
}
static __HOOK FixMultiBounds()
{
__asm
{
cmp eax, 0x7F7FFFFF
je AhhGary
mov ecx, dword ptr ds : [eax + 0x0C]
mov dword ptr ds : [ecx + 0x08] , 0x00000000
AhhGary:
ret
}
}
// make sure geckcustom.ini is written if bFastExit is enabled - credit to jazzisparis
bool GetINIExists()
{
char iniPath[MAX_PATH];
strcpy(iniPath, (const char*)0x00F2CC80);
strcat(iniPath, "GECKCustom.ini");
DWORD attr = GetFileAttributes(iniPath);
return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_DIRECTORY);
}
// fix destruction dialog close button - credit to roy
BOOL WINAPI hk_DialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_CLOSE)
{
EndDialog(hWnd, 0);
DestroyWindow(hWnd);
return TRUE;
}
return StdCall<LRESULT>(0x004E5E30, hWnd, uMsg, wParam, lParam);
}
// fix handle memory leak - credit to nukem
INT_PTR WINAPI DialogFuncOverride(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
DLGPROC proc = nullptr;
g_DialogMutex.lock();
{
auto itr = g_DialogOverrides.find(hwndDlg);
if (itr != g_DialogOverrides.end())
proc = itr->second.DialogFunc;
// if (is new entry)
if (!proc)
{
g_DialogOverrides[hwndDlg] = *DlgData;
proc = DlgData->DialogFunc;
delete DlgData;
DlgData = nullptr;
}
// Purge old entries every now and then
if (g_DialogOverrides.size() >= 50)
{
for (auto itr = g_DialogOverrides.begin(); itr != g_DialogOverrides.end();)
{
if (itr->first == hwndDlg || IsWindow(itr->first))
{
itr++;
continue;
}
itr = g_DialogOverrides.erase(itr);
}
}
}
g_DialogMutex.unlock();
return proc(hwndDlg, uMsg, wParam, lParam);
}
HWND WINAPI hk_CreateDialogParamA(HINSTANCE hInstance, LPCSTR lpTemplateName, HWND hWndParent, DLGPROC lpDialogFunc, LPARAM dwInitParam)
{
// Override certain default dialogs to use this DLL's resources and callbacks
switch ((uintptr_t)lpTemplateName)
{
case IDD_HAVOK_PREVIEW_DIALOG:// "Havok Preview Window"
hInstance = (HINSTANCE)& __ImageBase;
lpDialogFunc = HavokPreviewCallback;
break;
}
// EndDialog MUST NOT be used
DialogOverrideData* data = new DialogOverrideData;
data->DialogFunc = lpDialogFunc;
data->Param = dwInitParam;
data->IsDialog = false;
DlgData = data;
return CreateDialogParamA(hInstance, lpTemplateName, hWndParent, DialogFuncOverride, dwInitParam);
}
LRESULT CALLBACK SoundPickerListBoxCallback(HWND listBox, UINT Message, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
if (Message == WM_KEYDOWN)
{
if (wParam == VK_SPACE || wParam == VK_RETURN)
{
int index = SendMessageA(listBox, LB_GETCURSEL, 0, 0);
TESSound* sound = (TESSound*)SendMessageA(listBox, LB_GETITEMDATA, index, 0);
if (sound)
{
CdeclCall(0x5CBC90, sound, 0);
}
if (wParam == VK_RETURN)
{
EndDialog(GetParent(listBox), (INT_PTR)sound);
}
return FALSE;
}
}
else if (Message == WM_GETDLGCODE)
{
// Indicate that we want to handle specific keys like VK_RETURN
return DLGC_WANTALLKEYS;
}
return DefSubclassProc(listBox, Message, wParam, lParam);
}
void __cdecl OnSoundPickerPlaySound(void* buf, TESSound* sound, UInt32 flags)
{
// call a method that prevents sounds overlapping by only allowing a single sound at once
CdeclCall(0x5CBC90, sound, flags);
}
BOOL __stdcall SoundPickCallback(HWND hDlg, UINT Message, WPARAM wParam, LPARAM lParam)
{
constexpr int TIMER_ID = 1, TIMER_INTERVAL_MS = 1;
switch (Message)
{
case WM_INITDIALOG:
{
auto listBox = GetDlgItem(hDlg, 1018);
SendMessageA(listBox, WM_SETREDRAW, FALSE, 0);
SetWindowSubclass(listBox, SoundPickerListBoxCallback, 0, 0);
SetTimer(hDlg, TIMER_ID, TIMER_INTERVAL_MS, NULL);
// prevent overlapping sounds
WriteRelCall(0x47BD67, UInt32(OnSoundPickerPlaySound));
WriteRelCall(0x47BDF8, UInt32(OnSoundPickerPlaySound));
XUtil::PatchMemoryNop(0x47BD25, 5); // prevent sound stopping when closing the menu
break;
}
case WM_TIMER:
{
if (wParam == TIMER_ID)
{
ThisCall(0x885130, *(UInt32**)0xF226C0, 1); // BSAudioManager::Update
return TRUE;
}
}
case WM_DESTROY:
{
KillTimer(hDlg, TIMER_ID);
break;
}
}
auto result = CallWindowProc((WNDPROC)0x47BC70, hDlg, Message, wParam, lParam);
if (Message == WM_INITDIALOG)
{
auto listBox = GetDlgItem(hDlg, 1018);
SendMessageA(listBox, WM_SETREDRAW, TRUE, 0);
}
return result;
}
INT_PTR WINAPI hk_DialogBoxParamA(HINSTANCE hInstance, LPCSTR lpTemplateName, HWND hWndParent, DLGPROC lpDialogFunc, LPARAM dwInitParam)
{
if (lpDialogFunc == (DLGPROC)0x47BC70)
{
// add Return to preview the hovered item in the sound picker
lpDialogFunc = SoundPickCallback;
}
// EndDialog MUST be used
DialogOverrideData* data = new DialogOverrideData;
data->DialogFunc = lpDialogFunc;
data->Param = dwInitParam;
data->IsDialog = true;
DlgData = data;
return DialogBoxParamA(hInstance, lpTemplateName, hWndParent, DialogFuncOverride, dwInitParam);
}
BOOL WINAPI hk_EndDialog(HWND hDlg, INT_PTR nResult)
{
std::lock_guard<std::recursive_mutex> lock(g_DialogMutex);
// Fix for the CK calling EndDialog on a CreateDialogParamA window
auto itr = g_DialogOverrides.find(hDlg);
if (itr != g_DialogOverrides.end() && !itr->second.IsDialog)
{
DestroyWindow(hDlg);
return TRUE;
}
return EndDialog(hDlg, nResult);
}
__HOOK hk_QuestWindowLoad()
{
__asm
{
mov eax, dword ptr ds : [0x00ECC3C8]
mov cb_reset[0], 1
mov cb_reset[1], 1
mov cb_reset[2], 1
ret
}
}
LRESULT WINAPI hk_SendMessageA(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
if (hWnd && Msg == WM_DESTROY)
{
std::lock_guard<std::recursive_mutex> lock(g_DialogMutex);
// If this is a dialog, we can't call DestroyWindow on it
auto itr = g_DialogOverrides.find(hWnd);
if (itr != g_DialogOverrides.end())
{
if (!itr->second.IsDialog)
DestroyWindow(hWnd);
}
return 0;
}
if (Msg == CB_ADDSTRING || Msg == CB_RESETCONTENT)
{
if (config.bCacheComboboxes)
{
int id = GetDlgCtrlID(hWnd);
for (int i = 0; i < _countof(cb_ids); i++)
{
if (id == cb_ids[i])
{
if (Msg == CB_ADDSTRING)
{
if (cb_filled[i])
{
return CB_ERR;
}
}
else if (Msg == CB_RESETCONTENT)
{
if (!cb_reset[i])
{
cb_filled[i] = !!SendMessageA(hWnd, CB_GETCOUNT, NULL, NULL);
return CB_ERR;
}
cb_filled[i] = false;
cb_reset[i] = false;
}
break;
}
}
}
}
return SendMessageA(hWnd, Msg, wParam, lParam);
}
HWND __stdcall SplashScreenHook(HINSTANCE hInstance, LPCSTR lpTemplateName, HWND hWndParent, DLGPROC lpDialogFunc, LPARAM dwInitParam)
{
HWND hWnd = hk_CreateDialogParamA(hInstance, lpTemplateName, hWndParent, lpDialogFunc, dwInitParam);
SendMessage(GetDlgItem((HWND)hWnd, 1962), STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)LoadImage(ZeGaryHaxHandle, MAKEINTRESOURCE(IDB_SPLASHBITMAP), IMAGE_BITMAP, 0, 0, 0));
return hWnd;
}
// patch about dialog - credit to roy and nukem
HWND __stdcall AboutDialogHook(HINSTANCE hInstance, LPCSTR lpTemplateName, HWND hWndParent, DLGPROC lpDialogFunc, LPARAM dwInitParam)
{
HWND hWnd = hk_CreateDialogParamA(hInstance, lpTemplateName, hWndParent, lpDialogFunc, dwInitParam);
SendMessage(GetDlgItem((HWND)hWnd, 1963), STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)LoadImage(ZeGaryHaxHandle, MAKEINTRESOURCE(IDB_SPLASHBITMAP), IMAGE_BITMAP, 0, 0, 0));
return hWnd;
}
void __stdcall ConditionDataDialog_SetupColumnHeaders(HWND ListViewControl)
{
StdCall(0x4A1C10, ListViewControl);
// set the list view headers to resize automatically
PostMessageA(ListViewControl, LVM_SETCOLUMNWIDTH, 0, LVSCW_AUTOSIZE_USEHEADER);
PostMessageA(ListViewControl, LVM_SETCOLUMNWIDTH, 1, LVSCW_AUTOSIZE_USEHEADER);
PostMessageA(ListViewControl, LVM_SETCOLUMNWIDTH, 2, LVSCW_AUTOSIZE_USEHEADER);
PostMessageA(ListViewControl, LVM_SETCOLUMNWIDTH, 3, LVSCW_AUTOSIZE_USEHEADER);
PostMessageA(ListViewControl, LVM_SETCOLUMNWIDTH, 4, LVSCW_AUTOSIZE_USEHEADER);
PostMessageA(ListViewControl, LVM_SETCOLUMNWIDTH, 5, LVSCW_AUTOSIZE_USEHEADER);
}
// fix crash with esm as active file - credit to hlp
__HOOK hk_addr_42CD31()
{
__asm
{
test ecx, ecx
jne HahaGary
xor al, al
mov dword ptr ss : [esp] , 0x0042CD6D
ret
HahaGary:
mov esi, dword ptr ds : [ecx + 0x110]
mov edx, dword ptr ds : [eax]
ret
}
}
// patch ONAM count - credit to hlp
__HOOK hk_addr_4E26BA()
{
__asm
{
mov dword ptr ds : [esi + 0x434] , eax
mov edx, 0x04
mul edx
seto cl
ret
}
}
// Fix Rock-It Launcher crash - credit to jazzisparis
TESForm* (*GetFormByID)(UInt32 formID) = (TESForm * (*)(UInt32))0x004F9620;
bool __fastcall GetIsRIL(TESForm* form)
{
static UInt32 RIL_FormID = 0;
if (!RIL_FormID)
{
RIL_FormID = 0xFFFFFFFF;
DataHandler* dataHandler = *(DataHandler**)0x00ED3B0C;
if (dataHandler)
{
for (tList<ModInfo>::Iterator infoIter = dataHandler->modList.modInfoList.Begin(); !infoIter.End(); ++infoIter)
{
if (_stricmp(infoIter->name, "Fallout3.esm")) continue;
RIL_FormID = (infoIter->modIndex << 0x18) | 0x434B;
break;
}
}
}
return form->refID == RIL_FormID;
}
__HOOK CheckIsRILHook()
{
static const UInt32 kRetnAddr = 0x005B8FFD;
__asm
{
mov ecx, esi
call GetIsRIL
test al, al
jz notRIL
retn
notRIL:
push edi
mov edi, [esp + 0xC]
jmp kRetnAddr
}
}
// enable/disable spell checker - credit to roy
__HOOK hk_SpellCheck()
{
static const UInt32 kRetnAddrYes = 0x0041B613;
static const UInt32 kRetnAddrNo = 0x0041B61A;
__asm
{
cmp config.bEnableSpellChecker, 0
je AwwGary
call dword ptr ds : [0x00D23548] // GetDlgItem
jmp kRetnAddrYes
AwwGary:
add esp, 0x0C
jmp kRetnAddrNo
}
}
// make search and replace window stay open unless explicitly closed - credit to StewieA
void __declspec(naked) hk_SearchDestroyHook()
{
_asm
{
push 0x47CE8C
jmp dword ptr ds : [0x00D2353C] // MessageBoxA
}
}
BOOL IsWindowHidden(HWND hWnd)
{
return !(GetWindowLong(hWnd, GWL_STYLE) & WS_VISIBLE);
}
void RestoreCachedSearchAndReplaceWindow(HWND hDlg)
{
auto searchForDropdown = GetDlgItem(hDlg, 1439);
auto replaceWithDropdown = GetDlgItem(hDlg, 1440);
int index = -1;
auto selected = RenderWindow::SelectedData::GetSelected();
if (selected->selectedForms)
{
if (auto ref = CdeclCall<TESObjectREFR*>(0xC5D114, selected->selectedForms->ref, 0, 0xE8C57C, 0xE8E3E4, 0)) // DYNAMIC_CAST(TESForm, TESObjectREFR)
{
if (ref->baseForm)
{
index = SendMessageA(searchForDropdown, CB_SELECTSTRING, 0xFFFFFFFF, (LPARAM)ref->baseForm->GetEditorID());
}
}
}
if (index == -1)
{
SendMessageA(searchForDropdown, CB_SETCURSEL, 0, 0);
SendMessageA(replaceWithDropdown, CB_SETCURSEL, 1u, 0);
}
else
{
SendMessageA(replaceWithDropdown, CB_SETCURSEL, index + 1, 0);
}
ShowWindow(hDlg, SW_SHOW);
}
HWND cachedSearchAndReplaceWindow;
void __stdcall OnCreateSearchAndReplaceWindow(HINSTANCE hInstance, LPCSTR lpTemplateName, HWND hWndParent, DLGPROC lpDialogFunc, LPARAM dwInitParam)
{
if (cachedSearchAndReplaceWindow && IsWindow(cachedSearchAndReplaceWindow))
{
if (IsWindowHidden(cachedSearchAndReplaceWindow))
{
RestoreCachedSearchAndReplaceWindow(cachedSearchAndReplaceWindow);
}
else
{
// create a new dialog (case where there's multiple search dialogs opened, and set the last opened as the cached window)
cachedSearchAndReplaceWindow = CreateDialogParamA(hInstance, lpTemplateName, hWndParent, lpDialogFunc, dwInitParam);
}
}
else
{
cachedSearchAndReplaceWindow = CreateDialogParamA(hInstance, lpTemplateName, hWndParent, lpDialogFunc, dwInitParam);
}
}
void __stdcall OnDestroySearchAndReplaceWindow(HWND hWnd)
{
if (hWnd == cachedSearchAndReplaceWindow)
{
ShowWindow(hWnd, SW_HIDE);
}
else
{
DestroyWindow(hWnd);
}
}
int hk_inflateInit(z_stream_s* Stream, const char* Version, int Mode)
{
// Force inflateEnd to error out and skip frees
Stream->state = nullptr;
return 0;
}
int hk_inflate(z_stream_s* Stream, int Flush)
{
size_t outBytes = 0;
libdeflate_decompressor* decompressor = libdeflate_alloc_decompressor();
libdeflate_result result = libdeflate_zlib_decompress(decompressor, Stream->next_in, Stream->avail_in, Stream->next_out, Stream->avail_out, &outBytes);
libdeflate_free_decompressor(decompressor);
if (result == LIBDEFLATE_SUCCESS)
{
Stream->total_in = Stream->avail_in;
Stream->total_out = (uint32_t)outBytes;
return 1;
}
if (result == LIBDEFLATE_INSUFFICIENT_SPACE)
return -5;
return -2;
}
_declspec(naked) void hk_PreviewWindowCheckForeground() {
static const UInt32 returnAddr = 0x48CACF;
_asm {
/* the hWnd was already pushed in esi */
call dword ptr ds : [0xD2347C] //GetParent
push ebp
mov ebp, eax
call dword ptr ds : [0xD233A0] // GetActiveWindow
cmp eax, ebp
pop ebp
jne skipSetFocus
push esi
call dword ptr ds : [0xD234DC] //SetFocus
skipSetFocus:
jmp returnAddr
}
}
const char* recompileAllWarning = { "Are you sure you want to recompile every script in every plugin?\nYou should never need to do this." };
_declspec(naked) void RecompileAllWarningScriptHook() {
static const UInt32 retnAddr = 0x5C498A;
_asm {
push 0x104 // change default button to No
push 0xD2FA78
push recompileAllWarning
jmp retnAddr
}
}
_declspec(naked) void RecompileAllWarningMainHook() {
static const UInt32 retnAddr = 0x4442D3;
_asm {
push 0x104 // change default button to No
push 0xD2FA78
push recompileAllWarning
jmp retnAddr
}
}
void SetNthFileEnabled(HWND* pListView, int n, bool bEnabled)
{
if (n < 0)
{
return;
}
if (auto file = DataHandler::GetSingleton()->GetNthFile(n))
{
file->SetEnabled(bEnabled);
if (!file->IsEnabled())
{
file->SetActive(false);
if (file == *(ModInfo**)0xECF5D0)
{
*(ModInfo**)0xECF5D0 = 0;
}
}
SendMessageA(*pListView, LVM_UPDATE, n, 0);
}
}
bool AreAllFilesEnabled(HWND* pListView)
{
unsigned int index = -1;
do
{
index = SendMessageA(*pListView, LVM_GETNEXTITEM, index, LVIS_SELECTED);
if (index >= 0)
{
if (auto file = DataHandler::GetSingleton()->GetNthFile(index))
{
if (!file->IsEnabled())
{
return false;
}
}
}
} while (index != -1);
return true;
}
void ToggleSelectedFiles(HWND* pListView)
{
bool bEnabled = AreAllFilesEnabled(pListView);
unsigned int index = -1;
do
{
index = SendMessageA(*pListView, LVM_GETNEXTITEM, index, LVIS_SELECTED);
if (index != -1)
{
SetNthFileEnabled(pListView, index, !bEnabled);
}
} while (index != -1);
}
void doKonami(int);
void ResizeLoadEspEsmWindow(HWND hWnd, WORD newWidth, WORD newHeight);
WNDPROC originalLoadEspEsmFn;
BOOL CALLBACK LoadEspEsmCallback(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam) {
if (msg == WM_NOTIFY && ((LPNMHDR)lParam)->code == LVN_KEYDOWN)
{
auto key = ((LPNMLVKEYDOWN)lParam)->wVKey;
/* small konami easter egg */
doKonami(key);
HWND* pListView = *(HWND**)0xECF5C8;
bool isControlHeld = (GetKeyState(VK_CONTROL) & 0x8000);
if (isControlHeld)
{
if (key == 'A')
{
SelectAllItemsInListView(*pListView);
return true;
}
}
else if (key == VK_SPACE && config.bDataMenuSpaceTogglesSelectedFile)
{
ToggleSelectedFiles(pListView);
return true;
}
}
else if (msg == WM_SIZE)
{
WORD width = LOWORD(lParam);
WORD height = HIWORD(lParam);
ResizeLoadEspEsmWindow(hDlg, width, height);
}
else if (msg == WM_INITDIALOG)
{
auto result = CallWindowProc(originalLoadEspEsmFn, hDlg, msg, wParam, lParam);
auto listView = GetDlgItem(hDlg, 1056);
PostMessageA(listView, LVM_SETCOLUMNWIDTH, 0, LVSCW_AUTOSIZE_USEHEADER);
return result;
}
return CallWindowProc(originalLoadEspEsmFn, hDlg, msg, wParam, lParam);
}
void doKonami(int key) {
static int konamiStage = 0;
switch (konamiStage) {
case 0:
(key == VK_UP) ? konamiStage++ : konamiStage = 0;
break;
case 1:
(key == VK_UP) ? konamiStage++ : konamiStage = 0;
break;
case 2:
if (key == VK_DOWN) konamiStage++;
else if (key != VK_UP) konamiStage = 0;
break;
case 3:
(key == VK_DOWN) ? konamiStage++ : konamiStage = 0;
break;
case 4:
(key == VK_LEFT) ? konamiStage++ : konamiStage = 0;
break;
case 5:
(key == VK_RIGHT) ? konamiStage++ : konamiStage = 0;
break;
case 6:
(key == VK_LEFT) ? konamiStage++ : konamiStage = 0;
break;
case 7:
(key == VK_RIGHT) ? konamiStage++ : konamiStage = 0;
break;
case 8:
(key == 'B') ? konamiStage++ : konamiStage = 0;
break;
case 9:
if (key == 'A') {
Console_Print("Konami!");
}
konamiStage = 0;
break;
}
/* handles the case where up is pressed in the middle of the sequence */
if (konamiStage == 0 && key == VK_UP) konamiStage++;
}
BOOL __stdcall hk_SearchAndReplaceCallback(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam) {
return StdCall<LRESULT>(0x47C990, hDlg, msg, wParam, lParam);
}
_declspec(naked) void EndLoadingHook() {
PlaySound("MouseClick", NULL, SND_ASYNC);
_asm
{
// originalCode
add esp, 8
pop esi
retn
}
}
// hooks before movement speed is determined for flycam mode
static int lastFlycamTime = 0;
_declspec(naked) void FlycamMovementSpeedMultiplierHook() {
_asm
{
pushad
}
static const UInt32 retnAddr = 0x455D17;
*(float*)(0xED12C0) = config.fFlycamNormalMovementSpeed;
if (GetAsyncKeyState(VK_SHIFT) < 0) {
*(float*)(0xED12C0) *= config.fFlycamShiftMovementSpeed;
}
if (GetAsyncKeyState(VK_MENU) < 0) {
*(float*)(0xED12C0) *= config.fFlycamAltMovementSpeed;
}
/* fix flycam speed being dependent on framerate by slowing down movement if framerate is greater than 30fps (33ms/frame)*/
if (GetTickCount() - lastFlycamTime < 33) {
*(float*)(0xED12C0) *= (GetTickCount() - lastFlycamTime) / 33.0;
}
lastFlycamTime = GetTickCount();
_asm
{
popad
// originalCode
mov eax, dword ptr ds : [0xF1FBF4]
jmp retnAddr
}
}
// edi : difference in x pos,
// esp+0x324 : difference in y pos
// Hook to make flycam rotation speed based on x/y position difference rather than 1.0 for any difference
_declspec(naked) void FlycamRotationSpeedMultiplierHook() {
static const UInt32 retnAddr = 0x45D3F8;
_asm {
mov dword ptr ds : [0x00ED140C] , ebx
push edi
fild dword ptr ss : [esp]
add esp, 4
fmul config.fFlycamRotationSpeed
fstp dword ptr ss : [esp + 0xF0]
fild dword ptr ss : [esp + 0x324]
fmul config.fFlycamRotationSpeed
fstp dword ptr ss : [esp + 0x18]
jmp retnAddr
}
}
_declspec(naked) void ReferenceBatchActionDoButtonHook() {
static const UInt32 retnAddr = 0x411CD4;
_asm {
push dword ptr ds : [0xECED38] // replaces a 'push 0' with the selected action
push 1
push esi
jmp retnAddr
}
}
/* ideally this would be replaced with a wrapper around the Script Edit callback function */
_declspec(naked) void ScriptEditKeypressHook(HWND hWnd) {
static const UInt32 skipAddr = 0x5C40EC;
static const UInt32 saveAddr = 0x5C4961;
static const UInt32 retnAddr = 0x5C3ED3;
_asm {
ja skip
cmp ebx, 'S'
jne notSave
cmp byte ptr ds : [esp + 0x1F] , 0 // check if control pressed
jz notSave
jmp saveAddr
skip:
jmp skipAddr
notSave:
jmp retnAddr
}
}
bool __fastcall ScriptEdit__Save(byte* thiss, void* dummyEDX, HWND hDlg, char a3, char a4) {
bool saveSuccess = ThisCall<bool>(0x5C2F40, thiss, hDlg, a3, a4);
if (saveSuccess) {
SendDlgItemMessageA(hDlg, 1166, EM_SETMODIFY, 0, 0);
// remove the '*' from the end of the window name
char windowName[MAX_PATH];
int len = GetWindowTextA(hDlg, windowName, MAX_PATH) - 1;
if (len > 2 && windowName[len] == '*') {
windowName[len - 1] = '\0'; // strip the '* '
SetWindowTextA(hDlg, windowName);
}
}
return saveSuccess;
}
float GetRefSpeedMultiplier() {
float multiplier = 1.0F;
if (GetAsyncKeyState(VK_SHIFT) < 0) {
multiplier *= config.fMovementShiftMultiplier;
}
if (GetAsyncKeyState(VK_MENU) < 0) {
multiplier *= config.fMovementAltMultiplier;