Added opengl hook

This commit is contained in:
Nemirtingas 2019-07-31 22:19:44 +02:00
parent a60a106daf
commit 6328b59b0b
5 changed files with 879 additions and 18 deletions

View file

@ -7,6 +7,7 @@
#include "DX11_Hook.h"
#include "DX10_Hook.h"
#include "DX9_Hook.h"
#include "OpenGL_Hook.h"
#include <algorithm>
@ -17,26 +18,30 @@ decltype(LoadLibraryExW )* _LoadLibraryExW = LoadLibraryExW;
void create_hookA(const char* libname)
{
if (!strcmp(libname, "d3d9.dll"))
if (!_stricmp(libname, "d3d9.dll"))
DX9_Hook::Create();
else if (!strcmp(libname, "d3d10.dll"))
else if (!_stricmp(libname, "d3d10.dll"))
DX10_Hook::Create();
else if (!strcmp(libname, "d3d11.dll"))
else if (!_stricmp(libname, "d3d11.dll"))
DX11_Hook::Create();
else if (!strcmp(libname, "d3d12.dll"))
else if (!_stricmp(libname, "d3d12.dll"))
DX12_Hook::Create();
else if (!_stricmp(libname, "opengl32.dll"))
OpenGL_Hook::Create();
}
void create_hookW(const wchar_t *libname)
{
if (!wcscmp(libname, L"d3d9.dll"))
if (!_wcsicmp(libname, L"d3d9.dll"))
DX9_Hook::Create();
else if (!wcscmp(libname, L"d3d10.dll"))
else if (!_wcsicmp(libname, L"d3d10.dll"))
DX10_Hook::Create();
else if (!wcscmp(libname, L"d3d11.dll"))
else if (!_wcsicmp(libname, L"d3d11.dll"))
DX11_Hook::Create();
else if (!wcscmp(libname, L"d3d12.dll"))
else if (!_wcsicmp(libname, L"d3d12.dll"))
DX12_Hook::Create();
else if (!_wcsicmp(libname, L"opengl32.dll"))
OpenGL_Hook::Create();
}
HMODULE WINAPI mLoadLibraryA(LPCTSTR lpLibFileName)
@ -88,7 +93,7 @@ void Hook_Manager::HookRenderer(Steam_Overlay *ovlay)
{
overlay = ovlay;
HookLoadLibrary();
std::vector<std::string> const libraries = { "d3d12.dll", "d3d11.dll", "d3d10.dll", "d3d9.dll" };
std::vector<std::string> const libraries = { "opengl32.dll", "d3d12.dll", "d3d11.dll", "d3d10.dll", "d3d9.dll" };
std::vector<std::string>::const_iterator it = libraries.begin();
while (it != libraries.end())
{
@ -102,15 +107,7 @@ void Hook_Manager::HookRenderer(Steam_Overlay *ovlay)
if (it == libraries.end())
break;
if (*it == "d3d9.dll")
DX9_Hook::Create();
else if (*it == "d3d10.dll")
DX10_Hook::Create();
else if (*it == "d3d11.dll")
DX11_Hook::Create();
else if (*it == "d3d12.dll")
DX12_Hook::Create();
create_hookA(it->c_str());
++it;
}
}

View file

