From b3d4fd10d0c6d35aa6073f8d26eee72f90de9fe2 Mon Sep 17 00:00:00 2001 From: Grinch_ Date: Mon, 10 Jan 2022 23:43:09 +0600 Subject: [PATCH] Code improvements, [III] Fix Set car speed crash --- src/animation.cpp | 5 ++- src/cheatmenu.cpp | 22 +++++++--- src/cheatmenu.h | 4 +- src/{hook.cpp => d3dhook.cpp} | 82 +++++++++++++++++++++++------------ src/d3dhook.h | 40 +++++++++++++++++ src/dllmain.cpp | 3 +- src/hook.h | 32 -------------- src/hotkeys.cpp | 2 +- src/hotkeys.h | 3 +- src/menuinfo.h | 2 +- src/neon.cpp | 2 +- src/neon.h | 2 +- src/paint.cpp | 2 +- src/paint.h | 2 +- src/pch.h | 2 +- src/util.cpp | 14 +++++- src/util.h | 1 + src/vehicle.cpp | 20 ++++----- tools/premake5.lua | 8 ++-- 19 files changed, 153 insertions(+), 95 deletions(-) rename src/{hook.cpp => d3dhook.cpp} (76%) create mode 100644 src/d3dhook.h delete mode 100644 src/hook.h diff --git a/src/animation.cpp b/src/animation.cpp index c7af8cb..1d170e8 100644 --- a/src/animation.cpp +++ b/src/animation.cpp @@ -6,7 +6,6 @@ #ifndef GTASA #include #include -#include "../depend/kiero/minhook/MinHook.h" #include "eAnimations.h" #include #endif @@ -15,6 +14,10 @@ #include #endif +#ifdef GTAVC +#include "../depend/kiero/minhook/MinHook.h" +#endif + #ifdef GTASA void Animation::PlayCutscene(std::string& rootKey, std::string& cutsceneId, std::string& interior) { diff --git a/src/cheatmenu.cpp b/src/cheatmenu.cpp index 5a6f65f..a5ab878 100644 --- a/src/cheatmenu.cpp +++ b/src/cheatmenu.cpp @@ -3,6 +3,7 @@ #include "menuinfo.h" #include "ui.h" #include "updater.h" +#include "d3dhook.h" void CheatMenu::DrawWindow() { @@ -15,7 +16,7 @@ void CheatMenu::DrawWindow() { gConfig.WriteToDisk(); bRunning = false; - m_bShowMouse = false; + D3dHook::SetMouseState(false); } } else @@ -76,9 +77,12 @@ void CheatMenu::DrawWindow() CheatMenu::CheatMenu() { - ImGui::CreateContext(); + if (!D3dHook::InjectHook(DrawWindow)) + { + return; + } + ApplyStyle(); - pCallbackFunc = std::bind(&DrawWindow); // Load menu settings Ui::m_HeaderId = gConfig.GetValue("window.idnum", -1); @@ -105,19 +109,25 @@ CheatMenu::CheatMenu() m_Commands::m_bShowMenu = !m_Commands::m_bShowMenu; } - if (m_bShowMouse != m_bShowMenu) + bool mouseState = D3dHook::GetMouseState(); + if (mouseState != m_bShowMenu) { - if (m_bShowMouse) // Only write when the menu closes + if (mouseState) // Only write when the menu closes { gConfig.WriteToDisk(); } - m_bShowMouse = m_bShowMenu; + D3dHook::SetMouseState(m_bShowMenu); } } }; } +CheatMenu::~CheatMenu() +{ + D3dHook::RemoveHook(); +} + void CheatMenu::ShowWelcomeScreen() { ImGui::BeginChild("WelcomeScreen"); diff --git a/src/cheatmenu.h b/src/cheatmenu.h index 00adfa7..8f69e8f 100644 --- a/src/cheatmenu.h +++ b/src/cheatmenu.h @@ -11,7 +11,6 @@ #pragma once #include "animation.h" #include "game.h" -#include "hook.h" #include "menu.h" #include "ped.h" #include "player.h" @@ -20,7 +19,7 @@ #include "visual.h" #include "weapon.h" -class CheatMenu : Hook, Animation, Game, Menu, Ped, Player, Teleport, Vehicle, Visual, Weapon +class CheatMenu : Animation, Game, Menu, Ped, Player, Teleport, Vehicle, Visual, Weapon { private: static inline bool m_bShowMenu = false; @@ -41,6 +40,7 @@ private: public: CheatMenu(); + ~CheatMenu(); static void ResetMenuSize(); }; diff --git a/src/hook.cpp b/src/d3dhook.cpp similarity index 76% rename from src/hook.cpp rename to src/d3dhook.cpp index 0a01446..7c310bc 100644 --- a/src/hook.cpp +++ b/src/d3dhook.cpp @@ -1,5 +1,5 @@ #include "pch.h" -#include "hook.h" +#include "d3dhook.h" #include "../depend/kiero/kiero.h" #include "../depend/kiero/minhook/MinHook.h" #include "../depend/imgui/imgui_impl_dx9.h" @@ -8,8 +8,19 @@ #include #define DIMOUSE ((LPDIRECTINPUTDEVICE8)(RsGlobal.ps->diMouse)) +extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); -LRESULT Hook::WndProc(const HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) +bool D3dHook::GetMouseState() +{ + return mouseShown; +} + +void D3dHook::SetMouseState(bool state) +{ + mouseShown = state; +} + +LRESULT D3dHook::hkWndProc(const HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { ImGui_ImplWin32_WndProcHandler(hWnd, uMsg, wParam, lParam); @@ -24,14 +35,14 @@ LRESULT Hook::WndProc(const HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) return CallWindowProc(oWndProc, hWnd, uMsg, wParam, lParam); } -HRESULT Hook::Reset(IDirect3DDevice9* pDevice, D3DPRESENT_PARAMETERS* pPresentationParameters) +HRESULT D3dHook::hkReset(IDirect3DDevice9* pDevice, D3DPRESENT_PARAMETERS* pPresentationParameters) { ImGui_ImplDX9_InvalidateDeviceObjects(); return oReset(pDevice, pPresentationParameters); } -void Hook::RenderFrame(void* ptr) +void D3dHook::ProcessFrame(void* ptr) { if (!ImGui::GetCurrentContext()) { @@ -43,7 +54,7 @@ void Hook::RenderFrame(void* ptr) if (bInit) { - ShowMouse(m_bShowMouse); + ProcessMouse(); // Scale the menu if game resolution changed static ImVec2 fScreenSize = ImVec2(-1, -1); @@ -88,7 +99,7 @@ void Hook::RenderFrame(void* ptr) if (pCallbackFunc != nullptr) { - pCallbackFunc(); + ((void(*)())pCallbackFunc)(); } ImGui::EndFrame(); @@ -137,23 +148,23 @@ void Hook::RenderFrame(void* ptr) io.IniFilename = nullptr; io.LogFilename = nullptr; io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; - oWndProc = (WNDPROC)SetWindowLongPtr(RsGlobal.ps->window, GWL_WNDPROC, (LRESULT)WndProc); + oWndProc = (WNDPROC)SetWindowLongPtr(RsGlobal.ps->window, GWL_WNDPROC, (LRESULT)hkWndProc); } } -HRESULT Hook::Dx9Handler(IDirect3DDevice9* pDevice) +HRESULT D3dHook::hkEndScene(IDirect3DDevice9* pDevice) { - RenderFrame(pDevice); + ProcessFrame(pDevice); return oEndScene(pDevice); } -HRESULT Hook::Dx11Handler(IDXGISwapChain* pSwapChain, UINT SyncInterval, UINT Flags) +HRESULT D3dHook::hkPresent(IDXGISwapChain* pSwapChain, UINT SyncInterval, UINT Flags) { - RenderFrame(pSwapChain); - return oPresent11(pSwapChain, SyncInterval, Flags); + ProcessFrame(pSwapChain); + return oPresent(pSwapChain, SyncInterval, Flags); } -void Hook::ShowMouse(bool state) +void D3dHook::ProcessMouse() { // Disable player controls for controllers bool bMouseDisabled = false; @@ -163,7 +174,7 @@ void Hook::ShowMouse(bool state) isController = !isController; #endif - if (isController && (state || bMouseDisabled)) + if (isController && (mouseShown || bMouseDisabled)) { #ifdef GTASA @@ -175,12 +186,12 @@ void Hook::ShowMouse(bool state) if (pad) { - if (state) + if (mouseShown) { bMouseDisabled = true; #ifdef GTA3 pad->m_bDisablePlayerControls = true; -#else //GTAVC & GTASA +#else pad->DisablePlayerControls = true; #endif } @@ -189,18 +200,19 @@ void Hook::ShowMouse(bool state) bMouseDisabled = false; #ifdef GTA3 pad->m_bDisablePlayerControls = false; -#else //GTAVC & GTASA +#else pad->DisablePlayerControls = false; #endif } } } - if (m_bMouseVisibility != state) + static bool mouseState; + if (mouseState != mouseShown) { - ImGui::GetIO().MouseDrawCursor = state; + ImGui::GetIO().MouseDrawCursor = mouseShown; - if (state) + if (mouseShown) { patch::SetUChar(BY_GAME(0x6194A0, 0x6020A0, 0x580D20), 0xC3); // psSetMousePos @@ -227,19 +239,27 @@ void Hook::ShowMouse(bool state) CPad::ClearMouseHistory(); #endif CPad::UpdatePads(); - m_bMouseVisibility = state; + mouseState = mouseShown; } } -Hook::Hook() +bool D3dHook::InjectHook(void *pCallback) { - - // Nvidia Overlay crash fix + ImGui::CreateContext(); + + /* + Must check for d3d9 first! + Seems to crash with nvidia geforce experience overlay + if anything else is checked before d3d9 + */ if (init(kiero::RenderType::D3D9) == kiero::Status::Success) { gRenderer = Render_DirectX9; - kiero::bind(16, (void**)&oReset, Reset); - kiero::bind(42, (void**)&oEndScene, Dx9Handler); + kiero::bind(16, (void**)&oReset, hkReset); + kiero::bind(42, (void**)&oEndScene, hkEndScene); + pCallbackFunc = pCallback; + + return true; } else { @@ -247,13 +267,19 @@ Hook::Hook() if (init(kiero::RenderType::D3D11) == kiero::Status::Success) { gRenderer = Render_DirectX11; - kiero::bind(8, (void**)&oPresent11, Dx11Handler); + kiero::bind(8, (void**)&oPresent, hkPresent); + pCallbackFunc = pCallback; + + return true; } } + + return false; } -Hook::~Hook() +void D3dHook::RemoveHook() { + pCallbackFunc = nullptr; SetWindowLongPtr(RsGlobal.ps->window, GWL_WNDPROC, (LRESULT)oWndProc); ImGui_ImplDX9_Shutdown(); ImGui_ImplWin32_Shutdown(); diff --git a/src/d3dhook.h b/src/d3dhook.h new file mode 100644 index 0000000..3a3ccf9 --- /dev/null +++ b/src/d3dhook.h @@ -0,0 +1,40 @@ +#pragma once +#include "pch.h" + +class D3dHook +{ +private: + using f_EndScene = HRESULT(CALLBACK*)(IDirect3DDevice9*); + using f_Present11 = HRESULT(CALLBACK*)(IDXGISwapChain*, UINT, UINT); + using f_Reset = HRESULT(CALLBACK*)(IDirect3DDevice9*, D3DPRESENT_PARAMETERS*); + + static inline WNDPROC oWndProc; + static inline f_Present11 oPresent; + static inline f_EndScene oEndScene; + static inline f_Reset oReset; + static inline bool mouseShown; + static inline void* pCallbackFunc = nullptr; + + + static void CALLBACK ProcessFrame(void* ptr); + static LRESULT CALLBACK hkWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); + static void ProcessMouse(); + + // DirectX9 + static HRESULT CALLBACK hkEndScene(IDirect3DDevice9* pDevice); + static HRESULT CALLBACK hkReset(IDirect3DDevice9* pDevice, D3DPRESENT_PARAMETERS* pPresentationParameters); + + // DirectX11, Renderhook + static HRESULT CALLBACK hkPresent(IDXGISwapChain* pSwapChain, UINT SyncInterval, UINT Flags); + +public: + + D3dHook() = delete; + D3dHook(D3dHook const&) = delete; + void operator=(D3dHook const&) = delete; + + static bool GetMouseState(); + static bool InjectHook(void *pCallback); + static void RemoveHook(); + static void SetMouseState(bool state); +}; diff --git a/src/dllmain.cpp b/src/dllmain.cpp index 9259d21..98ec276 100644 --- a/src/dllmain.cpp +++ b/src/dllmain.cpp @@ -1,8 +1,8 @@ #include "pch.h" -#include "hook.h" #include "cheatmenu.h" #include "updater.h" #include "menuinfo.h" +#include "d3dhook.h" void MenuThread(void* param) { @@ -62,6 +62,7 @@ void MenuThread(void* param) gLog << "Starting...\nVersion: " MENU_TITLE "\nAuthor: Grinch_\nDiscord: " DISCORD_INVITE "\nMore Info: " GITHUB_LINK "\n" << std::endl; + CheatMenu menu; diff --git a/src/hook.h b/src/hook.h deleted file mode 100644 index 300e57c..0000000 --- a/src/hook.h +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once -#include "pch.h" - -using f_EndScene = HRESULT(CALLBACK*)(IDirect3DDevice9*); -using f_Present11 = HRESULT(CALLBACK*)(IDXGISwapChain*, UINT, UINT); -using f_Reset = HRESULT(CALLBACK*)(IDirect3DDevice9*, D3DPRESENT_PARAMETERS*); - -extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); - -class Hook -{ -private: - static inline WNDPROC oWndProc; - static inline f_Present11 oPresent11; - static inline f_EndScene oEndScene; - static inline f_Reset oReset; - static inline bool m_bMouseVisibility; - - static void CALLBACK RenderFrame(void* ptr); - static HRESULT CALLBACK Dx9Handler(IDirect3DDevice9* pDevice); - static HRESULT CALLBACK Dx11Handler(IDXGISwapChain* pSwapChain, UINT SyncInterval, UINT Flags); - static HRESULT CALLBACK Reset(IDirect3DDevice9* pDevice, D3DPRESENT_PARAMETERS* pPresentationParameters); - static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); - static void ShowMouse(bool state); - -public: - static inline bool m_bShowMouse = false; - static inline std::function pCallbackFunc = nullptr; - - Hook(); - ~Hook(); -}; diff --git a/src/hotkeys.cpp b/src/hotkeys.cpp index aff2459..9b787f6 100644 --- a/src/hotkeys.cpp +++ b/src/hotkeys.cpp @@ -17,7 +17,7 @@ Hotkey vehInstantStop; bool Hotkey::DrawUI(const char* label) { - bool active = m_CurrentHotkey == label; + bool active = (m_CurrentHotkey == label); bool state = false; if (active) diff --git a/src/hotkeys.h b/src/hotkeys.h index f880bf6..986c9bb 100644 --- a/src/hotkeys.h +++ b/src/hotkeys.h @@ -12,8 +12,7 @@ private: bool m_bPressed; public: - int m_key1; - int m_key2; + int m_key1, m_key2; Hotkey(int key1 = -1, int key2 = -1) : m_key1(key1), m_key2(key2) diff --git a/src/menuinfo.h b/src/menuinfo.h index e244e67..cfa928e 100644 --- a/src/menuinfo.h +++ b/src/menuinfo.h @@ -2,5 +2,5 @@ #define MENU_NAME "Cheat Menu" #define MENU_VERSION_NUMBER "3.1" #define MENU_VERSION MENU_VERSION_NUMBER"-beta" -#define BUILD_NUMBER "20220103" +#define BUILD_NUMBER "20220110" #define MENU_TITLE MENU_NAME " v" MENU_VERSION diff --git a/src/neon.cpp b/src/neon.cpp index 4e38ccb..16a49d1 100644 --- a/src/neon.cpp +++ b/src/neon.cpp @@ -178,7 +178,7 @@ static RwTexture* LoadTextureFromMemory(char* data, unsigned int size) return RwTextureCreate(raster); } -void Neon::InitHooks() +void Neon::InjectHooks() { static bool init; if (init) diff --git a/src/neon.h b/src/neon.h index 2387ede..fe54f8f 100644 --- a/src/neon.h +++ b/src/neon.h @@ -35,7 +35,7 @@ public: Neon() = delete; Neon(Neon&) = delete; - static void InitHooks(); + static void InjectHooks(); static void Install(CVehicle* veh, int red, int green, int blue); static bool IsInstalled(CVehicle* veh); static bool IsPulsingEnabled(CVehicle* veh); diff --git a/src/paint.cpp b/src/paint.cpp index 0131f5a..ee652da 100644 --- a/src/paint.cpp +++ b/src/paint.cpp @@ -27,7 +27,7 @@ #include "NodeName.h" #include "util.h" -void Paint::InitHooks() +void Paint::InjectHooks() { static bool init; diff --git a/src/paint.h b/src/paint.h index 2c869d3..df8b9a9 100644 --- a/src/paint.h +++ b/src/paint.h @@ -76,7 +76,7 @@ private: public: static inline ResourceStore m_TextureData { "textures", eResourceType::TYPE_IMAGE, ImVec2(100, 80) }; - static void InitHooks(); + static void InjectHooks(); static void GenerateNodeList(CVehicle* pVeh, std::vector& names_vec); static void SetNodeColor(CVehicle* pVeh, std::string node_name, CRGBA color, bool filter_mat = false); static void SetNodeTexture(CVehicle* pVeh, std::string node_name, std::string texturename, bool filter_mat = false); diff --git a/src/pch.h b/src/pch.h index 93cad05..e367a67 100644 --- a/src/pch.h +++ b/src/pch.h @@ -70,7 +70,7 @@ #include "json.h" #include "hotkeys.h" -#include "vKeys.h" +#include "vkeys.h" #include "resourcestore.h" #include "fontmgr.h" diff --git a/src/util.cpp b/src/util.cpp index b03f6e7..ec0a9d3 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -2,9 +2,21 @@ #include "util.h" #include "psapi.h" +void Util::SetCarForwardSpeed(CVehicle *pVeh, float speed) +{ +#ifdef GTA3 + CVector inVec = pVeh->m_matrix.up; + double speedFactor = speed * 0.016766668; + pVeh->m_vecMoveSpeed.x = speedFactor * inVec.x; + pVeh->m_vecMoveSpeed.y = speedFactor * inVec.y; + pVeh->m_vecMoveSpeed.z = speedFactor * inVec.z; +#else + Command(CPools::GetVehicleRef(pVeh), 0); +#endif +} + std::string Util::GetLocationName(CVector* pos) { - #ifdef GTASA CPlayerPed *pPlayer = FindPlayerPed(); int hplayer = CPools::GetPedRef(pPlayer); diff --git a/src/util.h b/src/util.h index d0f4b83..17d7a0c 100644 --- a/src/util.h +++ b/src/util.h @@ -20,6 +20,7 @@ public: static int GetLargestGangInZone(); #endif + static void SetCarForwardSpeed(CVehicle *pVeh, float speed); static CPed* GetClosestPed(); static CVehicle* GetClosestVehicle(); static void GetCPUUsageInit(); diff --git a/src/vehicle.cpp b/src/vehicle.cpp index 3c84def..8102acf 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -73,8 +73,8 @@ Vehicle::Vehicle() { #ifdef GTASA FileHandler::FetchHandlingID(m_VehicleIDE); - Neon::InitHooks(); - Paint::InitHooks(); + Neon::InjectHooks(); + Paint::InjectHooks(); #endif FileHandler::FetchColorData(m_CarcolsColorData); @@ -120,12 +120,12 @@ Vehicle::Vehicle() if (vehInstantStart.Pressed()) { - Command(hveh, 40.0f); + Util::SetCarForwardSpeed(pVeh, 40.0f); } if (vehInstantStop.Pressed()) { - Command(hveh, 0); + Util::SetCarForwardSpeed(pVeh, 0.0f); } if (m_bNoDamage) @@ -157,7 +157,7 @@ Vehicle::Vehicle() if (m_bLockSpeed) { - Command(hveh, m_fLockSpeed); + Util::SetCarForwardSpeed(pVeh, m_fLockSpeed); } #ifdef GTASA @@ -457,7 +457,7 @@ void Vehicle::SpawnVehicle(std::string& rootkey, std::string& vehName, std::stri if (m_Spawner::m_bSpawnInside) { Command(hplayer, hveh); - Command(hveh, speed); + Util::SetCarForwardSpeed(veh, speed); } Command(hveh); Command(hveh); @@ -497,9 +497,7 @@ void Vehicle::SpawnVehicle(std::string& rootkey, std::string& vehName, std::stri #endif Command(hplayer, hveh); -#ifndef GTA3 - Command(hveh, speed); -#endif + Util::SetCarForwardSpeed(veh, speed); } else { @@ -1025,14 +1023,14 @@ void Vehicle::Draw() if (ImGui::Button("Set speed##brn", ImVec2(Ui::GetSize(2)))) { - Command(hVeh, m_fLockSpeed); + Util::SetCarForwardSpeed(pVeh, m_fLockSpeed); } ImGui::SameLine(); if (ImGui::Button("Instant stop##brn", ImVec2(Ui::GetSize(2)))) { - Command(hVeh, 0); + Util::SetCarForwardSpeed(pVeh, 0.0f); } } } diff --git a/tools/premake5.lua b/tools/premake5.lua index e1595aa..d46aa69 100644 --- a/tools/premake5.lua +++ b/tools/premake5.lua @@ -58,8 +58,8 @@ project "CheatMenuIII" "../src/cheatmenu.cpp", "../src/pch.h", "../src/pch.cpp", - "../src/hook.h", - "../src/hook.cpp", + "../src/d3dhook.h", + "../src/d3dhook.cpp", "../src/updater.h", "../src/updater.cpp", "../src/json.h", @@ -151,8 +151,8 @@ project "CheatMenuVC" "../src/cheatmenu.cpp", "../src/pch.h", "../src/pch.cpp", - "../src/hook.h", - "../src/hook.cpp", + "../src/d3dhook.h", + "../src/d3dhook.cpp", "../src/updater.h", "../src/updater.cpp", "../src/json.h",