From 437ebbcb7fd2ca543b384d820ecea22e786ad482 Mon Sep 17 00:00:00 2001 From: std-nullptr <73208302+std-nullptr@users.noreply.github.com> Date: Wed, 9 Dec 2020 08:31:16 -0600 Subject: [PATCH] Re-implemented Kiero but stripped down for just what we need (#79) --- AmongUsMenu.vcxproj | 2 + AmongUsMenu.vcxproj.filters | 5 +-- hooks/DirectX.cpp | 88 ++++++++++++++----------------------- hooks/DirectX.h | 8 ++-- hooks/_hooks.cpp | 12 +++-- includes/directx11.cpp | 80 +++++++++++++++++++++++++++++++++ includes/directx11.h | 12 +++++ 7 files changed, 140 insertions(+), 67 deletions(-) create mode 100644 includes/directx11.cpp create mode 100644 includes/directx11.h diff --git a/AmongUsMenu.vcxproj b/AmongUsMenu.vcxproj index f55e8289..6f2c4732 100644 --- a/AmongUsMenu.vcxproj +++ b/AmongUsMenu.vcxproj @@ -39,6 +39,7 @@ + @@ -111,6 +112,7 @@ + diff --git a/AmongUsMenu.vcxproj.filters b/AmongUsMenu.vcxproj.filters index f75f2408..3fff46d1 100644 --- a/AmongUsMenu.vcxproj.filters +++ b/AmongUsMenu.vcxproj.filters @@ -179,6 +179,7 @@ hooks + @@ -323,6 +324,7 @@ hooks + @@ -346,9 +348,6 @@ {166264ce-d359-420e-ba62-adba2279ff8f} - - {20efd3a7-e4c8-4470-a08e-648e1abcd71e} - {0ea105ce-0c37-46f9-8a5e-b845cb40a743} diff --git a/hooks/DirectX.cpp b/hooks/DirectX.cpp index 8ff063d0..d2c9b91d 100644 --- a/hooks/DirectX.cpp +++ b/hooks/DirectX.cpp @@ -45,34 +45,42 @@ LRESULT __stdcall dWndProc(const HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPa return CallWindowProc(oWndProc, hWnd, uMsg, wParam, lParam); } -HRESULT __stdcall dPresent(IDXGISwapChain* pSwapChain, UINT syncInterval, UINT flags) { +bool ImGuiInitialization(IDXGISwapChain* pSwapChain) { + if (SUCCEEDED(pSwapChain->GetDevice(__uuidof(ID3D11Device), (void**)&pDevice))) { + pDevice->GetImmediateContext(&pContext); + DXGI_SWAP_CHAIN_DESC sd; + pSwapChain->GetDesc(&sd); + window = sd.OutputWindow; + ID3D11Texture2D* pBackBuffer = NULL; + pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer); + pDevice->CreateRenderTargetView(pBackBuffer, NULL, &pRenderTargetView); + pBackBuffer->Release(); + oWndProc = (WNDPROC)SetWindowLongPtr(window, GWLP_WNDPROC, (LONG_PTR)dWndProc); + + ImGui::CreateContext(); + ImGuiIO& io = ImGui::GetIO(); + io.ConfigFlags = ImGuiConfigFlags_NoMouseCursorChange; + ImGui_ImplWin32_Init(window); + ImGui_ImplDX11_Init(pDevice, pContext); + + maps.push_back({ D3D11Image(Resource(IDB_PNG1), pDevice), 277.F, 77.F, 11.5F }); + maps.push_back({ D3D11Image(Resource(IDB_PNG2), pDevice), 115.F, 240.F, 9.25F }); + maps.push_back({ D3D11Image(Resource(IDB_PNG3), pDevice), 8.F, 21.F, 10.F }); + + return true; + } + + return false; +} + +HRESULT __stdcall dPresent(IDXGISwapChain* __this, UINT SyncInterval, UINT Flags) { WaitForSingleObject(hPresentMutex, 0); //We're claiming ownership, but we'll be damned if we're going to wait if (!State.ImGuiInitialized) { - if (SUCCEEDED(pSwapChain->GetDevice(__uuidof(ID3D11Device), (void**)&pDevice))) { - pDevice->GetImmediateContext(&pContext); - DXGI_SWAP_CHAIN_DESC sd; - pSwapChain->GetDesc(&sd); - window = sd.OutputWindow; - ID3D11Texture2D* pBackBuffer; - pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer); - pDevice->CreateRenderTargetView(pBackBuffer, NULL, &pRenderTargetView); - pBackBuffer->Release(); - oWndProc = (WNDPROC)SetWindowLongPtr(window, GWLP_WNDPROC, (LONG_PTR)dWndProc); - - ImGui::CreateContext(); - ImGuiIO& io = ImGui::GetIO(); - io.ConfigFlags = ImGuiConfigFlags_NoMouseCursorChange; - ImGui_ImplWin32_Init(window); - ImGui_ImplDX11_Init(pDevice, pContext); - - maps.push_back({ D3D11Image(Resource(IDB_PNG1), pDevice), 277.F, 77.F, 11.5F }); - maps.push_back({ D3D11Image(Resource(IDB_PNG2), pDevice), 115.F, 240.F, 9.25F }); - maps.push_back({ D3D11Image(Resource(IDB_PNG3), pDevice), 8.F, 21.F, 10.F }); - + if (ImGuiInitialization(__this)) { State.ImGuiInitialized = true; } else { ReleaseMutex(hPresentMutex); - return oPresent(pSwapChain, syncInterval, flags); + return oPresent(__this, SyncInterval, Flags); } } @@ -147,39 +155,7 @@ HRESULT __stdcall dPresent(IDXGISwapChain* pSwapChain, UINT syncInterval, UINT f ReleaseMutex(hPresentMutex); - return oPresent(pSwapChain, syncInterval, flags); -} - -void DirectX::Initialize() -{ - ID3D11Device* pDevice = NULL; - IDXGISwapChain* pSwapchain = NULL; - - DXGI_SWAP_CHAIN_DESC swapChainDesc{ 0 }; - - hPresentMutex = CreateMutex(NULL, FALSE, NULL); - - swapChainDesc.BufferCount = 1; - swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; - swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; - swapChainDesc.OutputWindow = GetDesktopWindow(); - swapChainDesc.Windowed = TRUE; - swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; - swapChainDesc.SampleDesc.Count = 1; - - HRESULT result = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_REFERENCE, NULL, 0, NULL, 0, D3D11_SDK_VERSION, &swapChainDesc, &pSwapchain, &pDevice, NULL, NULL); - - if (GetLastError() == 0x594) SetLastError(0); // Ignore any errors related to the output window - - if (FAILED(result)) return; - - void** pVMT = *(void***)pSwapchain; - oPresent = (D3D_PRESENT_FUNCTION)pVMT[8]; - - if (pDevice) { - pDevice->Release(); - pDevice = NULL; - } + return oPresent(__this, SyncInterval, Flags); } void DirectX::Shutdown() { diff --git a/hooks/DirectX.h b/hooks/DirectX.h index 5732522d..d06c57c3 100644 --- a/hooks/DirectX.h +++ b/hooks/DirectX.h @@ -1,5 +1,6 @@ #pragma once #include "resources.h" +#include "directx11.h" #include struct MapTexture { @@ -9,14 +10,11 @@ struct MapTexture { float scale; }; -typedef HRESULT(__stdcall* D3D_PRESENT_FUNCTION)(IDXGISwapChain* pThis, UINT SyncInterval, UINT Flags); - -extern D3D_PRESENT_FUNCTION oPresent; extern std::vector maps; -HRESULT __stdcall dPresent(IDXGISwapChain* pSwapChain, UINT syncInterval, UINT flags); +extern D3D_PRESENT_FUNCTION oPresent; +HRESULT __stdcall dPresent(IDXGISwapChain* __this, UINT SyncInterval, UINT Flags); namespace DirectX { - void Initialize(); void Shutdown(); } \ No newline at end of file diff --git a/hooks/_hooks.cpp b/hooks/_hooks.cpp index eaf85b82..8e3c218f 100644 --- a/hooks/_hooks.cpp +++ b/hooks/_hooks.cpp @@ -12,7 +12,7 @@ bool HookFunction(PVOID* ppPointer, PVOID pDetour, const char* functionName) { std::cout << "Failed to hook " << functionName << std::endl; return false; } - std::cout << "Hooked " << functionName << std::endl; + //std::cout << "Hooked " << functionName << std::endl; return true; } @@ -22,6 +22,14 @@ void DetourInitilization() { DetourTransactionBegin(); DetourUpdateThread(GetCurrentThread()); + directx11 d3d11 = directx11(); + if (!d3d11.presentFunction) { + std::cout << "Unable to retrieve IDXGISwapChain::Present method" << std::endl; + return; + } else { + oPresent = d3d11.presentFunction; + } + HOOKFUNC(SceneManager_Internal_ActiveSceneChanged); HOOKFUNC(HatManager_GetUnlockedHats); HOOKFUNC(HatManager_GetUnlockedPets); @@ -54,8 +62,6 @@ void DetourInitilization() { HOOKFUNC(MeetingHud_Awake); HOOKFUNC(MeetingHud_Close); HOOKFUNC(Constants_ShouldFlipSkeld); - - DirectX::Initialize(); if (!HookFunction(&(PVOID&)oPresent, dPresent, "D3D_PRESENT_FUNCTION")) return; DetourTransactionCommit(); diff --git a/includes/directx11.cpp b/includes/directx11.cpp new file mode 100644 index 00000000..cb917325 --- /dev/null +++ b/includes/directx11.cpp @@ -0,0 +1,80 @@ +#include "directx11.h" +#define WIN32_LEAN_AND_MEAN +#include + +directx11::directx11() +{ + WNDCLASSEX windowClass = { + sizeof(WNDCLASSEX), + CS_HREDRAW | CS_VREDRAW, + DefWindowProc, + 0, + 0, + GetModuleHandle(NULL), + NULL, + NULL, + NULL, + NULL, + L"DirectX 11", + NULL, + }; + + RegisterClassEx(&windowClass); + HWND window = CreateWindow(windowClass.lpszClassName, L"DirectX 11 Window", WS_OVERLAPPEDWINDOW, 0, 0, 100, 100, NULL, NULL, windowClass.hInstance, NULL); + + D3D_FEATURE_LEVEL featureLevel; + const D3D_FEATURE_LEVEL featureLevels[] = { D3D_FEATURE_LEVEL_10_1, D3D_FEATURE_LEVEL_11_0 }; + + DXGI_MODE_DESC bufferDesc = { + 100, + 100, + {60, 1}, + DXGI_FORMAT_R8G8B8A8_UNORM, + DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED, + DXGI_MODE_SCALING_UNSPECIFIED, + }; + + DXGI_SWAP_CHAIN_DESC swapChainDesc = { + bufferDesc, + {1, 0}, + DXGI_USAGE_RENDER_TARGET_OUTPUT, + 1, + window, + 1, + DXGI_SWAP_EFFECT_DISCARD, + DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH, + }; + + IDXGISwapChain* pSwapChain; + ID3D11Device* pDevice; + ID3D11DeviceContext* pContext; + + HRESULT result = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, featureLevels, 2, D3D11_SDK_VERSION, &swapChainDesc, &pSwapChain, &pDevice, &featureLevel, &pContext); + + if (FAILED(result)) { + DestroyWindow(window); + UnregisterClass(windowClass.lpszClassName, windowClass.hInstance); + return; + } + + void** pVMT = *(void***)pSwapChain; + presentFunction = (D3D_PRESENT_FUNCTION)pVMT[8]; + + if (pSwapChain) { + pSwapChain->Release(); + pSwapChain = NULL; + } + + if (pDevice) { + pDevice->Release(); + pDevice = NULL; + } + + if (pContext) { + pContext->Release(); + pContext = NULL; + } + + DestroyWindow(window); + UnregisterClass(windowClass.lpszClassName, windowClass.hInstance); +} \ No newline at end of file diff --git a/includes/directx11.h b/includes/directx11.h new file mode 100644 index 00000000..bda55dd1 --- /dev/null +++ b/includes/directx11.h @@ -0,0 +1,12 @@ +#pragma once +#include +#include +#include + +typedef HRESULT(__stdcall* D3D_PRESENT_FUNCTION)(IDXGISwapChain* __this, UINT SyncInterval, UINT Flags); + +class directx11 { +public: + D3D_PRESENT_FUNCTION presentFunction; + directx11(); +}; \ No newline at end of file