From 8e43db9a20d481704c4f5b6b0e71cbb45706e762 Mon Sep 17 00:00:00 2001
From: Surasia <74399067+Surasia@users.noreply.github.com>
Date: Tue, 13 Aug 2024 00:03:19 +0200
Subject: [PATCH] 0.2.0: Hooking HavokScript
---
InfExt/InfExt.vcxproj | 3 +-
InfExt/InfExt.vcxproj.filters | 9 +-
InfExt/src/Client.cpp | 193 ++++++++-------
InfExt/src/Client.hpp | 9 +-
InfExt/src/HavokScript/HavokScript.cpp | 71 +++++-
InfExt/src/HavokScript/HavokScript.hpp | 42 +++-
.../src/HavokScript/HksCompilerSettings.hpp | 21 ++
InfExt/src/Lua/LuaHook.cpp | 99 ++------
InfExt/src/Lua/LuaHook.hpp | 18 +-
InfExt/src/Memory.cpp | 104 ++++----
InfExt/src/Memory.hpp | 40 +--
InfExt/src/Misc/ChromaSDK.cpp | 20 +-
InfExt/src/Misc/ChromaSDK.hpp | 24 +-
InfExt/src/Variant/Variant.cpp | 233 ------------------
InfExt/src/Variant/Variant.hpp | 50 ----
README.md | 45 +---
licenses/LuaLicense.txt | 8 -
licenses/Sol2License.txt | 20 --
vcpkg.json | 4 +-
19 files changed, 354 insertions(+), 659 deletions(-)
create mode 100644 InfExt/src/HavokScript/HksCompilerSettings.hpp
delete mode 100644 InfExt/src/Variant/Variant.cpp
delete mode 100644 InfExt/src/Variant/Variant.hpp
delete mode 100644 licenses/LuaLicense.txt
delete mode 100644 licenses/Sol2License.txt
diff --git a/InfExt/InfExt.vcxproj b/InfExt/InfExt.vcxproj
index ce8d1da..d1980c8 100644
--- a/InfExt/InfExt.vcxproj
+++ b/InfExt/InfExt.vcxproj
@@ -79,11 +79,11 @@
+
-
@@ -91,7 +91,6 @@
-
diff --git a/InfExt/InfExt.vcxproj.filters b/InfExt/InfExt.vcxproj.filters
index 8708a46..30e2f78 100644
--- a/InfExt/InfExt.vcxproj.filters
+++ b/InfExt/InfExt.vcxproj.filters
@@ -26,15 +26,15 @@
Header Files
-
- Header Files
-
Header Files
Header Files
+
+ Header Files
+
@@ -49,9 +49,6 @@
Source Files
-
- Source Files
-
Source Files
diff --git a/InfExt/src/Client.cpp b/InfExt/src/Client.cpp
index 50eb8b8..9284b1e 100644
--- a/InfExt/src/Client.cpp
+++ b/InfExt/src/Client.cpp
@@ -1,117 +1,120 @@
#include "Client.hpp"
-#include "Exports.hpp"
#include "./HavokScript/HavokScript.hpp"
#include "./Lua/LuaHook.hpp"
#include "./Misc/ChromaSDK.hpp"
-#include "./Variant/Variant.hpp"
-
-std::mutex consoleMutex;
-std::condition_variable cv;
-std::atomic stopThread(false);
-std::thread mainThread;
-
-DWORD WINAPI CreateConsole()
-{
- FILE *dummy = nullptr;
- AllocConsole();
- AttachConsole(GetCurrentProcessId());
- {
- std::lock_guard lock(consoleMutex);
- if (freopen_s(&dummy, "CONIN$", "r", stdin) != 0 || freopen_s(&dummy, "CONOUT$", "w", stdout) != 0 ||
- freopen_s(&dummy, "CONOUT$", "w", stderr) != 0) {
- std::cerr << "Failed to redirect console streams !" << std::endl;
- return 1;
- }
- }
- return 0;
+#include "Exports.hpp"
+
+DWORD WINAPI CreateConsole() {
+ FILE* dummy = nullptr;
+ AllocConsole();
+ AttachConsole(GetCurrentProcessId());
+ {
+ std::lock_guard lock(consoleMutex);
+ if (freopen_s(&dummy, "CONIN$", "r", stdin) != 0 ||
+ freopen_s(&dummy, "CONOUT$", "w", stdout) != 0 ||
+ freopen_s(&dummy, "CONOUT$", "w", stderr) != 0) {
+ std::cerr << "Failed to redirect console streams !" << std::endl;
+ return 1;
+ }
+ }
+ return 0;
}
-DWORD WINAPI DestroyConsole()
-{
- if (FreeConsole() == 0) {
- std::cerr << "Console failed to close!" << std::endl;
- }
-
- {
- std::lock_guard lock(consoleMutex);
- freopen_s(reinterpret_cast(stdin), "NUL:", "r", stdin);
- freopen_s(reinterpret_cast(stdout), "NUL:", "r", stdout);
- freopen_s(reinterpret_cast(stderr), "NUL:", "r", stderr);
- }
-
- std::cin.clear();
- std::cout.clear();
- std::cerr.clear();
- return 0;
+DWORD WINAPI DestroyConsole() {
+ if (FreeConsole() == 0) {
+ std::cerr << "Console failed to close!" << std::endl;
+ }
+
+ {
+ std::lock_guard lock(consoleMutex);
+ freopen_s(reinterpret_cast(stdin), "NUL:", "r", stdin);
+ freopen_s(reinterpret_cast(stdout), "NUL:", "r", stdout);
+ freopen_s(reinterpret_cast(stderr), "NUL:", "r", stderr);
+ }
+
+ std::cin.clear();
+ std::cout.clear();
+ std::cerr.clear();
+ return 0;
}
-DWORD WINAPI DestroyHook()
-{
- stopThread = true;
- cv.notify_all();
+DWORD WINAPI DestroyHook() {
+ stopThread = true;
+ cv.notify_all();
- if (mainThread.joinable()) {
- mainThread.join();
- }
+ if (mainThread.joinable()) {
+ mainThread.join();
+ }
- MH_DisableHook(MH_ALL_HOOKS);
- MH_Uninitialize();
- return 0;
-}
+ MH_DisableHook(MH_ALL_HOOKS);
+ MH_Uninitialize();
-void ProcessCommands()
-{
- while (!stopThread) {
- LuaVM::ProcessCommand();
- }
+ if (hMutex) {
+ ReleaseMutex(hMutex);
+ CloseHandle(hMutex);
+ hMutex = NULL;
+ }
+
+ return 0;
}
+void ProcessCommands() {
+ {
+ std::unique_lock lock(consoleMutex);
+ if (cv.wait_for(lock, std::chrono::seconds(10),
+ [] { return stopThread.load(); })) {
+ return; // Wait after Steam prints debug info.
+ }
+ }
+ while (!stopThread) {
+ LuaVM::ProcessCommand();
+ }
+}
-static DWORD SetupHook()
-{
- uintptr_t ModuleBase = 0;
+static DWORD SetupHook() {
+ uintptr_t ModuleBase = 0;
- LPCSTR moduleName = "HaloInfinite.exe";
- ModuleBase = reinterpret_cast(GetModuleHandleA(moduleName));
- if (ModuleBase == 0) {
- std::cerr << "Failed to get module handle for " << moduleName << std::endl;
- return 1;
- }
+ LPCSTR moduleName = "HaloInfinite.exe";
+ ModuleBase = reinterpret_cast(GetModuleHandleA(moduleName));
+ if (ModuleBase == 0) {
+ std::cerr << "Failed to get module handle for " << moduleName << std::endl;
+ return 1;
+ }
- ChromaSDK::HookChroma(ModuleBase);
- Hks::HookHavokScript(ModuleBase);
- Variant::HookVariant(ModuleBase);
+ ChromaSDK::HookChroma(ModuleBase);
+ Hks::HookHavokScript(ModuleBase);
- LuaVM::InitializeLua();
- LuaVM::HookVariantFunctions();
- mainThread = std::thread(ProcessCommands);
- return 0;
+ mainThread = std::thread(ProcessCommands);
+ return 0;
}
-
-DWORD WINAPI MainThread(LPVOID lpParameter)
-{
- SetupHook();
- return 0;
+DWORD WINAPI MainThread(LPVOID lpParameter) {
+ SetupHook();
+ return 0;
}
-
-BOOL APIENTRY DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved)
-{
- switch (dwReason) {
- case DLL_PROCESS_ATTACH:
- CreateConsole();
- DisableThreadLibraryCalls(hModule);
- CreateThread(nullptr, 0, MainThread, nullptr, 0, nullptr);
- break;
- case DLL_PROCESS_DETACH:
- DestroyHook();
- LuaVM::CleanLua();
- DestroyConsole();
- FreeLibraryAndExitThread(hModule, TRUE);
- break;
- default:
- break;
- }
- return TRUE;
+BOOL APIENTRY DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved) {
+ switch (dwReason) {
+ case DLL_PROCESS_ATTACH:
+ hMutex = CreateMutexA(NULL, TRUE, "UniqueDLLInstanceMutex");
+ if (hMutex == NULL || GetLastError() == ERROR_ALREADY_EXISTS) {
+ if (hMutex) {
+ CloseHandle(hMutex);
+ hMutex = NULL;
+ }
+ return FALSE; // DLL is already loaded
+ }
+ CreateConsole();
+ DisableThreadLibraryCalls(hModule);
+ CreateThread(nullptr, 0, MainThread, nullptr, 0, nullptr);
+ break;
+ case DLL_PROCESS_DETACH:
+ DestroyHook();
+ DestroyConsole();
+ FreeLibraryAndExitThread(hModule, TRUE);
+ break;
+ default:
+ break;
+ }
+ return TRUE;
}
diff --git a/InfExt/src/Client.hpp b/InfExt/src/Client.hpp
index 3305b78..c8f4651 100644
--- a/InfExt/src/Client.hpp
+++ b/InfExt/src/Client.hpp
@@ -1,15 +1,13 @@
#ifndef CLIENT_HPP
#define CLIENT_HPP
-
-#include "../include/MinHook.h" // IWYU pragma: keep
+#include
#include // IWYU pragma: keep
#include
#include // IWYU pragma: keep
#include // IWYU pragma: keep
#include // IWYU pragma: keep
#include // IWYU pragma: keep
-#include
#include