mirror of
https://gitlab.com/Mr_Goldberg/goldberg_emulator.git
synced 2025-12-06 12:14:54 +01:00
Update Nemirtingas overlay to latest.
This commit is contained in:
parent
df94c38b0f
commit
c17fb0c931
82 changed files with 48737 additions and 59872 deletions
|
|
@ -1,26 +1,58 @@
|
|||
/*
|
||||
* Copyright (C) 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 "DX9_Hook.h"
|
||||
#include "Windows_Hook.h"
|
||||
#include "../Renderer_Detector.h"
|
||||
#include "../../dll/dll.h"
|
||||
|
||||
#ifdef EMU_OVERLAY
|
||||
#include "DirectX_VTables.h"
|
||||
|
||||
#include <imgui.h>
|
||||
#include <impls/windows/imgui_impl_dx9.h>
|
||||
#include <backends/imgui_impl_dx9.h>
|
||||
|
||||
DX9_Hook* DX9_Hook::_inst = nullptr;
|
||||
|
||||
bool DX9_Hook::start_hook()
|
||||
template<typename T>
|
||||
inline void SafeRelease(T*& pUnk)
|
||||
{
|
||||
if (!hooked)
|
||||
if (pUnk != nullptr)
|
||||
{
|
||||
if (!Windows_Hook::Inst()->start_hook())
|
||||
pUnk->Release();
|
||||
pUnk = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool DX9_Hook::StartHook(std::function<bool(bool)> key_combination_callback)
|
||||
{
|
||||
if (!_Hooked)
|
||||
{
|
||||
if (Reset == nullptr || Present == nullptr)
|
||||
{
|
||||
SPDLOG_WARN("Failed to hook DirectX 9: Rendering functions missing.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!Windows_Hook::Inst()->StartHook(key_combination_callback))
|
||||
return false;
|
||||
|
||||
PRINT_DEBUG("Hooked DirectX 9\n");
|
||||
hooked = true;
|
||||
|
||||
Renderer_Detector::Inst().renderer_found(this);
|
||||
_WindowsHooked = true;
|
||||
|
||||
SPDLOG_INFO("Hooked DirectX 9");
|
||||
_Hooked = true;
|
||||
|
||||
BeginHook();
|
||||
HookFuncs(
|
||||
|
|
@ -31,57 +63,89 @@ bool DX9_Hook::start_hook()
|
|||
{
|
||||
HookFuncs(
|
||||
std::make_pair<void**, void*>(&(PVOID&)PresentEx, &DX9_Hook::MyPresentEx)
|
||||
//std::make_pair<void**, void*>(&(PVOID&)EndScene, &DX9_Hook::MyEndScene)
|
||||
);
|
||||
}
|
||||
if (SwapChainPresent != nullptr)
|
||||
{
|
||||
HookFuncs(
|
||||
std::make_pair<void**, void*>(&(PVOID&)SwapChainPresent, &DX9_Hook::MySwapChainPresent)
|
||||
);
|
||||
}
|
||||
EndHook();
|
||||
|
||||
get_steam_client()->steam_overlay->HookReady();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void DX9_Hook::resetRenderState()
|
||||
bool DX9_Hook::IsStarted()
|
||||
{
|
||||
if (initialized)
|
||||
return _Hooked;
|
||||
}
|
||||
|
||||
void DX9_Hook::_ResetRenderState()
|
||||
{
|
||||
if (_Initialized)
|
||||
{
|
||||
initialized = false;
|
||||
OverlayHookReady(false);
|
||||
|
||||
ImGui_ImplDX9_Shutdown();
|
||||
Windows_Hook::Inst()->resetRenderState();
|
||||
Windows_Hook::Inst()->_ResetRenderState();
|
||||
ImGui::DestroyContext();
|
||||
|
||||
SafeRelease(_pDevice);
|
||||
|
||||
_LastWindow = nullptr;
|
||||
_Initialized = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Try to make this function and overlay's proc as short as possible or it might affect game's fps.
|
||||
void DX9_Hook::prepareForOverlay(IDirect3DDevice9 *pDevice)
|
||||
void DX9_Hook::_PrepareForOverlay(IDirect3DDevice9 *pDevice, HWND destWindow)
|
||||
{
|
||||
D3DDEVICE_CREATION_PARAMETERS param;
|
||||
pDevice->GetCreationParameters(¶m);
|
||||
if (!destWindow)
|
||||
{
|
||||
IDirect3DSwapChain9 *pSwapChain = nullptr;
|
||||
if (pDevice->GetSwapChain(0, &pSwapChain) == D3D_OK)
|
||||
{
|
||||
D3DPRESENT_PARAMETERS params;
|
||||
if (pSwapChain->GetPresentParameters(¶ms) == D3D_OK)
|
||||
{
|
||||
destWindow = params.hDeviceWindow;
|
||||
}
|
||||
|
||||
pSwapChain->Release();
|
||||
}
|
||||
}
|
||||
|
||||
//Is this necessary anymore?
|
||||
if (!destWindow)
|
||||
{
|
||||
D3DDEVICE_CREATION_PARAMETERS param;
|
||||
pDevice->GetCreationParameters(¶m);
|
||||
destWindow = param.hFocusWindow;
|
||||
}
|
||||
|
||||
// Workaround to detect if we changed window.
|
||||
if (param.hFocusWindow != Windows_Hook::Inst()->GetGameHwnd())
|
||||
resetRenderState();
|
||||
if (destWindow != _LastWindow || _pDevice != pDevice)
|
||||
_ResetRenderState();
|
||||
|
||||
if (!initialized)
|
||||
if (!_Initialized)
|
||||
{
|
||||
ImGui::CreateContext();
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
io.IniFilename = NULL;
|
||||
pDevice->AddRef();
|
||||
_pDevice = pDevice;
|
||||
|
||||
ImGui::CreateContext();
|
||||
ImGui_ImplDX9_Init(pDevice);
|
||||
|
||||
get_steam_client()->steam_overlay->CreateFonts();
|
||||
|
||||
initialized = true;
|
||||
_LastWindow = destWindow;
|
||||
_Initialized = true;
|
||||
OverlayHookReady(true);
|
||||
}
|
||||
|
||||
if (ImGui_ImplDX9_NewFrame())
|
||||
{
|
||||
Windows_Hook::Inst()->prepareForOverlay(param.hFocusWindow);
|
||||
|
||||
if (ImGui_ImplDX9_NewFrame() && Windows_Hook::Inst()->_PrepareForOverlay(destWindow))
|
||||
{
|
||||
ImGui::NewFrame();
|
||||
|
||||
get_steam_client()->steam_overlay->OverlayProc();
|
||||
OverlayProc();
|
||||
|
||||
ImGui::Render();
|
||||
|
||||
|
|
@ -91,55 +155,72 @@ void DX9_Hook::prepareForOverlay(IDirect3DDevice9 *pDevice)
|
|||
|
||||
HRESULT STDMETHODCALLTYPE DX9_Hook::MyReset(IDirect3DDevice9* _this, D3DPRESENT_PARAMETERS* pPresentationParameters)
|
||||
{
|
||||
DX9_Hook::Inst()->resetRenderState();
|
||||
return (_this->*DX9_Hook::Inst()->Reset)(pPresentationParameters);
|
||||
}
|
||||
|
||||
HRESULT STDMETHODCALLTYPE DX9_Hook::MyEndScene(IDirect3DDevice9* _this)
|
||||
{
|
||||
if( !DX9_Hook::Inst()->uses_present )
|
||||
DX9_Hook::Inst()->prepareForOverlay(_this);
|
||||
return (_this->*DX9_Hook::Inst()->EndScene)();
|
||||
auto inst = DX9_Hook::Inst();
|
||||
inst->_ResetRenderState();
|
||||
return (_this->*inst->Reset)(pPresentationParameters);
|
||||
}
|
||||
|
||||
HRESULT STDMETHODCALLTYPE DX9_Hook::MyPresent(IDirect3DDevice9* _this, CONST RECT* pSourceRect, CONST RECT* pDestRect, HWND hDestWindowOverride, CONST RGNDATA* pDirtyRegion)
|
||||
{
|
||||
DX9_Hook::Inst()->uses_present = true;
|
||||
DX9_Hook::Inst()->prepareForOverlay(_this);
|
||||
return (_this->*DX9_Hook::Inst()->Present)(pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
|
||||
auto inst = DX9_Hook::Inst();
|
||||
inst->_PrepareForOverlay(_this, hDestWindowOverride);
|
||||
return (_this->*inst->Present)(pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
|
||||
}
|
||||
|
||||
HRESULT STDMETHODCALLTYPE DX9_Hook::MyPresentEx(IDirect3DDevice9Ex* _this, CONST RECT* pSourceRect, CONST RECT* pDestRect, HWND hDestWindowOverride, CONST RGNDATA* pDirtyRegion, DWORD dwFlags)
|
||||
{
|
||||
DX9_Hook::Inst()->uses_present = true;
|
||||
DX9_Hook::Inst()->prepareForOverlay(_this);
|
||||
return (_this->*DX9_Hook::Inst()->PresentEx)(pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion, dwFlags);
|
||||
auto inst = DX9_Hook::Inst();
|
||||
inst->_PrepareForOverlay(_this, hDestWindowOverride);
|
||||
return (_this->*inst->PresentEx)(pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion, dwFlags);
|
||||
}
|
||||
|
||||
HRESULT STDMETHODCALLTYPE DX9_Hook::MySwapChainPresent(IDirect3DSwapChain9* _this, CONST RECT* pSourceRect, CONST RECT* pDestRect, HWND hDestWindowOverride, CONST RGNDATA* pDirtyRegion, DWORD dwFlags)
|
||||
{
|
||||
IDirect3DDevice9* pDevice;
|
||||
auto inst = DX9_Hook::Inst();
|
||||
|
||||
if (SUCCEEDED(_this->GetDevice(&pDevice)))
|
||||
{
|
||||
HWND destWindow = hDestWindowOverride;
|
||||
if (!destWindow)
|
||||
{
|
||||
D3DPRESENT_PARAMETERS param;
|
||||
if (_this->GetPresentParameters(¶m) == D3D_OK)
|
||||
{
|
||||
destWindow = param.hDeviceWindow;
|
||||
}
|
||||
}
|
||||
|
||||
inst->_PrepareForOverlay(pDevice, destWindow);
|
||||
pDevice->Release();
|
||||
}
|
||||
return (_this->*inst->SwapChainPresent)(pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion, dwFlags);
|
||||
}
|
||||
|
||||
DX9_Hook::DX9_Hook():
|
||||
initialized(false),
|
||||
hooked(false),
|
||||
uses_present(false),
|
||||
EndScene(nullptr),
|
||||
_Initialized(false),
|
||||
_Hooked(false),
|
||||
_WindowsHooked(false),
|
||||
_LastWindow(nullptr),
|
||||
Present(nullptr),
|
||||
PresentEx(nullptr),
|
||||
Reset(nullptr)
|
||||
{
|
||||
_library = LoadLibrary(DX9_DLL);
|
||||
}
|
||||
|
||||
DX9_Hook::~DX9_Hook()
|
||||
{
|
||||
PRINT_DEBUG("DX9 Hook removed\n");
|
||||
SPDLOG_INFO("DX9 Hook removed");
|
||||
|
||||
if (initialized)
|
||||
if (_WindowsHooked)
|
||||
delete Windows_Hook::Inst();
|
||||
|
||||
if (_Initialized)
|
||||
{
|
||||
ImGui_ImplDX9_InvalidateDeviceObjects();
|
||||
ImGui::DestroyContext();
|
||||
}
|
||||
|
||||
FreeLibrary(reinterpret_cast<HMODULE>(_library));
|
||||
|
||||
_inst = nullptr;
|
||||
}
|
||||
|
||||
|
|
@ -151,22 +232,94 @@ DX9_Hook* DX9_Hook::Inst()
|
|||
return _inst;
|
||||
}
|
||||
|
||||
const char* DX9_Hook::get_lib_name() const
|
||||
std::string DX9_Hook::GetLibraryName() const
|
||||
{
|
||||
return DX9_DLL;
|
||||
return LibraryName;
|
||||
}
|
||||
|
||||
void DX9_Hook::loadFunctions(IDirect3DDevice9* pDevice, bool ex)
|
||||
void DX9_Hook::LoadFunctions(decltype(Present) PresentFcn, decltype(Reset) ResetFcn, decltype(PresentEx) PresentExFcn, decltype(&IDirect3DSwapChain9::Present) SwapChainPresentFcn)
|
||||
{
|
||||
void** vTable = *reinterpret_cast<void***>(pDevice);
|
||||
Present = PresentFcn;
|
||||
Reset = ResetFcn;
|
||||
|
||||
#define LOAD_FUNC(X) (void*&)X = vTable[(int)IDirect3DDevice9VTable::X]
|
||||
LOAD_FUNC(Reset);
|
||||
LOAD_FUNC(EndScene);
|
||||
LOAD_FUNC(Present);
|
||||
if (ex)
|
||||
LOAD_FUNC(PresentEx);
|
||||
#undef LOAD_FUNC
|
||||
PresentEx = PresentExFcn;
|
||||
|
||||
SwapChainPresent = SwapChainPresentFcn;
|
||||
}
|
||||
|
||||
#endif//EMU_OVERLAY
|
||||
std::weak_ptr<uint64_t> DX9_Hook::CreateImageResource(const void* image_data, uint32_t width, uint32_t height)
|
||||
{
|
||||
IDirect3DTexture9** pTexture = new IDirect3DTexture9*(nullptr);
|
||||
|
||||
_pDevice->CreateTexture(
|
||||
width,
|
||||
height,
|
||||
1,
|
||||
D3DUSAGE_DYNAMIC,
|
||||
D3DFMT_A8R8G8B8,
|
||||
D3DPOOL_DEFAULT,
|
||||
pTexture,
|
||||
nullptr
|
||||
);
|
||||
|
||||
if (*pTexture != nullptr)
|
||||
{
|
||||
D3DLOCKED_RECT rect;
|
||||
if (SUCCEEDED((*pTexture)->LockRect(0, &rect, nullptr, D3DLOCK_DISCARD)))
|
||||
{
|
||||
const uint32_t* pixels = reinterpret_cast<const uint32_t*>(image_data);
|
||||
uint8_t* texture_bits = reinterpret_cast<uint8_t*>(rect.pBits);
|
||||
for (int32_t i = 0; i < height; ++i)
|
||||
{
|
||||
for (int32_t j = 0; j < width; ++j)
|
||||
{
|
||||
// RGBA to ARGB Conversion, DX9 doesn't have a RGBA loader
|
||||
uint32_t color = *pixels++;
|
||||
reinterpret_cast<uint32_t*>(texture_bits)[j] = ((color & 0xff) << 16) | (color & 0xff00) | ((color & 0xff0000) >> 16) | (color & 0xff000000);
|
||||
}
|
||||
texture_bits += rect.Pitch;
|
||||
}
|
||||
|
||||
if (FAILED((*pTexture)->UnlockRect(0)))
|
||||
{
|
||||
(*pTexture)->Release();
|
||||
delete pTexture;
|
||||
pTexture = nullptr;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
(*pTexture)->Release();
|
||||
delete pTexture;
|
||||
pTexture = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
if (pTexture == nullptr)
|
||||
return std::shared_ptr<uint64_t>();
|
||||
|
||||
auto ptr = std::shared_ptr<uint64_t>((uint64_t*)pTexture, [](uint64_t* handle)
|
||||
{
|
||||
if (handle != nullptr)
|
||||
{
|
||||
IDirect3DTexture9** resource = reinterpret_cast<IDirect3DTexture9**>(handle);
|
||||
(*resource)->Release();
|
||||
delete resource;
|
||||
}
|
||||
});
|
||||
|
||||
_ImageResources.emplace(ptr);
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void DX9_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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue