kiwidog

Battlefield 3 Direct3D Testing

Jul 28th, 2012
1,768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.73 KB | None | 0 0
  1. #include "main.h"
  2.  
  3. unsigned long BaseAddress = NULL;    // Hold Battlefield 3's Base Address
  4.  
  5. // credits: raiders
  6. ID3D11PixelShader* psRed = NULL;
  7. // raiders posted this here - http://www.unknowncheats.me/forum/direct3d/65135-directx-10-generateshader.html
  8. HRESULT GenerateShader(ID3D11Device* pD3DDevice, ID3D11PixelShader** pShader, float r, float g, float b)
  9. {
  10.     char szCast[] = "struct VS_OUT"
  11.     "{"
  12.     "    float4 Position   : SV_Position;"
  13.     "    float4 Color    : COLOR0;"
  14.     "};"
  15.  
  16.     "float4 main( VS_OUT input ) : SV_Target"
  17.     "{"
  18.     "    float4 fake;"
  19.     "    fake.a = 1.0;"
  20.     "    fake.r = %f;"
  21.     "    fake.g = %f;"
  22.     "    fake.b = %f;"
  23.     "    return fake;"
  24.     "}";
  25.     ID3D10Blob*    pBlob;
  26.     char szPixelShader[1000];
  27.  
  28.     sprintf(szPixelShader, szCast, r, g, b);
  29.  
  30.     HRESULT hr = D3DX11CompileFromMemory(szPixelShader, sizeof(szPixelShader), "shader", NULL, NULL, "main", "ps_4_0", NULL, NULL, NULL, &pBlob, NULL, NULL);
  31.    
  32.     if(FAILED(hr))
  33.         return hr;
  34.  
  35.     hr = pD3DDevice->CreatePixelShader((DWORD*)pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, pShader);
  36.  
  37.     if(FAILED(hr))
  38.         return hr;
  39.  
  40.     return S_OK;
  41. }
  42.  
  43. HRESULT WINAPI hkCreateVertexShader( ID3D11Device * This, const void *pShaderBytecode, SIZE_T BytecodeLength, ID3D11ClassLinkage *pClassLinkage, ID3D11VertexShader **ppVertexShader )
  44. {
  45.     __asm NOP;
  46.     return oCreateVertexShader( This, pShaderBytecode, BytecodeLength, pClassLinkage, ppVertexShader);
  47. }
  48.  
  49. HRESULT WINAPI hkPresent( IDXGISwapChain * This, UINT SyncInterval, UINT Flags )
  50. {
  51.     __asm NOP;
  52.     if ( !pFont )
  53.         if ( ppDevice && ppDeviceContext )
  54.             pFontFactory->CreateFontWrapper( ppDevice , L"Arial", &pFont );
  55.  
  56.     if ( pFont )
  57.     {
  58.         wchar_t wa[256];
  59.         swprintf( wa, 256, L"VertexCount: %i", vertexCount );
  60.         pFont->DrawString( ppDeviceContext, wa, 8.f, 10.f, 10.f, ((logVertexCount == true) ? 0xFF00FF00 : 0xFF0000FF), 0 );
  61.         swprintf( wa, 256, L"StartVertexLocation: %i", startVertexLocation );
  62.         pFont->DrawString( ppDeviceContext, wa, 8.f, 10.f, 20.f, ((logStartVertexLocation == true) ? 0xFF00FF00 : 0xFF0000FF), 0 );
  63.         // Check if our Shader got generated or not.
  64.         swprintf( wa, 256, L"psRed: %s", ((psRed == NULL) ? L"NULL" : L"FILLED" ) );
  65.         pFont->DrawString( ppDeviceContext, wa, 8.f, 10.f, 30.f, 0xFF00FF00, 0 );
  66.  
  67.         /*swprintf( wa, 256, L"IndexCount: %i", indexCount );
  68.         pFont->DrawString( ppDeviceContext, wa, 8.0f, 10.f, 10.f, ((logIndexCount == true) ? 0xFF00FF00 : 0xFF0000FF), 0 );// Text color, 0xAaBbGgRr
  69.         swprintf( wa, 256, L"StartIndexLocation: %i", startIndex );
  70.         pFont->DrawString( ppDeviceContext, wa, 8.f, 10.f, 20.f, ((logStartIndex == true) ? 0xFF00FF00 : 0xFF0000FF), 0 );
  71.         swprintf( wa, 256, L"BaseVertexLocation %i", baseVertex );
  72.         pFont->DrawString( ppDeviceContext, wa, 8.f, 10.f, 30.f, ((logBaseVertex == true) ? 0xFF00FF00 : 0xFF0000FF), 0 );
  73.         // Check if our Shader got generated or not.
  74.         swprintf( wa, 256, L"psRed: %s", ((psRed == NULL) ? L"NULL" : L"FILLED" ) );
  75.         pFont->DrawString( ppDeviceContext, wa, 8.f, 10.f, 40.f, 0xFF00FF00, 0 );*/
  76.     }
  77.  
  78.     return oPresent( This, SyncInterval, Flags );
  79. }
  80.  
  81. HRESULT WINAPI hkCreatePixelShader ( ID3D11Device * This, const void *pShaderBytecode, SIZE_T BytecodeLength, ID3D11ClassLinkage *pClassLinkage, ID3D11PixelShader **ppPixelShader )
  82. {
  83.     __asm NOP;
  84.     return oCreatePixelShader( This, pShaderBytecode, BytecodeLength, pClassLinkage, ppPixelShader );
  85. }
  86.  
  87. HRESULT WINAPI hkCreateGeometryShader( ID3D11Device * This, const void *pShaderBytecode, SIZE_T BytecodeLength, ID3D11ClassLinkage *pClassLinkage, ID3D11GeometryShader **ppGeometryShader )
  88. {
  89.     __asm NOP;
  90.     return oCreateGeometryShader( This, pShaderBytecode, BytecodeLength, pClassLinkage, ppGeometryShader );
  91. }
  92.  
  93. HRESULT WINAPI hkCreateTexture2D( ID3D11Device * This, const D3D11_TEXTURE2D_DESC *pDesc, void* Unknown, const D3D11_SUBRESOURCE_DATA *pInitialData, ID3D11Texture2D **ppTexture2D)
  94. {
  95.     __asm INT 3;
  96.     //add_log( "This: %X, D3D11_TEXTURE2D_DESC: %X, D3D11_SUBRESOURCE_DATA: %X, ID3D11Texture2D: %X", This, pDesc, pInitialData, ppTexture2D );
  97.     return oCreateTexture2D( This, pDesc, Unknown, pInitialData, ppTexture2D );
  98. }
  99.  
  100. HRESULT WINAPI hkDrawIndexed( ID3D11DeviceContext * This, UINT IndexCount, UINT StartIndexLocation, INT BaseVertexLocation )
  101. {
  102.     __asm NOP;
  103.  
  104.     if ( !psRed ) // Probably shouldn't do this here but whatevz'
  105.         GenerateShader( ppDevice, &psRed, 1.0f, 0.0f, 0.0f );
  106.  
  107.     /*if (psRed)
  108.         This->PSSetShader( psRed, NULL, NULL );
  109.     if ( logIndexCount && !logStartIndex && !logBaseVertex )
  110.         if ( indexCount == IndexCount )
  111.             This->PSSetShader( psRed, NULL, NULL );
  112.  
  113.     else if ( logIndexCount && logStartIndex && !logBaseVertex )
  114.         if ( indexCount == IndexCount && startIndex == startIndex )
  115.             This->PSSetShader( psRed, NULL, NULL );
  116.  
  117.     else if ( logIndexCount && logStartIndex && logBaseVertex )
  118.         if ( indexCount == IndexCount && startIndex == startIndex && baseVertex == baseVertex )
  119.             This->PSSetShader( psRed, NULL, NULL );
  120.  
  121.     else if ( !logIndexCount && logStartIndex && !logBaseVertex )
  122.         if ( startIndex == startIndex )
  123.             This->PSSetShader( psRed, NULL, NULL );
  124.  
  125.     else if ( !logIndexCount && logStartIndex && logBaseVertex )
  126.         if ( startIndex == startIndex )
  127.             This->PSSetShader( psRed, NULL, NULL );
  128.  
  129.     else if ( !logIndexCount && !logStartIndex && logBaseVertex )
  130.         if ( baseVertex == baseVertex )
  131.             This->PSSetShader( psRed, NULL, NULL );
  132.  
  133.     else if ( logIndexCount && !logStartIndex && logBaseVertex )
  134.         if ( indexCount == IndexCount && baseVertex == baseVertex )
  135.             This->PSSetShader( psRed, NULL, NULL );*/
  136.  
  137.     return oDrawIndexed( This, IndexCount, StartIndexLocation, BaseVertexLocation );
  138. }
  139. HRESULT WINAPI hkVSSetConstantBuffers(ID3D11DeviceContext* pDevice,UINT StartSlot, UINT NumBuffers,ID3D11Buffer *const *ppConstantBuffers)
  140. {
  141.     __asm nop;
  142.     return oVSSetConstantBuffers(pDevice ,StartSlot, NumBuffers,ppConstantBuffers);
  143. }
  144.  
  145. HRESULT WINAPI hkMap(ID3D11DeviceContext* pDevice,  ID3D11Resource *pResource, UINT Subresource, D3D11_MAP MapType, UINT MapFlags, D3D11_MAPPED_SUBRESOURCE *pMappedResource)
  146. {
  147.     __asm NOP;
  148.     // Could do this elsewhere but whatever, I already have it hooked for other testing
  149.     if ( !ppDevice || !ppDeviceContext )
  150.     {
  151.         ppDeviceContext = pDevice;
  152.         ppDeviceContext->GetDevice( &ppDevice );
  153.     }
  154.  
  155.     return oMap(pDevice,pResource,Subresource,MapType,MapFlags,pMappedResource);
  156. }
  157. bool ye = false;
  158. HRESULT WINAPI hkPSSetShaderResources(ID3D11DeviceContext * This, UINT StartSlot, UINT NumViews, ID3D11ShaderResourceView *const *ppShaderResourceViews)
  159. {
  160.     __asm NOP;
  161.     /*if ( StartSlot == 7 && ye)
  162.         return D3D_OK;*/
  163.     return oPSSetShaderResources( This, StartSlot, NumViews, ppShaderResourceViews );
  164. }
  165.  
  166. void WINAPI hkDrawInstanced(UINT VertexCountPerInstance, UINT InstanceCount, UINT StartVertexLocation, UINT StartInstanceLocation)
  167. {
  168. }
  169.  
  170. HRESULT WINAPI hkDraw( ID3D11DeviceContext * This, UINT VertexCount, UINT StartVertexLocation )
  171. {
  172.     __asm NOP;
  173.     if ( !ppDevice )
  174.         This->GetDevice( &ppDevice );
  175.  
  176.     if ( !ppDeviceContext )
  177.         ppDevice->GetImmediateContext( &ppDeviceContext );
  178.  
  179.     if ( !psRed ) // Probably shouldn't do this here but whatevz'
  180.         GenerateShader( ppDevice, &psRed, 1.0f, 0.0f, 0.0f );
  181.  
  182.     //if (psRed)
  183.     //    This->PSSetShader( psRed, NULL, NULL );
  184.  
  185.     if ( logVertexCount && !logStartVertexLocation )
  186.         if ( vertexCount == VertexCount )
  187.             return D3D_OK;
  188.             //This->PSSetShader( psRed, NULL, NULL );
  189.    
  190.     else if ( !logVertexCount && logStartVertexLocation )
  191.         if ( startVertexLocation == StartVertexLocation )
  192.             This->PSSetShader( psRed, NULL, NULL );
  193.  
  194.     else if ( logVertexCount && logStartVertexLocation )
  195.         if ( vertexCount == VertexCount && startVertexLocation == StartVertexLocation )
  196.             This->PSSetShader( psRed, NULL, NULL );
  197.  
  198.     return oDraw( This, VertexCount, StartVertexLocation );
  199. }
  200. // Logging Input
  201. DWORD WINAPI StartInput( LPVOID )
  202. {
  203.     for ( ;; )
  204.     {
  205.         // DrawIndexed
  206.         /*if ( GetAsyncKeyState( VK_F1 ) & 0x8000 )
  207.             if ( indexCount > 0 )
  208.                 indexCount--;
  209.  
  210.         if ( GetAsyncKeyState( VK_F2 ) & 0x8000 )
  211.             indexCount++;
  212.  
  213.         if ( GetAsyncKeyState( VK_F3 ) & 0x8000 )
  214.             if ( startIndex > 0 )
  215.                 startIndex--;
  216.  
  217.         if ( GetAsyncKeyState( VK_F4 ) & 0x8000 )
  218.             startIndex++;
  219.  
  220.         if ( GetAsyncKeyState( VK_F5 ) & 0x8000 )
  221.             if ( baseVertex > 0 )
  222.                 baseVertex--;
  223.  
  224.         if ( GetAsyncKeyState( VK_F6 ) & 0x8000 )
  225.             baseVertex++;
  226.  
  227.         if ( GetAsyncKeyState( VK_F7 ) & 0x8000 )
  228.             logIndexCount = !logIndexCount;
  229.  
  230.         if ( GetAsyncKeyState( VK_F8 ) & 0x8000 )
  231.             logStartIndex = !logStartIndex;
  232.  
  233.         if ( GetAsyncKeyState( VK_F9 ) & 0x8000 )
  234.             logBaseVertex = !logBaseVertex;*/
  235.  
  236.         if ( GetAsyncKeyState( VK_F1 ) & 0x8000 )
  237.             if ( vertexCount > 0 )
  238.                 vertexCount--;
  239.  
  240.         if ( GetAsyncKeyState( VK_F2 ) & 0x8000 )
  241.             vertexCount++;
  242.  
  243.         if ( GetAsyncKeyState( VK_F3 ) & 0x8000 )
  244.             if ( startVertexLocation > 0 )
  245.                 startVertexLocation--;
  246.  
  247.         if ( GetAsyncKeyState( VK_F4 ) & 0x8000 )
  248.             startVertexLocation++;
  249.  
  250.         if ( GetAsyncKeyState( VK_F5 ) & 0x8000 )
  251.             logVertexCount = !logVertexCount;
  252.  
  253.         if ( GetAsyncKeyState( VK_F6 ) & 0x8000 )
  254.             logStartVertexLocation = !logStartVertexLocation;
  255.  
  256.         if ( GetAsyncKeyState( VK_F1 ) & 0x8000 )
  257.             ye = !ye;
  258.         Sleep( 75 );
  259.     }
  260.     return 0;
  261. }
  262.  
  263. DWORD WINAPI StartRev( LPVOID )
  264. {
  265.     BaseAddress = (DWORD)GetModuleHandle( NULL );
  266.     /*while ( GetModuleHandle( "d3d11.dll" ) == NULL )
  267.         Sleep( 250 );*/
  268.  
  269.     unsigned long D3DPtr = *(unsigned long*)(BaseAddress + BF3D3DPTR); // Device Pointer Offset
  270.     while ( D3DPtr == NULL )
  271.     {
  272.         D3DPtr = *(unsigned long*)(BaseAddress + BF3D3DPTR);
  273.         Sleep( 250 );
  274.     }
  275.     // g_dxRenderer = 0xD8
  276.     unsigned long D3DDevice = *(unsigned long*)(D3DPtr + 0xD8);
  277.     unsigned long DeviceVTable = *(unsigned long*)D3DDevice;
  278.  
  279.     // 0xDC DeviceContext
  280.     // but the context function are func+1
  281.     unsigned long DXGISwapChainDevice = *(unsigned long*)(D3DPtr + 0xF4);
  282.     unsigned long SwapChainVTable = *(unsigned long*)DXGISwapChainDevice;
  283.  
  284.     unsigned long DeviceContext = *(unsigned long*)(D3DPtr + 0xDC);
  285.     unsigned long DeviceContextVTable = *(unsigned long*)DeviceContext;
  286.  
  287.     DWORD* devicePVTable = (DWORD*)(DeviceVTable); // The Address + sizeof(IUnknown)
  288.     DWORD* swapChainPVTable = (DWORD*)(SwapChainVTable);
  289.     DWORD* deviceContextPVTable = (DWORD*)(DeviceContextVTable);
  290.  
  291.     mFile.open("Battlefield_Reversing.log");
  292.     if ( mFile.is_open() )
  293.     {
  294.         HRESULT hRet = FW1CreateFactory( FW1_VERSION, &pFontFactory );
  295.         add_log("Started Hooking Functions...");
  296.         //oCreateVertexShader = (tCreateVertexShader)DetourFunction( (PBYTE)devicePVTable[12], (PBYTE)hkCreateVertexShader );
  297.         //oCreateGeometryShader = (tCreateGeometryShader)DetourFunction( (PBYTE)devicePVTable[13], (PBYTE)hkCreateGeometryShader );
  298.         //oCreatePixelShader = (tCreatePixelShader)DetourFunction( (PBYTE)devicePVTable[15], (PBYTE)hkCreatePixelShader );
  299.         oMap = (tMap)DetourFunction( (PBYTE)deviceContextPVTable[14], (PBYTE)hkMap );
  300.         //oVSSetConstantBuffers = (tVSSetConstantBuffers)DetourFunction( (PBYTE)deviceContextPVTable[7], (PBYTE)hkVSSetConstantBuffers );
  301.         oPresent = (tPresent)DetourFunction( (PBYTE)swapChainPVTable[8], (PBYTE)hkPresent );
  302.         oDrawIndexed = (tDrawIndexed)DetourFunction( (PBYTE)deviceContextPVTable[12], (PBYTE)hkDrawIndexed );
  303.         //oCreateTexture2D = (tCreateTexture2D)DetourFunction( (PBYTE)devicePVTable[5], (PBYTE)hkCreateTexture2D ); // ask fatboy help
  304.         //oPSSetShaderResources = (tPSSetShaderResources)DetourFunction( (PBYTE)deviceContextPVTable[8], (PBYTE)hkPSSetShaderResources );
  305.         oDraw = (tDraw) DetourFunction( (PBYTE)deviceContextPVTable[13], (PBYTE)hkDraw );
  306.         add_log("All Functions Hooked!");
  307.     }
  308.  
  309.     return 0;
  310. }
  311.  
  312. BOOL WINAPI DllMain( HMODULE hModule, DWORD dwReason, LPVOID lpvReserved )
  313. {
  314.     if ( dwReason == DLL_PROCESS_ATTACH )
  315.     {
  316.         CreateThread( 0, 0, StartRev, 0, 0, 0 );
  317.         CreateThread( 0, 0, StartInput, 0, 0, 0 );
  318.     }
  319.     return true;
  320. }
Advertisement
Add Comment
Please, Sign In to add comment