//---------------------------------------------------------------------------------------------------------------------------------
// File: main.cpp
//---------------------------------------------------------------------------------------------------------------------------------
#include <windows.h>
#include <assert.h>
#include <iostream>
#include <d3d9.h>
#include <d3dx9.h>
#include <detours.h>
#pragma comment (lib, "d3dx9.lib")
#pragma comment (lib, "d3d9.lib")
#pragma comment( lib, "winmm.lib" )
//---------------------------------------------------------------------------------------------------------------------------------
using namespace std;
//---------------------------------------------------------------------------------------------------------------------------------
#define HOOK(func,addy) o##func = (t##func)DetourFunction((PBYTE)addy,(PBYTE)hk##func)
#define ES 0
#define DIP 1
#define RES 2
#define Terrorist (( NumVertices == 1588 && primCount == 2013 ) || ( NumVertices == 911 && primCount == 996 ) || ( NumVertices == 526 && primCount == 594 ) || ( NumVertices == 389 && primCount == 386 ) || ( NumVertices == 1510 && primCount == 1871 ) || ( NumVertices == 873 && primCount == 986 ) || ( NumVertices == 563 && primCount == 590 ) || ( NumVertices == 368 && primCount == 377 ) || ( NumVertices == 1311 && primCount == 1812 ) || ( NumVertices == 735 && primCount == 899 ) || ( NumVertices == 454 && primCount == 519 ) || ( NumVertices == 323 && primCount == 338 ) || ( NumVertices == 1488 && primCount == 1819 ) || ( NumVertices == 835 && primCount == 899 ) || ( NumVertices == 554 && primCount == 509 ) || ( NumVertices == 408 && primCount == 344 ))
#define CounterTerrorist (( NumVertices == 1609 && primCount == 1794 ) || ( NumVertices == 899 && primCount == 910 ) || ( NumVertices == 513 && primCount == 451 ) || ( NumVertices == 369 && primCount == 297 ) || ( NumVertices == 1424 && primCount == 1858 ) || ( NumVertices == 814 && primCount == 945 ) || ( NumVertices == 475 && primCount == 510 ) || ( NumVertices == 343 && primCount == 344 ) || ( NumVertices == 1498 && primCount == 1822 ) || ( NumVertices == 929 && primCount == 1007 ) || ( NumVertices == 499 && primCount == 533 ) || ( NumVertices == 409 && primCount == 433 ) || ( NumVertices == 1404 && primCount == 1919 ) || ( NumVertices == 811 && primCount == 989 ) || ( NumVertices == 466 && primCount == 550 ) || ( NumVertices == 324 && primCount == 372 ))
//---------------------------------------------------------------------------------------------------------------------------------
typedef HRESULT (WINAPI* tReset)( LPDIRECT3DDEVICE9 pDevice, D3DPRESENT_PARAMETERS* pPresentationParameters );
tReset oReset;
typedef HRESULT (WINAPI* tEndScene)( LPDIRECT3DDEVICE9 pDevice );
tEndScene oEndScene;
typedef HRESULT (WINAPI* tDrawIndexedPrimitive)( LPDIRECT3DDEVICE9 pDevice, D3DPRIMITIVETYPE PrimType,INT BaseVertexIndex,UINT MinVertexIndex,UINT NumVertices,UINT startIndex,UINT primCount );
tDrawIndexedPrimitive oDrawIndexedPrimitive;
#define SAFE_RELEASE(x) if (x) { x->Release(); x = NULL; }
LPDIRECT3DPIXELSHADER9 sGreen, sRed, sBlue,sYellow;
LPDIRECT3DSURFACE9 pBackBuffer = NULL;
LPDIRECT3DSURFACE9 pSurface = NULL;
LPDIRECT3DDEVICE9 pd3dDevice;
D3DVIEWPORT9 pViewport;
D3DLOCKED_RECT pLockedRect;
D3DCOLOR myColor;
DWORD gametick = timeGetTime();
//---------------------------------------------------------------------------------------------------------------------------------
HRESULT PixelGetColor( D3DCOLOR *color, int x, int y )
{
BYTE red, green, blue;
if(pSurface==NULL) pd3dDevice->CreateOffscreenPlainSurface( pViewport.Width, pViewport.Height,
D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &pSurface, NULL );
pd3dDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer );
pd3dDevice->GetRenderTargetData( pBackBuffer, pSurface );
pSurface->LockRect(&pLockedRect,0,D3DLOCK_READONLY);
BYTE *pBits = (BYTE*)pLockedRect.pBits;
blue = pBits[y*pLockedRect.Pitch+x*4];
green = pBits[y*pLockedRect.Pitch+x*4+1];
red = pBits[y*pLockedRect.Pitch+x*4+2];
*color = D3DCOLOR_XRGB(red, green, blue);
pSurface->UnlockRect();
SAFE_RELEASE(pSurface);
return S_OK;
}
//-----------------------------------------------------------------------------------------------------------------------------------
HRESULT CreateMyShader(IDirect3DDevice9 *pD3Ddev, IDirect3DPixelShader9 **pShader, float r, float g, float b)
{
char szShader[ 256 ];
ID3DXBuffer *pShaderBuf = NULL;
sprintf_s( szShader, "ps.1.1\ndef c0, %f, %f, %f, %f\nmov r0,c0", r, g, b, 1.0f );
D3DXAssembleShader( szShader, (strlen(szShader)+1), NULL, NULL, 0, &pShaderBuf, NULL );
if( FAILED( pD3Ddev->CreatePixelShader((const DWORD*)pShaderBuf->GetBufferPointer(), pShader)) )return E_FAIL;
return S_OK;
}
//---------------------------------------------------------------------------------------------------------------------------------
bool bInitShaders = false;
HRESULT WINAPI hkEndScene( LPDIRECT3DDEVICE9 pDevice )
{
HRESULT hRet = oEndScene( pDevice );
if (pd3dDevice == NULL){
pd3dDevice = pDevice;
}
if( bInitShaders == false )
{
CreateMyShader(pDevice, &sBlue, 0, 0, 255);
CreateMyShader(pDevice, &sGreen, 0, 255, 0);
CreateMyShader(pDevice, &sRed, 255, 0, 0);
CreateMyShader(pDevice, &sYellow, 255, 255, 0);
bInitShaders = true;
}
pDevice->GetViewport( &pViewport );
if ( timeGetTime() - gametick > 100 )
{
PixelGetColor( &myColor, pViewport.Width/2, pViewport.Height/2 );
gametick = timeGetTime();
}
if (myColor == 0xFF00FF00){
mouse_event( MOUSEEVENTF_LEFTDOWN,0,0,NULL,NULL );
if (GetAsyncKeyState(VK_LBUTTON) < 0)
mouse_event( MOUSEEVENTF_LEFTUP,0,0,NULL,NULL );
}
if (myColor == 0xFFFFFF00){
mouse_event( MOUSEEVENTF_LEFTDOWN,0,0,NULL,NULL );
if (GetAsyncKeyState(VK_LBUTTON) < 0)
mouse_event( MOUSEEVENTF_LEFTUP,0,0,NULL,NULL );
}
return hRet;
}
//---------------------------------------------------------------------------------------------------------------------------------
HRESULT WINAPI hkDrawIndexedPrimitive( LPDIRECT3DDEVICE9 pDevice, D3DPRIMITIVETYPE PrimType,INT BaseVertexIndex,UINT MinVertexIndex,UINT NumVertices,UINT startIndex,UINT primCount )
{
HRESULT hRet = oDrawIndexedPrimitive( pDevice, PrimType, BaseVertexIndex, MinVertexIndex, NumVertices, startIndex, primCount );
LPDIRECT3DPIXELSHADER9 pShader = NULL;
LPDIRECT3DVERTEXBUFFER9 Stream_Data;
UINT Offset = 0;
UINT Stride = 0;
if(pDevice->GetStreamSource(0, &Stream_Data, &Offset, &Stride) == D3D_OK)
Stream_Data->Release();
if (CounterTerrorist)
{
pDevice->SetPixelShader(sGreen);
pDevice->SetRenderState(D3DRS_ZENABLE,false);
oDrawIndexedPrimitive(pDevice, PrimType, BaseVertexIndex, MinVertexIndex, NumVertices, startIndex, primCount);
pDevice->SetRenderState(D3DRS_ZENABLE,true);
oDrawIndexedPrimitive(pDevice, PrimType, BaseVertexIndex, MinVertexIndex, NumVertices, startIndex, primCount);
}
if (Terrorist)
{
pDevice->SetPixelShader(sYellow);
pDevice->SetRenderState(D3DRS_ZENABLE,false);
oDrawIndexedPrimitive(pDevice, PrimType, BaseVertexIndex, MinVertexIndex, NumVertices, startIndex, primCount);
pDevice->SetRenderState(D3DRS_ZENABLE,true);
oDrawIndexedPrimitive(pDevice, PrimType, BaseVertexIndex, MinVertexIndex, NumVertices, startIndex, primCount);
}
return hRet;
}
//---------------------------------------------------------------------------------------------------------------------------------
HRESULT WINAPI hkReset( LPDIRECT3DDEVICE9 pDevice, D3DPRESENT_PARAMETERS* pPresentationParameters )
{
HRESULT hRet = oReset( pDevice, pPresentationParameters );
myColor = 0x00;
return hRet;
}
//---------------------------------------------------------------------------------------------------------------------------------
LRESULT CALLBACK MsgProc( HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam){return DefWindowProc(hwnd, uMsg, wParam, lParam );}
void InitD3D( DWORD* table )
{
WNDCLASSEX wc = {sizeof(WNDCLASSEX),CS_CLASSDC,MsgProc,0L,0L,GetModuleHandle(NULL),NULL,NULL,NULL,NULL,L"DX",NULL};
RegisterClassEx( &wc );
HWND hWnd = CreateWindow( L"DX",NULL,WS_OVERLAPPEDWINDOW,100,100,300,300,GetDesktopWindow(),NULL,wc.hInstance,NULL );
LPDIRECT3D9 pD3D = Direct3DCreate9( D3D_SDK_VERSION );
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
LPDIRECT3DDEVICE9 pd3dDevice;
pD3D->CreateDevice( D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&pd3dDevice );
DWORD* pVTable = (DWORD*)pd3dDevice;
pVTable = (DWORD*)pVTable[0];
table[ES] = pVTable[42];
table[DIP] = pVTable[82];
table[RES] = pVTable[16];
DestroyWindow( hWnd );
}
//---------------------------------------------------------------------------------------------------------------------------------
DWORD WINAPI MyThread( LPVOID )
{
DWORD VTable[3] = {0};
while( GetModuleHandle(L"d3d9.dll")==NULL ){
Sleep( 250 );
}
InitD3D( VTable );
HOOK( EndScene,VTable[ES] );
HOOK( DrawIndexedPrimitive,VTable[DIP] );
HOOK( Reset,VTable[RES] );
return 0;
}
//---------------------------------------------------------------------------------------------------------------------------------
BOOL WINAPI DllMain( HMODULE hModule, DWORD dwReason, LPVOID lpvReserved )
{
if( dwReason == DLL_PROCESS_ATTACH ){
CreateThread( 0,0,MyThread,0,0,0 );
}
return TRUE;
}