-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdllmain.c
2416 lines (2105 loc) · 68.7 KB
/
dllmain.c
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
/*
*****************************************************************
* Drakan: Order of the Flame All in One Patch (DLL part) *
* *
* Copyright © 2015 - 2018 UCyborg *
* *
* This software uses 3rd-party code, which is subject *
* to their respective licenses. *
* *
* This software is provided 'as-is', without any express *
* or implied warranty. In no event will the authors be held *
* liable for any damages arising from the use of this *
* software. *
* *
* 1. The origin of this software must not be misrepresented; *
* you must not claim that you wrote the original software. *
* If you use this software in a product, an acknowledgment *
* (see the following) in the product documentation is *
* required. *
* *
* 2. Altered versions in source or binary form must be *
* plainly marked as such, and must not be misrepresented *
* as being the original software. *
* *
* 3. This notice may not be removed or altered from any *
* source or binary distribution. *
*****************************************************************
*/
#define _CRT_SECURE_NO_WARNINGS
#define DIRECTDRAW_VERSION 0x0600
#define DIRECTINPUT_VERSION 0x0600
#define DIRECTSOUND_VERSION 0x0600
#if _MSC_VER >= 1400
#include <intrin.h>
#endif
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <WinSock2.h>
#include <Windows.h>
#include <ImageHlp.h>
#include <ShlObj.h>
#include <ddraw.h>
#include <dinput.h>
#include <dsound.h>
#include "detours.h"
#pragma comment(lib, "ddraw")
#pragma comment(lib, "ImageHlp")
#pragma comment(lib, "WinMM")
#pragma comment(lib, "WS2_32")
#ifndef _countof
#define _countof(array) (sizeof(array)/sizeof(array[0]))
#endif
#define NAKED __declspec(naked)
#define EXE_CHECKSUM 0x89E91
#define RFL_CHECKSUM 0x1B9740
#define INI_NAME "Arokh.ini"
// not sure what exactly causes this to be needed
// uncomment if it crashes on Windows 9x
//#define WIN9X_HACK
void DebugPrintf(char *fmt, ...)
{
// supposedly max length that can be delivered via OutputDebugString
char string[4092];
va_list ap;
va_start(ap, fmt);
_vsnprintf(string, sizeof(string), fmt, ap);
va_end(ap);
OutputDebugString(string);
}
LONG (WINAPI *O_RegSetValueEx)(HKEY, LPCTSTR, DWORD, DWORD, /*const*/ BYTE *, DWORD);
LONG WINAPI H_RegSetValueEx(HKEY hKey, LPCTSTR lpValueName, DWORD Reserved, DWORD dwType, /*const*/ BYTE *lpData, DWORD cbData)
{
if (!strcmp(lpValueName, "Settings101"))
{
if (*(PDWORD_PTR)0x487A2C)
{
// this updates fullscreen/windowed flag on exit as the game doesn't do it
lpData[32] ^= (-!(*(PBYTE)((*(PDWORD_PTR)0x487A2C) + 0x30) & 2) ^ lpData[32]) & 1;
}
}
return O_RegSetValueEx(hKey, lpValueName, Reserved, dwType, lpData, cbData);
}
BOOL __stdcall DDCheckWindowedCap(GUID FAR *lpGUID)
{
LPDIRECTDRAW lpDD;
DDCAPS DDDriverCaps;
if (!DirectDrawCreate(lpGUID, &lpDD, NULL))
{
DDDriverCaps.dwSize = sizeof(DDCAPS);
IDirectDraw_GetCaps(lpDD, &DDDriverCaps, NULL);
IDirectDraw_Release(lpDD);
return DDDriverCaps.dwCaps2 & DDCAPS2_CANRENDERWINDOWED;
}
return FALSE;
}
typedef struct displaydevice_s
{
GUID *device;
BOOL windowedAllowed;
struct displaydevice_s *next;
} displaydevice_t;
// engine's representation of enumerated display devices
typedef struct riotdisplaydevice_s
{
BOOL primary;
GUID guid;
} riotdisplaydevice_t;
displaydevice_t displayDeviceHead;
displaydevice_t *displayDevicePrev = &displayDeviceHead;
// the original function in Drakan.exe has been extended a little
// the engine assumes windowed mode shouldn't be available for non-primary display devices,
// which is a valid assumption for 3D accelerators of the time (Voodoo cards), but not for modern multi-monitor setups
int (__fastcall *O_InitDisplay)(void *, void *, BOOL, BOOL);
int __fastcall H_InitDisplay(void *This, void *unused, BOOL windowedAllowed, BOOL dedicated)
{
riotdisplaydevice_t *riotDevice;
if (dedicated) SetErrorMode(SEM_NOGPFAULTERRORBOX);
riotDevice = (riotdisplaydevice_t *)0x4835B4;
if (displayDeviceHead.next)
{
displaydevice_t *next;
displaydevice_t *cur = displayDeviceHead.next;
do
{
// loop through enumerated devices and set windowedAllowed if applicable for the device user selected
if (!windowedAllowed && !riotDevice->primary && !memcmp(cur->device, &riotDevice->guid, sizeof(GUID)))
{
windowedAllowed = cur->windowedAllowed;
}
// and free the memory while at it, it won't be needed again
next = cur->next;
free(cur->device);
free(cur);
cur = next;
} while (cur);
displayDeviceHead.next = NULL;
}
else if (!riotDevice->primary)
{
windowedAllowed = DDCheckWindowedCap(&riotDevice->guid);
}
// the result of windowedAllowed variable determines whether fullscreen toggle key (F4) actually budges
// the check to determine whether Windowed mode checkbox in Riot Engine Options should work is somewhere else in Drakan.exe
return O_InitDisplay(This, unused, windowedAllowed, dedicated);
}
BOOL (WINAPI *O_DDEnumCallback)(GUID FAR *, LPSTR, LPSTR, LPVOID);
BOOL WINAPI DDEnumCallback(GUID FAR *lpGUID, LPSTR lpDriverDescription, LPSTR lpDriverName, LPVOID lpContext)
{
// we haven't enumerated primary device yet, where windowed mode is always allowed
if (!displayDeviceHead.windowedAllowed)
{
displayDeviceHead.windowedAllowed = TRUE;
}
else
{
if (displayDevicePrev->next = malloc(sizeof(displaydevice_t)))
{
if (displayDevicePrev->next->device = malloc(sizeof(GUID)))
{
memcpy(displayDevicePrev->next->device, lpGUID, sizeof(GUID));
displayDevicePrev->next->windowedAllowed = DDCheckWindowedCap(lpGUID);
displayDevicePrev->next->next = NULL;
displayDevicePrev = displayDevicePrev->next;
}
else
{
free(displayDevicePrev->next);
displayDevicePrev->next = NULL;
}
}
}
return O_DDEnumCallback(lpGUID, lpDriverDescription, lpDriverName, lpContext);
}
BOOL WINAPI DDEnumCallbackEx(GUID FAR *lpGUID, LPSTR lpDriverDescription, LPSTR lpDriverName, LPVOID lpContext, HMONITOR hm)
{
return DDEnumCallback(lpGUID, lpDriverDescription, lpDriverName, lpContext);
}
HRESULT (WINAPI *PTR_DirectDrawEnumerateEx)(LPDDENUMCALLBACKEX, LPVOID, DWORD);
HRESULT (WINAPI *O_DirectDrawEnumerate)(LPDDENUMCALLBACK, LPVOID);
HRESULT WINAPI H_DirectDrawEnumerate(LPDDENUMCALLBACK lpCallback, LPVOID lpContext)
{
// save the address of engine's callback function so we can call it after saving needed information about each display device
O_DDEnumCallback = lpCallback;
// DirectDrawEnumerateEx doesn't exist on Windows NT 4.0
if (PTR_DirectDrawEnumerateEx) return PTR_DirectDrawEnumerateEx(DDEnumCallbackEx, lpContext, DDENUM_ATTACHEDSECONDARYDEVICES | DDENUM_DETACHEDSECONDARYDEVICES | DDENUM_NONDISPLAYDEVICES);
return O_DirectDrawEnumerate(DDEnumCallback, lpContext);
}
typedef struct displaymode_s
{
DWORD width;
DWORD height;
DWORD bpp;
} displaymode_t;
// this array is used for display modes in windowed mode
displaymode_t *displayModes = (displaymode_t *)0x48C000;
size_t index;
HRESULT WINAPI EnumModesCallback(LPDDSURFACEDESC lpDDSurfaceDesc, LPVOID lpContext)
{
if (index >= 128) return DDENUMRET_CANCEL;
// we're only interested in modes matching our desktop bit depth
if (lpDDSurfaceDesc->ddpfPixelFormat.dwRGBBitCount == ((LPDDSURFACEDESC)lpContext)->ddpfPixelFormat.dwRGBBitCount)
{
displayModes[index].width = lpDDSurfaceDesc->dwWidth;
displayModes[index].height = lpDDSurfaceDesc->dwHeight;
displayModes[index].bpp = lpDDSurfaceDesc->ddpfPixelFormat.dwRGBBitCount;
index++;
}
return DDENUMRET_OK;
}
int CompareDisplayModes(const void *p, const void *q)
{
int pp = ((displaymode_t *)p)->width * ((displaymode_t *)p)->height;
int qq = ((displaymode_t *)q)->width * ((displaymode_t *)q)->height;
return (pp - qq);
}
// because Win95 doesn't have this :P
HMONITOR (WINAPI *PTR_MonitorFromWindow)(HWND, DWORD);
BOOL (WINAPI *PTR_GetMonitorInfo)(HMONITOR, LPMONITORINFO);
/*
*****************************************************************
* Borderless windowed mode magic *
* *
* This mostly relies on tracking window state using vars below *
* and the sequence of calls to those APIs made by the game *
* and adjusting the window properties accordingly. Forgot *
* exact meaning of those flags, (windowFlags & 4) means just *
* started the game. *
* *
* It's a little more complicated than it would need to be *
* because if we show the window without borders for the first *
* time, putting the borders back at later point results in the *
* missing icon. *
*****************************************************************
*/
DWORD windowFlags = 4;
LONG currentWidth;
LONG currentHeight;
LONG width;
LONG height;
// to be saved to Arokh.ini, first needed here
char BorderlessWindowHooks[] = "0";
char BorderlessTopmost[] = "0";
BOOL (WINAPI *O_AdjustWindowRectEx)(LPRECT, DWORD, BOOL, DWORD);
BOOL WINAPI H_AdjustWindowRectEx(LPRECT lpRect, DWORD dwStyle, BOOL bMenu, DWORD dwExStyle)
{
HWND hWnd;
// dedicated server is running
if (*(PDWORD)0x487E18)
{
return O_AdjustWindowRectEx(lpRect, dwStyle, bMenu, dwExStyle);
}
// we need the window handle to work with
__asm
{
mov eax, dword ptr ss:[ebp + 4h]
mov dword ptr ss:[hWnd], eax
}
// if we're in fullscreeen mode
if (*(PBYTE)((*(PDWORD_PTR)0x487A2C) + 0x30) & 2)
{
if (dwStyle & WS_POPUP)
{
dwStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE;
SetWindowLongPtr(hWnd, GWL_STYLE, dwStyle);
windowFlags = 1;
}
return O_AdjustWindowRectEx(lpRect, dwStyle, bMenu, dwExStyle);
}
// figure out dimensions of the monitor on which our window resides
if (PTR_MonitorFromWindow)
{
HMONITOR hMonitor;
MONITORINFO hInfo;
hMonitor = PTR_MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
hInfo.cbSize = sizeof(MONITORINFO);
PTR_GetMonitorInfo(hMonitor, &hInfo);
currentWidth = hInfo.rcMonitor.right - hInfo.rcMonitor.left;
currentHeight = hInfo.rcMonitor.bottom - hInfo.rcMonitor.top;
}
else
{
currentWidth = GetSystemMetrics(SM_CXSCREEN);
currentHeight = GetSystemMetrics(SM_CYSCREEN);
}
// get dimensions of window client area (game resolution)
width = lpRect->right;
height = lpRect->bottom;
// decide whether we need to change the borders
if (width >= currentWidth && height >= currentHeight)
{
if (!(windowFlags & 4))
{
if (dwStyle & WS_CAPTION)
{
dwStyle = WS_POPUP | WS_VISIBLE;
SetWindowLongPtr(hWnd, GWL_STYLE, dwStyle);
windowFlags = 3;
}
}
else
{
dwStyle = WS_POPUP | WS_VISIBLE;
windowFlags |= 2;
}
}
else
{
if (dwStyle & WS_POPUP)
{
dwStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE;
SetWindowLongPtr(hWnd, GWL_STYLE, dwStyle);
windowFlags = 1;
}
}
return O_AdjustWindowRectEx(lpRect, dwStyle, bMenu, dwExStyle);
}
BOOL (WINAPI *O_SetWindowPos)(HWND, HWND, int, int, int, int, UINT);
BOOL WINAPI H_SetWindowPos(HWND hWnd, HWND hWndInsertAfter, int X, int Y, int cx, int cy, UINT uFlags)
{
// dedicated server is running
if (*(PDWORD)0x487E18)
{
return O_SetWindowPos(hWnd, hWndInsertAfter, X, Y, cx, cy, uFlags);
}
if (uFlags == (SWP_NOMOVE | SWP_NOZORDER) && *(PBYTE)((*(PDWORD_PTR)0x487A2C) + 0x30) & 2)
{
// ignore this call in fullscreen or it may mess up other windows' sizes and positions
return FALSE;
}
else if (*BorderlessWindowHooks != '0')
{
if (uFlags & SWP_NOMOVE)
{
if (windowFlags & 1)
{
uFlags |= SWP_FRAMECHANGED;
if (windowFlags & 2)
{
uFlags &= ~SWP_NOMOVE;
if (PTR_MonitorFromWindow)
{
HMONITOR hMonitor;
MONITORINFO hInfo;
hMonitor = PTR_MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
hInfo.cbSize = sizeof(MONITORINFO);
PTR_GetMonitorInfo(hMonitor, &hInfo);
X = hInfo.rcMonitor.left;
Y = hInfo.rcMonitor.top;
}
else
{
X = Y = 0;
}
if (*BorderlessTopmost != '0')
{
hWndInsertAfter = HWND_TOPMOST;
uFlags &= ~SWP_NOZORDER;
}
}
else if (*BorderlessTopmost != '0')
{
hWndInsertAfter = HWND_NOTOPMOST;
uFlags &= ~SWP_NOZORDER;
}
windowFlags &= ~1;
}
}
else if (windowFlags & 2)
{
if (PTR_MonitorFromWindow)
{
HMONITOR hMonitor;
MONITORINFO hInfo;
hMonitor = PTR_MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
hInfo.cbSize = sizeof(MONITORINFO);
PTR_GetMonitorInfo(hMonitor, &hInfo);
X = hInfo.rcMonitor.left;
Y = hInfo.rcMonitor.top;
}
else
{
X = Y = 0;
}
if (*BorderlessTopmost != '0')
{
hWndInsertAfter = HWND_TOPMOST;
}
}
}
return O_SetWindowPos(hWnd, hWndInsertAfter, X, Y, cx, cy, uFlags);
}
BOOL (WINAPI *O_ShowWindow)(HWND, int);
BOOL WINAPI H_ShowWindow(HWND hWnd, int nCmdShow)
{
BOOL ret = O_ShowWindow(hWnd, nCmdShow);
// no dedicated server, please
if (!(*(PDWORD)0x487E18) && (windowFlags & 4))
{
if (currentWidth && width >= currentWidth && height >= currentHeight)
{
HWND hWndInsertAfter = *BorderlessTopmost != '0' ? HWND_TOPMOST : hWnd;
SetWindowLongPtr(hWnd, GWL_STYLE, WS_POPUP | WS_VISIBLE);
O_SetWindowPos(hWnd, hWndInsertAfter, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOSIZE);
}
windowFlags &= ~4;
}
return ret;
}
// just minimizes the borderless window at user discretion
LRESULT (CALLBACK *O_WindowProc)(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK H_WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_ACTIVATE && !LOWORD(wParam))
{
if (!(*(PDWORD_PTR)0x487A2C && *(PBYTE)((*(PDWORD_PTR)0x487A2C) + 0x30) & 2))
{
if (windowFlags & 2)
{
O_ShowWindow(hWnd, SW_MINIMIZE);
}
}
}
return O_WindowProc(hWnd, uMsg, wParam, lParam);
}
// allow resizing of dedicated server window if desired, not that nice without adjusting the actual output resolution
HWND (WINAPI *O_CreateWindowEx)(DWORD, LPCTSTR, LPCTSTR, DWORD, int, int, int, int, HWND, HMENU, HINSTANCE, LPVOID);
HWND WINAPI H_CreateWindowEx(DWORD dwExStyle, LPCTSTR lpClassName, LPCTSTR lpWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam)
{
if (*(PDWORD)0x487E18 && lpWindowName && !strcmp(lpWindowName, "Riot Engine"))
{
dwStyle |= WS_THICKFRAME | WS_MAXIMIZEBOX;
}
return O_CreateWindowEx(dwExStyle, lpClassName, lpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);
}
/*
*****************************************************
* Simple solution to give us working server browser *
* *
* Original server browser code uses HTTP protocol *
*****************************************************
*/
char serverListURL[128];
char *pathToServersTXT;
// master server address and location of server list are separate arguments,
// splitting code is in DllMain
void (__fastcall *O_SetMasterAddr)(void *, void *, char *, char *);
void __fastcall H_SetMasterAddr(void *This, void *unused, char *oMasterServerAddr, char *oPathToServersTXT)
{
O_SetMasterAddr(This, unused, serverListURL, pathToServersTXT);
}
char gameServerAddr[32];
void (*O_FixServerAddr)(void);
void H_FixServerAddr(void)
{
char *recvServerAddr;
char *recvServerPort;
// get the game server address from master
__asm mov recvServerAddr, edx
// separate IP from port
recvServerPort = strchr(recvServerAddr, ':');
*recvServerPort++ = '\0';
// feed the address in format the game likes
// I don't know the purpose of middle integer
// it's not used by the game
sprintf(gameServerAddr, "%s 0 %s", recvServerAddr, recvServerPort);
__asm mov edx, offset gameServerAddr
O_FixServerAddr();
}
/*
**********************************************************
* NEW server browser backend code using GameSpy protocol *
**********************************************************
*/
// Some good stuff borrowed from Luigi Auriemma's gslist utility
#include "gsmsalg.h"
#define BUFFERSIZE 8192
#define GSQUERY "\\gamename\\drakan" \
"\\enctype\\0" \
"\\validate\\%s" \
"\\final\\" \
"\\list\\cmp" \
"\\gamename\\drakan"
/*
finds the value of key in the data buffer and return a new
string containing the value or NULL if nothing has been found
no modifications are made on the input data
*/
char * __stdcall keyval(char *data, char *key)
{
size_t nt = 0,
skip = 1;
for (;;)
{
char *p = strchr(data, '\\');
if (nt & 1)
{
if (p && !_strnicmp(data, key, p - data))
{
skip = 0;
}
}
else
{
if (!skip)
{
char *val;
size_t len;
if (!p) p = data + strlen(data);
len = p - data;
val = malloc(len + 1);
if (val)
{
memcpy(val, data, len);
val[len] = '\0';
}
// unlikely
else val = (char *)-1;
return val;
}
}
if (!p) break;
nt++;
data = p + 1;
}
return NULL;
}
// pre-defined 2nd argument values for PrintBrowserStatus function
const BYTE browserStatusNormal[] = { 0xE0, 0xFF, 0xFF, 0xFF, 0xE0, 0xFF, 0xFF, 0xFF, 0x20, 0xB0, 0xFF, 0xFF, 0x20, 0xB0, 0xFF, 0xFF };
const BYTE browserStatusOrange[] = { 0x20, 0xFF, 0xFF, 0xFF, 0x20, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF };
const BYTE browserStatusGreen[] = { 0x00, 0xFF, 0xD0, 0xFF, 0x00, 0xFF, 0xD0, 0xFF, 0x00, 0x80, 0x40, 0xFF, 0x00, 0x80, 0x40, 0xFF };
HMODULE hDragon;
void (__fastcall *RFL_PrintBrowserStatus)(void *, void *, char *, const BYTE *);
// displays specified text in a little black rectangle in the Join Game menu
void __stdcall PrintBrowserStatus(char *text, const BYTE *color)
{
void *This = (void *)(*(PDWORD_PTR)((DWORD_PTR)hDragon + 0x1855D8));
if (This)
{
RFL_PrintBrowserStatus(This, NULL, text, color);
}
}
int (__fastcall *O_InitConnect)(void *, void *, char *, u_long, DWORD);
int __fastcall H_InitConnect(void *This, void *unused, char *addr, u_long port, DWORD timeout)
{
int ret;
if (!(ret = O_InitConnect(This, unused, addr, port, timeout)))
{
PrintBrowserStatus("Unable To Resolve Hostname", browserStatusOrange);
}
return ret;
}
u_long tcpport;
void (__fastcall *O_Connect)(void *, void *, u_long, char *, int, int);
void __fastcall H_Connect(void *This, void *unused, u_long port, char *addr, DWORD timeout, int unkwn)
{
O_Connect(This, unused, tcpport, addr, timeout, unkwn);
}
#define SB_BASIC_SECURE_DONE (1 << 0)
#define SB_RECV_SHOWN (1 << 1)
char *recvBuf;
size_t recvLen;
size_t dynLen;
DWORD sbFlags;
int (__fastcall *O_ConnectCallback)(void *, void *, DWORD);
int __fastcall H_ConnectCallback(void *This, void *unused, DWORD err)
{
if (!err)
{
sbFlags = 0;
recvLen = 0;
dynLen = BUFFERSIZE;
recvBuf = malloc(dynLen + 1);
PrintBrowserStatus("Connection Established", browserStatusGreen);
}
else
{
char *errorStr;
switch (err)
{
case WSAECONNREFUSED:
errorStr = "Connection Refused";
break;
case WSAETIMEDOUT:
errorStr = "Connection Timed Out";
break;
case WSAENETUNREACH:
errorStr = "Unreachable Network";
break;
default:
errorStr = "Connection Attempt Failed With Error";
}
PrintBrowserStatus(errorStr, browserStatusOrange);
}
return 1;
}
#pragma pack(push, 1)
typedef struct ipport_s
{
u_long ip;
u_short port;
} ipport_t;
#pragma pack(pop)
int (__stdcall *O_ReceiveCallback)(DWORD, u_long, void *);
int __stdcall H_ReceiveCallback(DWORD err, u_long size, void *ptr)
{
SOCKET sock;
if (err)
{
return O_ReceiveCallback(err, size, ptr);
}
sock = *(SOCKET *)(&ptr + 4);
if (recvBuf)
{
if (recvLen + size > dynLen)
{
char *buf;
do dynLen += BUFFERSIZE; while (recvLen + size > dynLen);
buf = realloc(recvBuf, dynLen + 1);
if (buf) recvBuf = buf;
else
{
shutdown(sock, SD_BOTH);
DebugPrintf("ReceiveCallback() (2)::Out of memory, unable to reallocate %u bytes, shutting down socket %d\n", dynLen, sock);
return 1;
}
}
if (recv(sock, recvBuf + recvLen, size, 0) == SOCKET_ERROR)
{
return O_ReceiveCallback(err = WSAGetLastError(), size, ptr);
}
recvLen += size;
recvBuf[recvLen] = '\0';
if (!(sbFlags & SB_BASIC_SECURE_DONE))
{
char *validate;
char *secure;
int sendlen;
validate = &recvBuf[dynLen / 2];
secure = keyval(recvBuf, "secure");
if (secure)
{
if (secure != (char *)-1)
{
gsseckey(validate, secure, "zCt4De", 0);
free(secure);
PrintBrowserStatus("Authenticating...", browserStatusOrange);
}
else
{
shutdown(sock, SD_BOTH);
DebugPrintf("ReceiveCallback() (2)::Out of memory, NULL secure, shutting down socket %d\n", sock);
return 1;
}
}
else
{
*validate = '\0';
DebugPrintf("ReceiveCallback() (2)::Received reply from master server: %s\nSending query with empty validate field...\n", recvBuf);
}
sendlen = sprintf(recvBuf, GSQUERY, validate);
send(sock, recvBuf, sendlen, 0);
recvLen = 0;
sbFlags |= SB_BASIC_SECURE_DONE;
}
else if (!(sbFlags & SB_RECV_SHOWN))
{
sbFlags |= SB_RECV_SHOWN;
PrintBrowserStatus("Receiving...", browserStatusGreen);
}
}
else
{
shutdown(sock, SD_BOTH);
DebugPrintf("ReceiveCallback() (2)::Out of memory, NULL recvBuf, shutting down socket %d\n", sock);
}
return 1;
}
void (__fastcall *O_Close)(void *, void *);
void __fastcall H_Close(void *This, void *unused)
{
if (recvBuf)
{
free(recvBuf);
recvBuf = NULL;
}
O_Close(This, unused);
}
int (__fastcall *O_CloseCallback)(void *, void *, DWORD);
int __fastcall H_CloseCallback(void *This, void *unused, DWORD err)
{
if (!err)
{
if (recvLen >= 7 && !strcmp(recvBuf + recvLen - 7, "\\final\\"))
{
recvLen -= 7;
if (recvLen)
{
ipport_t *ipport;
for (ipport = (ipport_t *)recvBuf; recvLen >= 6; ipport++, recvLen -= 6)
{
char *ip = inet_ntoa(*(struct in_addr *)&ipport->ip);
u_long port = ntohs(ipport->port);
__asm
{
mov eax, dword ptr ds:[487bf4h]
push 2
mov ecx, dword ptr ds:[eax + 5eh]
push port
mov edx, dword ptr ds:[ecx]
push ip
call dword ptr ds:[edx + 4h]
}
}
PrintBrowserStatus("Querying Servers...", browserStatusGreen);
}
else PrintBrowserStatus("No Servers Listed", browserStatusOrange);
}
else PrintBrowserStatus("Unexpected Response", browserStatusOrange);
}
else
{
char *errorStr;
switch (err)
{
case WSAECONNRESET:
errorStr = "Connection Reset";
break;
case WSAECONNABORTED:
errorStr = "Connection Aborted";
break;
default:
errorStr = "Connection Closed With Error";
}
PrintBrowserStatus(errorStr, browserStatusOrange);
}
return O_CloseCallback(This, unused, err);
}
/*
***********************************************************************
* Texel alignment *
* *
* Adjusts coordinates for text rendering a little to fix distorted *
* text when multisample anti-aliasing is used. Effective when *
* Fix Texture Coordinates checkbox is checked in Riot Engine Options. *
***********************************************************************
*/
DWORD_PTR retAddr = 0x437BA6;
void (*O_TexelAlignment)(void);
NAKED void H_TexelAlignment(void)
{
__asm
{
// hack to prevent the map misalignment
fld dword ptr ss:[esp + 158h]
fcomp dword ptr ds:[4793bch]
fstsw ax
test ah, 41h
jz end
mov eax, dword ptr ds:[ebx + 18h]
mov dword ptr ss:[esp + 38h], 0h
mov dword ptr ss:[esp + 34h], eax
lea eax, [esp + 50h]
fild qword ptr ss:[esp + 34h]
fdivr dword ptr ds:[47951ch]
fld dword ptr ds:[47951ch]
mov ecx, 4h
loopy:
// D3DVERTEX.x -= 0.5f;
fld dword ptr ds:[eax]
fsub st,st(1)
fstp dword ptr ds:[eax]
// D3DVERTEX.y -= 0.5f;
fld dword ptr ds:[eax + 4h]
fsub st,st(1)
fstp dword ptr ds:[eax + 4h]
// ???
// D3DVERTEX.tu -= 0.5f / [esp + 34h];
fld dword ptr ds:[eax + 18h]
fsub st,st(2)
fstp dword ptr ds:[eax + 18h]
add eax, 38h
dec ecx
jnz loopy
fstp st
fstp st
end:
jmp dword ptr ds:[retAddr]
}
}
/*
**********************************************************************
* Calls DirectDraw SetDisplayMode method with specified refresh rate *
**********************************************************************
*/
DWORD refreshRate;
void (*O_SetDisplayMode)(void);
NAKED void H_SetDisplayMode(void)
{
__asm
{
push ecx
push esi
push dword ptr ds:[refreshRate]
push dword ptr ss:[esp + 18h]
push dword ptr ss:[esp + 18h]
push dword ptr ss:[esp + 18h]
call dword ptr ds:[O_SetDisplayMode]
pop ecx
test eax,eax
jz end
// failed, let system pick whatever works
push esi
push esi
push dword ptr ss:[esp + 14h]
push dword ptr ss:[esp + 14h]
push dword ptr ss:[esp + 14h]
call dword ptr ds:[O_SetDisplayMode]
end:
retn 14h
}
}
/*
**********
* Timing *
**********
*/
BOOL (WINAPI *O_QueryPerformanceFrequency)(LARGE_INTEGER *);
BOOL WINAPI H_QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency)
{
return FALSE;
}
DWORD (WINAPI *kernel32_GetTickCount)(void);
DWORD WINAPI WinMM_timeGetTime(void)
{
return timeGetTime();
}
/*
****************************************************************
* Accurate frame limiter *
* *
* Adapted from http://www.geisswerks.com/ryan/FAQS/timing.html *
* copyright (c)2002+ Ryan M. Geiss *
****************************************************************
*/
LARGE_INTEGER frequency;
double ticks_to_wait;
BOOL useQPC;
int (__fastcall *O_GameFrame)(void *, void *);
int __fastcall H_GameFrame(void *This, void *unused)
{
static LARGE_INTEGER prev_end_of_frame;
LARGE_INTEGER t;
int ret = O_GameFrame(This, unused);
// already limited elsewhere; server running or no window focus
// ideally, we should also detect if we're called to update loading bar
if (*(PDWORD)0x487E18 || !*(PDWORD)0x4841F0) goto end;
for (;;)
{
LARGE_INTEGER ticks_passed;
double ticks_left;
if (useQPC)
QueryPerformanceCounter(&t);
else
t.LowPart = timeGetTime();
// time wrap
if (t.QuadPart - prev_end_of_frame.QuadPart < 0)
break;
ticks_passed.QuadPart = t.QuadPart - prev_end_of_frame.QuadPart;
if (ticks_passed.QuadPart >= ticks_to_wait)
break;
ticks_left = ticks_to_wait - ticks_passed.QuadPart;
// If > 0.002s left, do Sleep(1), which will actually sleep some
// steady amount, probably 1-2 ms,
// and do so in a nice way (CPU meter drops; laptop battery spared).
if (ticks_left > frequency.QuadPart * 2 / 1000)
Sleep(1);
}
prev_end_of_frame.QuadPart = t.QuadPart;
end:
return ret;
}
/*
****************************************************