From 7b0fa7c7f669fe9a1d507712effd744a43b5d333 Mon Sep 17 00:00:00 2001 From: Grinch_ Date: Wed, 8 Jun 2022 03:55:49 +0630 Subject: [PATCH] Refactor hotkey class --- src/game.cpp | 10 ++-- src/hotkeys.cpp | 66 ++++++++++----------- src/hotkeys.h | 21 ++++--- src/json.h | 2 +- src/menu.cpp | 150 ++++++------------------------------------------ src/pch.cpp | 33 ++++++----- src/pch.h | 1 - src/rpc.h | 7 +-- src/version.h | 2 +- 9 files changed, 89 insertions(+), 203 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index 13b93c7..e4a0df4 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -290,7 +290,7 @@ void Game::FreeCam() m_Freecam::m_fTotalMouse.y = -150; } - if (freeCamTeleportPlayer.Pressed()) + if (KeyPressed(VK_RETURN)) { CPlayerPed* player = FindPlayerPed(); CVector pos = m_Freecam::m_pPed->GetPosition(); @@ -321,9 +321,9 @@ void Game::FreeCam() speed *= 2; } - if (freeCamForward.Pressed() || freeCamBackward.Pressed()) + if (freeCamForward.PressedBasic() || freeCamBackward.PressedBasic()) { - if (freeCamForward.Pressed()) + if (freeCamBackward.PressedBasic()) { speed *= -1; } @@ -335,9 +335,9 @@ void Game::FreeCam() pos.z += speed * 2 * sin(m_Freecam::m_fTotalMouse.y / 3 * 3.14159f / 180.0f); } - if (freeCamLeft.Pressed() || freeCamRight.Pressed()) + if (freeCamLeft.PressedBasic() || freeCamRight.PressedBasic()) { - if (freeCamLeft.Pressed()) + if (freeCamLeft.PressedBasic()) { speed *= -1; } diff --git a/src/hotkeys.cpp b/src/hotkeys.cpp index dca3a75..98d821d 100644 --- a/src/hotkeys.cpp +++ b/src/hotkeys.cpp @@ -1,17 +1,31 @@ #include "pch.h" #include "hotkeys.h" +const size_t HOTKEY_START = 3; +const size_t HOTKEY_END = 135; + +Hotkey::Hotkey(int key1, int key2, const std::string& configPath) + : m_key1(key1), m_key2(key2), m_ConfigPath("Hotkeys." + configPath) +{ + if (m_ConfigPath != "") + { + m_key1 = gConfig.GetValue(m_ConfigPath + ".1", m_key1); + m_key2 = gConfig.GetValue(m_ConfigPath + ".2", m_key2); + } +} + bool Hotkey::DrawUI(const char* label) { bool active = (m_CurrentHotkey == label); bool state = false; + // Check for pressed keys if (active) { ImGui::PushStyleColor(ImGuiCol_Button, ImGui::GetStyle().Colors[ImGuiCol_ButtonActive]); ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImGui::GetStyle().Colors[ImGuiCol_ButtonActive]); - for (int key = 3; key != 135; ++key) + for (size_t key = HOTKEY_START; key != HOTKEY_END; ++key) { if (KeyPressed(key)) { @@ -20,7 +34,7 @@ bool Hotkey::DrawUI(const char* label) } } - for (int key = 135; key != 3; --key) + for (size_t key = HOTKEY_END; key != HOTKEY_START; --key) { if (KeyPressed(key)) { @@ -30,24 +44,9 @@ bool Hotkey::DrawUI(const char* label) } } - std::string text; - - if (m_key1 != VK_NONE) - { - text = key_names[m_key1 - 1]; - } - else - { - text = "None"; - } - - if (m_key1 != m_key2) - { - text += (" + " + key_names[m_key2 - 1]); - } - - if (ImGui::Button((text + std::string("##") + std::string(label)).c_str(), - ImVec2(ImGui::GetWindowContentRegionWidth() / 3.5, ImGui::GetFrameHeight()*1.35f))) + std::string btnText = GetNameString(); + ImVec2 size {ImGui::GetWindowContentRegionWidth() / 3.5f, ImGui::GetFrameHeight()*1.35f}; + if (ImGui::Button(std::format("{}##{}", btnText, label).c_str(), size)) { if (!active) { @@ -59,8 +58,17 @@ bool Hotkey::DrawUI(const char* label) { m_CurrentHotkey = ""; state = true; + + // Save the hotkeys in config file + if (m_ConfigPath != "") + { + gConfig.SetValue(m_ConfigPath + "1", m_key1); + gConfig.SetValue(m_ConfigPath + "2", m_key2); + } } + + // Reset hotkeys to NONE on right click if (ImGui::IsMouseClicked(ImGuiMouseButton_Right)) { if (ImGui::IsItemHovered()) @@ -104,23 +112,17 @@ bool Hotkey::Pressed() return false; } +bool Hotkey::PressedBasic() +{ + return (m_CurrentHotkey == "") ? KeyPressed(m_key1) && KeyPressed(m_key2) : false; +} + std::string Hotkey::GetNameString() { - std::string text; - - if (m_key1 != VK_NONE) - { - text = key_names[m_key1 - 1]; - } - else - { - text = "None"; - } - + std::string text = (m_key1 == VK_NONE) ? "None" : key_names[m_key1 - 1]; if (m_key1 != m_key2) { text += (" + " + key_names[m_key2 - 1]); } - return text; } \ No newline at end of file diff --git a/src/hotkeys.h b/src/hotkeys.h index b98b3d7..6ae281b 100644 --- a/src/hotkeys.h +++ b/src/hotkeys.h @@ -2,24 +2,29 @@ #include /* - ImGui HotKey Implementation class - Handles hotkeys and hotkey-related functions + HotKey Implementation Class + Handles events, save-load, name-fetch, ui etc. */ class Hotkey { private: static inline std::string m_CurrentHotkey; bool m_bPressed; + int m_key1, m_key2; + std::string m_ConfigPath; // config save path public: - int m_key1, m_key2; - - Hotkey(int key1 = -1, int key2 = -1) - : m_key1(key1), m_key2(key2) - {} + Hotkey(int key1 = -1, int key2 = -1, const std::string& configPath = ""); // Draws ui to change the hotkeys from frontend bool DrawUI(const char* label); - bool Pressed(); + + // Returns formatted name of the hotkeys std::string GetNameString(); + + // Returns true if the hotkeys are pressed + bool Pressed(); + + // Returns true if the hotkeys are pressed, with no checks + bool PressedBasic(); }; diff --git a/src/json.h b/src/json.h index 1dde727..74f44b3 100644 --- a/src/json.h +++ b/src/json.h @@ -20,7 +20,7 @@ public: // specialize since typeid(std::string) doesn't work template - T inline GetValue(std::string&& key, T&& defaultVal) + T inline GetValue(std::string&& key, const T& defaultVal) { try { diff --git a/src/menu.cpp b/src/menu.cpp index 1bd9459..348ecec 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -34,55 +34,6 @@ void Menu::Init() m_Overlay::textColor[3] = gConfig.GetValue("overlay.text_color.a", 1.0f); m_bDiscordRPC = gConfig.GetValue("menu.discord_rpc", false); - // Hotkeys - aimSkinChanger.m_key1 = gConfig.GetValue("hotkey.aim_skin_changer.key1", VK_RETURN); - aimSkinChanger.m_key2 = gConfig.GetValue("hotkey.aim_skin_changer.key2", VK_RETURN); - - freeCam.m_key1 = gConfig.GetValue("hotkey.freecam.key1", VK_F6); - freeCam.m_key2 = gConfig.GetValue("hotkey.freecam.key2", VK_F6); - - freeCamForward.m_key1 = gConfig.GetValue("hotkey.freeCamForward.key1", VK_KEY_I); - freeCamForward.m_key2 = gConfig.GetValue("hotkey.freeCamForward.key2", VK_KEY_I); - - freeCamBackward.m_key1 = gConfig.GetValue("hotkey.freeCamBackward.key1", VK_KEY_K); - freeCamBackward.m_key2 = gConfig.GetValue("hotkey.freeCamBackward.key2", VK_KEY_K); - - freeCamLeft.m_key1 = gConfig.GetValue("hotkey.freeCamLeft.key1", VK_KEY_J); - freeCamLeft.m_key2 = gConfig.GetValue("hotkey.freeCamLeft.key2", VK_KEY_J); - - freeCamRight.m_key1 = gConfig.GetValue("hotkey.freeCamRight.key1", VK_KEY_L); - freeCamRight.m_key2 = gConfig.GetValue("hotkey.freeCamRight.key2", VK_KEY_L); - - quickSceenShot.m_key1 = gConfig.GetValue("hotkey.quick_screenshot.key1", VK_F5); - quickSceenShot.m_key2 = gConfig.GetValue("hotkey.quick_screenshot.key2", VK_F5); - - quickTeleport.m_key1 = gConfig.GetValue("hotkey.quick_tp.key1", VK_KEY_X); - quickTeleport.m_key2 = gConfig.GetValue("hotkey.quick_tp.key2", VK_KEY_Y); - - menuOpen.m_key1 = gConfig.GetValue("hotkey.menu_open.key1", VK_LCONTROL); - menuOpen.m_key2 = gConfig.GetValue("hotkey.menu_open.key2", VK_KEY_M); - - commandWindow.m_key1 = gConfig.GetValue("hotkey.command_window.key1", VK_LMENU); - commandWindow.m_key2 = gConfig.GetValue("hotkey.command_window.key2", VK_KEY_C); - - flipVeh.m_key1 = gConfig.GetValue("hotkey.flip_veh.key1", VK_NONE); - flipVeh.m_key2 = gConfig.GetValue("hotkey.flip_veh.key2", VK_NONE); - - fixVeh.m_key1 = gConfig.GetValue("hotkey.fix_veh.key1", VK_NONE); - fixVeh.m_key2 = gConfig.GetValue("hotkey.fix_veh.key2", VK_NONE); - - godMode.m_key1 = gConfig.GetValue("hotkey.god_mode.key1", VK_NONE); - godMode.m_key2 = gConfig.GetValue("hotkey.god_mode.key2", VK_NONE); - - vehEngine.m_key1 = gConfig.GetValue("hotkey.veh_engine.key1", VK_NONE); - vehEngine.m_key2 = gConfig.GetValue("hotkey.veh_engine.key2", VK_NONE); - - vehInstantStart.m_key1 = gConfig.GetValue("hotkey.veh_instant_start.key1", VK_NONE); - vehInstantStart.m_key2 = gConfig.GetValue("hotkey.veh_instant_start.key2", VK_NONE); - - vehInstantStop.m_key1 = gConfig.GetValue("hotkey.veh_instant_stop.key1", VK_NONE); - vehInstantStop.m_key2 = gConfig.GetValue("hotkey.veh_instant_stop.key2", VK_NONE); - Util::GetCPUUsageInit(); MEMORYSTATUSEX memInfo; memInfo.dwLength = sizeof(MEMORYSTATUSEX); @@ -499,97 +450,28 @@ void Menu::ShowPage() Ui::ShowTooltip(TEXT("Menu.UsageText")); ImGui::Spacing(); ImGui::BeginChild("Hotkeys"); - if (menuOpen.DrawUI(TEXT("Menu.OpenMenuKey"))) - { - gConfig.SetValue("hotkey.menu_open.key1", menuOpen.m_key1); - gConfig.SetValue("hotkey.menu_open.key2", menuOpen.m_key2); - } - if (commandWindow.DrawUI(TEXT("Menu.OpenCMDKey"))) - { - gConfig.SetValue("hotkey.command_window.key1", commandWindow.m_key1); - gConfig.SetValue("hotkey.command_window.key2", commandWindow.m_key2); - } + menuOpen.DrawUI(TEXT("Menu.OpenMenuKey")); + commandWindow.DrawUI(TEXT("Menu.OpenCMDKey")); ImGui::Dummy(ImVec2(0, 10)); - if (aimSkinChanger.DrawUI(TEXT("Menu.SkinChangerKey"))) - { - gConfig.SetValue("hotkey.aim_skin_changer.key1", aimSkinChanger.m_key1); - gConfig.SetValue("hotkey.aim_skin_changer.key2", aimSkinChanger.m_key2); - } - if (quickSceenShot.DrawUI(TEXT("Menu.QuickSSKey"))) - { - gConfig.SetValue("hotkey.quick_screenshot.key1", quickSceenShot.m_key1); - gConfig.SetValue("hotkey.quick_screenshot.key2", quickSceenShot.m_key2); - } - if (freeCam.DrawUI(TEXT("Menu.FreecamKey"))) - { - gConfig.SetValue("hotkey.freecam.key1", freeCam.m_key1); - gConfig.SetValue("hotkey.freecam.key2", freeCam.m_key2); - } - if (freeCamForward.DrawUI(TEXT("Menu.FreecamForwardKey"))) - { - gConfig.SetValue("hotkey.freeCamForward.key1", freeCamForward.m_key1); - gConfig.SetValue("hotkey.freeCamForward.key2", freeCamForward.m_key2); - } - if (freeCamBackward.DrawUI(TEXT("Menu.FreecamBackwardKey"))) - { - gConfig.SetValue("hotkey.freeCamBackward.key1", freeCamBackward.m_key1); - gConfig.SetValue("hotkey.freeCamBackward.key2", freeCamBackward.m_key2); - } - if (freeCamLeft.DrawUI(TEXT("Menu.FreecamLeftKey"))) - { - gConfig.SetValue("hotkey.freeCamLeft.key1", freeCamLeft.m_key1); - gConfig.SetValue("hotkey.freeCamLeft.key2", freeCamLeft.m_key2); - } - if (freeCamRight.DrawUI(TEXT("Menu.FreecamRightKey"))) - { - gConfig.SetValue("hotkey.freeCamRight.key1", freeCamRight.m_key1); - gConfig.SetValue("hotkey.freeCamRight.key2", freeCamRight.m_key2); - } - if (quickTeleport.DrawUI(TEXT("Menu.QuickTPKey"))) - { - gConfig.SetValue("hotkey.quick_tp.key1", quickTeleport.m_key1); - gConfig.SetValue("hotkey.quick_tp.key2", quickTeleport.m_key2); - } + aimSkinChanger.DrawUI(TEXT("Menu.SkinChangerKey")); + quickSceenShot.DrawUI(TEXT("Menu.QuickSSKey")); + freeCam.DrawUI(TEXT("Menu.FreecamKey")); + freeCamForward.DrawUI(TEXT("Menu.FreecamForwardKey")); + freeCamBackward.DrawUI(TEXT("Menu.FreecamBackwardKey")); + freeCamLeft.DrawUI(TEXT("Menu.FreecamLeftKey")); + freeCamRight.DrawUI(TEXT("Menu.FreecamRightKey")); + quickTeleport.DrawUI(TEXT("Menu.QuickTPKey")); ImGui::Dummy(ImVec2(0, 10)); - if (fixVeh.DrawUI(TEXT("Menu.FixVehKey"))) - { - gConfig.SetValue("hotkey.fix_veh.key1", fixVeh.m_key1); - gConfig.SetValue("hotkey.fix_veh.key2", fixVeh.m_key2); - } - - if (flipVeh.DrawUI(TEXT("Menu.FlipVehKey"))) - { - gConfig.SetValue("hotkey.flip_veh.key1", flipVeh.m_key1); - gConfig.SetValue("hotkey.flip_veh.key2", flipVeh.m_key2); - } - - if (godMode.DrawUI(TEXT("Menu.GodModeKey"))) - { - gConfig.SetValue("hotkey.god_mode.key1", godMode.m_key1); - gConfig.SetValue("hotkey.god_mode.key2", godMode.m_key2); - } - - if (vehEngine.DrawUI(TEXT("Menu.VehEngineKey"))) - { - gConfig.SetValue("hotkey.veh_engine.key1", vehEngine.m_key1); - gConfig.SetValue("hotkey.veh_engine.key2", vehEngine.m_key2); - } - - if (vehInstantStart.DrawUI(TEXT("Menu.VehStartKey"))) - { - gConfig.SetValue("hotkey.veh_instant_start.key1", vehInstantStart.m_key1); - gConfig.SetValue("hotkey.veh_instant_start.key2", vehInstantStart.m_key2); - } - - if (vehInstantStop.DrawUI(TEXT("Menu.VehStopKey"))) - { - gConfig.SetValue("hotkey.veh_instant_stop.key1", vehInstantStop.m_key1); - gConfig.SetValue("hotkey.veh_instant_stop.key2", vehInstantStop.m_key2); - } + fixVeh.DrawUI(TEXT("Menu.FixVehKey")); + flipVeh.DrawUI(TEXT("Menu.FlipVehKey")); + godMode.DrawUI(TEXT("Menu.GodModeKey")); + vehEngine.DrawUI(TEXT("Menu.VehEngineKey")); + vehInstantStart.DrawUI(TEXT("Menu.VehStartKey")); + vehInstantStop.DrawUI(TEXT("Menu.VehStopKey")); ImGui::Dummy(ImVec2(0, 10)); diff --git a/src/pch.cpp b/src/pch.cpp index 68d705a..c7edae7 100644 --- a/src/pch.cpp +++ b/src/pch.cpp @@ -3,20 +3,19 @@ eRenderer gRenderer = Render_Unknown; std::ofstream gLog = std::ofstream("CheatMenu.log"); CJson gConfig = CJson("config"); -Hotkey aimSkinChanger; -Hotkey freeCam; -Hotkey freeCamForward; -Hotkey freeCamBackward; -Hotkey freeCamLeft; -Hotkey freeCamRight; -Hotkey commandWindow; -Hotkey fixVeh; -Hotkey flipVeh; -Hotkey freeCamTeleportPlayer; -Hotkey godMode; -Hotkey menuOpen; -Hotkey quickSceenShot; -Hotkey quickTeleport; -Hotkey vehEngine; -Hotkey vehInstantStart; -Hotkey vehInstantStop; \ No newline at end of file +Hotkey aimSkinChanger {VK_RETURN, VK_RETURN, "AimSkinChanger"}; +Hotkey freeCam {VK_F6, VK_F6, "Freecam.Toggle"}; +Hotkey freeCamForward {VK_KEY_I, VK_KEY_I, "Freecam.Forward"};; +Hotkey freeCamBackward {VK_KEY_K, VK_KEY_K, "Freecam.Backward"};; +Hotkey freeCamLeft {VK_KEY_J, VK_KEY_J, "Freecam.Left"};; +Hotkey freeCamRight {VK_KEY_L, VK_KEY_L, "Freecam.Right"};; +Hotkey commandWindow {VK_LCONTROL, VK_KEY_C, "CommandWindowToggle"};; +Hotkey fixVeh {VK_NONE, VK_NONE, "Vehicle.Fix"}; +Hotkey flipVeh {VK_NONE, VK_NONE, "Vehicle.Flip"}; +Hotkey godMode {VK_NONE, VK_NONE, "GodMode"}; +Hotkey menuOpen {VK_LCONTROL, VK_KEY_M, "MenuToggle"}; +Hotkey quickSceenShot {VK_F5, VK_F5, "QuickScreenshot"}; +Hotkey quickTeleport {VK_KEY_X, VK_KEY_Y, "QuickTeleport"}; +Hotkey vehEngine {VK_NONE, VK_NONE, "Vehicle.EngineToggle"}; +Hotkey vehInstantStart {VK_NONE, VK_NONE, "Vehicle.InstantStart"}; +Hotkey vehInstantStop {VK_NONE, VK_NONE, "Vehicle.InstantStop"}; \ No newline at end of file diff --git a/src/pch.h b/src/pch.h index 24d1d93..0896ad0 100644 --- a/src/pch.h +++ b/src/pch.h @@ -117,7 +117,6 @@ extern Hotkey freeCamRight; extern Hotkey commandWindow; extern Hotkey fixVeh; extern Hotkey flipVeh; -extern Hotkey freeCamTeleportPlayer; extern Hotkey godMode; extern Hotkey menuOpen; extern Hotkey quickSceenShot; diff --git a/src/rpc.h b/src/rpc.h index 021b539..7028cf3 100644 --- a/src/rpc.h +++ b/src/rpc.h @@ -1,5 +1,4 @@ #pragma once -#include class RPC { @@ -8,8 +7,8 @@ private: { const char* state; const char* details; - int64_t startTimestamp; - int64_t endTimestamp; + long long startTimestamp; + long long endTimestamp; const char* largeImageKey; const char* largeImageText; const char* smallImageKey; @@ -20,7 +19,7 @@ private: const char* matchSecret; const char* joinSecret; const char* spectateSecret; - int8_t instance; + char instance; } DiscordRichPresence; static inline FARPROC f_Init, f_ShutDown, f_Update; diff --git a/src/version.h b/src/version.h index 97a287a..d2a817c 100644 --- a/src/version.h +++ b/src/version.h @@ -2,5 +2,5 @@ #define MENU_NAME "Cheat Menu" #define MENU_VERSION_NUMBER "3.3" #define MENU_VERSION MENU_VERSION_NUMBER"-beta" -#define BUILD_NUMBER "20220606" +#define BUILD_NUMBER "20220523" #define MENU_TITLE MENU_NAME " v" MENU_VERSION