Skip to content

Commit

Permalink
D3D9: Fix compile errors
Browse files Browse the repository at this point in the history
  • Loading branch information
MeFisto94 committed Nov 26, 2023
1 parent c1baf96 commit d637a58
Showing 1 changed file with 76 additions and 66 deletions.
142 changes: 76 additions & 66 deletions D3D9/D3D9HookManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,81 +79,91 @@ public void Load()
logger.Debug("D3D9Hook: Before device creation");
using (Direct3D d3d = new Direct3D())
{
using (var device = new Device(d3d, 0, DeviceType.NullReference, IntPtr.Zero, CreateFlags.HardwareVertexProcessing,
new PresentParameters { BackBufferWidth = 1, BackBufferHeight = 1, DeviceWindowHandle = WindowHandle }))
using (var device = new Device(d3d, 0, DeviceType.NullReference, IntPtr.Zero,
CreateFlags.HardwareVertexProcessing,
new PresentParameters
{ BackBufferWidth = 1, BackBufferHeight = 1, DeviceWindowHandle = WindowHandle }))
{
logger.Debug("D3D9Hook: Device created");
Id3dDeviceFunctionAddresses.AddRange(Functions.GetVTblAddresses(device.NativePointer, Functions.D3D9_DEVICE_METHOD_COUNT));
Id3dDeviceFunctionAddresses.AddRange(Functions.GetVTblAddresses(device.NativePointer,
Functions.D3D9_DEVICE_METHOD_COUNT));

try
{
using (var d3dEx = new Direct3DEx())
{
logger.Debug("D3D9Hook: Direct3DEx...");

using (var deviceEx = new DeviceEx(d3dEx, 0, DeviceType.NullReference, IntPtr.Zero, CreateFlags.HardwareVertexProcessing,
new PresentParameters { BackBufferWidth = 1, BackBufferHeight = 1, DeviceWindowHandle = WindowHandle },
new DisplayModeEx { Width = 800, Height = 600 }))
try
{
logger.Debug("D3D9Hook: DeviceEx created - PresentEx supported");
Id3dDeviceFunctionAddresses.AddRange(Functions.GetVTblAddresses(deviceEx.NativePointer,
Functions.D3D9_DEVICE_METHOD_COUNT, Functions.D3D9Ex_DEVICE_METHOD_COUNT));
_supportsDirect3D9Ex = true;
using (var d3dEx = new Direct3DEx())
{
logger.Debug("D3D9Hook: Direct3DEx...");

using (var deviceEx = new DeviceEx(d3dEx, 0, DeviceType.NullReference, IntPtr.Zero,
CreateFlags.HardwareVertexProcessing,
new PresentParameters
{
BackBufferWidth = 1, BackBufferHeight = 1, DeviceWindowHandle = WindowHandle
},
new DisplayModeEx { Width = 800, Height = 600 }))
{
logger.Debug("D3D9Hook: DeviceEx created - PresentEx supported");
Id3dDeviceFunctionAddresses.AddRange(Functions.GetVTblAddresses(deviceEx.NativePointer,
Functions.D3D9_DEVICE_METHOD_COUNT, Functions.D3D9Ex_DEVICE_METHOD_COUNT));
_supportsDirect3D9Ex = true;
}
}
}
catch (Exception)
{
_supportsDirect3D9Ex = false;
}
}
}
catch (Exception)
{
_supportsDirect3D9Ex = false;
}

Direct3DDevice_BeginSceneHook = new Hook<Direct3D9Device_BeginSceneDelegate>(
Id3dDeviceFunctionAddresses[(int)Direct3DDevice9FunctionOrdinals.BeginScene],
BeginSceneHook, this);

// We want to hook each method of the IDirect3DDevice9 interface that we are interested in
// 42 - EndScene (we will retrieve the back buffer here)
Direct3DDevice_EndSceneHook = new Hook<Direct3D9Device_EndSceneDelegate>(
Id3dDeviceFunctionAddresses[(int)Direct3DDevice9FunctionOrdinals.EndScene],
// On Windows 7 64-bit w/ 32-bit app and d3d9 dll version 6.1.7600.16385, the address is equiv to:
// (IntPtr)(GetModuleHandle("d3d9").ToInt32() + 0x1ce09),
// A 64-bit app would use 0xff18
// Note: GetD3D9DeviceFunctionAddress will output these addresses to a log file
EndSceneHook, this);

unsafe
{
// If Direct3D9Ex is available - hook the PresentEx
if (_supportsDirect3D9Ex)
{
Direct3DDeviceEx_PresentExHook = new Hook<Direct3D9DeviceEx_PresentExDelegate>(
Id3dDeviceFunctionAddresses[(int)Direct3DDevice9ExFunctionOrdinals.PresentEx],
PresentExHook, this);
}
Direct3DDevice_BeginSceneHook = new Hook<Direct3D9Device_BeginSceneDelegate>(
Id3dDeviceFunctionAddresses[(int)Direct3DDevice9FunctionOrdinals.BeginScene],
BeginSceneHook, this);

// We want to hook each method of the IDirect3DDevice9 interface that we are interested in
// 42 - EndScene (we will retrieve the back buffer here)
Direct3DDevice_EndSceneHook = new Hook<Direct3D9Device_EndSceneDelegate>(
Id3dDeviceFunctionAddresses[(int)Direct3DDevice9FunctionOrdinals.EndScene],
// On Windows 7 64-bit w/ 32-bit app and d3d9 dll version 6.1.7600.16385, the address is equiv to:
// (IntPtr)(GetModuleHandle("d3d9").ToInt32() + 0x1ce09),
// A 64-bit app would use 0xff18
// Note: GetD3D9DeviceFunctionAddress will output these addresses to a log file
EndSceneHook, this);

unsafe
{
// If Direct3D9Ex is available - hook the PresentEx
if (_supportsDirect3D9Ex)
{
Direct3DDeviceEx_PresentExHook = new Hook<Direct3D9DeviceEx_PresentExDelegate>(
Id3dDeviceFunctionAddresses[(int)Direct3DDevice9ExFunctionOrdinals.PresentEx],
PresentExHook, this);
}

// Always hook Present also (device will only call Present or PresentEx not both)
Direct3DDevice_PresentHook = new Hook<Direct3D9Device_PresentDelegate>(
Id3dDeviceFunctionAddresses[(int)Direct3DDevice9FunctionOrdinals.Present],
PresentHook, this);
}

// Always hook Present also (device will only call Present or PresentEx not both)
Direct3DDevice_PresentHook = new Hook<Direct3D9Device_PresentDelegate>(
Id3dDeviceFunctionAddresses[(int)Direct3DDevice9FunctionOrdinals.Present],
PresentHook, this);
}
// 16 - Reset (called on resolution change or windowed/fullscreen change - we will reset some things as well)
Direct3DDevice_ResetHook = new Hook<Direct3D9Device_ResetDelegate>(
Id3dDeviceFunctionAddresses[(int)Direct3DDevice9FunctionOrdinals.Reset],
// On Windows 7 64-bit w/ 32-bit app and d3d9 dll version 6.1.7600.16385, the address is equiv to:
//(IntPtr)(GetModuleHandle("d3d9").ToInt32() + 0x58dda),
// A 64-bit app would use 0x3b3a0
// Note: GetD3D9DeviceFunctionAddress will output these addresses to a log file
ResetHook, this);

Hooks.Add(Direct3DDevice_BeginSceneHook);
Hooks.Add(Direct3DDevice_EndSceneHook);
Hooks.Add(Direct3DDevice_PresentHook);
if (_supportsDirect3D9Ex)
{
Hooks.Add(Direct3DDeviceEx_PresentExHook);
}

// 16 - Reset (called on resolution change or windowed/fullscreen change - we will reset some things as well)
Direct3DDevice_ResetHook = new Hook<Direct3D9Device_ResetDelegate>(
Id3dDeviceFunctionAddresses[(int)Direct3DDevice9FunctionOrdinals.Reset],
// On Windows 7 64-bit w/ 32-bit app and d3d9 dll version 6.1.7600.16385, the address is equiv to:
//(IntPtr)(GetModuleHandle("d3d9").ToInt32() + 0x58dda),
// A 64-bit app would use 0x3b3a0
// Note: GetD3D9DeviceFunctionAddress will output these addresses to a log file
ResetHook, this);

Hooks.Add(Direct3DDevice_BeginSceneHook);
Hooks.Add(Direct3DDevice_EndSceneHook);
Hooks.Add(Direct3DDevice_PresentHook);
if (_supportsDirect3D9Ex)
{
Hooks.Add(Direct3DDeviceEx_PresentExHook);
Hooks.Add(Direct3DDevice_ResetHook);
}
}
Hooks.Add(Direct3DDevice_ResetHook);
}

public void Unload()
Expand Down

0 comments on commit d637a58

Please sign in to comment.