Update nemirtingas overlay to latest.

This commit is contained in:
Mr_Goldberg 2022-08-10 03:22:23 -04:00
parent 1ad2818e28
commit 04022c005f
No known key found for this signature in database
GPG key ID: 8597D87419DEF278
15 changed files with 187 additions and 95 deletions

View file

@ -25,6 +25,7 @@
#include "System/String.hpp"
#include "System/System.h"
#include "System/Library.h"
#include "System/ScopedLock.hpp"
#if defined(WIN64) || defined(_WIN64) || defined(__MINGW64__) \
|| defined(WIN32) || defined(_WIN32) || defined(__MINGW32__)
@ -77,10 +78,6 @@ public:
delete vulkan_hook;
}
bool force_stop_detection;
std::condition_variable detect_renderer_thread_cv;
std::mutex destroy_render_thread_mutex;
private:
Renderer_Detector():
dxgi_hooked(false),
@ -98,8 +95,7 @@ private:
dx12_hook(nullptr),
opengl_hook(nullptr),
vulkan_hook(nullptr),
detection_done(false),
force_stop_detection(false)
detection_done(false)
{
std::wstring tmp(4096, L'\0');
tmp.resize(GetSystemDirectoryW(&tmp[0], tmp.size()));
@ -147,6 +143,8 @@ private:
Vulkan_Hook* vulkan_hook;
bool detection_done;
std::condition_variable stop_detection_cv;
std::mutex stop_detection_mutex;
HWND dummyWindow = nullptr;
std::wstring _WindowClassName;
@ -431,14 +429,14 @@ private:
}
void HookDX9Present(IDirect3DDevice9* pDevice, bool ex, IDirect3DSwapChain9* pSwapChain,
decltype(&IDirect3DDevice9::Present)& pfnPresent,
decltype(&IDirect3DDevice9::Reset)& pfnReset,
decltype(&IDirect3DDevice9Ex::PresentEx)& pfnPresentEx,
decltype(&IDirect3DSwapChain9::Present)& pfnSwapChainPresent)
void*& pfnPresent,
void*& pfnReset,
void*& pfnPresentEx,
void*& pfnSwapChainPresent)
{
void** vTable = *reinterpret_cast<void***>(pDevice);
(void*&)pfnPresent = vTable[(int)IDirect3DDevice9VTable::Present];
(void*&)pfnReset = vTable[(int)IDirect3DDevice9VTable::Reset];
pfnPresent = vTable[(int)IDirect3DDevice9VTable::Present];
pfnReset = vTable[(int)IDirect3DDevice9VTable::Reset];
(void*&)IDirect3DDevice9Present = vTable[(int)IDirect3DDevice9VTable::Present];
@ -448,7 +446,7 @@ private:
if (ex)
{
(void*&)pfnPresentEx = vTable[(int)IDirect3DDevice9VTable::PresentEx];
pfnPresentEx = vTable[(int)IDirect3DDevice9VTable::PresentEx];
(void*&)IDirect3DDevice9ExPresentEx = vTable[(int)IDirect3DDevice9VTable::PresentEx];
detection_hooks.BeginHook();
@ -463,9 +461,9 @@ private:
if (pSwapChain != nullptr)
{
IDirect3DSwapChain9VTable* vTable = *reinterpret_cast<IDirect3DSwapChain9VTable**>(pSwapChain);
pfnSwapChainPresent = vTable->pPresent;
IDirect3DSwapChain9Present = vTable->pPresent;
vTable = *reinterpret_cast<void***>(pSwapChain);
pfnSwapChainPresent = vTable[(int)IDirect3DSwapChain9VTable::Present];
(void*&)IDirect3DSwapChain9Present = vTable[(int)IDirect3DSwapChain9VTable::Present];
detection_hooks.BeginHook();
detection_hooks.HookFunc(std::pair<void**, void*>{ (void**)&IDirect3DSwapChain9Present, (void*)&MyDX9SwapChainPresent });
@ -553,7 +551,7 @@ private:
decltype(&IDirect3DDevice9Ex::PresentEx) pfnPresentEx;
decltype(&IDirect3DSwapChain9::Present) pfnSwapChainPresent;
HookDX9Present(pDevice, Direct3DCreate9Ex != nullptr, pSwapChain, pfnPresent, pfnReset, pfnPresentEx, pfnSwapChainPresent);
HookDX9Present(pDevice, Direct3DCreate9Ex != nullptr, pSwapChain, (void*&)pfnPresent, (void*&)pfnReset, (void*&)pfnPresentEx, (void*&)pfnSwapChainPresent);
dx9_hook = DX9_Hook::Inst();
dx9_hook->LibraryName = library_path;
@ -1045,7 +1043,10 @@ public:
std::lock_guard<std::mutex> lk(renderer_mutex);
if (detection_done)
{
if (renderer_hook != nullptr)
return renderer_hook;
detection_done = false;
}
if (CreateHWND() == nullptr)
@ -1069,8 +1070,9 @@ public:
auto start_time = std::chrono::steady_clock::now();
do
{
std::unique_lock<std::mutex> lck(destroy_render_thread_mutex);
if (force_stop_detection) break;
std::unique_lock<std::mutex> lck(stop_detection_mutex);
if (detection_done)
break;
for (auto const& library : libraries)
{
@ -1083,9 +1085,7 @@ public:
}
}
detect_renderer_thread_cv.wait_for(lck, std::chrono::milliseconds(100));
if (force_stop_detection) break;
stop_detection_cv.wait_for(lck, std::chrono::milliseconds{ 100 });
} while (!detection_done && (timeout.count() == -1 || (std::chrono::steady_clock::now() - start_time) <= timeout));
{
@ -1093,19 +1093,38 @@ public:
DestroyHWND();
detection_done = true;
detection_hooks.UnhookAll();
dxgi_hooked = false;
dxgi1_2_hooked = false;
dx12_hooked = false;
dx11_hooked = false;
dx10_hooked = false;
dx9_hooked = false;
opengl_hooked = false;
vulkan_hooked = false;
delete dx9_hook ; dx9_hook = nullptr;
delete dx10_hook ; dx10_hook = nullptr;
delete dx11_hook ; dx11_hook = nullptr;
delete dx12_hook ; dx12_hook = nullptr;
delete opengl_hook; opengl_hook = nullptr;
delete vulkan_hook; vulkan_hook = nullptr;
detection_hooks.UnhookAll();
}
SPDLOG_TRACE("Renderer detection done {}.", (void*)renderer_hook);
return renderer_hook;
}
void stop_detection()
{
{
System::scoped_lock lk(renderer_mutex, stop_detection_mutex);
detection_done = true;
}
stop_detection_cv.notify_all();
}
};
Renderer_Detector* Renderer_Detector::instance = nullptr;
@ -1158,6 +1177,8 @@ private:
OpenGLX_Hook* openglx_hook;
bool detection_done;
std::condition_variable stop_detection_cv;
std::mutex stop_detection_mutex;
static void MyglXSwapBuffers(Display* dpy, GLXDrawable drawable)
{
@ -1231,7 +1252,12 @@ public:
{
std::lock_guard<std::mutex> lk(renderer_mutex);
if (detection_done)
{
if (renderer_hook != nullptr)
return renderer_hook;
detection_done = false;
}
}
SPDLOG_TRACE("Started renderer detection.");
@ -1239,6 +1265,10 @@ public:
auto start_time = std::chrono::steady_clock::now();
do
{
std::unique_lock<std::mutex> lck(stop_detection_mutex);
if (detection_done)
break;
for (auto const& library : libraries)
{
void* lib_handle = System::Library::GetLibraryHandle(library.first);
@ -1249,12 +1279,18 @@ public:
(this->*library.second)(lib_path);
}
}
std::this_thread::sleep_for(std::chrono::milliseconds{ 100 });
stop_detection_cv.wait_for(lck, std::chrono::milliseconds{ 100 });
} while (!detection_done && (timeout.count() == -1 || (std::chrono::steady_clock::now() - start_time) <= timeout));
{
std::lock_guard<std::mutex> lk(renderer_mutex);
detection_done = true;
detection_hooks.UnhookAll();
openglx_hooked = false;
delete openglx_hook; openglx_hook = nullptr;
//delete vulkan_hook; vulkan_hook = nullptr;
}
@ -1263,6 +1299,15 @@ public:
return renderer_hook;
}
void stop_detection()
{
{
System::scoped_lock lk(renderer_mutex, stop_detection_mutex);
detection_done = true;
}
stop_detection_cv.notify_all();
}
};
Renderer_Detector* Renderer_Detector::instance = nullptr;
@ -1311,6 +1356,8 @@ private:
OpenGL_Hook* opengl_hook;
bool detection_done;
std::condition_variable stop_detection_cv;
std::mutex stop_detection_mutex;
static int64_t MyCGLFlushDrawable(CGLDrawable_t* glDrawable)
{
@ -1384,7 +1431,12 @@ public:
{
std::lock_guard<std::mutex> lk(renderer_mutex);
if (detection_done)
{
if (renderer_hook != nullptr)
return renderer_hook;
detection_done = false;
}
}
SPDLOG_TRACE("Started renderer detection.");
@ -1392,6 +1444,10 @@ public:
auto start_time = std::chrono::steady_clock::now();
do
{
std::unique_lock<std::mutex> lck(stop_detection_mutex);
if (detection_done)
break;
for (auto const& library : libraries)
{
void* lib_handle = System::Library::GetLibraryHandle(library.first);
@ -1402,12 +1458,18 @@ public:
(this->*library.second)(lib_path);
}
}
std::this_thread::sleep_for(std::chrono::milliseconds{ 100 });
stop_detection_cv.wait_for(lck, std::chrono::milliseconds{ 100 });
} while (!detection_done && (timeout.count() == -1 || (std::chrono::steady_clock::now() - start_time) <= timeout));
{
std::lock_guard<std::mutex> lk(renderer_mutex);
detection_done = true;
detection_hooks.UnhookAll();
opengl_hooked = false;
delete opengl_hook; opengl_hook = nullptr;
//delete vulkan_hook; vulkan_hook = nullptr;
}
@ -1416,21 +1478,31 @@ public:
return renderer_hook;
}
void stop_detection()
{
{
System::scoped_lock lk(renderer_mutex, stop_detection_mutex);
detection_done = true;
}
stop_detection_cv.notify_all();
}
};
Renderer_Detector* Renderer_Detector::instance = nullptr;
#endif
std::future<Renderer_Hook*> detect_renderer(std::chrono::milliseconds timeout)
namespace ingame_overlay {
std::future<Renderer_Hook*> DetectRenderer(std::chrono::milliseconds timeout)
{
return std::async(std::launch::async, &Renderer_Detector::detect_renderer, Renderer_Detector::Inst(), timeout);
}
void stop_renderer_detector()
void StopRendererDetection()
{
Renderer_Detector::Inst()->destroy_render_thread_mutex.lock();
Renderer_Detector::Inst()->force_stop_detection = true;
Renderer_Detector::Inst()->destroy_render_thread_mutex.unlock();
Renderer_Detector::Inst()->detect_renderer_thread_cv.notify_all();
Renderer_Detector::Inst()->stop_detection();
}
}

