From 28d7a752a8796826cde73f50c85914a7c46705af Mon Sep 17 00:00:00 2001 From: Grinch_ Date: Sun, 28 Feb 2021 02:40:51 +0600 Subject: [PATCH] Make menu reloadable --- CMakeLists.txt | 1 + src/CheatMenu.cpp | 15 ++++++----- src/Json.cpp | 6 ++++- src/Neon.cpp | 5 ++-- src/Neon.h | 2 +- src/Paint.cpp | 2 +- src/Paint.h | 2 +- src/VehExtender.h | 29 ++++++++++++++++++++ src/pch.h | 1 + tools/CMakeLists.txt | 16 +++++++++++ tools/Injector.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++ 11 files changed, 129 insertions(+), 14 deletions(-) create mode 100644 src/VehExtender.h create mode 100644 tools/CMakeLists.txt create mode 100644 tools/Injector.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 52da4df..0f732e5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ set(DIRECTX9_SDK_DIR $ENV{DIRECTX9_SDK_DIR}) ################################################################################ add_subdirectory(deps) add_subdirectory(tests) +add_subdirectory(tools) ################################################################################ # Source groups diff --git a/src/CheatMenu.cpp b/src/CheatMenu.cpp index 14d9ff9..17d0583 100644 --- a/src/CheatMenu.cpp +++ b/src/CheatMenu.cpp @@ -171,12 +171,12 @@ void HasGameInit() void MenuThread(void* param) { // Wait till the game is initialized - Events::initGameEvent.after += HasGameInit; + Events::processScriptsEvent += HasGameInit; while (!Globals::game_init) Sleep(1000); - Events::initGameEvent.after -= HasGameInit; + Events::processScriptsEvent -= HasGameInit; if (GetModuleHandle("SAMP.dll")) { @@ -191,27 +191,28 @@ void MenuThread(void* param) return; } - flog << "Starting...\nVersion: " MENU_TITLE "\nAuthor: Grinch_\nDiscord: " DISCORD_INVITE "\nMore Info: " GITHUB_LINK "\n\n" << std::endl; + flog << "Starting...\nVersion: " MENU_TITLE "\nAuthor: Grinch_\nDiscord: " DISCORD_INVITE "\nMore Info: " GITHUB_LINK "\n" << std::endl; CFastman92limitAdjuster::Init(); CheatMenu *menu = new CheatMenu; while (true) { - Sleep(50); - - if (KeyPressed(VK_TAB)) + Sleep(100); + if (KeyPressed(VK_LSHIFT) && KeyPressed(VK_BACK)) break; } delete menu; Sleep(100); + CHud::SetHelpMessage("CheatMenu unloaded",false,false,false); + flog << "Unloaded" << std::endl; // reset mouse patches patch::SetUChar(0x6194A0, 0xE9); patch::SetUChar(0x746ED0, 0xA1); patch::SetRaw(0x53F41F, (void*)"\x85\xC0\x0F\x8C", 4); - FreeLibraryAndExitThread(NULL,0); + FreeLibraryAndExitThread(GetModuleHandle("CheatMenu.asi"),0); } BOOL WINAPI DllMain(HINSTANCE hDllHandle, DWORD nReason, LPVOID Reserved) diff --git a/src/Json.cpp b/src/Json.cpp index 350a2cf..4ba8889 100644 --- a/src/Json.cpp +++ b/src/Json.cpp @@ -22,7 +22,11 @@ CJson::CJson(const char* name) else { data = "{}"_json; - flog << "File doesn't exist " << file_path << std::endl; + + if (file_path.find("config")) + flog << "Creating config.json file" << std::endl; + else + flog << "Failed to locate file " << file_path << std::endl; } } diff --git a/src/Neon.cpp b/src/Neon.cpp index 02a3ebe..d38c79d 100644 --- a/src/Neon.cpp +++ b/src/Neon.cpp @@ -2,10 +2,9 @@ #include "Neon.h" #include "Util.h" -VehicleExtendedData Neon::VehNeon; +VehExtender Neon::VehNeon; RwTexture* Neon::neon_texture = nullptr; - void Neon::RenderEvent(CVehicle *pVeh) { NeonData* data = &VehNeon.Get(pVeh); @@ -48,7 +47,7 @@ Neon::Neon() } Neon::~Neon() -{ +{ Events::vehicleRenderEvent -= RenderEvent; RwTextureDestroy(neon_texture); } diff --git a/src/Neon.h b/src/Neon.h index e9baee7..34eb1e3 100644 --- a/src/Neon.h +++ b/src/Neon.h @@ -23,7 +23,7 @@ private: } }; - static VehicleExtendedData VehNeon; + static VehExtender VehNeon; public: Neon(); diff --git a/src/Paint.cpp b/src/Paint.cpp index 0e68eaf..793911b 100644 --- a/src/Paint.cpp +++ b/src/Paint.cpp @@ -30,7 +30,7 @@ std::vector Paint::veh_nodes::names_vec{ "Default" }; std::string Paint::veh_nodes::selected = "Default"; -VehicleExtendedData Paint::vehdata; +VehExtender Paint::vehdata; std::map> Paint::textures; diff --git a/src/Paint.h b/src/Paint.h index b8c7b35..f11dcf5 100644 --- a/src/Paint.h +++ b/src/Paint.h @@ -73,7 +73,7 @@ private: void resetMaterialTexture(RpMaterial* material); }; - static VehicleExtendedData vehdata; + static VehExtender vehdata; protected: diff --git a/src/VehExtender.h b/src/VehExtender.h new file mode 100644 index 0000000..1ef6714 --- /dev/null +++ b/src/VehExtender.h @@ -0,0 +1,29 @@ +/* + VS Code extension doesn't work well with template classes +*/ + +#pragma once +#include +#include "CVehicle.h" + +template +class VehExtender +{ +public: + VehExtender(){}; + VehExtender(const VehExtender&) = delete; + + T& Get(CVehicle *veh) + { + static std::vector> data; + + for (auto it = data.begin(); it < data.end(); ++it) + { + if (it->first == veh) + return it->second; + } + + data.push_back({veh, T(veh)}); + return data.back().second; + } +}; \ No newline at end of file diff --git a/src/pch.h b/src/pch.h index 42d48d3..ed9543a 100644 --- a/src/pch.h +++ b/src/pch.h @@ -61,6 +61,7 @@ #include "Events.h" #include "Json.h" #include "VKeys.h" +#include "VehExtender.h" // Globals typedef std::vector> CallbackTable; diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt new file mode 100644 index 0000000..6f6761b --- /dev/null +++ b/tools/CMakeLists.txt @@ -0,0 +1,16 @@ +################################################################################ +# Build Tools +################################################################################ + +cmake_minimum_required(VERSION 3.0) +project(CheatMenuInjector) + +add_executable(CheatMenuInjector WIN32 "Injector.cpp") + +set_target_properties(CheatMenuInjector PROPERTIES + RUNTIME_OUTPUT_DIRECTORY "${GTA_SA_DIR}/$<0:>/" +) + +target_link_options(${PROJECT_NAME} PRIVATE + /SUBSYSTEM:CONSOLE +) \ No newline at end of file diff --git a/tools/Injector.cpp b/tools/Injector.cpp new file mode 100644 index 0000000..dca6d87 --- /dev/null +++ b/tools/Injector.cpp @@ -0,0 +1,64 @@ +/* + Simple injector + Taken from GuidedHacking +*/ +#include +#include + +DWORD GetProcId(const char* procName) +{ + DWORD procId = 0; + HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); + + if (hSnap != INVALID_HANDLE_VALUE) + { + PROCESSENTRY32 procEntry; + procEntry.dwSize = sizeof(procEntry); + + if (Process32First(hSnap, &procEntry)) + { + do + { + if (!_stricmp((const char*)procEntry.szExeFile, procName)) + { + procId = procEntry.th32ProcessID; + break; + } + } while (Process32Next(hSnap, &procEntry)); + } + } + CloseHandle(hSnap); + return procId; +} + +int main() +{ + const char* dllPath = "./CheatMenu.asi"; + const char* procName = "gta_sa.exe"; + DWORD procId = 0; + + while (!procId) + { + procId = GetProcId(procName); + Sleep(30); + } + + HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, 0, procId); + + if (hProc && hProc != INVALID_HANDLE_VALUE) + { + void* loc = VirtualAllocEx(hProc, 0, MAX_PATH, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); + + WriteProcessMemory(hProc, loc, dllPath, strlen(dllPath) + 1, 0); + + HANDLE hThread = CreateRemoteThread(hProc, 0, 0, (LPTHREAD_START_ROUTINE)LoadLibraryA, loc, 0, 0); + + if (hThread) + CloseHandle(hThread); + } + + if (hProc) + CloseHandle(hProc); + + return 0; +} \ No newline at end of file