@ -0,0 +1,144 @@
#include "../dll/base.h"
#include "OpenGL_Hook.h"
#include "Hook_Manager.h"
#include <imgui.h>
#include <impls/imgui_impl_win32.h>
#include <impls/imgui_impl_opengl3.h>
#include <GL/glew.h>
#pragma comment(lib, "opengl32")
#pragma comment(lib, "glew32s")
// This is created by OpenGL_Hook::Create, and deleted by the Hook_Manager if not used
static OpenGL_Hook* hook;
void OpenGL_Hook::hook_ogl()
{
if (!_hooked)
{
PRINT_DEBUG("Hooked OpenGL\n");
_hooked = true;
Hook_Manager::Inst().FoundHook(this);
GLenum err = glewInit();
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
PRINT_DEBUG("Error: %s\n", glewGetErrorString(err));
}
UnhookAll();
BeginHook();
HookFuncs(
std::make_pair<void**, void*>(&(PVOID&)wglSwapBuffers, &OpenGL_Hook::MywglSwapBuffers)
);
EndHook();
}
}
void OpenGL_Hook::resetRenderState()
{
if (initialized)
{
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplWin32_Shutdown();
ImGui::DestroyContext();
initialized = false;
}
}
void OpenGL_Hook::prepareForOverlay(HDC hDC)
{
HWND hWnd = WindowFromDC(hDC);
RECT rect;
GetClientRect(hWnd, &rect);
if (!initialized)
{
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.IniFilename = NULL;
Hook_Manager::Inst().ChangeGameWindow(hWnd);
ImGui_ImplWin32_Init(hWnd);
ImGui_ImplOpenGL3_Init();
initialized = true;
}
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
Hook_Manager::Inst().CallOverlayProc(rect.right, rect.bottom);
ImGui::EndFrame();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
/////////////////////////////////////////////////////////////////////////////////////
// OpenGL Initialization functions
BOOL WINAPI OpenGL_Hook::MywglMakeCurrent(HDC hDC, HGLRC hGLRC)
{
auto res = hook->wglMakeCurrent(hDC, hGLRC);
hook->hook_ogl();
return res;
}
/////////////////////////////////////////////////////////////////////////////////////
BOOL WINAPI OpenGL_Hook::MywglSwapBuffers(HDC hDC)
{
hook->prepareForOverlay(hDC);
return hook->wglSwapBuffers(hDC);
}
OpenGL_Hook::OpenGL_Hook():
initialized(false),
wglSwapBuffers(nullptr)
{
_dll = GetModuleHandle(DLL_NAME);
_hooked = false;
// Hook to wglMakeCurrent so we know when it gets called.
// If its called, then OpenGL will be used to render the overlay.
wglMakeCurrent = (decltype(wglMakeCurrent))GetProcAddress(_dll, "wglMakeCurrent");
wglSwapBuffers = (decltype(wglSwapBuffers))GetProcAddress(_dll, "wglSwapBuffers");
BeginHook();
HookFuncs(
std::make_pair<void**, void*>(&(PVOID&)wglMakeCurrent, &OpenGL_Hook::MywglMakeCurrent)
);
EndHook();
}
OpenGL_Hook::~OpenGL_Hook()
{
PRINT_DEBUG("OpenGL Hook removed\n");
if (_hooked)
{
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplWin32_Shutdown();
ImGui::DestroyContext();
}
hook = nullptr;
}
void OpenGL_Hook::Create()
{
if (hook == nullptr)
{
hook = new OpenGL_Hook;
// Register the hook to the Hook Manager
Hook_Manager::Inst().AddHook(hook);
}
}

View file

@ -0,0 +1,41 @@
#ifndef __INCLUDED_OPENGL_HOOK_H__
#define __INCLUDED_OPENGL_HOOK_H__
#include "DirectX_VTables.h"
#include "Base_Hook.h"
class OpenGL_Hook : public Base_Hook
{
public:
static constexpr const char DLL_NAME[] = "opengl32.dll";
using wglSwapBuffers_t = BOOL(WINAPI)(HDC);
using wglMakeCurrent_t = BOOL(WINAPI)(HDC, HGLRC);
private:
// Variables
bool initialized;
// Functions
OpenGL_Hook();
virtual ~OpenGL_Hook();
void hook_ogl();
void resetRenderState();
void prepareForOverlay(HDC hDC);
// Hook to render functions
static BOOL WINAPI MywglSwapBuffers(HDC hDC);
wglSwapBuffers_t* wglSwapBuffers;
// Hook functions so we know we use OGL
static BOOL WINAPI MywglMakeCurrent(HDC hDC, HGLRC hGLRC);
wglMakeCurrent_t* wglMakeCurrent;
public:
static void Create(); // Initialize OGL Hook.
};
#endif//__INCLUDED_OPENGL_HOOK_H__