Refactored code

This commit is contained in:
Grinch_ 2021-01-11 10:07:10 -08:00
parent 5ad3db3170
commit 1ad5b46e0d
8 changed files with 57 additions and 97 deletions

View File

@ -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",

View File

@ -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<IDirect3DDevice9*>(Globals::device));
}
else
{
// for dx11 device ptr is swapchain
reinterpret_cast<IDXGISwapChain*>(ptr)->GetDevice(__uuidof(ID3D11Device), (void**)&Globals::device);
ID3D11DeviceContext* context;
reinterpret_cast<ID3D11Device*>(Globals::device)->GetImmediateContext(&context);
ImGui_ImplDX11_Init(reinterpret_cast<ID3D11Device*>(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<ID3D11Device*>(Globals::device)->GetImmediateContext(&context);
ImGui::CreateContext();
ImGui_ImplWin32_Init(desc.OutputWindow);
ImGui_ImplDX11_Init(reinterpret_cast<ID3D11Device*>(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
{

View File

@ -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<void()> window_func;
static void ShowMouse(bool state);
public:
Hook();

View File

@ -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;

View File

@ -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;

View File

@ -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})

View File

@ -1,5 +1,5 @@
// #include <Windows.h>
// #include "../vendor\fla\Main.h"
#include <Windows.h>
#include "../vendor\fla\Main.h"
BOOL WINAPI DllMain(
HINSTANCE hinstDLL, // handle to DLL module

View File

@ -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);
}