2019-07-31 20:19:44 +00:00
|
|
|
#include "OpenGL_Hook.h"
|
2019-08-18 14:22:07 +00:00
|
|
|
#include "Windows_Hook.h"
|
2019-09-01 18:53:16 +00:00
|
|
|
#include "../Renderer_Detector.h"
|
|
|
|
#include "../../dll/dll.h"
|
2019-08-18 14:22:07 +00:00
|
|
|
|
2020-01-19 17:55:14 +00:00
|
|
|
#ifdef EMU_OVERLAY
|
2019-07-31 20:19:44 +00:00
|
|
|
|
|
|
|
#include <imgui.h>
|
|
|
|
#include <impls/imgui_impl_opengl3.h>
|
|
|
|
|
|
|
|
#include <GL/glew.h>
|
|
|
|
|
2019-09-01 18:53:16 +00:00
|
|
|
#include "../steam_overlay.h"
|
2019-07-31 20:19:44 +00:00
|
|
|
|
2019-08-16 17:10:12 +00:00
|
|
|
OpenGL_Hook* OpenGL_Hook::_inst = nullptr;
|
2019-07-31 20:19:44 +00:00
|
|
|
|
2019-08-16 17:10:12 +00:00
|
|
|
bool OpenGL_Hook::start_hook()
|
2019-07-31 20:19:44 +00:00
|
|
|
{
|
2019-08-25 19:22:25 +00:00
|
|
|
bool res = true;
|
2019-08-27 13:38:07 +00:00
|
|
|
if (!hooked)
|
2019-07-31 20:19:44 +00:00
|
|
|
{
|
2019-08-27 13:38:07 +00:00
|
|
|
if (!Windows_Hook::Inst()->start_hook())
|
2019-08-18 15:12:57 +00:00
|
|
|
return false;
|
2019-07-31 20:19:44 +00:00
|
|
|
|
|
|
|
GLenum err = glewInit();
|
2019-08-02 07:07:53 +00:00
|
|
|
|
|
|
|
if (err == GLEW_OK)
|
|
|
|
{
|
|
|
|
PRINT_DEBUG("Hooked OpenGL\n");
|
2019-08-18 15:12:57 +00:00
|
|
|
|
2019-08-27 13:38:07 +00:00
|
|
|
hooked = true;
|
|
|
|
Renderer_Detector::Inst().renderer_found(this);
|
2019-08-18 15:12:57 +00:00
|
|
|
|
2019-08-02 07:07:53 +00:00
|
|
|
UnhookAll();
|
|
|
|
BeginHook();
|
|
|
|
HookFuncs(
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)wglSwapBuffers, &OpenGL_Hook::MywglSwapBuffers)
|
|
|
|
);
|
|
|
|
EndHook();
|
2019-08-18 14:22:07 +00:00
|
|
|
|
2019-08-18 15:12:57 +00:00
|
|
|
get_steam_client()->steam_overlay->HookReady();
|
2019-08-02 07:07:53 +00:00
|
|
|
}
|
|
|
|
else
|
2019-07-31 20:19:44 +00:00
|
|
|
{
|
2019-08-02 07:07:53 +00:00
|
|
|
PRINT_DEBUG("Failed to hook OpenGL\n");
|
2019-07-31 20:19:44 +00:00
|
|
|
/* Problem: glewInit failed, something is seriously wrong. */
|
|
|
|
PRINT_DEBUG("Error: %s\n", glewGetErrorString(err));
|
2019-08-25 19:22:25 +00:00
|
|
|
res = false;
|
2019-07-31 20:19:44 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-16 17:10:12 +00:00
|
|
|
return true;
|
2019-07-31 20:19:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGL_Hook::resetRenderState()
|
|
|
|
{
|
|
|
|
if (initialized)
|
|
|
|
{
|
|
|
|
ImGui_ImplOpenGL3_Shutdown();
|
2019-08-27 13:38:07 +00:00
|
|
|
Windows_Hook::Inst()->resetRenderState();
|
2019-07-31 20:19:44 +00:00
|
|
|
ImGui::DestroyContext();
|
|
|
|
|
|
|
|
initialized = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-14 12:55:31 +00:00
|
|
|
// Try to make this function and overlay's proc as short as possible or it might affect game's fps.
|
2019-07-31 20:19:44 +00:00
|
|
|
void OpenGL_Hook::prepareForOverlay(HDC hDC)
|
|
|
|
{
|
|
|
|
HWND hWnd = WindowFromDC(hDC);
|
|
|
|
|
2019-08-27 13:38:07 +00:00
|
|
|
if (hWnd != Windows_Hook::Inst()->GetGameHwnd())
|
2019-08-16 08:36:44 +00:00
|
|
|
resetRenderState();
|
|
|
|
|
2019-07-31 20:19:44 +00:00
|
|
|
if (!initialized)
|
|
|
|
{
|
|
|
|
ImGui::CreateContext();
|
|
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
|
|
io.IniFilename = NULL;
|
|
|
|
|
|
|
|
ImGui_ImplOpenGL3_Init();
|
|
|
|
|
2019-09-05 07:00:02 +00:00
|
|
|
get_steam_client()->steam_overlay->CreateFonts();
|
|
|
|
|
2019-07-31 20:19:44 +00:00
|
|
|
initialized = true;
|
|
|
|
}
|
2021-01-18 03:58:42 +00:00
|
|
|
|
|
|
|
ImGui_ImplOpenGL3_NewFrame();
|
|
|
|
|
2020-01-10 07:10:33 +00:00
|
|
|
{
|
|
|
|
Windows_Hook::Inst()->prepareForOverlay(hWnd);
|
2019-07-31 20:19:44 +00:00
|
|
|
|
2020-01-10 07:10:33 +00:00
|
|
|
ImGui::NewFrame();
|
2019-07-31 20:19:44 +00:00
|
|
|
|
2020-01-10 07:10:33 +00:00
|
|
|
get_steam_client()->steam_overlay->OverlayProc();
|
2019-07-31 20:19:44 +00:00
|
|
|
|
2020-01-10 07:10:33 +00:00
|
|
|
ImGui::Render();
|
2019-07-31 20:19:44 +00:00
|
|
|
|
2020-01-10 07:10:33 +00:00
|
|
|
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
|
|
|
}
|
2019-07-31 20:19:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI OpenGL_Hook::MywglSwapBuffers(HDC hDC)
|
|
|
|
{
|
2019-08-16 17:10:12 +00:00
|
|
|
OpenGL_Hook::Inst()->prepareForOverlay(hDC);
|
|
|
|
return OpenGL_Hook::Inst()->wglSwapBuffers(hDC);
|
2019-07-31 20:19:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OpenGL_Hook::OpenGL_Hook():
|
|
|
|
initialized(false),
|
2019-08-27 13:38:07 +00:00
|
|
|
hooked(false),
|
2019-07-31 20:19:44 +00:00
|
|
|
wglSwapBuffers(nullptr)
|
|
|
|
{
|
2020-07-16 09:56:24 +00:00
|
|
|
_library = LoadLibrary(OPENGL_DLL);
|
2019-07-31 20:19:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OpenGL_Hook::~OpenGL_Hook()
|
|
|
|
{
|
|
|
|
PRINT_DEBUG("OpenGL Hook removed\n");
|
|
|
|
|
2019-08-27 13:38:07 +00:00
|
|
|
if (initialized)
|
|
|
|
{
|
|
|
|
ImGui_ImplOpenGL3_Shutdown();
|
|
|
|
ImGui::DestroyContext();
|
|
|
|
}
|
2019-08-18 14:19:28 +00:00
|
|
|
|
|
|
|
FreeLibrary(reinterpret_cast<HMODULE>(_library));
|
2019-07-31 20:19:44 +00:00
|
|
|
|
2019-08-16 17:10:12 +00:00
|
|
|
_inst = nullptr;
|
2019-07-31 20:19:44 +00:00
|
|
|
}
|
|
|
|
|
2019-08-16 17:10:12 +00:00
|
|
|
OpenGL_Hook* OpenGL_Hook::Inst()
|
2019-07-31 20:19:44 +00:00
|
|
|
{
|
2019-08-16 17:10:12 +00:00
|
|
|
if (_inst == nullptr)
|
|
|
|
_inst = new OpenGL_Hook;
|
|
|
|
|
|
|
|
return _inst;
|
2019-08-14 12:55:31 +00:00
|
|
|
}
|
|
|
|
|
2019-08-26 14:38:01 +00:00
|
|
|
const char* OpenGL_Hook::get_lib_name() const
|
|
|
|
{
|
2020-07-16 09:56:24 +00:00
|
|
|
return OPENGL_DLL;
|
2019-08-26 14:38:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 13:38:07 +00:00
|
|
|
void OpenGL_Hook::loadFunctions(wglSwapBuffers_t pfnwglSwapBuffers)
|
|
|
|
{
|
|
|
|
wglSwapBuffers = pfnwglSwapBuffers;
|
|
|
|
}
|
|
|
|
|
2020-01-19 17:55:14 +00:00
|
|
|
#endif//EMU_OVERLAY
|