Advertisement
Guest User

Viano

a guest
Sep 4th, 2009
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.57 KB | None | 0 0
  1. using System;
  2. using System.Runtime.InteropServices;
  3.  
  4. using VolvicDLL;
  5.  
  6. namespace MyDirect3D9
  7. {
  8.     public unsafe class IDirect3D9 : IDisposable
  9.     {
  10.         public DelegateCreateDevice RealCreateDevice;
  11.  
  12.         // A pointer to the native IDirect3D9 object that we are providing overrides for.
  13.         public Win32.D3D9.IDirect3D9* NativeIDirect3D9
  14.         {
  15.             get;
  16.             private set;
  17.         }
  18.         // A pointer to the original array of virtual functions.  We keep this around so we can call the originals.
  19.         private IntPtr* OriginalVFTable = null;
  20.  
  21.         #region Construction
  22.         // For the case where we already have a native IDirect3D9 object and we want to override some of it's functions.
  23.         public unsafe IDirect3D9(Win32.D3D9.IDirect3D9* InNativeIDirect3D9)
  24.         {
  25.             NativeIDirect3D9 = InNativeIDirect3D9;
  26.  
  27.             // Override the functions in NativeIDirect3D9 with our own.
  28.             OverrideFunctions();
  29.         }
  30.  
  31.         // For the case where we don't have a native IDirect3D object so we want one created for us.
  32.         public IDirect3D9(ushort SdkVersion)
  33.         {
  34.             VolvicMain.Interface.L("Same here, no IDirect3D ...");
  35.             // Create the real IDirect3D9 object.
  36.             NativeIDirect3D9 = Win32.D3D9.Direct3DCreate9(SdkVersion);
  37.  
  38.             // Override the functions in NativeIDirect3D9 with our own.
  39.             OverrideFunctions();
  40.         }
  41.         #endregion
  42.  
  43.         #region Destruction
  44.         ~IDirect3D9()
  45.         {
  46.             Dispose(true);
  47.         }
  48.  
  49.         public void Dispose()
  50.         {
  51.             Dispose(false);
  52.         }
  53.  
  54.         // Cleanup resources.  Destructing == true means we are getting garbage collected so don't reference any managed resources.
  55.         private void Dispose(bool Destructing)
  56.         {
  57.             if (OriginalVFTable != null)
  58.             {
  59.                 Win32.Kernel32.HeapFree(Win32.Kernel32.GetProcessHeap(), 0, *OriginalVFTable);
  60.                 OriginalVFTable = null;
  61.             }
  62.         }
  63.         #endregion
  64.  
  65.         #region Virtual Function Table Management
  66.         // Backup the original native virtual function table and overwrite the pointer to it with our own (which is a copy of the original).
  67.         private void InitializeVFTable()
  68.         {
  69.             // If we don't have a real IDirect3D9 object yet then do nothing.
  70.             if (NativeIDirect3D9 == null) return;
  71.            
  72.             // Save off the original VFTable (only if it really is the original).
  73.             if (OriginalVFTable == null) OriginalVFTable = NativeIDirect3D9->VFTable;
  74.            
  75.             // IDirect3D9 has 17 members.
  76.             UInt32 VFTableLength = 17;
  77.             // Allocate space for our new VFTable.
  78.             IntPtr* NewVFTable = (IntPtr*)Win32.Kernel32.HeapAlloc(Win32.Kernel32.GetProcessHeap(), 0, (UIntPtr)(VFTableLength * sizeof(IntPtr)));
  79.            
  80.             // Copy all of the original function pointers into our new VFTable.
  81.             for (int i = 0; i < VFTableLength; i++)
  82.             {
  83.                 NewVFTable[i] = OriginalVFTable[i];
  84.             }
  85.            
  86.             // Set the Real IDirect3D9 implementation's VFTable to point at our custom one.
  87.             NativeIDirect3D9->VFTable = NewVFTable;
  88.         }
  89.  
  90.         // Reset the native virtual function table to point back at the original.
  91.         private void ResetVFTable()
  92.         {
  93.             // If the original table is not defined do nothing.
  94.             if (OriginalVFTable == null) return;
  95.             // If the original table points to the same place as the current one do nothing.
  96.             if (OriginalVFTable == NativeIDirect3D9->VFTable) return;
  97.             // Cleanup memory allocated for our custom VFTable.
  98.             Win32.Kernel32.HeapFree(Win32.Kernel32.GetProcessHeap(), 0, *NativeIDirect3D9->VFTable);
  99.             // Set the VFTable back to the original.
  100.             NativeIDirect3D9->VFTable = OriginalVFTable;
  101.             // Set the original VFTable back to null.
  102.             OriginalVFTable = null;
  103.         }
  104.  
  105.         private void OverrideFunctions()
  106.         {
  107.             InitializeVFTable();
  108.  
  109.             // #0: STDMETHOD(QueryInterface)(THIS_ REFIID riid, void** ppvObj) PURE;
  110.             // #1: STDMETHOD_(ULONG,AddRef)(THIS) PURE;
  111.             // #2: STDMETHOD_(ULONG,Release)(THIS) PURE;
  112.             // TODO: Override this and Dispose this object if it is going to return 0.
  113.             // #3: STDMETHOD(RegisterSoftwareDevice)(THIS_ void* pInitializeFunction) PURE;
  114.             // #4: STDMETHOD_(UINT, GetAdapterCount)(THIS) PURE;
  115.             // #5: STDMETHOD(GetAdapterIdentifier)(THIS_ UINT Adapter,DWORD Flags,D3DADAPTER_IDENTIFIER9* pIdentifier) PURE;
  116.             // #6: STDMETHOD_(UINT, GetAdapterModeCount)(THIS_ UINT Adapter,D3DFORMAT Format) PURE;
  117.             // #7: STDMETHOD(EnumAdapterModes)(THIS_ UINT Adapter,D3DFORMAT Format,UINT Mode,D3DDISPLAYMODE* pMode) PURE;
  118.             // #8: STDMETHOD(GetAdapterDisplayMode)(THIS_ UINT Adapter,D3DDISPLAYMODE* pMode) PURE;
  119.             // #9: STDMETHOD(CheckDeviceType)(THIS_ UINT Adapter,D3DDEVTYPE DevType,D3DFORMAT AdapterFormat,D3DFORMAT BackBufferFormat,BOOL bWindowed) PURE;
  120.             // #10: STDMETHOD(CheckDeviceFormat)(THIS_ UINT Adapter,D3DDEVTYPE DeviceType,D3DFORMAT AdapterFormat,DWORD Usage,D3DRESOURCETYPE RType,D3DFORMAT CheckFormat) PURE;
  121.             // #11: STDMETHOD(CheckDeviceMultiSampleType)(THIS_ UINT Adapter,D3DDEVTYPE DeviceType,D3DFORMAT SurfaceFormat,BOOL Windowed,D3DMULTISAMPLE_TYPE MultiSampleType,DWORD* pQualityLevels) PURE;
  122.             // #12: STDMETHOD(CheckDepthStencilMatch)(THIS_ UINT Adapter,D3DDEVTYPE DeviceType,D3DFORMAT AdapterFormat,D3DFORMAT RenderTargetFormat,D3DFORMAT DepthStencilFormat) PURE;
  123.             // #13: STDMETHOD(CheckDeviceFormatConversion)(THIS_ UINT Adapter,D3DDEVTYPE DeviceType,D3DFORMAT SourceFormat,D3DFORMAT TargetFormat) PURE;
  124.             // #14: STDMETHOD(GetDeviceCaps)(THIS_ UINT Adapter,D3DDEVTYPE DeviceType,D3DCAPS9* pCaps) PURE;
  125.             // #15: STDMETHOD_(HMONITOR, GetAdapterMonitor)(THIS_ UINT Adapter) PURE;
  126.             // #16: STDMETHOD(CreateDevice)(THIS_ UINT Adapter,D3DDEVTYPE DeviceType,HWND hFocusWindow,DWORD BehaviorFlags,D3DPRESENT_PARAMETERS* pPresentationParameters,IDirect3DDevice9** ppReturnedDeviceInterface) PURE;
  127.         }
  128.         #endregion
  129.  
  130.         #region IDirect3D9 Interface Function Implementations
  131.         public delegate uint DelegateCreateDevice(
  132.             Win32.D3D9.IDirect3D9* This, uint adapter, uint deviceType, IntPtr focusWindow, uint behaviorFlags,
  133.             IntPtr presentationParameters, Win32.D3D9.IDirect3DDevice9* deviceInterface);
  134.         public uint CreateDevice(Win32.D3D9.IDirect3D9* This, uint adapter, uint deviceType, IntPtr focusWindow,
  135.                                  uint behaviorFlags, IntPtr presentationParameters,
  136.                                  Win32.D3D9.IDirect3DDevice9* deviceInterface)
  137.         {
  138.             VolvicMain.Interface.L("Never here ...");
  139.             RealCreateDevice =
  140.                 (DelegateCreateDevice)
  141.                 Marshal.GetDelegateForFunctionPointer(OriginalVFTable[16], typeof(DelegateCreateDevice));
  142.  
  143.             uint CreateDevice = RealCreateDevice(This, adapter, deviceType, focusWindow, behaviorFlags,
  144.                                                  presentationParameters, deviceInterface);
  145.  
  146.  
  147.             if (CreateDevice == 0)
  148.             {
  149.                 VolvicMain.Interface.L("Creating device ...");
  150.             }
  151.             return CreateDevice;
  152.         }
  153.         #endregion
  154.     }
  155. }
  156.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement