238 lines
6.2 KiB
C++
Raw Normal View History

2022-08-05 02:06:42 -04:00
/*
* Copyright (C) 2019-2020 Nemirtingas
* This file is part of the ingame overlay project
*
* The ingame overlay project is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* The ingame overlay project is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with the ingame overlay project; if not, see
* <http://www.gnu.org/licenses/>.
*/
#include <glad/gl.h>
2019-09-01 20:53:16 +02:00
#include "OpenGLX_Hook.h"
#include "X11_Hook.h"
#include <imgui.h>
2022-08-05 02:06:42 -04:00
#include <backends/imgui_impl_opengl3.h>
2019-09-01 20:53:16 +02:00
OpenGLX_Hook* OpenGLX_Hook::_inst = nullptr;
2022-08-05 02:06:42 -04:00
constexpr decltype(OpenGLX_Hook::DLL_NAME) OpenGLX_Hook::DLL_NAME;
2022-08-13 14:32:43 -04:00
bool OpenGLX_Hook::StartHook(std::function<bool(bool)> key_combination_callback, std::set<ingame_overlay::ToggleKey> toggle_keys)
2019-09-01 20:53:16 +02:00
{
2022-08-05 02:06:42 -04:00
if (!_Hooked)
2019-09-01 20:53:16 +02:00
{
2022-08-05 02:06:42 -04:00
if (glXSwapBuffers == nullptr)
{
SPDLOG_WARN("Failed to hook OpenGLX: Rendering functions missing.");
2019-09-01 20:53:16 +02:00
return false;
2022-08-05 02:06:42 -04:00
}
2019-09-01 20:53:16 +02:00
2022-08-13 14:32:43 -04:00
if (!X11_Hook::Inst()->StartHook(key_combination_callback, toggle_keys))
2022-08-05 02:06:42 -04:00
return false;
2019-09-01 20:53:16 +02:00
2022-08-05 02:06:42 -04:00
_X11Hooked = true;
2019-09-01 20:53:16 +02:00
2022-08-05 02:06:42 -04:00
SPDLOG_INFO("Hooked OpenGLX");
2019-09-01 20:53:16 +02:00
2022-08-05 02:06:42 -04:00
_Hooked = true;
2019-09-01 20:53:16 +02:00
2022-08-05 02:06:42 -04:00
UnhookAll();
BeginHook();
HookFuncs(
std::make_pair<void**, void*>((void**)&glXSwapBuffers, (void*)&OpenGLX_Hook::MyglXSwapBuffers)
);
EndHook();
2019-09-01 20:53:16 +02:00
}
return true;
}
2022-08-05 02:06:42 -04:00
bool OpenGLX_Hook::IsStarted()
2019-09-01 20:53:16 +02:00
{
2022-08-05 02:06:42 -04:00
return _Hooked;
}
void OpenGLX_Hook::_ResetRenderState()
{
if (_Initialized)
2019-09-01 20:53:16 +02:00
{
2022-08-05 02:06:42 -04:00
OverlayHookReady(false);
2019-09-01 20:53:16 +02:00
ImGui_ImplOpenGL3_Shutdown();
2022-08-05 02:06:42 -04:00
X11_Hook::Inst()->ResetRenderState();
2019-09-01 20:53:16 +02:00
ImGui::DestroyContext();
2022-08-05 02:06:42 -04:00
glXDestroyContext(_Display, _Context);
_Display = nullptr;
_Initialized = false;
2019-09-01 20:53:16 +02:00
}
}
// Try to make this function and overlay's proc as short as possible or it might affect game's fps.
2022-08-05 02:06:42 -04:00
void OpenGLX_Hook::_PrepareForOverlay(Display* display, GLXDrawable drawable)
2019-09-01 20:53:16 +02:00
{
2022-08-05 02:06:42 -04:00
if( !_Initialized )
2019-09-01 20:53:16 +02:00
{
ImGui::CreateContext();
ImGui_ImplOpenGL3_Init();
2022-08-05 02:06:42 -04:00
//int attributes[] = { //can't be const b/c X11 doesn't like it. Not sure if that's intentional or just stupid.
// GLX_RGBA, //apparently nothing comes after this?
// GLX_RED_SIZE, 8,
// GLX_GREEN_SIZE, 8,
// GLX_BLUE_SIZE, 8,
// GLX_ALPHA_SIZE, 8,
// //Ideally, the size would be 32 (or at least 24), but I have actually seen
// // this size (on a modern OS even).
// GLX_DEPTH_SIZE, 16,
// GLX_DOUBLEBUFFER, True,
// None
//};
//
//XVisualInfo* visual_info = glXChooseVisual(_Display, DefaultScreen(_Display), attributes);
//if (visual_info == nullptr)
// return;
//
//_Context = glXCreateContext(_Display, visual_info, nullptr, True);
//if (_Context == nullptr)
// return;
_Display = display;
2022-08-10 03:22:23 -04:00
X11_Hook::Inst()->SetInitialWindowSize(_Display, (Window)drawable);
2022-08-05 02:06:42 -04:00
_Initialized = true;
OverlayHookReady(true);
2019-09-01 20:53:16 +02:00
}
2022-08-05 02:06:42 -04:00
//auto oldContext = glXGetCurrentContext();
2019-09-01 20:53:16 +02:00
2022-08-05 02:06:42 -04:00
//glXMakeCurrent(_Display, drawable, _Context);
2019-09-01 20:53:16 +02:00
2022-08-05 02:06:42 -04:00
if (ImGui_ImplOpenGL3_NewFrame() && X11_Hook::Inst()->PrepareForOverlay(_Display, (Window)drawable))
{
ImGui::NewFrame();
2019-09-01 20:53:16 +02:00
2022-08-05 02:06:42 -04:00
OverlayProc();
2022-08-05 02:06:42 -04:00
ImGui::Render();
2022-08-05 02:06:42 -04:00
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
2022-08-05 02:06:42 -04:00
//glXMakeCurrent(_Display, drawable, oldContext);
2019-09-01 20:53:16 +02:00
}
void OpenGLX_Hook::MyglXSwapBuffers(Display* display, GLXDrawable drawable)
{
2022-08-05 02:06:42 -04:00
OpenGLX_Hook::Inst()->_PrepareForOverlay(display, drawable);
OpenGLX_Hook::Inst()->glXSwapBuffers(display, drawable);
2019-09-01 20:53:16 +02:00
}
OpenGLX_Hook::OpenGLX_Hook():
2022-08-05 02:06:42 -04:00
_Initialized(false),
_Hooked(false),
_X11Hooked(false),
glXSwapBuffers(nullptr)
2019-09-01 20:53:16 +02:00
{
//_library = dlopen(DLL_NAME);
}
OpenGLX_Hook::~OpenGLX_Hook()
{
2022-08-05 02:06:42 -04:00
SPDLOG_INFO("OpenGLX Hook removed");
2019-09-01 20:53:16 +02:00
2022-08-05 02:06:42 -04:00
if (_X11Hooked)
delete X11_Hook::Inst();
if (_Initialized)
2019-09-01 20:53:16 +02:00
{
ImGui_ImplOpenGL3_Shutdown();
ImGui::DestroyContext();
2022-08-05 02:06:42 -04:00
glXDestroyContext(_Display, _Context);
2019-09-01 20:53:16 +02:00
}
//dlclose(_library);
_inst = nullptr;
}
OpenGLX_Hook* OpenGLX_Hook::Inst()
{
if (_inst == nullptr)
_inst = new OpenGLX_Hook;
return _inst;
}
2022-08-05 02:06:42 -04:00
std::string OpenGLX_Hook::GetLibraryName() const
2019-09-01 20:53:16 +02:00
{
2022-08-05 02:06:42 -04:00
return LibraryName;
2019-09-01 20:53:16 +02:00
}
2022-08-05 02:06:42 -04:00
void OpenGLX_Hook::LoadFunctions(decltype(::glXSwapBuffers)* pfnglXSwapBuffers)
2019-09-01 20:53:16 +02:00
{
2022-08-05 02:06:42 -04:00
glXSwapBuffers = pfnglXSwapBuffers;
}
std::weak_ptr<uint64_t> OpenGLX_Hook::CreateImageResource(const void* image_data, uint32_t width, uint32_t height)
{
GLuint* texture = new GLuint(0);
glGenTextures(1, texture);
if (glGetError() != GL_NO_ERROR)
{
delete texture;
return std::shared_ptr<uint64_t>(nullptr);
}
// Save old texture id
GLint oldTex;
glGetIntegerv(GL_TEXTURE_BINDING_2D, &oldTex);
glBindTexture(GL_TEXTURE_2D, *texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Upload pixels into texture
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data);
glBindTexture(GL_TEXTURE_2D, oldTex);
auto ptr = std::shared_ptr<uint64_t>((uint64_t*)texture, [](uint64_t* handle)
{
if (handle != nullptr)
{
GLuint* texture = (GLuint*)handle;
glDeleteTextures(1, texture);
delete texture;
}
});
_ImageResources.emplace(ptr);
return ptr;
2019-09-01 20:53:16 +02:00
}
2022-08-05 02:06:42 -04:00
void OpenGLX_Hook::ReleaseImageResource(std::weak_ptr<uint64_t> resource)
{
auto ptr = resource.lock();
if (ptr)
{
auto it = _ImageResources.find(ptr);
if (it != _ImageResources.end())
_ImageResources.erase(it);
}
}