Code:
#include "Main.h"
#include "Models.h"
HRESULT (WINAPI* pReset)(LPDIRECT3DDEVICE9, D3DPRESENT_PARAMETERS*);
HRESULT (WINAPI* pBegineScene)(LPDIRECT3DDEVICE9);
HRESULT (WINAPI* pDrawPrim)(LPDIRECT3DDEVICE9, D3DPRIMITIVETYPE, INT, UINT, UINT, UINT, UINT);
HRESULT (WINAPI* pEndScene)(LPDIRECT3DDEVICE9);
HRESULT (WINAPI* pPresent)( LPDIRECT3DDEVICE9 Device,
CONST RECT* pSourceRect, CONST RECT* pDestRect,
HWND hDestWindowOverride, CONST RGNDATA* pDirtyRegion );
D3DVIEWPORT9 D3DVP = {NULL};
LPD3DXFONT PFONT = NULL;
LPDIRECT3DSWAPCHAIN9 Second = NULL;
LPDIRECT3DTEXTURE9 Blue = NULL;
LPDIRECT3DTEXTURE9 Red = NULL;
LPDIRECT3DTEXTURE9 Yellow = NULL;
LPDIRECT3DTEXTURE9 Green = NULL;
HRESULT WINAPI DrawPrim( LPDIRECT3DDEVICE9 pDevice, D3DPRIMITIVETYPE Type, INT Base,
UINT Min, UINT NumVertices, UINT Start, UINT PrimitiveCount )
{
LPDIRECT3DVERTEXBUFFER9 Stream = NULL;
UINT Offset = 0;
UINT Stride = 0;
BOOL TeamTE = FALSE;
BOOL TeamCTE = FALSE;
BOOL TeamC4 = FALSE;
if( pDevice->GetStreamSource( 0, &Stream, &Offset, &Stride ) == D3D_OK )
Stream->Release( );
if( TE_DX8 || TE ) TeamTE = TRUE;
else if( CTE_DX8 || CTE ) TeamCTE = TRUE;
else if( C4 ) TeamC4 = TRUE;
if( Second )
if( TeamTE || TeamCTE )
{
LPDIRECT3DSURFACE9 Surf = NULL;
LPDIRECT3DBASETEXTURE9 Texture = NULL;
LPDIRECT3DSURFACE9 Depth = NULL;
if( pDevice->GetTexture( 0, &Texture ) == S_OK )
{
if( pDevice->GetRenderTarget( 0, &Surf ) == S_OK )
{
if( pDevice->GetDepthStencilSurface( &Depth ) == S_OK )
{
LPDIRECT3DSURFACE9 pBackBuffer = NULL;
if( Second->GetBackBuffer( 0,
D3DBACKBUFFER_TYPE_MONO, &pBackBuffer ) == S_OK )
{
pDevice->SetRenderTarget( 0, pBackBuffer );
pDevice->SetDepthStencilSurface( Depth );
pDevice->SetRenderState( D3DRS_ZENABLE, FALSE );
if( Blue && TeamCTE )
pDevice->SetTexture( 0, Red );
if( Red && TeamTE )
pDevice->SetTexture( 0, Blue );
pDrawPrim( pDevice, Type, Base, Min, NumVertices,
Start, PrimitiveCount );
pDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
if( Yellow && TeamCTE )
pDevice->SetTexture( 0, Yellow );
if( Green && TeamTE )
pDevice->SetTexture( 0, Green );
pDrawPrim( pDevice, Type, Base, Min, NumVertices,
Start, PrimitiveCount );
D3D_RELEASE( pBackBuffer )
}
pDevice->SetTexture( 0, Texture );
pDevice->SetRenderTarget( 0, Surf );
pDevice->SetDepthStencilSurface( Depth );
D3D_RELEASE( Depth )
}
D3D_RELEASE( Surf )
}
D3D_RELEASE( Texture )
}
}
return pDrawPrim( pDevice, Type, Base, Min,
NumVertices, Start, PrimitiveCount );
}
HRESULT WINAPI Reset( LPDIRECT3DDEVICE9 pDevice,
D3DPRESENT_PARAMETERS* PPR )
{
D3D_RELEASE( PFONT )
D3D_RELEASE( Blue )
D3D_RELEASE( Red )
D3D_RELEASE( Yellow )
D3D_RELEASE( Green )
return pReset( pDevice, PPR );
}
HRESULT WINAPI BegineScene( LPDIRECT3DDEVICE9 pDev )
{
if( !Blue ) GenTex( pDev, &Blue, D3DCOLOR_ARGB( 255, 20, 20, 255 ) );
if( !Red ) GenTex( pDev, &Red, D3DCOLOR_ARGB( 255, 255, 20, 20 ) );
if( !Yellow ) GenTex( pDev, &Yellow, D3DCOLOR_ARGB( 255, 255, 255, 20 ) );
if( !Green ) GenTex( pDev, &Green, D3DCOLOR_ARGB( 255, 20, 255, 20 ) );
if( !Second && Transparent )
{
D3DPRESENT_PARAMETERS pP = {0};
LPDIRECT3DSWAPCHAIN9 Swap = NULL;
if( pDev->GetSwapChain( 0, &Swap ) == S_OK )
{
if( Swap->GetPresentParameters( &pP ) == S_OK )
pDev->CreateAdditionalSwapChain( &pP, &Second );
D3D_RELEASE( Swap )
}
}
return pBegineScene( pDev );
}
void WINAPI Draw( LPDIRECT3DDEVICE9 pDev )
{
if( !PFONT )
return;
RECT FRECT = { D3DVP.Width-400, D3DVP.Height-50,
D3DVP.Width-200, D3DVP.Height };
PFONT->DrawTextA( 0, "Overlay Players", -1,
&FRECT, DT_NOCLIP | DT_CENTER |
DT_VCENTER, 0xFFFF0000 );
}
void WINAPI DrawCross( LPDIRECT3DDEVICE9 Device )
{
LONG ScreenWidthHalf = D3DVP.Width/2;
LONG ScreenHeightHalf = D3DVP.Height/2;
D3DRECT CrossA ={ ScreenWidthHalf, ScreenHeightHalf - 5,
ScreenWidthHalf + 1, ScreenHeightHalf + 6};
Device->Clear( 1, &CrossA, D3DCLEAR_TARGET,
D3DCOLOR_ARGB( 255, 255, 20, 20 ), 0, 0 );
D3DRECT CrossB ={ ScreenWidthHalf - 5, ScreenHeightHalf,
ScreenWidthHalf + 6, ScreenHeightHalf + 1};
Device->Clear( 1, &CrossB, D3DCLEAR_TARGET,
D3DCOLOR_ARGB( 255, 255, 20, 20 ), 0, 0 );
}
void WINAPI GetTarget( LPDIRECT3DDEVICE9 Device )
{
if( !Second )
return;
LPDIRECT3DSURFACE9 Surf = NULL;
LPDIRECT3DSURFACE9 pBackBuffer = NULL;
if( Device->GetRenderTarget( 0, &Surf ) == S_OK )
{
if( Second->GetBackBuffer( 0, D3DBACKBUFFER_TYPE_MONO,
&pBackBuffer ) == S_OK )
{
Device->SetRenderTarget( 0, pBackBuffer );
Draw( Device );
DrawCross( Device );
Device->SetRenderTarget( 0, Surf );
D3D_RELEASE( pBackBuffer )
}
D3D_RELEASE( Surf )
}
}
HRESULT WINAPI EndScene( LPDIRECT3DDEVICE9 pDev )
{
pDev->GetViewport( &D3DVP );
if( !PFONT )
D3DXCreateFontA( pDev, 14, 0, FW_BOLD, 0, FALSE, DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
"Cambria", &PFONT );
GetTarget( pDev );
if( Transparent ) SetWindowSize( );
return pEndScene( pDev );
}
HRESULT WINAPI Present( LPDIRECT3DDEVICE9 Device,
CONST RECT* pSourceRect, CONST RECT* pDestRect,
HWND hDestWindowOverride, CONST RGNDATA* pDirtyRegion )
{
if( Second )
{
LPDIRECT3DSURFACE9 Surf = NULL;
Second->Present( NULL, NULL, Transparent, NULL, 0 );
if( Device->GetRenderTarget( 0, &Surf ) == S_OK )
{
LPDIRECT3DSURFACE9 pBackBuffer = NULL;
if( Second->GetBackBuffer( 0, D3DBACKBUFFER_TYPE_MONO,
&pBackBuffer ) == S_OK )
{
Device->SetRenderTarget( 0, pBackBuffer );
Device->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_COLORVALUE( 0.0f, 0.0f, 0.0f, 0.0f ), 1.0f, 0L );
Device->SetRenderTarget( 0, Surf );
D3D_RELEASE( pBackBuffer )
}
D3D_RELEASE( Surf )
}
}
return pPresent( Device, pSourceRect, pDestRect,
hDestWindowOverride, pDirtyRegion );
}
VOID WINAPI HkD3D( PDWORD vTable )
{
DWORD OLD;
VirtualProtect( &vTable[0], 118 * 4, 0x40, &OLD );
*(PDWORD)&pReset = (DWORD)vTable[16];
*(PDWORD)&pPresent = (DWORD)vTable[17];
*(PDWORD)&pBegineScene = (DWORD)vTable[41];
*(PDWORD)&pEndScene = (DWORD)vTable[42];
*(PDWORD)&pDrawPrim = (DWORD)vTable[82];
while( 1 )
{
vTable[82] = (DWORD)DrawPrim;
vTable[16] = (DWORD)Reset;
vTable[17] = (DWORD)Present;
vTable[41] = (DWORD)BegineScene;
vTable[42] = (DWORD)EndScene;
Sleep( 100 );
}
}
BOOL WINAPI GenTex( LPDIRECT3DDEVICE9 pDev,
LPDIRECT3DTEXTURE9 * ppD3Dtex, DWORD Color )
{
D3DLOCKED_RECT d3dlr = {NULL};
IDirect3DTexture9 * Temp = NULL;
if( FAILED( pDev->CreateTexture( 100, 100, 1, 0, D3DFMT_A8R8G8B8,
D3DPOOL_DEFAULT, ppD3Dtex, NULL ) ) )
return FALSE;
if( FAILED( pDev->CreateTexture( 100, 100, 1, 0, D3DFMT_A8R8G8B8,
D3DPOOL_SYSTEMMEM, &Temp, NULL ) ) )
{
D3D_RELEASE( (*ppD3Dtex) )
return FALSE;
}
if( Temp->LockRect( 0, &d3dlr, 0, 0 ) == S_OK )
{
for( int i = 0;i < 100*100;i++ )
((PDWORD)d3dlr.pBits)[i] = Color;
Temp->UnlockRect( 0 );
pDev->UpdateTexture( Temp, *ppD3Dtex );
}
else
{
D3D_RELEASE( Temp )
D3D_RELEASE( (*ppD3Dtex) )
return FALSE;
}
D3D_RELEASE( Temp )
return TRUE;
}
main.cpp
Code:
#include "Main.h"
static TCHAR szWindowClass[] = _T("Overlay");
static TCHAR szTitle[] = _T("Overlay Players");
WNDCLASSEX WC = {NULL};
MSG msg = {NULL};
RECT GameRect = {NULL};
RECT WRect = {NULL};
WINDOWINFO GameWinfo = {NULL};
LONG BorderSizeLeft = NULL;
LONG BorderSizeTop = NULL;
MARGINS Margins = {-1};
HINSTANCE Game = NULL;
HWND GameWindow = NULL;
HWND Transparent = NULL;
LRESULT CALLBACK WndProc( HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam )
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
EndPaint( hWnd, &ps );
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc( hWnd, message, wParam, lParam );
break;
}
return 0;
}
bool WINAPI bDataCompare( const BYTE* pData, const BYTE* bMask, const char* szMask )
{
for( ; *szMask; ++szMask, ++pData, ++bMask )
{
if( *szMask == 'x' && *pData != *bMask )
{
return false;
}
}
return (*szMask) == NULL;
}
DWORD WINAPI dwFindPattern( DWORD dwAddress, DWORD dwSize, BYTE* pbMask, char* szMask )
{
for( DWORD i = NULL; i < dwSize; i++ )
{
if( bDataCompare( (BYTE*)( dwAddress + i ), pbMask, szMask ) )
{
return (DWORD)( dwAddress + i );
}
}
return 0;
}
void SetWindowSize( )
{
GetWindowRect( GameWindow, &GameRect );
GetWindowRect( Transparent, &WRect );
LONG GameWidth = D3DVP.Width;
LONG GameHeight = D3DVP.Height;
LONG GameX = GameRect.left;
LONG GameY = GameRect.top;
LONG Width = WRect.right - WRect.left;
LONG Height = WRect.bottom - WRect.top;
LONG X = WRect.left - BorderSizeLeft;
LONG Y = WRect.top - BorderSizeTop;
if( GameWidth != Width || GameHeight != Height ||
GameX != X || GameY != Y )
{
SetWindowPos( Transparent, HWND_TOPMOST, GameX + BorderSizeLeft,
GameY + BorderSizeTop, D3DVP.Width, D3DVP.Height, SWP_FRAMECHANGED );
}
}
void WINAPI CreateTransparentWindow( )
{
do{
GameWindow = FindWindowA( 0, "Counter-Strike Source" );
Sleep( 100 );
} while( !GameWindow );
do{
Game = GetModuleHandleA( "hl2.exe" );
Sleep( 100 );
} while( !Game );
WC.cbSize = sizeof(WNDCLASSEX);
WC.style = CS_HREDRAW | CS_VREDRAW;
WC.lpfnWndProc = WndProc;
WC.hInstance = Game;
WC.hCursor = LoadCursor( NULL, IDC_ARROW );
WC.hbrBackground = 0;
WC.lpszClassName = szWindowClass;
RegisterClassEx( &WC );
Transparent = CreateWindowExA( 0L, szWindowClass, szTitle,
WS_CAPTION | WS_SYSMENU,
CW_USEDEFAULT, CW_USEDEFAULT,
500, 500, 0, NULL, Game, NULL );
ShowWindow( Transparent, SW_SHOW );
UpdateWindow( Transparent );
SetWindowSize( );
GetWindowInfo( GameWindow, &GameWinfo );
BorderSizeLeft = GameWinfo.rcClient.left - GameWinfo.rcWindow.left;
BorderSizeTop = GameWinfo.rcClient.top - GameWinfo.rcWindow.top;
LONG lStyle = GetWindowLong( Transparent, GWL_STYLE );
lStyle &= ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZE | WS_MAXIMIZE | WS_SYSMENU);
SetWindowLongA( Transparent, GWL_STYLE, lStyle );
SetWindowLong( Transparent, GWL_EXSTYLE, GetWindowLong( Transparent, GWL_EXSTYLE) |
WS_EX_LAYERED | WS_EX_TRANSPARENT );
SetLayeredWindowAttributes( Transparent, RGB( 255, 255, 255 ), 0, LWA_COLORKEY );
DwmExtendFrameIntoClientArea( Transparent, &Margins );
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
VOID WINAPI GetD3D( VOID )
{
DWORD D3D9 = (DWORD)
LoadLibraryA( "d3d9.dll" );
Sleep( 1000 );
DWORD D3DPattern = dwFindPattern( D3D9, 0x128000,
(PBYTE)"\xC7\x06\x00\x00\x00\x00\x89\x86\x00\x00\x00\x00\x89\x86",
"xx????xx????xx" );
while( !*(PDWORD)(D3DPattern+2) )
Sleep( 500 );
HkD3D( (PDWORD)*(PDWORD)(D3DPattern+2) );
}
BOOL WINAPI DllMain( HINSTANCE hModule, DWORD dwReason, LPVOID lpvReserved )
{
if( dwReason == DLL_PROCESS_ATTACH )
{
DisableThreadLibraryCalls( hModule );
CreateThread( 0, 0, (LPTHREAD_START_ROUTINE)GetD3D, 0, 0, 0);
CreateThread( 0, 0, (LPTHREAD_START_ROUTINE)CreateTransparentWindow, 0, 0, 0);
}
return TRUE;
}
models.h
Bookmarks