📄 glhook.pas
字号:
unit glhook;
//this method of hooking is with the asumption that the draw routines aren't multithreaded
interface
uses windows,opengl12,sysutils,keylistener;
procedure HookOpenGLFunctions;
type TAPIInfo = record
location: Pointer;
Original: Array [0..4] of byte;
Jump: Array [0..4] of byte;
end;
type TglBindTexture=procedure(target: TGLEnum; texture: TGLuint); stdcall;
TglBegin=procedure(mode: TGLEnum); stdcall;
TglCallList=procedure(list: TGLuint); stdcall;
TglCallLists=procedure(n: TGLsizei; atype: TGLEnum; lists: Pointer); stdcall;
TglClear=procedure(mask: TGLbitfield); stdcall;
TglDisable=procedure(cap: TGLEnum); stdcall;
TglEnable=procedure(cap: TGLEnum); stdcall;
TglEnd=procedure; stdcall;
var opengl32dll: thandle;
HookedOpenGL: boolean;
glBeginInfo: TAPIInfo;
glBindTextureInfo: TAPIInfo;
glCallListInfo: TAPIInfo;
glCallListsInfo: TAPIInfo;
glClearInfo: TAPIInfo;
glDisableInfo: TAPIInfo;
glEnableInfo: TAPIInfo;
glEndInfo: TAPIInfo;
procedure glEnd_hook; stdcall;
procedure glBegin_hook(mode: TGLEnum); stdcall;
procedure glBindTexture_Hook(target: TGLEnum; texture: TGLuint); stdcall;
procedure glCallList_Hook(list: TGLuint); stdcall;
procedure glCallLists_Hook(n: TGLsizei; atype: TGLEnum; lists: Pointer); stdcall;
procedure glClear_Hook(mask: TGLbitfield); stdcall;
procedure glEnable_Hook(cap: TGLEnum); stdcall;
implementation
//----------------------------------------------------
procedure glBindTexture_hook(target: TGLEnum; texture: TGLuint); stdcall;
begin
asm
push esi
push edi
lea esi,glBindTextureInfo.original[0]
mov edi,glBindTextureInfo.location
movsd
movsb
pop edi
pop esi
end;
//do my stuff here
TglBindTexture(glBindTextureInfo.location)(target,texture);
asm
push esi
push edi
lea esi,glBindTextureInfo.jump[0]
mov edi,glBindTextureInfo.location
movsd
movsb
pop edi
pop esi
end;
end;
procedure glCallLists_Hook(n: TGLsizei; atype: TGLEnum; lists: Pointer); stdcall;
begin
asm
push esi
push edi
lea esi,glCallListsInfo.original[0]
mov edi,glCallListsInfo.location
movsd
movsb
pop edi
pop esi
end;
outputdebugstring('glCallLists called');
TglCallLists(glCallListsInfo.location)(n,atype,lists);
asm
push esi
push edi
lea esi,glCallListsInfo.jump[0]
mov edi,glCallListsInfo.location
movsd
movsb
pop edi
pop esi
end;
end;
procedure glCallList_Hook(list: TGLuint); stdcall;
begin
asm
push esi
push edi
lea esi,glCallListInfo.original[0]
mov edi,glCallListInfo.location
movsd
movsb
pop edi
pop esi
end;
TglCallList(glCallListInfo.location)(list);
asm
push esi
push edi
lea esi,glCallListInfo.jump[0]
mov edi,glCallListInfo.location
movsd
movsb
pop edi
pop esi
end;
end;
procedure glClear_hook(mask: TGLbitfield); stdcall;
begin
asm
push esi
push edi
lea esi,glClearInfo.original[0]
mov edi,glClearInfo.location
movsd
movsb
pop edi
pop esi
end;
TglClear(glClearInfo.location)(mask);
asm
push esi
push edi
lea esi,glClearInfo.jump[0]
mov edi,glClearInfo.location
movsd
movsb
pop edi
pop esi
end;
end;
procedure glEnable_Hook(cap: TGLEnum); stdcall;
begin
asm
push esi
push edi
lea esi,glEnableInfo.original[0]
mov edi,glEnableInfo.location
movsd
movsb
pop edi
pop esi
end;
TglEnable(glEnableInfo.location)(cap);
if lighting=1 then glDisable(GL_LIGHTING) else if lighting=0 then glEnable(GL_LIGHTING);
if textures=1 then glDisable(GL_TEXTURE_2D) else if textures=0 then glEnable(GL_TEXTURE_2D);
if depthtest=1 then
begin
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
end else if depthtest=0 then glEnable(GL_DEPTH_TEST);
if fog=1 then glDisable(GL_FOG) else if fog=0 then glEnable(GL_FOG);
asm
push esi
push edi
lea esi,glEnableInfo.jump[0]
mov edi,glEnableInfo.location
movsd
movsb
pop edi
pop esi
end;
end;
procedure glBegin_hook(mode: TGLEnum); stdcall;
begin
asm
push esi
push edi
lea esi,glBeginInfo.original[0]
mov edi,glBeginInfo.location
movsd
movsb
pop edi
pop esi
end;
//glPushMatrix;
glBegin(GL_QUADS);
glLoadIdentity;
glColor3b(120,90,50);
glVertex2d(0,0);
glVertex2d(100,0);
glVertex2d(100,100);
glVertex2d(0,100);
glEnd;
//glpopmatrix;
glColor3b(127,127,127);
TglBegin(glBeginInfo.location)(mode);
asm
push esi
push edi
lea esi,glBeginInfo.jump[0]
mov edi,glBeginInfo.location
movsd
movsb
pop edi
pop esi
end;
end;
procedure glEnd_hook; stdcall;
begin
asm
push esi
push edi
lea esi,glEndInfo.original[0]
mov edi,glEndInfo.location
movsd
movsb
pop edi
pop esi
end;
//do my stuff here
TglEnd(glEndInfo.location);
asm
push esi
push edi
lea esi,glEndInfo.jump[0]
mov edi,glEndInfo.location
movsd
movsb
pop edi
pop esi
end;
end;
procedure HookOpenGLFunctions;
var op:dword;
begin
opengl32dll:=Loadlibrary('opengl32.dll');
initopengl; //so other functions (not declared by me) will work too
if opengl32dll<>0 then
begin
//glbegin
glBeginInfo.location:=GetProcAddress(opengl32dll,'glBegin');
if VirtualProtect(glBeginInfo.location,5,PAGE_EXECUTE_READWRITE,op) then
begin
glBeginInfo.jump[0]:=$e9;
pdword(@glBeginInfo.jump[1])^:=dword(@glBegin_Hook)-dword(glBeginInfo.location)-5;
try
asm
push edi
push esi
lea edi,glBeginInfo.original[0]
mov esi,glBeginInfo.location
movsd
movsb
lea esi,glBeginInfo.jump[0]
mov edi,glBeginInfo.location
movsd
movsb
pop esi
pop edi
end;
except
end;
end;
//glEnd
glEndInfo.location:=GetProcAddress(opengl32dll,'glEnd');
if VirtualProtect(glEndInfo.location,5,PAGE_EXECUTE_READWRITE,op) then
begin
glEndInfo.jump[0]:=$e9;
pdword(@glEndInfo.jump[1])^:=dword(@glEnd_Hook)-dword(glEndInfo.location)-5;
try
asm
push edi
push esi
lea edi,glEndInfo.original[0]
mov esi,glEndInfo.location
movsd
movsb
lea esi,glEndInfo.jump[0]
mov edi,glEndInfo.location
movsd
movsb
pop esi
pop edi
end;
except
end;
end;
//glEnable
glEnableInfo.location:=GetProcAddress(opengl32dll,'glEnable');
if VirtualProtect(glEnableInfo.location,5,PAGE_EXECUTE_READWRITE,op) then
begin
glEnableInfo.jump[0]:=$e9;
pdword(@glEnableInfo.jump[1])^:=dword(@glEnable_Hook)-dword(glEnableInfo.location)-5;
try
asm
push edi
push esi
lea edi,glEnableInfo.original[0]
mov esi,glEnableInfo.location
movsd
movsb
lea esi,glEnableInfo.jump[0]
mov edi,glEnableInfo.location
movsd
movsb
pop esi
pop edi
end;
except
end;
end;
//glBindTexture
glBindTextureInfo.location:=GetProcAddress(opengl32dll,'glBindTexture');
if VirtualProtect(glBindTextureInfo.location,5,PAGE_EXECUTE_READWRITE,op) then
begin
glBindTextureInfo.jump[0]:=$e9;
pdword(@glBindTextureInfo.jump[1])^:=dword(@glBindTexture_Hook)-dword(glBindTextureInfo.location)-5;
try
asm
push edi
push esi
lea edi,glBindTextureInfo.original[0]
mov esi,glBindTextureInfo.location
movsd
movsb
lea esi,glBindTextureInfo.jump[0]
mov edi,glBindTextureInfo.location
movsd
movsb
pop esi
pop edi
end;
except
end;
end;
//glCallList
glCallListInfo.location:=GetProcAddress(opengl32dll,'glCallList');
if VirtualProtect(glCallListInfo.location,5,PAGE_EXECUTE_READWRITE,op) then
begin
glCallListInfo.jump[0]:=$e9;
pdword(@glCallListInfo.jump[1])^:=dword(@glCallList_Hook)-dword(glCallListInfo.location)-5;
try
asm
push edi
push esi
lea edi,glCallListInfo.original[0]
mov esi,glCallListInfo.location
movsd
movsb
lea esi,glCallListInfo.jump[0]
mov edi,glCallListInfo.location
movsd
movsb
pop esi
pop edi
end;
except
end;
end;
//glcalllists
glCallListsInfo.location:=GetProcAddress(opengl32dll,'glCallLists');
if VirtualProtect(glCallListsInfo.location,5,PAGE_EXECUTE_READWRITE,op) then
begin
glCallListsInfo.jump[0]:=$e9;
pdword(@glCallListsInfo.jump[1])^:=dword(@glCallLists_Hook)-dword(glCallListsInfo.location)-5;
try
asm
push edi
push esi
lea edi,glCallListsInfo.original[0]
mov esi,glCallListsInfo.location
movsd
movsb
lea esi,glCallListsInfo.jump[0]
mov edi,glCallListsInfo.location
movsd
movsb
pop esi
pop edi
end;
except
end;
end;
//glClear
glClearInfo.location:=GetProcAddress(opengl32dll,'glClear');
if VirtualProtect(glClearInfo.location,5,PAGE_EXECUTE_READWRITE,op) then
begin
glClearInfo.jump[0]:=$e9;
pdword(@glClearInfo.jump[1])^:=dword(@glClear_Hook)-dword(glClearInfo.location)-5;
try
asm
push edi
push esi
lea edi,glClearInfo.original[0]
mov esi,glClearInfo.location
movsd
movsb
lea esi,glClearInfo.jump[0]
mov edi,glClearInfo.location
movsd
movsb
pop esi
pop edi
end;
except
end;
end;
end;
Hookedopengl:=true;
outputdebugstring('hooked opengl');
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -