From 1ad5b46e0dda16983ba3f4e43172df4b67ae8549 Mon Sep 17 00:00:00 2001 From: Grinch_ Date: Mon, 11 Jan 2021 10:07:10 -0800 Subject: [PATCH] Refactored code --- .vscode/c_cpp_properties.json | 2 +- src/Hook.cpp | 130 ++++++++++++---------------------- src/Hook.h | 11 ++- src/pch.cpp | 2 +- src/pch.h | 2 +- src/tests/CMakeLists.txt | 1 + src/{ => tests}/Dllmain.cpp | 4 +- src/tests/Test.cpp | 2 +- 8 files changed, 57 insertions(+), 97 deletions(-) rename src/{ => tests}/Dllmain.cpp (92%) diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 94549b4..655de58 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -4,7 +4,7 @@ "name": "Win32", "includePath": [ "${workspaceFolder}/src/", - "${DIRECTX9_SDK_DIR}/include", + "${DIRECTX9_SDK_DIR}/Include", "${PLUGIN_SDK_DIR}/plugin_sa", "${PLUGIN_SDK_DIR}/plugin_sa/game_sa", "${PLUGIN_SDK_DIR}/shared", diff --git a/src/Hook.cpp b/src/Hook.cpp index 304905a..bbe3438 100644 --- a/src/Hook.cpp +++ b/src/Hook.cpp @@ -4,9 +4,9 @@ #include "vendor/kiero/minhook/MinHook.h" WNDPROC Hook::oWndProc = NULL; -f_Reset Hook::oReset9 = NULL; f_Present11 Hook::oPresent11 = NULL; f_Present9 Hook::oPresent9 = NULL; +f_Reset Hook::oReset9 = NULL; bool Hook::mouse_visibility = false; bool Hook::show_mouse = false; @@ -34,14 +34,14 @@ LRESULT Hook::InputProc(const HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam return CallWindowProc(oWndProc, hWnd, uMsg, wParam, lParam); } -HRESULT Hook::ResetDx9(IDirect3DDevice9 * pDevice, D3DPRESENT_PARAMETERS * pPresentationParameters) +HRESULT Hook::Reset(IDirect3DDevice9 * pDevice, D3DPRESENT_PARAMETERS * pPresentationParameters) { ImGui_ImplDX9_InvalidateDeviceObjects(); return oReset9(pDevice, pPresentationParameters); } -HRESULT Hook::PresentDx9(IDirect3DDevice9 *pDevice, RECT* pSourceRect, RECT* pDestRect, HWND hDestWindowOverride, RGNDATA* pDirtyRegion) +HRESULT Hook::Present(void *ptr, int u1, int u2, int u3, int u4) { ImGuiIO& io = ImGui::GetIO(); @@ -54,20 +54,28 @@ HRESULT Hook::PresentDx9(IDirect3DDevice9 *pDevice, RECT* pSourceRect, RECT* pDe } // Change font size if the game resolution changes - if (Globals::font_screen_size.x != screen::GetScreenWidth() - && Globals::font_screen_size.y != screen::GetScreenHeight()) + if (Globals::screen_size.x != screen::GetScreenWidth() + && Globals::screen_size.y != screen::GetScreenHeight()) { int font_size = int(screen::GetScreenHeight() / 54.85); // manually tested io.FontDefault = io.Fonts->AddFontFromFileTTF("C:/Windows/Fonts/trebucbd.ttf", font_size); io.Fonts->Build(); - ImGui_ImplDX9_InvalidateDeviceObjects(); - Globals::font_screen_size = ImVec2(screen::GetScreenWidth(), screen::GetScreenHeight()); + if (Globals::renderer == Render_DirectX9) + ImGui_ImplDX9_InvalidateDeviceObjects(); + else + ImGui_ImplDX11_InvalidateDeviceObjects(); + + Globals::screen_size = ImVec2(screen::GetScreenWidth(), screen::GetScreenHeight()); } - ImGui_ImplDX9_NewFrame(); ImGui_ImplWin32_NewFrame(); + if (Globals::renderer == Render_DirectX9) + ImGui_ImplDX9_NewFrame(); + else + ImGui_ImplDX11_NewFrame(); + ImGui::NewFrame(); if (window_func != NULL) @@ -75,7 +83,11 @@ HRESULT Hook::PresentDx9(IDirect3DDevice9 *pDevice, RECT* pSourceRect, RECT* pDe ImGui::EndFrame(); ImGui::Render(); - ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData()); + + if (Globals::renderer == Render_DirectX9) + ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData()); + else + ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData()); } else { @@ -85,7 +97,22 @@ HRESULT Hook::PresentDx9(IDirect3DDevice9 *pDevice, RECT* pSourceRect, RECT* pDe ImGuiStyle& style = ImGui::GetStyle(); ImGui_ImplWin32_Init(RsGlobal.ps->window); - ImGui_ImplDX9_Init(pDevice); + + if (Globals::renderer == Render_DirectX9) + { + Globals::device = ptr; + ImGui_ImplDX9_Init(reinterpret_cast(Globals::device)); + } + else + { + // for dx11 device ptr is swapchain + reinterpret_cast(ptr)->GetDevice(__uuidof(ID3D11Device), (void**)&Globals::device); + ID3D11DeviceContext* context; + reinterpret_cast(Globals::device)->GetImmediateContext(&context); + + ImGui_ImplDX11_Init(reinterpret_cast(Globals::device), context); + } + ImGui_ImplWin32_EnableDpiAwareness(); io.IniFilename = NULL; @@ -95,73 +122,10 @@ HRESULT Hook::PresentDx9(IDirect3DDevice9 *pDevice, RECT* pSourceRect, RECT* pDe oWndProc = (WNDPROC)SetWindowLongPtr(RsGlobal.ps->window, GWL_WNDPROC, (LRESULT)InputProc); } - return oPresent9(pDevice, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);; -} - -HRESULT Hook::PresentDx11(IDXGISwapChain* pSwapChain, UINT SyncInterval, UINT Flags) -{ - ImGuiIO& io = ImGui::GetIO(); - - if (Globals::init_done) - { - if (mouse_visibility != show_mouse) - { - Hook::ShowMouse(show_mouse); - mouse_visibility = show_mouse; - } - - // Change font size if the game resolution changes - if (Globals::font_screen_size.x != screen::GetScreenWidth() - && Globals::font_screen_size.y != screen::GetScreenHeight()) - { - int font_size = int(screen::GetScreenHeight() / 54.85); // manually tested - - io.FontDefault = io.Fonts->AddFontFromFileTTF("C:/Windows/Fonts/trebucbd.ttf", font_size); - io.Fonts->Build(); - ImGui_ImplDX11_InvalidateDeviceObjects(); - - Globals::font_screen_size = ImVec2(screen::GetScreenWidth(), screen::GetScreenHeight()); - } - - ImGui_ImplDX11_NewFrame(); - ImGui_ImplWin32_NewFrame(); - ImGui::NewFrame(); - - if (window_func != NULL) - window_func(); - - ImGui::EndFrame(); - ImGui::Render(); - ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData()); - - } + if (Globals::renderer == Render_DirectX9) + return oPresent9((IDirect3DDevice9*)ptr, (RECT*)u1, (RECT*)u2, (HWND)u3, (RGNDATA*)u4); else - { - Globals::init_done = true; - - ImGuiStyle& style = ImGui::GetStyle(); - - DXGI_SWAP_CHAIN_DESC desc; - pSwapChain->GetDesc(&desc); - - pSwapChain->GetDevice(__uuidof(ID3D11Device), (void**)&Globals::device); - ID3D11DeviceContext* context; - reinterpret_cast(Globals::device)->GetImmediateContext(&context); - - ImGui::CreateContext(); - ImGui_ImplWin32_Init(desc.OutputWindow); - ImGui_ImplDX11_Init(reinterpret_cast(Globals::device), context); - ImGui_ImplWin32_EnableDpiAwareness(); - - io.IniFilename = NULL; - io.LogFilename = NULL; - - style.WindowTitleAlign = ImVec2(0.5, 0.5); - - oWndProc = (WNDPROC)SetWindowLongPtr(RsGlobal.ps->window, GWL_WNDPROC, (LONG)InputProc); - } - - return oPresent11(pSwapChain, SyncInterval, Flags); + return oPresent11((IDXGISwapChain*)ptr, u1, u2); } // Thanks imring @@ -198,23 +162,19 @@ Hook::Hook() { if (kiero::init(kiero::RenderType::D3D9) == kiero::Status::Success) { - if (kiero::bind(16, (void**)&oReset9, ResetDx9) == kiero::Status::Success - && kiero::bind(17, (void**)&oPresent9, PresentDx9) == kiero::Status::Success) - { - Globals::renderer = Render_DirectX9; + Globals::renderer = Render_DirectX9; + if (kiero::bind(16, (void**)&oReset9, Reset) == kiero::Status::Success + && kiero::bind(17, (void**)&oPresent9, Present) == kiero::Status::Success) flog << "Successfully hooked dx9 device." << std::endl; - } } else { // gtaRenderHook if (kiero::init(kiero::RenderType::D3D11) == kiero::Status::Success) { - if (kiero::bind(8, (void**)&oPresent11, PresentDx11) == kiero::Status::Success) - { - Globals::renderer = Render_DirectX11; + Globals::renderer = Render_DirectX11; + if (kiero::bind(8, (void**)&oPresent11, Present) == kiero::Status::Success) flog << "Successfully hooked dx11 device." << std::endl; - } } else { diff --git a/src/Hook.h b/src/Hook.h index dc08cd6..12a8c43 100644 --- a/src/Hook.h +++ b/src/Hook.h @@ -1,8 +1,8 @@ #pragma once -typedef HRESULT(CALLBACK *f_Reset)(IDirect3DDevice9*, D3DPRESENT_PARAMETERS*); typedef HRESULT(CALLBACK *f_Present9)(IDirect3DDevice9*, RECT*, RECT*, HWND, RGNDATA*); typedef HRESULT(CALLBACK *f_Present11)(IDXGISwapChain*, UINT, UINT); +typedef HRESULT(CALLBACK *f_Reset)(IDirect3DDevice9*, D3DPRESENT_PARAMETERS*); extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); @@ -10,20 +10,19 @@ class Hook { private: static WNDPROC oWndProc; - static f_Reset oReset9; static f_Present11 oPresent11; static f_Present9 oPresent9; + static f_Reset oReset9; static bool mouse_visibility; - static HRESULT CALLBACK ResetDx9(IDirect3DDevice9 * pDevice, D3DPRESENT_PARAMETERS* pPresentationParameters); - static HRESULT CALLBACK PresentDx9(IDirect3DDevice9 *pDevice, RECT* pSourceRect, RECT* pDestRect, HWND hDestWindowOverride, RGNDATA* pDirtyRegion); - static HRESULT CALLBACK PresentDx11(IDXGISwapChain* pSwapChain, UINT SyncInterval, UINT Flags); + static HRESULT CALLBACK Present(void *ptr, int u1, int u2, int u3 = NULL, int u4 = NULL); + static HRESULT CALLBACK Reset(IDirect3DDevice9 * pDevice, D3DPRESENT_PARAMETERS * pPresentationParameters); static LRESULT CALLBACK InputProc(const HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); + static void ShowMouse(bool state); protected: static bool show_mouse; static std::function window_func; - static void ShowMouse(bool state); public: Hook(); diff --git a/src/pch.cpp b/src/pch.cpp index ab92b9c..45472bb 100644 --- a/src/pch.cpp +++ b/src/pch.cpp @@ -3,7 +3,7 @@ std::string Globals::header_id = ""; int Globals::last_key_timer = 0; ImVec2 Globals::menu_size = ImVec2(screen::GetScreenWidth()/4, screen::GetScreenHeight()/1.2); -ImVec2 Globals::font_screen_size = ImVec2(-1, -1); +ImVec2 Globals::screen_size = ImVec2(-1, -1); bool Globals::show_menu = false; bool Globals::init_done = false; Renderer Globals::renderer = Render_Unknown; diff --git a/src/pch.h b/src/pch.h index 801bfc6..3b849f4 100644 --- a/src/pch.h +++ b/src/pch.h @@ -83,7 +83,7 @@ struct Globals static std::string header_id; static int last_key_timer; static ImVec2 menu_size; - static ImVec2 font_screen_size; + static ImVec2 screen_size; static bool show_menu; static bool init_done; static Renderer renderer; diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index 71299cf..8189086 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt @@ -18,6 +18,7 @@ set(test_files "../vendor/fla/Library/Library.h" "../vendor/fla/Library/Library.cpp" "Test.cpp" + "Dllmain.cpp" ) add_library(${PROJECT_NAME} SHARED ${test_files}) diff --git a/src/Dllmain.cpp b/src/tests/Dllmain.cpp similarity index 92% rename from src/Dllmain.cpp rename to src/tests/Dllmain.cpp index 3b7c09f..2bcaf01 100644 --- a/src/Dllmain.cpp +++ b/src/tests/Dllmain.cpp @@ -1,5 +1,5 @@ -// #include -// #include "../vendor\fla\Main.h" +#include +#include "../vendor\fla\Main.h" BOOL WINAPI DllMain( HINSTANCE hinstDLL, // handle to DLL module diff --git a/src/tests/Test.cpp b/src/tests/Test.cpp index 2e7c23a..7cca001 100644 --- a/src/tests/Test.cpp +++ b/src/tests/Test.cpp @@ -20,7 +20,7 @@ public: if(KeyPressed(VK_UP) && player && player->m_pVehicle) { - uint8_replacement &primary_color = *(uint8_replacement *)(int(player->m_pVehicle) + 0x434); + uint8_replacement &primary_color = *(uint8_replacement*)&player->m_pVehicle->m_nPrimaryColor; primary_color = 74; CHud::SetHelpMessage("Color changed",false,false,false); }