mirror of
https://gitlab.com/Mr_Goldberg/goldberg_emulator
synced 2025-07-07 15:04:28 +08:00
Update Nemirtingas overlay to latest.
This commit is contained in:
@ -1,151 +1,164 @@
|
||||
/*
|
||||
* 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>
|
||||
|
||||
#include "OpenGLX_Hook.h"
|
||||
#include "X11_Hook.h"
|
||||
#include "../Renderer_Detector.h"
|
||||
#include "../dll/dll.h"
|
||||
|
||||
#ifdef __LINUX__
|
||||
#ifdef EMU_OVERLAY
|
||||
|
||||
#include <imgui.h>
|
||||
#include <impls/imgui_impl_opengl3.h>
|
||||
|
||||
#include "../steam_overlay.h"
|
||||
#include <backends/imgui_impl_opengl3.h>
|
||||
|
||||
OpenGLX_Hook* OpenGLX_Hook::_inst = nullptr;
|
||||
|
||||
bool OpenGLX_Hook::start_hook()
|
||||
constexpr decltype(OpenGLX_Hook::DLL_NAME) OpenGLX_Hook::DLL_NAME;
|
||||
|
||||
bool OpenGLX_Hook::StartHook(std::function<bool(bool)> key_combination_callback)
|
||||
{
|
||||
bool res = true;
|
||||
if (!hooked)
|
||||
if (!_Hooked)
|
||||
{
|
||||
if (!X11_Hook::Inst()->start_hook())
|
||||
if (glXSwapBuffers == nullptr)
|
||||
{
|
||||
SPDLOG_WARN("Failed to hook OpenGLX: Rendering functions missing.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!X11_Hook::Inst()->StartHook(key_combination_callback))
|
||||
return false;
|
||||
|
||||
GLenum err = glewInit();
|
||||
_X11Hooked = true;
|
||||
|
||||
if (err == GLEW_OK)
|
||||
{
|
||||
PRINT_DEBUG("Hooked OpenGLX\n");
|
||||
SPDLOG_INFO("Hooked OpenGLX");
|
||||
|
||||
hooked = true;
|
||||
Renderer_Detector::Inst().renderer_found(this);
|
||||
_Hooked = true;
|
||||
|
||||
/*
|
||||
UnhookAll();
|
||||
BeginHook();
|
||||
HookFuncs(
|
||||
std::make_pair<void**, void*>(&(void*&)_glXSwapBuffers, (void*)&OpenGLX_Hook::MyglXSwapBuffers)
|
||||
);
|
||||
EndHook();
|
||||
*/
|
||||
|
||||
get_steam_client()->steam_overlay->HookReady();
|
||||
}
|
||||
else
|
||||
{
|
||||
PRINT_DEBUG("Failed to hook OpenGLX\n");
|
||||
/* Problem: glewInit failed, something is seriously wrong. */
|
||||
PRINT_DEBUG("Error: %s\n", glewGetErrorString(err));
|
||||
res = false;
|
||||
}
|
||||
UnhookAll();
|
||||
BeginHook();
|
||||
HookFuncs(
|
||||
std::make_pair<void**, void*>((void**)&glXSwapBuffers, (void*)&OpenGLX_Hook::MyglXSwapBuffers)
|
||||
);
|
||||
EndHook();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void OpenGLX_Hook::resetRenderState()
|
||||
bool OpenGLX_Hook::IsStarted()
|
||||
{
|
||||
if (initialized)
|
||||
return _Hooked;
|
||||
}
|
||||
|
||||
void OpenGLX_Hook::_ResetRenderState()
|
||||
{
|
||||
if (_Initialized)
|
||||
{
|
||||
OverlayHookReady(false);
|
||||
|
||||
ImGui_ImplOpenGL3_Shutdown();
|
||||
X11_Hook::Inst()->resetRenderState();
|
||||
X11_Hook::Inst()->ResetRenderState();
|
||||
ImGui::DestroyContext();
|
||||
|
||||
glXDestroyContext(display, context);
|
||||
display = nullptr;
|
||||
initialized = false;
|
||||
glXDestroyContext(_Display, _Context);
|
||||
_Display = nullptr;
|
||||
_Initialized = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Try to make this function and overlay's proc as short as possible or it might affect game's fps.
|
||||
void OpenGLX_Hook::prepareForOverlay(Display* display, GLXDrawable drawable)
|
||||
void OpenGLX_Hook::_PrepareForOverlay(Display* display, GLXDrawable drawable)
|
||||
{
|
||||
PRINT_DEBUG("Called SwapBuffer hook");
|
||||
|
||||
if( (Window)drawable != X11_Hook::Inst()->get_game_wnd() )
|
||||
X11_Hook::Inst()->resetRenderState();
|
||||
|
||||
if( ! initialized )
|
||||
if( !_Initialized )
|
||||
{
|
||||
ImGui::CreateContext();
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
io.IniFilename = NULL;
|
||||
|
||||
ImGui_ImplOpenGL3_Init();
|
||||
|
||||
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
|
||||
};
|
||||
//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;
|
||||
|
||||
XVisualInfo* visual_info = glXChooseVisual(display, DefaultScreen(display), attributes);
|
||||
_Display = display;
|
||||
|
||||
context = glXCreateContext(display, visual_info, nullptr, True);
|
||||
this->display = display;
|
||||
|
||||
get_steam_client()->steam_overlay->CreateFonts();
|
||||
|
||||
initialized = true;
|
||||
_Initialized = true;
|
||||
OverlayHookReady(true);
|
||||
}
|
||||
|
||||
auto oldContext = glXGetCurrentContext();
|
||||
//auto oldContext = glXGetCurrentContext();
|
||||
|
||||
glXMakeCurrent(display, drawable, context);
|
||||
//glXMakeCurrent(_Display, drawable, _Context);
|
||||
|
||||
ImGui_ImplOpenGL3_NewFrame();
|
||||
X11_Hook::Inst()->prepareForOverlay(display, (Window)drawable);
|
||||
if (ImGui_ImplOpenGL3_NewFrame() && X11_Hook::Inst()->PrepareForOverlay(_Display, (Window)drawable))
|
||||
{
|
||||
ImGui::NewFrame();
|
||||
|
||||
ImGui::NewFrame();
|
||||
OverlayProc();
|
||||
|
||||
get_steam_client()->steam_overlay->OverlayProc();
|
||||
ImGui::Render();
|
||||
|
||||
ImGui::Render();
|
||||
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
||||
}
|
||||
|
||||
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
||||
|
||||
glXMakeCurrent(display, drawable, oldContext);
|
||||
//glXMakeCurrent(_Display, drawable, oldContext);
|
||||
}
|
||||
|
||||
void OpenGLX_Hook::MyglXSwapBuffers(Display* display, GLXDrawable drawable)
|
||||
{
|
||||
OpenGLX_Hook::Inst()->prepareForOverlay(display, drawable);
|
||||
OpenGLX_Hook::Inst()->_glXSwapBuffers(display, drawable);
|
||||
OpenGLX_Hook::Inst()->_PrepareForOverlay(display, drawable);
|
||||
OpenGLX_Hook::Inst()->glXSwapBuffers(display, drawable);
|
||||
}
|
||||
|
||||
OpenGLX_Hook::OpenGLX_Hook():
|
||||
initialized(false),
|
||||
hooked(false),
|
||||
_glXSwapBuffers(nullptr)
|
||||
_Initialized(false),
|
||||
_Hooked(false),
|
||||
_X11Hooked(false),
|
||||
glXSwapBuffers(nullptr)
|
||||
{
|
||||
//_library = dlopen(DLL_NAME);
|
||||
}
|
||||
|
||||
OpenGLX_Hook::~OpenGLX_Hook()
|
||||
{
|
||||
PRINT_DEBUG("OpenGLX Hook removed\n");
|
||||
SPDLOG_INFO("OpenGLX Hook removed");
|
||||
|
||||
if (initialized)
|
||||
if (_X11Hooked)
|
||||
delete X11_Hook::Inst();
|
||||
|
||||
if (_Initialized)
|
||||
{
|
||||
ImGui_ImplOpenGL3_Shutdown();
|
||||
ImGui::DestroyContext();
|
||||
glXDestroyContext(display, context);
|
||||
glXDestroyContext(_Display, _Context);
|
||||
}
|
||||
|
||||
//dlclose(_library);
|
||||
@ -161,15 +174,62 @@ OpenGLX_Hook* OpenGLX_Hook::Inst()
|
||||
return _inst;
|
||||
}
|
||||
|
||||
const char* OpenGLX_Hook::get_lib_name() const
|
||||
std::string OpenGLX_Hook::GetLibraryName() const
|
||||
{
|
||||
return DLL_NAME;
|
||||
return LibraryName;
|
||||
}
|
||||
|
||||
void OpenGLX_Hook::loadFunctions(decltype(glXSwapBuffers)* pfnglXSwapBuffers)
|
||||
void OpenGLX_Hook::LoadFunctions(decltype(::glXSwapBuffers)* pfnglXSwapBuffers)
|
||||
{
|
||||
_glXSwapBuffers = pfnglXSwapBuffers;
|
||||
glXSwapBuffers = pfnglXSwapBuffers;
|
||||
}
|
||||
|
||||
#endif//EMU_OVERLAY
|
||||
#endif//__LINUX__
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user