View file

@ -27,5 +27,9 @@
#include "Renderer_Hook.h"
std::future<Renderer_Hook*> detect_renderer(std::chrono::milliseconds timeout = std::chrono::milliseconds{-1});
void stop_renderer_detector();
namespace ingame_overlay {
std::future<Renderer_Hook*> DetectRenderer(std::chrono::milliseconds timeout = std::chrono::milliseconds{ -1 });
void StopRendererDetection();
}

View file

@ -110,6 +110,8 @@ void OpenGLX_Hook::_PrepareForOverlay(Display* display, GLXDrawable drawable)
_Display = display;
X11_Hook::Inst()->SetInitialWindowSize(_Display, (Window)drawable);
_Initialized = true;
OverlayHookReady(true);
}

View file

@ -83,6 +83,18 @@ void X11_Hook::ResetRenderState()
}
}
void X11_Hook::SetInitialWindowSize(Display* display, Window wnd)
{
unsigned int width, height;
Window unused_window;
int unused_int;
unsigned int unused_unsigned_int;
XGetGeometry(display, wnd, &unused_window, &unused_int, &unused_int, &width, &height, &unused_unsigned_int, &unused_unsigned_int);
ImGui::GetIO().DisplaySize = ImVec2((float)width, (float)height);
}
bool X11_Hook::PrepareForOverlay(Display *display, Window wnd)
{
if(!_Hooked)

View file

@ -60,6 +60,7 @@ public:
virtual ~X11_Hook();
void ResetRenderState();
void SetInitialWindowSize(Display* display, Window wnd);
bool PrepareForOverlay(Display *display, Window wnd);
Window GetGameWnd() const{ return _GameWnd; }

View file

@ -196,14 +196,14 @@ void Steam_Overlay::SetupOverlay()
if (!setup_overlay_called)
{
setup_overlay_called = true;
future_renderer = detect_renderer();
future_renderer = ingame_overlay::DetectRenderer();
}
}
void Steam_Overlay::UnSetupOverlay()
{
stop_renderer_detector();
ingame_overlay::StopRendererDetection();
if (!Ready() && future_renderer.valid()) {
if (future_renderer.wait_for(std::chrono::milliseconds{500}) == std::future_status::ready) {
future_renderer.get();

View file

@ -82,7 +82,7 @@ void DX10_Hook::_ResetRenderState()
OverlayHookReady(false);
ImGui_ImplDX10_Shutdown();
Windows_Hook::Inst()->_ResetRenderState();
Windows_Hook::Inst()->ResetRenderState();
ImGui::DestroyContext();
SafeRelease(mainRenderTargetView);
@ -118,11 +118,13 @@ void DX10_Hook::_PrepareForOverlay(IDXGISwapChain* pSwapChain)
ImGui::CreateContext();
ImGui_ImplDX10_Init(pDevice);
Windows_Hook::Inst()->SetInitialWindowSize(desc.OutputWindow);
_Initialized = true;
OverlayHookReady(true);
}
if (ImGui_ImplDX10_NewFrame() && Windows_Hook::Inst()->_PrepareForOverlay(desc.OutputWindow))
if (ImGui_ImplDX10_NewFrame() && Windows_Hook::Inst()->PrepareForOverlay(desc.OutputWindow))
{
ImGui::NewFrame();

View file

@ -92,7 +92,7 @@ void DX11_Hook::_ResetRenderState()
OverlayHookReady(false);
ImGui_ImplDX11_Shutdown();
Windows_Hook::Inst()->_ResetRenderState();
Windows_Hook::Inst()->ResetRenderState();
//ImGui::DestroyContext();
SafeRelease(mainRenderTargetView);
@ -151,11 +151,13 @@ void DX11_Hook::_PrepareForOverlay(IDXGISwapChain* pSwapChain)
ImGui_ImplDX11_Init(pDevice, pContext);
Windows_Hook::Inst()->SetInitialWindowSize(desc.OutputWindow);
_Initialized = true;
OverlayHookReady(true);
}
if (ImGui_ImplDX11_NewFrame() && Windows_Hook::Inst()->_PrepareForOverlay(desc.OutputWindow))
if (ImGui_ImplDX11_NewFrame() && Windows_Hook::Inst()->PrepareForOverlay(desc.OutputWindow))
{
ImGui::NewFrame();

View file

@ -137,7 +137,7 @@ void DX12_Hook::_ResetRenderState()
OverlayHookReady(false);
ImGui_ImplDX12_Shutdown();
Windows_Hook::Inst()->_ResetRenderState();
Windows_Hook::Inst()->ResetRenderState();
ImGui::DestroyContext();
OverlayFrames.clear();
@ -271,11 +271,13 @@ void DX12_Hook::_PrepareForOverlay(IDXGISwapChain* pSwapChain, ID3D12CommandQueu
//heaps.cpu_handle,
//heaps.gpu_handle);
Windows_Hook::Inst()->SetInitialWindowSize(sc_desc.OutputWindow);
_Initialized = true;
OverlayHookReady(true);
}
if (ImGui_ImplDX12_NewFrame() && Windows_Hook::Inst()->_PrepareForOverlay(sc_desc.OutputWindow))
if (ImGui_ImplDX12_NewFrame() && Windows_Hook::Inst()->PrepareForOverlay(sc_desc.OutputWindow))
{
ImGui::NewFrame();

View file

@ -88,7 +88,7 @@ void DX9_Hook::_ResetRenderState()
OverlayHookReady(false);
ImGui_ImplDX9_Shutdown();
Windows_Hook::Inst()->_ResetRenderState();
Windows_Hook::Inst()->ResetRenderState();
ImGui::DestroyContext();
SafeRelease(_pDevice);
@ -137,11 +137,14 @@ void DX9_Hook::_PrepareForOverlay(IDirect3DDevice9 *pDevice, HWND destWindow)
ImGui_ImplDX9_Init(pDevice);
_LastWindow = destWindow;
Windows_Hook::Inst()->SetInitialWindowSize(destWindow);
_Initialized = true;
OverlayHookReady(true);
}
if (ImGui_ImplDX9_NewFrame() && Windows_Hook::Inst()->_PrepareForOverlay(destWindow))
if (ImGui_ImplDX9_NewFrame() && Windows_Hook::Inst()->PrepareForOverlay(destWindow))
{
ImGui::NewFrame();

View file

@ -464,9 +464,7 @@ enum class IDirect3DDevice9VTable
GetDisplayModeEx,
};
struct IDirect3DSwapChain9VTable
{
enum class Index
enum class IDirect3DSwapChain9VTable
{
// IUnknown
QueryInterface,
@ -487,20 +485,3 @@ struct IDirect3DSwapChain9VTable
GetPresentStats,
GetDisplayModeEx,
};
decltype(&IDirect3DSwapChain9::QueryInterface) pQueryInterface;
decltype(&IDirect3DSwapChain9::AddRef) pAddRef;
decltype(&IDirect3DSwapChain9::Release) pRelease;
decltype(&IDirect3DSwapChain9::Present) pPresent;
decltype(&IDirect3DSwapChain9::GetFrontBufferData) pGetFrontBufferData;
decltype(&IDirect3DSwapChain9::GetBackBuffer) pGetBackBuffer;
decltype(&IDirect3DSwapChain9::GetRasterStatus) pGetRasterStatus;
decltype(&IDirect3DSwapChain9::GetDisplayMode) pGetDisplayMode;
decltype(&IDirect3DSwapChain9::GetDevice) pGetDevice;
decltype(&IDirect3DSwapChain9::GetPresentParameters) pGetPresentParameters;
decltype(&IDirect3DSwapChain9Ex::GetLastPresentCount) pGetLastPresentCount;
decltype(&IDirect3DSwapChain9Ex::GetPresentStats) pGetPresentStats;
decltype(&IDirect3DSwapChain9Ex::GetDisplayModeEx) pGetDisplayModeEx;
};

View file

@ -68,10 +68,10 @@ void OpenGL_Hook::_ResetRenderState()
OverlayHookReady(false);
ImGui_ImplOpenGL3_Shutdown();
Windows_Hook::Inst()->_ResetRenderState();
Windows_Hook::Inst()->ResetRenderState();
ImGui::DestroyContext();
last_window = nullptr;
_LastWindow = nullptr;
_Initialized = false;
}
}
@ -81,7 +81,7 @@ void OpenGL_Hook::_PrepareForOverlay(HDC hDC)
{
HWND hWnd = WindowFromDC(hDC);
if (hWnd != last_window)
if (hWnd != _LastWindow)
_ResetRenderState();
if (!_Initialized)
@ -89,12 +89,15 @@ void OpenGL_Hook::_PrepareForOverlay(HDC hDC)
ImGui::CreateContext();
ImGui_ImplOpenGL3_Init();
last_window = hWnd;
_LastWindow = hWnd;
Windows_Hook::Inst()->SetInitialWindowSize(hWnd);
_Initialized = true;
OverlayHookReady(true);
}
if (ImGui_ImplOpenGL3_NewFrame() && Windows_Hook::Inst()->_PrepareForOverlay(hWnd))
if (ImGui_ImplOpenGL3_NewFrame() && Windows_Hook::Inst()->PrepareForOverlay(hWnd))
{
ImGui::NewFrame();
@ -117,7 +120,7 @@ OpenGL_Hook::OpenGL_Hook():
_Hooked(false),
_WindowsHooked(false),
_Initialized(false),
last_window(nullptr),
_LastWindow(nullptr),
wglSwapBuffers(nullptr)
{
}

View file

@ -37,7 +37,7 @@ private:
bool _Hooked;
bool _WindowsHooked;
bool _Initialized;
HWND last_window;
HWND _LastWindow;
std::set<std::shared_ptr<uint64_t>> _ImageResources;
// Functions

View file

@ -88,7 +88,7 @@ bool Windows_Hook::StartHook(std::function<bool(bool)>& _key_combination_callbac
return true;
}
void Windows_Hook::_ResetRenderState()
void Windows_Hook::ResetRenderState()
{
if (_Initialized)
{
@ -100,10 +100,17 @@ void Windows_Hook::_ResetRenderState()
}
}
bool Windows_Hook::_PrepareForOverlay(HWND hWnd)
void Windows_Hook::SetInitialWindowSize(HWND hWnd)
{
RECT rect = { 0, 0, 0, 0 };
::GetClientRect(hWnd, &rect);
ImGui::GetIO().DisplaySize = ImVec2((float)(rect.right - rect.left), (float)(rect.bottom - rect.top));
}
bool Windows_Hook::PrepareForOverlay(HWND hWnd)
{
if (_GameHwnd != hWnd)
_ResetRenderState();
ResetRenderState();
if (!_Initialized)
{
@ -374,7 +381,7 @@ Windows_Hook::~Windows_Hook()
{
SPDLOG_INFO("Windows Hook removed");
_ResetRenderState();
ResetRenderState();
_inst = nullptr;
}

View file

@ -68,8 +68,9 @@ public:
virtual ~Windows_Hook();
void _ResetRenderState();
bool _PrepareForOverlay(HWND hWnd);
void ResetRenderState();
void SetInitialWindowSize(HWND hWnd);
bool PrepareForOverlay(HWND hWnd);
HWND GetGameHwnd() const;
WNDPROC GetGameWndProc() const;