-
Notifications
You must be signed in to change notification settings - Fork 148
/
Hooks.cpp
930 lines (790 loc) · 23.8 KB
/
Hooks.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
#include <cassert>
#include <cstdio>
#include <cstring>
#include <system_error>
#ifndef _WIN32
#include <sys/mman.h>
#include <unistd.h>
#endif
#include "Audio/Audio.h"
#include "Config.h"
#include "Console.h"
#include "Drawing/SoftwareDrawingEngine.h"
#include "Entities/EntityManager.h"
#include "Environment.h"
#include "Game.h"
#include "GameCommands/GameCommands.h"
#include "Graphics/Colour.h"
#include "Graphics/Gfx.h"
#include "Gui.h"
#include "IndustryManager.h"
#include "Input.h"
#include "Interop.hpp"
#include "Map/AnimationManager.h"
#include "Map/Tile.h"
#include "Map/TileManager.h"
#include "Map/WaveManager.h"
#include "Objects/ObjectManager.h"
#include "OpenLoco.h"
#include "Paint/Paint.h"
#include "Platform/Platform.h"
#include "S5/S5.h"
#include "Scenario.h"
#include "Station.h"
#include "StationManager.h"
#include "Title.h"
#include "TownManager.h"
#include "Tutorial.h"
#include "Ui.h"
#include "Ui/ProgressBar.h"
#include "Ui/WindowManager.h"
#include "Vehicles/Vehicle.h"
#include "ViewportManager.h"
#include "Widget.h"
#include <OpenLoco/Core/FileSystem.hpp>
#include <OpenLoco/Utility/String.hpp>
using namespace OpenLoco;
#define STUB() Console::logVerbose(__FUNCTION__)
#ifdef _MSC_VER
#define STDCALL __stdcall
#define CDECL __cdecl
#elif defined(__GNUC__)
#define STDCALL __attribute__((stdcall))
#define CDECL __attribute__((cdecl))
#else
#error Unknown compiler, please define STDCALL and CDECL
#endif
#pragma warning(push)
// MSVC ignores C++17's [[maybe_unused]] attribute on functions, so just disable the warning
#pragma warning(disable : 4505) // unreferenced local function has been removed.
#ifdef _NO_LOCO_WIN32_
FORCE_ALIGN_ARG_POINTER
static void STDCALL fn_40447f()
{
STUB();
return;
}
FORCE_ALIGN_ARG_POINTER
static void STDCALL fn_404b68(int a0, int a1, int a2, int a3)
{
STUB();
return;
}
FORCE_ALIGN_ARG_POINTER
static int STDCALL getNumDSoundDevices()
{
STUB();
return 0;
}
#endif // _NO_LOCO_WIN32_
#pragma pack(push, 1)
struct PaletteEntry
{
uint8_t b, g, r, a;
};
#pragma pack(pop)
using SetPaletteFunc = void (*)(const PaletteEntry* palette, int32_t index, int32_t count);
static Interop::loco_global<SetPaletteFunc, 0x0052524C> _setPaletteCallback;
#ifdef _NO_LOCO_WIN32_
FORCE_ALIGN_ARG_POINTER
static void CDECL fn_4054a3(const PaletteEntry* palette, int32_t index, int32_t count)
{
if (_setPaletteCallback != nullptr)
{
(*_setPaletteCallback)(palette, index, count);
}
}
FORCE_ALIGN_ARG_POINTER
static uint32_t STDCALL lib_timeGetTime()
{
return Platform::getTime();
}
// typedef bool (CALLBACK *LPDSENUMCALLBACKA)(LPGUID, char*, char*, void*);
FORCE_ALIGN_ARG_POINTER
static long STDCALL fn_DirectSoundEnumerateA(void* pDSEnumCallback, void* pContext)
{
STUB();
return 0;
}
FORCE_ALIGN_ARG_POINTER
static void STDCALL fn_4078be()
{
STUB();
return;
}
FORCE_ALIGN_ARG_POINTER
static void STDCALL fn_4078fe()
{
STUB();
return;
}
FORCE_ALIGN_ARG_POINTER
static void STDCALL fn_407b26()
{
STUB();
return;
}
/// region Progress bar
FORCE_ALIGN_ARG_POINTER
static void CDECL fn_4080bb(char* lpWindowName, uint32_t a1)
{
Console::log("Create progress bar");
}
FORCE_ALIGN_ARG_POINTER
static void CDECL fn_408163()
{
Console::log("Destroy progress bar");
}
FORCE_ALIGN_ARG_POINTER
static void CDECL fn_40817b(uint16_t arg0)
{
Console::log("SendMessage(PBM_SETRANGE, %d)", arg0);
Console::log("SendMessage(PBM_SETSTEP, %d)", 1);
}
FORCE_ALIGN_ARG_POINTER
static void CDECL fn_4081ad(int32_t wParam)
{
Console::log("SendMessage(PBM_SETPOS, %d)", wParam);
}
/// endregion
FORCE_ALIGN_ARG_POINTER
static uint32_t CDECL fn_FileSeekSet(FILE* a0, int32_t distance)
{
Console::logVerbose("seek %d bytes from start", distance);
fseek(a0, distance, SEEK_SET);
return ftell(a0);
}
FORCE_ALIGN_ARG_POINTER
static uint32_t CDECL fn_FileSeekFromCurrent(FILE* a0, int32_t distance)
{
Console::logVerbose("seek %d bytes from current", distance);
fseek(a0, distance, SEEK_CUR);
return ftell(a0);
}
FORCE_ALIGN_ARG_POINTER
static uint32_t CDECL fn_FileSeekFromEnd(FILE* a0, int32_t distance)
{
Console::logVerbose("seek %d bytes from end", distance);
fseek(a0, distance, SEEK_END);
return ftell(a0);
}
FORCE_ALIGN_ARG_POINTER
static int32_t CDECL fn_FileRead(FILE* a0, char* buffer, int32_t size)
{
Console::logVerbose("read %d bytes (%d)", size, fileno(a0));
size = fread(buffer, 1, size, a0);
return size;
}
typedef struct FindFileData
{
uint32_t dwFileAttributes;
uint32_t ftCreationTime[2];
uint32_t ftLastAccessTime[2];
uint32_t ftLastWriteTime[2];
uint32_t nFileSizeHigh;
uint32_t nFileSizeLow;
uint32_t r0;
uint32_t r1;
char cFilename[260];
char cAlternateFileName[14];
} FindFileData;
class Session
{
public:
std::vector<fs::path> fileList;
};
#define FILE_ATTRIBUTE_DIRECTORY 0x10
FORCE_ALIGN_ARG_POINTER
static Session* CDECL fn_FindFirstFile(char* lpFileName, FindFileData* out)
{
Console::logVerbose("%s (%s)", __FUNCTION__, lpFileName);
Session* data = new Session;
fs::path path = lpFileName;
path.remove_filename();
fs::directory_iterator iter(path), end;
while (iter != end)
{
data->fileList.push_back(iter->path());
++iter;
}
Utility::strcpy_safe(out->cFilename, data->fileList[0].filename().u8string().c_str());
if (fs::is_directory(data->fileList[0]))
{
out->dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
}
else
{
out->dwFileAttributes &= ~FILE_ATTRIBUTE_DIRECTORY;
}
data->fileList.erase(data->fileList.begin());
return data;
}
FORCE_ALIGN_ARG_POINTER
static uint32_t CDECL fn_FindNextFile(Session* data, FindFileData* out)
{
STUB();
if (data->fileList.size() == 0)
{
return 0;
}
Utility::strcpy_safe(out->cFilename, data->fileList[0].filename().u8string().c_str());
if (fs::is_directory(data->fileList[0]))
{
out->dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
}
else
{
out->dwFileAttributes &= ~FILE_ATTRIBUTE_DIRECTORY;
}
data->fileList.erase(data->fileList.begin());
return 1;
}
FORCE_ALIGN_ARG_POINTER
static void CDECL fn_FindClose(Session* data)
{
STUB();
delete data;
}
#endif // _NO_LOCO_WIN32_
FORCE_ALIGN_ARG_POINTER
[[maybe_unused]] static void CDECL fnc0(void)
{
STUB();
}
FORCE_ALIGN_ARG_POINTER
[[maybe_unused]] static void CDECL fnc1(int i1)
{
STUB();
}
FORCE_ALIGN_ARG_POINTER
[[maybe_unused]] static void CDECL fnc2(int i1, int i2)
{
STUB();
}
FORCE_ALIGN_ARG_POINTER
[[maybe_unused]] static void STDCALL fn0()
{
return;
}
FORCE_ALIGN_ARG_POINTER
[[maybe_unused]] static void STDCALL fn1(int i1)
{
return;
}
FORCE_ALIGN_ARG_POINTER
[[maybe_unused]] static void STDCALL fn2(int i1, int i2)
{
STUB();
}
FORCE_ALIGN_ARG_POINTER
static void* CDECL fn_malloc(uint32_t size)
{
return malloc(size);
}
FORCE_ALIGN_ARG_POINTER
static void* CDECL fn_realloc(void* block, uint32_t size)
{
return realloc(block, size);
}
FORCE_ALIGN_ARG_POINTER
static void CDECL fn_free(void* block)
{
return free(block);
}
#ifdef _NO_LOCO_WIN32_
FORCE_ALIGN_ARG_POINTER
static void STDCALL fn_dump(uint32_t address)
{
Console::log("Missing hook: 0x%x", address);
}
enum
{
DS_OK = 0,
DSERR_NODRIVER = 0x88780078,
};
FORCE_ALIGN_ARG_POINTER
static uint32_t STDCALL lib_DirectSoundCreate(void* lpGuid, void* ppDS, void* pUnkOuter)
{
Console::log("lib_DirectSoundCreate(%lx, %lx, %lx)", (uintptr_t)lpGuid, (uintptr_t)ppDS, (uintptr_t)pUnkOuter);
return DSERR_NODRIVER;
}
FORCE_ALIGN_ARG_POINTER
static uint32_t STDCALL lib_CreateRectRgn(int x1, int y1, int x2, int y2)
{
Console::log("CreateRectRgn(%d, %d, %d, %d)", x1, y1, x2, y2);
return 0;
}
FORCE_ALIGN_ARG_POINTER
static uint32_t STDCALL lib_GetUpdateRgn(uintptr_t hWnd, uintptr_t hRgn, bool bErase)
{
Console::log("GetUpdateRgn(%lx, %lx, %d)", hWnd, hRgn, bErase);
return 0;
}
FORCE_ALIGN_ARG_POINTER
static void* STDCALL lib_OpenMutexA(uint32_t dwDesiredAccess, bool bInheritHandle, char* lpName)
{
Console::log("OpenMutexA(0x%x, %d, %s)", dwDesiredAccess, bInheritHandle, lpName);
return nullptr;
}
FORCE_ALIGN_ARG_POINTER
static uint32_t STDCALL lib_DeleteFileA(char* lpFileName)
{
Console::log("DeleteFileA(%s)", lpFileName);
return 0;
}
FORCE_ALIGN_ARG_POINTER
static uint32_t STDCALL lib_WriteFile(
FILE* hFile,
char* buffer,
size_t nNumberOfBytesToWrite,
uint32_t* lpNumberOfBytesWritten,
uintptr_t lpOverlapped)
{
*lpNumberOfBytesWritten = fwrite(buffer, 1, nNumberOfBytesToWrite, hFile);
Console::logVerbose("WriteFile(%s)", buffer);
return 1;
}
#define GENERIC_READ 0x80000000
#define GENERIC_WRITE 0x40000000
#define CREATE_NEW 1
#define CREATE_ALWAYS 2
#define OPEN_EXISTING 3
#define OPEN_ALWAYS 4
#define TRUNCATE_EXISTING 5
FORCE_ALIGN_ARG_POINTER
static int32_t STDCALL lib_CreateFileA(
char* lpFileName,
uint32_t dwDesiredAccess,
uint32_t dwShareMode,
uintptr_t lpSecurityAttributes,
uint32_t dwCreationDisposition,
uint32_t dwFlagsAndAttributes,
uintptr_t hTemplateFile)
{
Console::logVerbose("CreateFile(%s, 0x%x, 0x%x)", lpFileName, dwDesiredAccess, dwCreationDisposition);
FILE* pFILE = nullptr;
if (dwDesiredAccess == GENERIC_READ && dwCreationDisposition == OPEN_EXISTING)
{
pFILE = fopen(lpFileName, "r");
}
else if (dwDesiredAccess == GENERIC_WRITE && dwCreationDisposition == CREATE_ALWAYS)
{
pFILE = fopen(lpFileName, "w");
}
else
{
assert(false);
}
if (pFILE == nullptr)
{
return -1;
}
return (int32_t)pFILE;
}
FORCE_ALIGN_ARG_POINTER
static uint32_t STDCALL lib_SetFileAttributesA(char* lpFileName, uint32_t dwFileAttributes)
{
// FILE_ATTRIBUTE_NORMAL = 0x80
assert(dwFileAttributes == 0x80);
Console::log("SetFileAttributes(%s, %x)", lpFileName, dwFileAttributes);
std::error_code ec;
auto path = fs::path(lpFileName);
auto perms = fs::status(path, ec).permissions();
if (!ec)
{
lib_CreateFileA(lpFileName, dwFileAttributes, 0, 0, 0, 0, 0);
}
fs::permissions(path, fs::perms::owner_read | fs::perms::owner_write | perms, ec);
return !ec;
}
FORCE_ALIGN_ARG_POINTER
static void* STDCALL lib_CreateMutexA(uintptr_t lmMutexAttributes, bool bInitialOwner, char* lpName)
{
Console::log("CreateMutexA(0x%lx, %d, %s)", lmMutexAttributes, bInitialOwner, lpName);
return nullptr;
}
FORCE_ALIGN_ARG_POINTER
static uint32_t STDCALL lib_CloseHandle(void* hObject)
{
auto file = (FILE*)hObject;
return fclose(file) == 0;
}
FORCE_ALIGN_ARG_POINTER
static void STDCALL lib_PostQuitMessage(int32_t exitCode)
{
Console::log("lib_PostQuitMessage(%d)", exitCode);
exit(exitCode);
}
#endif // _NO_LOCO_WIN32_
#pragma warning(pop)
static void registerMemoryHooks()
{
using namespace OpenLoco::Interop;
// Hook Locomotion's alloc / free routines so that we don't
// allocate a block in one module and freeing it in another.
writeJmp(0x4D1401, (void*)&fn_malloc);
writeJmp(0x406BF7, (void*)&fn_malloc);
writeJmp(0x4D1B28, (void*)&fn_realloc);
writeJmp(0x406C02, (void*)&fn_realloc);
writeJmp(0x4D1355, (void*)&fn_free);
writeJmp(0x406C12, (void*)&fn_free);
}
#ifdef _NO_LOCO_WIN32_
static void registerNoWin32Hooks()
{
using namespace OpenLoco::Interop;
writeJmp(0x40447f, (void*)&fn_40447f);
writeJmp(0x404b68, (void*)&fn_404b68);
writeJmp(0x404e8c, (void*)&getNumDSoundDevices);
writeJmp(0x4064fa, (void*)&fn0);
writeJmp(0x4054a3, (void*)&fn_4054a3);
writeJmp(0x4072ec, (void*)&fn0);
writeJmp(0x4078be, (void*)&fn_4078be);
writeJmp(0x4078fe, (void*)&fn_4078fe);
writeJmp(0x407b26, (void*)&fn_407b26);
writeJmp(0x4080bb, (void*)&fn_4080bb);
writeJmp(0x408163, (void*)&fn_408163);
writeJmp(0x40817b, (void*)&fn_40817b);
writeJmp(0x4081ad, (void*)&fn_4081ad);
writeJmp(0x4081c5, (void*)&fn_FileSeekSet);
writeJmp(0x4081d8, (void*)&fn_FileSeekFromCurrent);
writeJmp(0x4081eb, (void*)&fn_FileSeekFromEnd);
writeJmp(0x4081fe, (void*)&fn_FileRead);
writeJmp(0x40830e, (void*)&fn_FindFirstFile);
writeJmp(0x40831d, (void*)&fn_FindNextFile);
writeJmp(0x40832c, (void*)&fn_FindClose);
writeJmp(0x4d0fac, (void*)&fn_DirectSoundEnumerateA);
// fill DLL hooks for ease of debugging
for (int i = 0x4d7000; i <= 0x4d72d8; i += 4)
{
hookDump(i, (void*)&fn_dump);
}
// dsound.dll
hookLib(0x4d7024, (void*)&lib_DirectSoundCreate);
// gdi32.dll
hookLib(0x4d7078, (void*)&lib_CreateRectRgn);
// kernel32.dll
hookLib(0x4d70e0, (void*)&lib_CreateMutexA);
hookLib(0x4d70e4, (void*)&lib_OpenMutexA);
hookLib(0x4d70f0, (void*)&lib_WriteFile);
hookLib(0x4d70f4, (void*)&lib_DeleteFileA);
hookLib(0x4d70f8, (void*)&lib_SetFileAttributesA);
hookLib(0x4d70fC, (void*)&lib_CreateFileA);
// user32.dll
hookLib(0x4d71e8, (void*)&lib_PostQuitMessage);
hookLib(0x4d714c, (void*)&lib_CloseHandle);
hookLib(0x4d7248, (void*)&lib_GetUpdateRgn);
hookLib(0x4d72b0, (void*)&lib_timeGetTime);
}
#endif // _NO_LOCO_WIN32_
void OpenLoco::Interop::loadSections()
{
#ifndef _WIN32
int32_t err = mprotect((void*)0x401000, 0x4d7000 - 0x401000, PROT_READ | PROT_WRITE | PROT_EXEC);
if (err != 0)
{
perror("mprotect");
}
err = mprotect((void*)0x4d7000, 0x1162000 - 0x4d7000, PROT_READ | PROT_WRITE);
if (err != 0)
{
perror("mprotect");
}
#endif
}
static void registerAudioHooks()
{
using namespace OpenLoco::Interop;
writeRet(0x0048AB36);
writeRet(0x00404B40);
registerHook(
0x0048A18C,
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
Audio::updateSounds();
return 0;
});
registerHook(
0x00489C6A,
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
Audio::stopVehicleNoise();
return 0;
});
registerHook(
0x0048A4BF,
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
Audio::playSound(X86Pointer<Vehicles::Vehicle2or6>(regs.esi));
return 0;
});
registerHook(
0x00489CB5,
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
Audio::playSound((Audio::SoundId)regs.eax, { regs.cx, regs.dx, regs.bp }, regs.ebx);
return 0;
});
registerHook(
0x00489F1B,
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
Audio::playSound((Audio::SoundId)regs.eax, { regs.cx, regs.dx, regs.bp }, regs.edi, regs.ebx);
return 0;
});
}
void OpenLoco::Interop::registerHooks()
{
using namespace OpenLoco::Ui::Windows;
registerMemoryHooks();
#ifdef _NO_LOCO_WIN32_
registerNoWin32Hooks();
#endif // _NO_LOCO_WIN32_
registerAudioHooks();
registerHook(
0x00431695,
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
registers backup = regs;
OpenLoco::sub_431695(0);
regs = backup;
return 0;
});
registerHook(
0x004416B5,
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
using namespace OpenLoco::Environment;
auto buffer = (char*)0x009D0D72;
auto path = getPath((PathId)regs.ebx);
// TODO: use Utility::strlcpy with the buffer size instead of std::strcpy, if possible
std::strcpy(buffer, path.make_preferred().u8string().c_str());
regs.ebx = X86Pointer(buffer);
return 0;
});
// Replace Ui::update() with our own
registerHook(
0x004524C1,
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
Ui::update();
return 0;
});
registerHook(
0x00407BA3,
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
auto cursor = (Ui::CursorId)regs.edx;
Ui::setCursor(cursor);
return 0;
});
registerHook(
0x00407231,
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
OpenLoco::Input::sub_407231();
return 0;
});
registerHook(
0x00451025,
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
registers backup = regs;
auto drawingCtx = Gfx::getDrawingEngine().getDrawingContext();
auto pos = drawingCtx.drawString(*X86Pointer<Gfx::RenderTarget>(regs.edi), regs.cx, regs.dx, static_cast<Colour>(regs.al), X86Pointer<uint8_t>(regs.esi));
regs = backup;
regs.cx = pos.x;
regs.dx = pos.y;
return 0;
});
// Until handling of State::viewportLeft has been implemented in mouse_input...
registerHook(
0x00490F6C,
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
Ui::Windows::StationList::open(CompanyId(regs.ax));
return 0;
});
registerHook(
0x004958C6,
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
registers backup = regs;
char* buffer = X86Pointer<char>(regs.edi);
void* args = X86Pointer(regs.ecx);
buffer = StringManager::formatString(buffer, regs.eax, args);
regs = backup;
regs.edi = X86Pointer(buffer);
return 0;
});
registerHook(
0x00438A6C,
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
Gui::init();
return 0;
});
/* This can be removed after implementing signal place and remove game commands.
* It fixes an original bug with those two game commands.
*/
registerHook(
0x004A2AD7,
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
addr<0x001135F88, uint16_t>() = 0;
regs.esi = 0x004A2AF0;
regs.edi = 0x004A2CE7;
call(0x004A2E46, regs);
// Set ebp register to a nice large non-negative number.
// This fixes exorbitant prices for signals on Linux and macOS.
// Value must be greater than the cost for placing a signal.
regs.ebp = 0xC0FFEE;
return 0;
});
registerHook(
0x004CA4DF,
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
registers backup = regs;
Ui::Window* window = X86Pointer<Ui::Window>(regs.esi);
auto rt = X86Pointer<Gfx::RenderTarget>(regs.edi);
window->draw(rt);
regs = backup;
return 0;
});
registerHook(
0x004CF63B,
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
registers backup = regs;
Gfx::renderAndUpdate();
regs = backup;
return 0;
});
Ui::ProgressBar::registerHooks();
Map::TileManager::registerHooks();
Map::AnimationManager::registerHooks();
Ui::Windows::TextInput::registerHooks();
Ui::Windows::ToolTip::registerHooks();
Ui::Windows::Vehicle::registerHooks();
Ui::Windows::BuildVehicle::registerHooks();
Ui::Windows::Error::registerHooks();
Ui::Windows::Construction::registerHooks();
Ui::WindowManager::registerHooks();
Ui::ViewportManager::registerHooks();
GameCommands::registerHooks();
Scenario::registerHooks();
StationManager::registerHooks();
TownManager::registerHooks();
S5::registerHooks();
Title::registerHooks();
OpenLoco::Tutorial::registerHooks();
Paint::registerHooks();
Config::registerHooks();
ObjectManager::registerHooks();
// Part of 0x004691FA
registerHook(
0x0046956E,
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
registers backup = regs;
Map::Pos2 pos(regs.ax, regs.cx);
Map::SurfaceElement* surface = X86Pointer<Map::SurfaceElement>(regs.esi);
Map::WaveManager::createWave(*surface, pos);
regs = backup;
return 0;
});
registerHook(
0x004AB655,
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
Vehicles::VehicleBase* v = X86Pointer<Vehicles::VehicleBase>(regs.esi);
v->asVehicleBody()->secondaryAnimationUpdate();
return 0;
});
registerHook(
0x004392BD,
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
Gui::resize();
return 0;
});
registerHook(
0x004C6456,
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
registers backup = regs;
Ui::Window* window = X86Pointer<Ui::Window>(regs.esi);
window->viewportsUpdatePosition();
regs = backup;
return 0;
});
registerHook(
0x004C9513,
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
registers backup = regs;
Ui::Window* window = X86Pointer<Ui::Window>(regs.esi);
int16_t x = regs.ax;
int16_t y = regs.bx;
auto widgetIndex = window->findWidgetAt(x, y);
regs = backup;
regs.edx = widgetIndex;
if (widgetIndex == -1)
{
regs.edi = X86Pointer(&window->widgets[0]);
}
else
{
regs.edi = X86Pointer(&window->widgets[widgetIndex]);
}
return 0;
});
registerHook(
0x004CA115,
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
registers backup = regs;
Ui::Window* window = X86Pointer<Ui::Window>(regs.esi);
window->updateScrollWidgets();
regs = backup;
return 0;
});
registerHook(
0x004CA17F,
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
registers backup = regs;
Ui::Window* window = X86Pointer<Ui::Window>(regs.esi);
window->initScrollWidgets();
regs = backup;
return 0;
});
registerHook(
0x004C57C0,
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
registers backup = regs;
initialiseViewports();
regs = backup;
return 0;
});
registerHook(
0x004C5DD5,
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
registers backup = regs;
Gfx::render(regs.ax, regs.bx, regs.dx, regs.bp);
regs = backup;
return 0;
});
// Remove check for is road in use when removing roads. It is
// quite annoying when it's sometimes only the player's own
// vehicles that are using it.
writeNop(0x004776DD, 6);
registerHook(
0x0047024A,
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
registers backup = regs;
auto* entity = reinterpret_cast<EntityBase*>(regs.esi);
EntityManager::freeEntity(entity);
regs = backup;
return 0;
});
registerHook(
0x00448C79,
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
registers backup = regs;
Gfx::RenderTarget* rt = X86Pointer<Gfx::RenderTarget>(regs.edi);
auto drawingCtx = Gfx::getDrawingEngine().getDrawingContext();
drawingCtx.drawImage(*rt, { regs.cx, regs.dx }, ImageId::fromUInt32(regs.ebx));
regs = backup;
return 0;
});
registerHook(
0x00454A43,
[](registers& regs) FORCE_ALIGN_ARG_POINTER -> uint8_t {
registers backup = regs;
Map::Pos2 pos{ regs.ax, regs.cx };
uint8_t primaryWall = regs.bl;
uint8_t secondaryWall = regs.bh;
auto* industry = IndustryManager::get(static_cast<IndustryId>(regs.dh));
industry->expandGrounds(pos, primaryWall, secondaryWall, regs.dl);
regs = backup;
return 0;
});
}