// Add new member variables
uintptr_t enginePowerAddress;
bool enginePowerModified;
float originalEnginePower;
float TARGET_POWER;
uintptr_t speedAddress;
bool speedModified;
float originalSpeed;
float TARGET_SPEED;
bool freezeThread;
std::thread freezeWorker;
// Add new tire wear members
uintptr_t tireWearAddress;
bool tireWearModified;
BYTE tireWearOriginalBytes[8];
const BYTE TIRE_PATTERN[8] = { 0xF3, 0x0F, 0x11, 0x9B, 0x44, 0x03, 0x00, 0x00 };
// Add new member variables for engine power 2
uintptr_t enginePower2Address;
bool enginePower2Modified;
float originalEnginePower2;
float TARGET_POWER2;
// Add new member for game year function
uintptr_t gameYearFuncAddress;
// Original pattern to find
const BYTE pattern[6] = { 0x89, 0x0D, 0xA7, 0xB8, 0x06, 0x00 };
const size_t patternSize = sizeof(pattern);
mutable HANDLE hConsole;
// Add Mono-specific typedefs and function pointers
typedef void* (*mono_get_root_domain)();
typedef void* (*mono_thread_attach)(void* domain);
typedef void* (*mono_assembly_get_image)(void* assembly);
typedef void* (*mono_class_from_name)(void* image, const char* name_space, const char* name);
typedef void* (*mono_class_get_method_from_name)(void* klass, const char* name, int param_count);
typedef void* (*mono_runtime_invoke)(void* method, void* obj, void** params, void** exc);
// Add Mono function pointers as members
mono_get_root_domain get_root_domain;
mono_thread_attach thread_attach;
mono_assembly_get_image assembly_get_image;
mono_class_from_name class_from_name;
mono_class_get_method_from_name class_get_method_from_name;
mono_runtime_invoke runtime_invoke;
HMODULE monoModule;
void* monoDomain;
bool monoInitialized;
// Add these new members to the private section of MemoryInjector class:
struct SharedData {
void* monoDomain;
bool finished;
};
void* AllocateSharedMemory(SIZE_T size) {
return VirtualAllocEx(hProcess, nullptr, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
}
bool WriteProcessBytes(void* dest, const void* src, SIZE_T size) {
return WriteProcessMemory(hProcess, dest, src, size, nullptr);
}
bool ReadProcessBytes(void* dest, const void* src, SIZE_T size) {
return ReadProcessMemory(hProcess, src, dest, size, nullptr);
}
void ReportError(const std::string& message, DWORD errorCode = GetLastError()) {
SetConsoleColor(RED);
std::cout << "[ERROR] " << message << "\n";
if (errorCode != 0) {
char* errorMsg = nullptr;
FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
errorCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&errorMsg,
0,
NULL
);
if (errorMsg) {
std::cout << "System Error: " << errorMsg << "\n";
LocalFree(errorMsg);
}
}
SetConsoleColor(WHITE);
}
void LogSuccess(const std::string& message) {
SetConsoleColor(GREEN);
std::cout << "[SUCCESS] " << message << "\n";
SetConsoleColor(WHITE);
}
uintptr_t AOBScan(HMODULE moduleBase, SIZE_T moduleSize) {
std::vector<BYTE> buffer(moduleSize);
SIZE_T bytesRead;
if (!ReadProcessMemory(hProcess, moduleBase, buffer.data(), moduleSize, &bytesRead)) {
ReportError("Failed to read module memory");
return 0;
}
for (size_t i = 0; i < moduleSize - patternSize; i++) {
if (memcmp(&buffer[i], pattern, patternSize) == 0) {
return (uintptr_t)moduleBase + i;
}
}
ReportError("Pattern not found in module");
return 0;
}
// Add new method to find engine power address
uintptr_t GetEnginePowerAddress(HMODULE dllBase) {
uintptr_t baseAddr = (uintptr_t)dllBase;
uintptr_t offsetBase = baseAddr + 0x000A2DB0;
SIZE_T bytesRead;
uintptr_t tempAddr;
if (!ReadProcessMemory(hProcess, (LPCVOID)offsetBase, &tempAddr, sizeof(tempAddr), &bytesRead)) {
ReportError("Failed to read engine power base address");
return 0;
}
return tempAddr + 0x57C; // Add final offset
}
uintptr_t GetSpeedAddress(HMODULE dllBase) {
uintptr_t baseAddr = (uintptr_t)dllBase;
uintptr_t offsetBase = baseAddr + 0x000A2DB0;
SIZE_T bytesRead;
uintptr_t tempAddr;
if (!ReadProcessMemory(hProcess, (LPCVOID)offsetBase, &tempAddr, sizeof(tempAddr), &bytesRead)) {
ReportError("Failed to read speed base address");
return 0;
}
return tempAddr + 0xC1C;
}
// Add new method to find engine power 2 address
uintptr_t GetEnginePower2Address(HMODULE dllBase) {
uintptr_t baseAddr = (uintptr_t)dllBase;
uintptr_t offsetBase = baseAddr + 0x000A11E0;
SIZE_T bytesRead;
uintptr_t tempAddr;
if (!ReadProcessMemory(hProcess, (LPCVOID)offsetBase, &tempAddr, sizeof(tempAddr), &bytesRead)) {
ReportError("Failed to read engine power 2 base address");
return 0;
}
// Follow pointer chain: FE0 -> FE0 -> 18 -> 10 -> 50 -> A8 -> A6C
uintptr_t offsets[] = { 0xFE0, 0xFE0, 0x18, 0x10, 0x50, 0xA8, 0xA6C };
for (size_t i = 0; i < sizeof(offsets) / sizeof(uintptr_t); i++) {
if (!ReadProcessMemory(hProcess, (LPCVOID)tempAddr, &tempAddr, sizeof(tempAddr), &bytesRead)) {
ReportError("Failed to follow engine power 2 pointer chain at offset " + std::to_string(i));
return 0;
}
tempAddr += offsets[i];
}
return tempAddr;
}
uintptr_t GetGameYearFuncAddress(DWORD processId) {
SIZE_T moduleSize;
HMODULE assemblyDll = GetModuleBaseAddress(processId, "Assembly-CSharp.dll", moduleSize);
if (!assemblyDll) {
ReportError("Could not find Assembly-CSharp.dll");
return 0;
}
// RVA (Relative Virtual Address) from the error message: 0x00016FC1
return (uintptr_t)assemblyDll + 0x00016FC1;
}
void FreezeValues() {
while (freezeThread) {
if (enginePowerModified || speedModified || enginePower2Modified) {
// Lock both speed and engine power if they're modified
if (enginePowerModified) {
WriteProcessMemory(hProcess, (LPVOID)enginePowerAddress, &TARGET_POWER, sizeof(TARGET_POWER), NULL);
}
if (enginePower2Modified) {
WriteProcessMemory(hProcess, (LPVOID)enginePower2Address, &TARGET_POWER2, sizeof(TARGET_POWER2), NULL);
}
if (speedModified) {
WriteProcessMemory(hProcess, (LPVOID)speedAddress, &TARGET_SPEED, sizeof(TARGET_SPEED), NULL);
}
Sleep(1); // Small delay to prevent excessive CPU usage
}
}
}
void CheckDllLoaded() {
if (!hProcess) return;
SIZE_T moduleSize;
HMODULE dllBase = GetModuleBaseAddress(GetProcessId(hProcess), "physimpl.dll", moduleSize);
if (!dllBase) {
// DLL not found, need to reinitialize
enginePowerModified = false;
speedModified = false;
tireWearModified = false;
isInjected = false;
LogMessage("Game state reset detected - settings cleared");
}
}
bool InitMonoFunctions() {
monoModule = GetModuleHandle(L"mono.dll");
if (!monoModule) {
ReportError("Could not find mono.dll");
return false;
}
// Get function addresses
get_root_domain = (mono_get_root_domain)GetProcAddress(monoModule, "mono_get_root_domain");
thread_attach = (mono_thread_attach)GetProcAddress(monoModule, "mono_thread_attach");
assembly_get_image = (mono_assembly_get_image)GetProcAddress(monoModule, "mono_assembly_get_image");
class_from_name = (mono_class_from_name)GetProcAddress(monoModule, "mono_class_from_name");
class_get_method_from_name = (mono_class_get_method_from_name)GetProcAddress(monoModule, "mono_class_get_method_from_name");
runtime_invoke = (mono_runtime_invoke)GetProcAddress(monoModule, "mono_runtime_invoke");
if (!get_root_domain || !thread_attach || !assembly_get_image ||
!class_from_name || !class_get_method_from_name || !runtime_invoke) {
ReportError("Failed to get required Mono function addresses");
return false;
}
// Get the Mono domain
monoDomain = get_root_domain();
if (!monoDomain) {
ReportError("Failed to get Mono domain");
return false;
}
// Attach our thread to the Mono runtime
thread_attach(monoDomain);
return true;
}
std::string GetGameBasePath(DWORD processId) {
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processId);
if (!hProcess) return "";
char exePath[MAX_PATH];
DWORD size = MAX_PATH;
QueryFullProcessImageNameA(hProcess, 0, exePath, &size);
CloseHandle(hProcess);
std::string path(exePath);
return path.substr(0, path.find_last_of("\\/"));
}
std::string GetManagedPath(DWORD processId) {
std::string basePath = GetGameBasePath(processId);
return basePath + "\\NASCARHeat5_Data\\Managed";
}
// Update GetMonoDllPath to use the Data folder
std::string GetMonoDllPath(DWORD processId) {
std::string basePath = GetGameBasePath(processId);
return basePath + "\\NASCARHeat5_Data\\Mono\\EmbedRuntime\\mono.dll";
}
// Add method to get Assembly-CSharp.dll path
std::string GetAssemblyPath(DWORD processId) {
return GetManagedPath(processId) + "\\Assembly-CSharp.dll";
}
// Update InitializeMonoRuntime method:
bool InitializeMonoRuntime() {
if (monoInitialized) return true;
HWND hwnd = FindWindowA(NULL, "NASCAR Heat 5");
if (!hwnd) {
ReportError("Game window not found");
return false;
}
DWORD processId;
GetWindowThreadProcessId(hwnd, &processId);
// Get the actual mono.dll path from the game directory
std::string monoDllPath = GetMonoDllPath(processId);
if (monoDllPath.empty()) {
ReportError("Could not find mono.dll path");
return false;
}
// Load the game's mono.dll into our process
HMODULE localMono = LoadLibraryA(monoDllPath.c_str());
if (!localMono) {
ReportError("Could not load mono.dll from: " + monoDllPath);
return false;
}
// Get function addresses
get_root_domain = (mono_get_root_domain)GetProcAddress(localMono, "mono_get_root_domain");
if (!get_root_domain) {
ReportError("Failed to get mono_get_root_domain function");
FreeLibrary(localMono);
return false;
}
// Allocate shared memory in target process
void* sharedMem = AllocateSharedMemory(sizeof(SharedData));
if (!sharedMem) {
ReportError("Failed to allocate shared memory");
FreeLibrary(localMono);
return false;
}
// Create the shellcode that will run in the target process
unsigned char shellcode[] = {
0x48, 0xB8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov rax, get_root_domain
0xFF, 0xD0, // call rax
0x48, 0xB9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov rcx, sharedMem
0x48, 0x89, 0x01, // mov [rcx], rax
0xC6, 0x41, 0x08, 0x01, // mov byte ptr [rcx+8], 1
0xC3 // ret
};
// Patch the shellcode with actual addresses
*(void**)(shellcode + 2) = get_root_domain;
*(void**)(shellcode + 14) = sharedMem;
// Allocate memory for shellcode in target process
void* remoteMem = VirtualAllocEx(hProcess, nullptr, sizeof(shellcode),
MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if (!remoteMem) {
ReportError("Failed to allocate memory for shellcode");
VirtualFreeEx(hProcess, sharedMem, 0, MEM_RELEASE);
FreeLibrary(localMono);
return false;
}
// Write shellcode to target process
if (!WriteProcessBytes(remoteMem, shellcode, sizeof(shellcode))) {
ReportError("Failed to write shellcode");
VirtualFreeEx(hProcess, remoteMem, 0, MEM_RELEASE);
VirtualFreeEx(hProcess, sharedMem, 0, MEM_RELEASE);
FreeLibrary(localMono);
return false;
}
// Create remote thread to execute shellcode
HANDLE hThread = CreateRemoteThread(hProcess, nullptr, 0,
(LPTHREAD_START_ROUTINE)remoteMem, nullptr, 0, nullptr);
if (!hThread) {
ReportError("Failed to create remote thread");
VirtualFreeEx(hProcess, remoteMem, 0, MEM_RELEASE);
VirtualFreeEx(hProcess, sharedMem, 0, MEM_RELEASE);
FreeLibrary(localMono);
return false;
}
// Wait for thread completion
if (WaitForSingleObject(hThread, 5000) != WAIT_OBJECT_0) {
ReportError("Thread execution timeout");
CloseHandle(hThread);
VirtualFreeEx(hProcess, remoteMem, 0, MEM_RELEASE);
VirtualFreeEx(hProcess, sharedMem, 0, MEM_RELEASE);
FreeLibrary(localMono);
return false;
}
// Read results
SharedData data = {};
if (!ReadProcessBytes(&data, sharedMem, sizeof(SharedData))) {
ReportError("Failed to read results");
CloseHandle(hThread);
VirtualFreeEx(hProcess, remoteMem, 0, MEM_RELEASE);
VirtualFreeEx(hProcess, sharedMem, 0, MEM_RELEASE);
FreeLibrary(localMono);
return false;
}
// Clean up
CloseHandle(hThread);
VirtualFreeEx(hProcess, remoteMem, 0, MEM_RELEASE);
VirtualFreeEx(hProcess, sharedMem, 0, MEM_RELEASE);
if (!data.finished || !data.monoDomain) {
ReportError("Failed to get Mono domain from game process");
FreeLibrary(localMono);
return false;
}
monoDomain = data.monoDomain;
monoModule = localMono;
// Initialize remaining Mono functions
thread_attach = (mono_thread_attach)GetProcAddress(localMono, "mono_thread_attach");
assembly_get_image = (mono_assembly_get_image)GetProcAddress(localMono, "mono_assembly_get_image");
class_from_name = (mono_class_from_name)GetProcAddress(localMono, "mono_class_from_name");
class_get_method_from_name = (mono_class_get_method_from_name)GetProcAddress(localMono, "mono_class_get_method_from_name");
runtime_invoke = (mono_runtime_invoke)GetProcAddress(localMono, "mono_runtime_invoke");
if (!thread_attach || !assembly_get_image || !class_from_name ||
!class_get_method_from_name || !runtime_invoke) {
ReportError("Failed to get required Mono function addresses");
FreeLibrary(localMono);
return false;
}
// Attach our thread to the runtime
thread_attach(monoDomain);
monoInitialized = true;
LogSuccess("Successfully initialized Mono runtime from: " + monoDllPath);
return true;
}
code Copy code
void LogMessage(const std::string& message) {
SetConsoleColor(WHITE);
std::cout << "INFO: " << message << "\n";
}
// Add to MemoryInjector class public section:
HANDLE GetProcessHandle() const { return hProcess; }
// Add these public getters for TARGET_POWER and TARGET_SPEED
float GetTargetPower() const { return TARGET_POWER; }
float GetTargetSpeed() const { return TARGET_SPEED; }
// Add public getter for TARGET_POWER2
float GetTargetPower2() const { return TARGET_POWER2; }
// Update constructor
MemoryInjector() : hProcess(nullptr), targetAddress(0), newMemory(nullptr),
isInjected(false), enginePowerAddress(0), enginePowerModified(false),
originalEnginePower(0.0f), enginePower2Address(0), enginePower2Modified(false),
originalEnginePower2(0.0f), speedAddress(0), speedModified(false),
originalSpeed(0.0f), freezeThread(false), tireWearAddress(0),
tireWearModified(false), TARGET_POWER(300.0f), TARGET_POWER2(300.0f),
TARGET_SPEED(300.0f), gameYearFuncAddress(0),
monoModule(nullptr), monoDomain(nullptr),
get_root_domain(nullptr), thread_attach(nullptr),
assembly_get_image(nullptr), class_from_name(nullptr),
class_get_method_from_name(nullptr), runtime_invoke(nullptr),
monoInitialized(false) {
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
memset(originalBytes, 0, sizeof(originalBytes));
memset(tireWearOriginalBytes, 0, sizeof(tireWearOriginalBytes));
}
~MemoryInjector() {
freezeThread = false;
if (freezeWorker.joinable()) {
freezeWorker.join();
}
Cleanup();
}
// Add new methods for engine power modification
bool ModifyEnginePower() {
CheckDllLoaded(); // Check if DLL is still loaded
if (!hProcess || !enginePowerAddress) {
ReportError("Engine power address not initialized");
return false;
}
if (!enginePowerModified) {
SIZE_T bytesRead;
if (!ReadProcessMemory(hProcess, (LPCVOID)enginePowerAddress, &originalEnginePower,
sizeof(originalEnginePower), &bytesRead)) {
ReportError("Failed to read original engine power");
return false;
}
enginePowerModified = true;
if (!freezeThread) {
freezeThread = true;
freezeWorker = std::thread(&MemoryInjector::FreezeValues, this);
}
LogSuccess("Engine power locked to " + std::to_string(TARGET_POWER));
}
return true;
}
bool RestoreEnginePower() {
if (!enginePowerModified) return true;
DWORD oldProtect;
VirtualProtectEx(hProcess, (LPVOID)enginePowerAddress, sizeof(float),
PAGE_EXECUTE_READWRITE, &oldProtect);
if (!WriteProcessMemory(hProcess, (LPVOID)enginePowerAddress, &originalEnginePower,
sizeof(originalEnginePower), NULL)) {
ReportError("Failed to restore engine power");
return false;
}
VirtualProtectEx(hProcess, (LPVOID)enginePowerAddress, sizeof(float), oldProtect, &oldProtect);
enginePowerModified = false;
LogSuccess("Engine power restored to original value");
return true;
}
bool ModifyEnginePower2() {
CheckDllLoaded();
if (!hProcess || !enginePower2Address) {
ReportError("Engine power 2 address not initialized");
return false;
}
if (!enginePower2Modified) {
SIZE_T bytesRead;
if (!ReadProcessMemory(hProcess, (LPCVOID)enginePower2Address, &originalEnginePower2,
sizeof(originalEnginePower2), &bytesRead)) {
ReportError("Failed to read original engine power 2");
return false;
}
enginePower2Modified = true;
if (!freezeThread) {
freezeThread = true;
freezeWorker = std::thread(&MemoryInjector::FreezeValues, this);
}
LogSuccess("Engine power 2 locked to " + std::to_string(TARGET_POWER2));
}
return true;
}
bool RestoreEnginePower2() {
if (!enginePower2Modified) return true;
DWORD oldProtect;
VirtualProtectEx(hProcess, (LPVOID)enginePower2Address, sizeof(float),
PAGE_EXECUTE_READWRITE, &oldProtect);
if (!WriteProcessMemory(hProcess, (LPVOID)enginePower2Address, &originalEnginePower2,
sizeof(originalEnginePower2), NULL)) {
ReportError("Failed to restore engine power 2");
return false;
}
VirtualProtectEx(hProcess, (LPVOID)enginePower2Address, sizeof(float), oldProtect, &oldProtect);
enginePower2Modified = false;
LogSuccess("Engine power 2 restored to original value");
return true;
}
bool ModifySpeed() {
CheckDllLoaded(); // Check if DLL is still loaded
if (!hProcess || !speedAddress) {
ReportError("Speed address not initialized");
return false;
}
if (!speedModified) {
SIZE_T bytesRead;
if (!ReadProcessMemory(hProcess, (LPCVOID)speedAddress, &originalSpeed,
sizeof(originalSpeed), &bytesRead)) {
ReportError("Failed to read original speed");
return false;
}
speedModified = true;
if (!freezeThread) {
freezeThread = true;
freezeWorker = std::thread(&MemoryInjector::FreezeValues, this);
}
LogSuccess("Speed locked to " + std::to_string(TARGET_SPEED));
}
return true;
}
bool RestoreSpeed() {
if (!speedModified) return true;
DWORD oldProtect;
VirtualProtectEx(hProcess, (LPVOID)speedAddress, sizeof(float),
PAGE_EXECUTE_READWRITE, &oldProtect);
if (!WriteProcessMemory(hProcess, (LPVOID)speedAddress, &originalSpeed,
sizeof(originalSpeed), NULL)) {
ReportError("Failed to restore speed");
return false;
}
VirtualProtectEx(hProcess, (LPVOID)speedAddress, sizeof(float), oldProtect, &oldProtect);
speedModified = false;
LogSuccess("Speed restored to original value");
return true;
}
bool ModifyTireWear() {
CheckDllLoaded(); // Check if DLL is still loaded
if (!hProcess || !tireWearAddress) {
ReportError("Tire wear address not initialized");
return false;
}
if (!tireWearModified) {
// Read original bytes
if (!ReadProcessMemory(hProcess, (LPCVOID)tireWearAddress, tireWearOriginalBytes,
sizeof(tireWearOriginalBytes), NULL)) {
ReportError("Failed to read original tire wear bytes");
return false;
}
// Write NOP instructions
BYTE nops[8] = { 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90 };
DWORD oldProtect;
VirtualProtectEx(hProcess, (LPVOID)tireWearAddress, sizeof(nops),
PAGE_EXECUTE_READWRITE, &oldProtect);
if (!WriteProcessMemory(hProcess, (LPVOID)tireWearAddress, nops, sizeof(nops), NULL)) {
ReportError("Failed to modify tire wear");
return false;
}
VirtualProtectEx(hProcess, (LPVOID)tireWearAddress, sizeof(nops), oldProtect, &oldProtect);
tireWearModified = true;
LogSuccess("Tire wear disabled");
}
return true;
}
bool RestoreTireWear() {
if (!tireWearModified) return true;
DWORD oldProtect;
VirtualProtectEx(hProcess, (LPVOID)tireWearAddress, sizeof(tireWearOriginalBytes),
PAGE_EXECUTE_READWRITE, &oldProtect);
if (!WriteProcessMemory(hProcess, (LPVOID)tireWearAddress, tireWearOriginalBytes,
sizeof(tireWearOriginalBytes), NULL)) {
ReportError("Failed to restore tire wear");
return false;
}
VirtualProtectEx(hProcess, (LPVOID)tireWearAddress, sizeof(tireWearOriginalBytes),
oldProtect, &oldProtect);
tireWearModified = false;
LogSuccess("Tire wear restored");
return true;
}
bool IsTireWearModified() const { return tireWearModified; }
// Add setters for target values with validation
bool SetTargetPower(float power) {
if (power < 0 || power > 1000) {
ReportError("Invalid power value. Must be between 0 and 1000");
return false;
}
TARGET_POWER = power;
return true;
}
bool SetTargetPower2(float power) {
if (power < 0 || power > 1000) {
ReportError("Invalid power value. Must be between 0 and 1000");
return false;
}
TARGET_POWER2 = power;
return true;
}
bool SetTargetSpeed(float speed) {
if (speed < 0 || speed > 1000) {
ReportError("Invalid speed value. Must be between 0 and 1000");
return false;
}
TARGET_SPEED = speed;
return true;
}
// Update Initialize method
bool Initialize(const std::string& windowName) {
if (hProcess) {
ReportError("Already initialized");
return false;
}
HWND hwnd = FindWindowA(NULL, windowName.c_str());
if (!hwnd) {
ReportError("Game window not found. Please make sure NASCAR Heat 5 is running.");
return false;
}
DWORD processId;
if (!GetWindowThreadProcessId(hwnd, &processId) || processId == 0) {
ReportError("Failed to get process ID from window");
return false;
}
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processId);
if (!hProcess) {
ReportError("Could not open process. Try running as administrator.");
return false;
}
SIZE_T moduleSize;
HMODULE dllBase = GetModuleBaseAddress(processId, "physimpl.dll", moduleSize);
if (!dllBase) {
ReportError("Could not find required game module (physimpl.dll)");
CloseHandle(hProcess);
hProcess = nullptr;
return false;
}
targetAddress = AOBScan(dllBase, moduleSize);
if (!targetAddress) {
ReportError("Memory pattern not found. Game version might be unsupported.");
CloseHandle(hProcess);
hProcess = nullptr;
return false;
}
// Add engine power address initialization
enginePowerAddress = GetEnginePowerAddress(dllBase);
if (!enginePowerAddress) {
ReportError("Failed to find engine power address");
return false;
}
LogSuccess("Engine power address found at 0x" + std::to_string(enginePowerAddress));
// Add engine power 2 address initialization
enginePower2Address = GetEnginePower2Address(dllBase);
if (!enginePower2Address) {
ReportError("Failed to find engine power 2 address");
return false;
}
LogSuccess("Engine power 2 address found at 0x" + std::to_string(enginePower2Address));
// Add speed address initialization after engine power
speedAddress = GetSpeedAddress(dllBase);
if (!speedAddress) {
ReportError("Failed to find speed address");
return false;
}
LogSuccess("Speed address found at 0x" + std::to_string(speedAddress));
// Scan for tire wear pattern
std::vector<BYTE> buffer(moduleSize);
SIZE_T bytesRead;
if (ReadProcessMemory(hProcess, dllBase, buffer.data(), moduleSize, &bytesRead)) {
for (size_t i = 0; i < moduleSize - sizeof(TIRE_PATTERN); i++) {
if (memcmp(&buffer[i], TIRE_PATTERN, sizeof(TIRE_PATTERN)) == 0) {
tireWearAddress = (uintptr_t)dllBase + i;
LogSuccess("Tire wear pattern found at 0x" + std::to_string(tireWearAddress));
break;
}
}
}
// Add game year function address initialization
gameYearFuncAddress = GetGameYearFuncAddress(processId);
if (!gameYearFuncAddress) {
ReportError("Failed to find game year function");
return false;
}
LogSuccess("Game year function found at 0x" + std::to_string(gameYearFuncAddress));
LogSuccess("Successfully connected to NASCAR Heat 5");
return true;
}
std::string GetGameYear() {
if (!InitializeMonoRuntime()) {
return "Failed to init Mono";
}
DWORD processId = GetProcessId(hProcess);
std::string assemblyPath = GetAssemblyPath(processId);
// Load the Assembly-CSharp.dll
HMODULE assemblyModule = LoadLibraryA(assemblyPath.c_str());
if (!assemblyModule) {
ReportError("Could not load Assembly-CSharp.dll from: " + assemblyPath);
return "Failed to load assembly";
}
// Find the Assembly-CSharp.dll image
void* image = assembly_get_image(assemblyModule);
if (!image) {
ReportError("Failed to get Assembly-CSharp.dll image");
FreeLibrary(assemblyModule);
return "Failed to get assembly";
}
// Get the CarManager class
void* klass = class_from_name(image, "MGI.CarManager", "CarManager");
if (!klass) {
ReportError("Failed to find CarManager class");
FreeLibrary(assemblyModule);
return "Failed to find class";
}
// Get the get_current_game_year_string method
void* method = class_get_method_from_name(klass, "get_current_game_year_string", 0);
if (!method) {
ReportError("Failed to find get_current_game_year_string method");
FreeLibrary(assemblyModule);
return "Failed to find method";
}
// Call the method
void* exc = nullptr;
void* result = runtime_invoke(method, nullptr, nullptr, &exc);
FreeLibrary(assemblyModule);
if (exc) {
ReportError("Exception occurred while calling get_current_game_year_string");
return "Method call failed";
}
if (!result) {
return "No result";
}
// Convert the result to string
char* monoString = *(char**)result;
return std::string(monoString ? monoString : "null");
}
bool Inject() {
CheckDllLoaded(); // Check if DLL is still loaded
if (!hProcess || !targetAddress || isInjected) {
ReportError("Invalid injection state");
return false;
}
// Read original bytes
if (!ReadProcessMemory(hProcess, (LPCVOID)targetAddress, originalBytes, sizeof(originalBytes), NULL)) {
ReportError("Failed to read original bytes");
return false;
}
// Simple patch: NOP instructions
BYTE patchBytes[6] = { 0x90, 0x90, 0x90, 0x90, 0x90, 0x90 }; // All NOPs
DWORD oldProtect;
if (!VirtualProtectEx(hProcess, (LPVOID)targetAddress, sizeof(patchBytes), PAGE_EXECUTE_READWRITE, &oldProtect)) {
ReportError("Failed to change memory protection");
return false;
}
if (!WriteProcessMemory(hProcess, (LPVOID)targetAddress, patchBytes, sizeof(patchBytes), NULL)) {
ReportError("Failed to write patch");
VirtualProtectEx(hProcess, (LPVOID)targetAddress, sizeof(patchBytes), oldProtect, &oldProtect);
return false;
}
VirtualProtectEx(hProcess, (LPVOID)targetAddress, sizeof(patchBytes), oldProtect, &oldProtect);
isInjected = true;
LogMessage("Successfully injected!");
return true;
}
bool Revert() {
CheckDllLoaded(); // Check if DLL is still loaded
if (!hProcess || !targetAddress || !isInjected) {
ReportError("Invalid revert state");
return false;
}
DWORD oldProtect;
if (!VirtualProtectEx(hProcess, (LPVOID)targetAddress, sizeof(originalBytes), PAGE_EXECUTE_READWRITE, &oldProtect)) {
ReportError("Failed to change protection for revert");
return false;
}
if (!WriteProcessMemory(hProcess, (LPVOID)targetAddress, originalBytes, sizeof(originalBytes), NULL)) {
ReportError("Failed to restore original bytes");
VirtualProtectEx(hProcess, (LPVOID)targetAddress, sizeof(originalBytes), oldProtect, &oldProtect);
return false;
}
VirtualProtectEx(hProcess, (LPVOID)targetAddress, sizeof(originalBytes), oldProtect, &oldProtect);
isInjected = false;
LogMessage("Successfully reverted changes!");
return true;
}
void Cleanup() {
if (isInjected) {
Revert();
}
if (enginePowerModified) {
RestoreEnginePower();
}
if (enginePower2Modified) {
RestoreEnginePower2();
}
if (speedModified) {
RestoreSpeed();
}
if (tireWearModified) {
RestoreTireWear();
}
if (hProcess) {
CloseHandle(hProcess);
hProcess = nullptr;
}
}
bool IsInjected() const { return isInjected; }
bool IsEnginePowerModified() const { return enginePowerModified; }
bool IsEnginePower2Modified() const { return enginePower2Modified; }
bool IsSpeedModified() const { return speedModified; }
bool IsProcessActive() const {
if (!hProcess) return false;
DWORD exitCode;
return GetExitCodeProcess(hProcess, &exitCode) && exitCode == STILL_ACTIVE;
}
HMODULE GetModuleBaseAddress(DWORD processId, const std::string& moduleName, SIZE_T& moduleSize) {
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, processId);
if (hSnapshot == INVALID_HANDLE_VALUE) {
ReportError("Failed to create module snapshot");
return nullptr;
}
MODULEENTRY32W me32 = { sizeof(MODULEENTRY32W) };
HMODULE result = nullptr;
moduleSize = 0;
if (!Module32FirstW(hSnapshot, &me32)) {
ReportError("Failed to get first module");
CloseHandle(hSnapshot);
return nullptr;
}
do {
std::wstring wideModuleName(me32.szModule);
std::string moduleNameStr(wideModuleName.begin(), wideModuleName.end());
if (_wcsicmp(me32.szModule, std::wstring(moduleName.begin(), moduleName.end()).c_str()) == 0) {
result = (HMODULE)me32.modBaseAddr;
moduleSize = me32.modBaseSize;
LogMessage("Target module " + moduleName + " found at 0x" + std::to_string((uintptr_t)result));
break;
}
} while (Module32NextW(hSnapshot, &me32));
CloseHandle(hSnapshot);
return result;
}