Skip to content

Commit aa64592

Browse files
committed
Improve and document CMultiplayerSA_Direct3D.cpp
1 parent 3304f26 commit aa64592

File tree

1 file changed

+52
-35
lines changed

1 file changed

+52
-35
lines changed

Client/multiplayer_sa/CMultiplayerSA_Direct3D.cpp

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,25 @@ BYTE RESTORE_Bytes_PreCreateDevice[6];
3636
void _cdecl OnPreCreateDevice(IDirect3D9* pDirect3D, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow, DWORD* BehaviorFlags,
3737
D3DPRESENT_PARAMETERS* pPresentationParameters, IDirect3DDevice9** ppReturnedDeviceInterface)
3838
{
39-
// Unpatch
40-
MemCpy((PVOID)RESTORE_Addr_PreCreateDevice, RESTORE_Bytes_PreCreateDevice, RESTORE_Size_PreCreateDevice);
41-
42-
// g_pCore->OnPreCreateDevice( pDirect3D, Adapter, DeviceType, hFocusWindow, *BehaviorFlags, pPresentationParameters );
43-
ms_pDirect3D = pDirect3D;
44-
ms_Adapter = Adapter;
45-
ms_DeviceType = DeviceType;
46-
ms_hFocusWindow = hFocusWindow;
47-
ms_BehaviorFlags = *BehaviorFlags;
48-
ms_pPresentationParameters = pPresentationParameters;
49-
ms_ppReturnedDeviceInterface = ppReturnedDeviceInterface;
39+
// Safely unpatch with validation
40+
if (RESTORE_Addr_PreCreateDevice &&
41+
RESTORE_Size_PreCreateDevice > 0 &&
42+
RESTORE_Size_PreCreateDevice <= sizeof(RESTORE_Bytes_PreCreateDevice))
43+
{
44+
MemCpy((PVOID)RESTORE_Addr_PreCreateDevice, RESTORE_Bytes_PreCreateDevice, RESTORE_Size_PreCreateDevice);
45+
}
46+
47+
// Validate critical parameters before dereferencing
48+
if (BehaviorFlags && pPresentationParameters)
49+
{
50+
ms_pDirect3D = pDirect3D;
51+
ms_Adapter = Adapter;
52+
ms_DeviceType = DeviceType;
53+
ms_hFocusWindow = hFocusWindow;
54+
ms_BehaviorFlags = *BehaviorFlags;
55+
ms_pPresentationParameters = pPresentationParameters;
56+
ms_ppReturnedDeviceInterface = ppReturnedDeviceInterface;
57+
}
5058
}
5159

5260
// Hook info
@@ -59,30 +67,35 @@ static void __declspec(naked) HOOK_PreCreateDevice()
5967

6068
__asm
6169
{
62-
// Run replaced code
63-
mov ecx,dword ptr ds:[0C97C20h]
64-
push 0C97C28h
65-
push 0C9C040h
66-
push eax
67-
mov eax,dword ptr ds:[00C97C1Ch]
70+
// Run replaced code - these pushes create the original function parameters
71+
mov ecx,dword ptr ds:[0C97C20h] // pDirect3D
72+
push 0C97C28h // ppReturnedDeviceInterface
73+
push 0C9C040h // pPresentationParameters
74+
push eax // BehaviorFlags (original eax)
75+
mov eax,dword ptr ds:[00C97C1Ch]
6876
mov edx, [ecx]
69-
push eax
77+
push eax // hFocusWindow
7078
mov eax,dword ptr ds:[008E2428h]
71-
push eax
72-
73-
mov eax, ds:0x0C97C24 // __RwD3DAdapterIndex
74-
push eax
75-
push ecx
76-
77-
pushad
78-
push [esp+32+4*6]
79-
push [esp+32+4*6]
80-
lea eax,[esp+32+4*6] // Turn BehaviorFlags into a pointer so we can modify it
81-
push eax
82-
push [esp+32+4*6]
83-
push [esp+32+4*6]
84-
push [esp+32+4*6]
85-
push [esp+32+4*6]
79+
push eax // DeviceType
80+
81+
mov eax, ds:0x0C97C24 // __RwD3DAdapterIndex
82+
push eax // Adapter
83+
push ecx // pDirect3D
84+
85+
// Now we have 7 parameters on stack (28 bytes)
86+
// Stack layout: [pDirect3D][Adapter][DeviceType][hFocusWindow][BehaviorFlags][pPresentationParameters][ppReturnedDeviceInterface]
87+
88+
pushad // Save all registers (32 bytes)
89+
90+
// Pass parameters to OnPreCreateDevice - stack offset is now 32 (pushad) + 28 (pushes) = 60
91+
push [esp+60+24] // ppReturnedDeviceInterface
92+
push [esp+60+20] // pPresentationParameters
93+
lea eax,[esp+60+16] // BehaviorFlags as pointer
94+
push eax
95+
push [esp+60+12] // hFocusWindow
96+
push [esp+60+8] // DeviceType
97+
push [esp+60+4] // Adapter
98+
push [esp+60+0] // pDirect3D
8699
call OnPreCreateDevice
87100
add esp, 4*7
88101
popad
@@ -101,8 +114,12 @@ static void __declspec(naked) HOOK_PreCreateDevice()
101114
////////////////////////////////////////////////////////////////
102115
HRESULT _cdecl OnPostCreateDevice(HRESULT hResult)
103116
{
104-
return g_pCore->OnPostCreateDevice(hResult, ms_pDirect3D, ms_Adapter, ms_DeviceType, ms_hFocusWindow, ms_BehaviorFlags, ms_pPresentationParameters,
105-
ms_ppReturnedDeviceInterface);
117+
if (g_pCore)
118+
{
119+
return g_pCore->OnPostCreateDevice(hResult, ms_pDirect3D, ms_Adapter, ms_DeviceType, ms_hFocusWindow, ms_BehaviorFlags, ms_pPresentationParameters,
120+
ms_ppReturnedDeviceInterface);
121+
}
122+
return hResult;
106123
}
107124

108125
// Hook info

0 commit comments

Comments
 (0)