Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
user-grinch committed Aug 2, 2024
1 parent b1ace96 commit 8c24fd1
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 117 deletions.
2 changes: 1 addition & 1 deletion .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"defines": [
"_DEBUG",
"_DX9_SDK_INSTALLED",
"RUNTIME_REDUX",
"RUNTIME_CLEO",
],
"windowsSdkVersion": "10.0.19041.0",
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30037/bin/Hostx64/x64/cl.exe",
Expand Down
Binary file modified examples/cleo5/imgui_test.cs
Binary file not shown.
92 changes: 57 additions & 35 deletions src/dllmain.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,62 @@
#include "pch.h"
#include <windows.h>
#include "opcodemgr.h"
#include "hook.h"
#include "wrapper.hpp"

#ifdef RUNTIME_CLEO
#include "MinHook.h"
#include "injector.hpp"
#include <psapi.h>
#include <windows.h>

HINSTANCE gDllHandle;

DWORD GetProcessIdFromModule(HMODULE hModule) {
DWORD processId = 0;
HANDLE hProcess = NULL;
MODULEINFO moduleInfo;

hProcess = GetCurrentProcess();
if (hProcess != NULL) {
if (GetModuleInformation(hProcess, hModule, &moduleInfo, sizeof(moduleInfo))) {
processId = GetProcessId(hProcess);
}
CloseHandle(hProcess);
}

return processId;
}

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) {
DWORD processId = 0;
GetWindowThreadProcessId(hwnd, &processId);

if (processId == static_cast<DWORD>(lParam)) {
return FALSE; // Stop
}
return TRUE; // Continue
}

bool HasGameLaunched(HMODULE hModule, int maxRetries, int sleepDuration) {
DWORD processId = GetProcessIdFromModule(hModule);
if (processId == 0) {
MessageBox(NULL, "Failed to get process ID", "ImGuiRedux", MB_ICONERROR);
return false;
}

int retries = 0;
while (retries < maxRetries) {
Sleep(sleepDuration);
if (!EnumWindows(EnumWindowsProc, static_cast<LPARAM>(processId))) {
return true;
}
retries++;
}
MessageBox(NULL, "Failed to detect game window.", "ImGuiRedux", MB_ICONERROR);
return false;
}
void ImGuiThread(void* param) {
if (!HasGameLaunched(gDllHandle, 30, 5000)) {
return;
}

void f_GTA_SPCheck() {
std::string moduleName = "SilentPatchSA.asi";
if (gGameVer == eGameVer::VC) {
moduleName = "SilentPatchVC.asi";
Expand All @@ -23,29 +71,6 @@ void f_GTA_SPCheck() {
};
return;
}
}
#endif

void ImGuiThread(void* param) {

#ifdef RUNTIME_CLEO
/*
Need SP for mouse fixes
Only need for classics
TODO: Get the mouse patches from MTA later
*/
if (gGameVer <= eGameVer::SA) {
MH_Initialize();
uint32_t addr = (gGameVer == eGameVer::SA) ? 0x5BF3A1 :
((gGameVer == eGameVer::VC) ? 0x4A5B6B : 0x48D52F);

void *ptr = NULL;
MH_CreateHook((void*)addr, f_GTA_SPCheck, &ptr);
MH_EnableHook(ptr);
}
#endif

Sleep(5000);

if (!Hook::Inject(&ScriptExData::DrawFrames)) {
MessageBox(HWND_DESKTOP, "Failed to inject dxhook..", "ImGuiRedux", MB_ICONERROR);
Expand All @@ -63,14 +88,11 @@ void __stdcall _wrapper(DWORD saveSlot) {
BOOL WINAPI DllMain(HINSTANCE hDllHandle, DWORD nReason, LPVOID Reserved) {
if (nReason == DLL_PROCESS_ATTACH) {
#ifdef RUNTIME_CLEO
auto gvm = injector::game_version_manager();
gvm.Detect();

if (gvm.GetMajorVersion() == 1 && gvm.GetMinorVersion() == 0) {
if (gvm.IsIII()) gGameVer = eGameVer::III;
if (gvm.IsVC()) gGameVer = eGameVer::VC;
if (gvm.IsSA()) gGameVer = eGameVer::SA;
CLEO::eGameVersion ver = CLEO::CLEO_GetGameVersion();
if (ver != CLEO::eGameVersion::GV_US10) {
MessageBox(HWND_DESKTOP, "Unknown game/ version detected. gta_sa.exe v1.0 US required!", "ImGuiRedux", MB_ICONERROR);
}
gGameVer = eGameVer::SA;
#else
if (GetModuleHandle("ImGuiCleoWin32.cleo")) {
Log("ImGuiReduxWin32: ImGuiCleoWin32 detected. Closing...");
Expand Down
8 changes: 5 additions & 3 deletions src/hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ void Hook::ProcessFrame(void* ptr) {

// Scale the menu if game resolution changed
static int height, width, RsGlobal;
#ifdef RUNTIME_CLEO

#ifndef _WIN64
if (gGameVer == eGameVer::III) {
RsGlobal = 0x8F4360;
width = injector::ReadMemory<int>(RsGlobal + 4, 0); // width
Expand All @@ -96,12 +97,13 @@ void Hook::ProcessFrame(void* ptr) {
width = rect.right - rect.left;
height = rect.bottom - rect.top;
}
#else
#else
RECT rect;
GetWindowRect(hwnd, &rect);
width = rect.right - rect.left;
height = rect.bottom - rect.top;
#endif

static ImVec2 fScreenSize = ImVec2(-1, -1);
if (fScreenSize.x != width && fScreenSize.y != height) {
if (gRenderer == eRenderer::Dx9) {
Expand Down Expand Up @@ -170,7 +172,7 @@ void Hook::ProcessFrame(void* ptr) {
}

if (gGameVer == eGameVer::SA) {
injector::MakeNOP(0x00531155, 5); // shift trigger fix
injector::MakeNOP(0x531155, 5); // shift trigger fix
}

if (gRenderer == eRenderer::Dx9) {
Expand Down
37 changes: 4 additions & 33 deletions src/opcodemgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,33 +43,6 @@ static RTN_TYPE RUNTIME_API ImGuiEnd(RUNTIME_CONTEXT ctx) {
return RTN_CONTINUE;
}

// static RTN_TYPE RUNTIME_API ImGuiBegin(RUNTIME_CONTEXT ctx) {
// char label[RUNTIME_STR_LEN];

// wGetStringWithFrame(ctx, label, RUNTIME_STR_LEN);
// bool openFlag = wGetBoolParam(ctx);
// bool noTitleBar = wGetBoolParam(ctx);
// bool noResize = wGetBoolParam(ctx);
// bool noMove = wGetBoolParam(ctx);
// bool autoResize = wGetBoolParam(ctx);

// ImGuiWindowFlags flags = ImGuiWindowFlags_NoCollapse;
// if (noTitleBar) flags |= ImGuiWindowFlags_NoTitleBar;
// if (noResize) flags |= ImGuiWindowFlags_NoResize;
// if (noMove) flags |= ImGuiWindowFlags_NoMove;
// if (autoResize) flags |= ImGuiWindowFlags_AlwaysAutoResize;

// ScriptExData* data = ScriptExData::Get();
// data->imgui += [=]() {
// bool isOpen = openFlag;
// ImGui::Begin(label, &isOpen, flags);
// data->SetData(label, 0, isOpen);
// };
// data->imgui.lastScriptCall = time(NULL);
// wSetIntParam(ctx, data->GetData(label, 0, true));
// return RTN_CONTINUE;
// }

static RTN_TYPE RUNTIME_API ImGuiButton(RUNTIME_CONTEXT ctx) {
char buf[RUNTIME_STR_LEN];
ImVec2 size;
Expand Down Expand Up @@ -314,14 +287,12 @@ static RTN_TYPE RUNTIME_API ImGuiGetFramerate(RUNTIME_CONTEXT ctx) {
}

static RTN_TYPE RUNTIME_API ImGuiGetVersion(RUNTIME_CONTEXT ctx) {
char* buf = const_cast<char*>(ImGui::GetVersion());
unsigned char len = static_cast<unsigned char>(strlen(buf));
wSetStringParam(ctx, buf);
wSetIntParam(ctx, IMGUI_VERSION_NUM);
return RTN_CONTINUE;
}

static RTN_TYPE RUNTIME_API ImGuiGetPluginVersion(RUNTIME_CONTEXT ctx) {
wSetFloatParam(ctx, IMGUI_REDUX_VERSION);
wSetIntParam(ctx, IMGUI_REDUX_VERSION_NUM);
return RTN_CONTINUE;
}

Expand Down Expand Up @@ -551,8 +522,8 @@ static RTN_TYPE RUNTIME_API ImGuiGetDisplaySize(RUNTIME_CONTEXT ctx) {
RECT rect;
GetClientRect(GetForegroundWindow(), &rect);

wSetFloatParam(ctx, rect.right - rect.left);
wSetFloatParam(ctx, rect.bottom - rect.top);
wSetFloatParam(ctx, float(rect.right - rect.left));
wSetFloatParam(ctx, float(rect.bottom - rect.top));
return RTN_CONTINUE;
}

Expand Down
3 changes: 2 additions & 1 deletion src/pch.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#define IMGUI_REDUX_VERSION 2.0f
#define IMGUI_REDUX_VERSION 3.0f
#define IMGUI_REDUX_VERSION_NUM 30000
#include "imgui.h"
#include "imgui_stdlib.h"
#include "scriptextender.hpp"
Expand Down
2 changes: 2 additions & 0 deletions src/scriptextender.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ class ScriptExData
case 2: // SA
scriptsPaused = *(bool*)0xB7CB49;
break;
default:
break;
}

if (curTime-lastScriptCall > 2 || scriptsPaused) {
Expand Down
7 changes: 0 additions & 7 deletions tools/Debug.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@ echo --------------------------------------------------
echo[
call "tools\Setup.bat"
MsBuild ImGuiRedux.sln /property:Configuration=Debug /t:ImGuiRedux /property:Platform=Win32
<<<<<<< HEAD
del "%GTA_SA_DIR%\cleo\CLEO_PLUGINS\ImGuiReduxWin32.cleo" /Q
del "%GTA_SA_DIR%\cleo\CLEO_PLUGINS\ImGuiReduxWin32.pdb" /Q
xcopy /s "bin\ImGuiReduxWin32.cleo" "%GTA_SA_DIR%\cleo\CLEO_PLUGINS\" /K /D /H /Y
xcopy /s "bin\ImGuiReduxWin32.pdb" "%GTA_SA_DIR%\cleo\CLEO_PLUGINS\" /K /D /H /Y
=======
del "%SA_DIR%\cleo\CLEO_PLUGINS\ImGuiReduxWin32.cleo" /Q
del "%SA_DIR%\cleo\CLEO_PLUGINS\ImGuiReduxWin32.pdb" /Q
xcopy /s "bin\ImGuiReduxWin32.cleo" "%SA_DIR%\cleo\CLEO_PLUGINS\" /K /D /H /Y
xcopy /s "bin\ImGuiReduxWin32.pdb" "%SA_DIR%\cleo\CLEO_PLUGINS\" /K /D /H /Y
>>>>>>> d1a2aba3c3bc211b773c9c44dee2eeb385378b81
7 changes: 0 additions & 7 deletions tools/Debug64.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@ echo --------------------------------------------------
echo[
call "tools\Setup.bat"
MsBuild ImGuiRedux.sln /property:Configuration=Release /t:ImGuiRedux /property:Platform=Win64
<<<<<<< HEAD
del "%GTA_SA_DIR%\cleo\CLEO_PLUGINS\ImGuiReduxWin64.cleo" /Q
del "%GTA_SA_DIR%\cleo\CLEO_PLUGINS\ImGuiReduxWin64.pdb" /Q
xcopy /s "bin\ImGuiReduxWin64.cleo" "%GTA_SA_DIR%\cleo\CLEO_PLUGINS\" /K /D /H /Y
xcopy /s "bin\ImGuiReduxWin64.pdb" "%GTA_SA_DIR%\cleo\CLEO_PLUGINS\" /K /D /H /Y
=======
del "%SA_DIR%\cleo\CLEO_PLUGINS\ImGuiReduxWin64.cleo" /Q
del "%SA_DIR%\cleo\CLEO_PLUGINS\ImGuiReduxWin64.pdb" /Q
xcopy /s "bin\ImGuiReduxWin64.cleo" "%SA_DIR%\cleo\CLEO_PLUGINS\" /K /D /H /Y
xcopy /s "bin\ImGuiReduxWin64.pdb" "%SA_DIR%\cleo\CLEO_PLUGINS\" /K /D /H /Y
>>>>>>> d1a2aba3c3bc211b773c9c44dee2eeb385378b81
7 changes: 0 additions & 7 deletions tools/DebugCleo.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@ echo --------------------------------------------------
echo[
call "tools\Setup.bat"
MsBuild ImGuiRedux.sln /property:Configuration=DebugCleo /t:ImGuiRedux /property:Platform=Win32
<<<<<<< HEAD
del "%GTA_SA_DIR%\cleo\CLEO_PLUGINS\ImGuiCleoWin32.cleo" /Q
del "%GTA_SA_DIR%\cleo\CLEO_PLUGINS\ImGuiCleoWin32.pdb" /Q
xcopy /s "bin\ImGuiCleoWin32.cleo" "%GTA_SA_DIR%\cleo\CLEO_PLUGINS\" /K /D /H /Y
xcopy /s "bin\ImGuiCleoWin32.pdb" "%GTA_SA_DIR%\cleo\CLEO_PLUGINS\" /K /D /H /Y
=======
del "%SA_DIR%\cleo\CLEO_PLUGINS\ImGuiCleoWin32.cleo" /Q
del "%SA_DIR%\cleo\CLEO_PLUGINS\ImGuiCleoWin32.pdb" /Q
xcopy /s "bin\ImGuiCleoWin32.cleo" "%SA_DIR%\cleo\CLEO_PLUGINS\" /K /D /H /Y
xcopy /s "bin\ImGuiCleoWin32.pdb" "%SA_DIR%\cleo\CLEO_PLUGINS\" /K /D /H /Y
>>>>>>> d1a2aba3c3bc211b773c9c44dee2eeb385378b81
5 changes: 0 additions & 5 deletions tools/Release.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,5 @@ echo --------------------------------------------------
echo[
call "tools\Setup.bat"
MsBuild ImGuiRedux.sln /property:Configuration=Release /t:ImGuiRedux /property:Platform=Win32
<<<<<<< HEAD
del "%GTA_SA_DIR%\cleo\CLEO_PLUGINS\ImGuiReduxWin32.cleo" /Q
xcopy /s "bin\ImGuiReduxWin32.cleo" "%GTA_SA_DIR%\cleo\CLEO_PLUGINS\" /K /D /H /Y
=======
del "%SA_DIR%\cleo\CLEO_PLUGINS\ImGuiReduxWin32.cleo" /Q
xcopy /s "bin\ImGuiReduxWin32.cleo" "%SA_DIR%\cleo\CLEO_PLUGINS\" /K /D /H /Y
>>>>>>> d1a2aba3c3bc211b773c9c44dee2eeb385378b81
5 changes: 0 additions & 5 deletions tools/Release64.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,5 @@ echo --------------------------------------------------
echo[
call "tools\Setup.bat"
MsBuild ImGuiRedux.sln /property:Configuration=Release /t:ImGuiRedux /property:Platform=Win64
<<<<<<< HEAD
del "%GTA_SA_DIR%\cleo\CLEO_PLUGINS\ImGuiReduxWin64.cleo" /Q
xcopy /s "bin\ImGuiReduxWin64.cleo" "%GTA_SA_DIR%\cleo\CLEO_PLUGINS\" /K /D /H /Y
=======
del "%SA_DIR%\cleo\CLEO_PLUGINS\ImGuiReduxWin64.cleo" /Q
xcopy /s "bin\ImGuiReduxWin64.cleo" "%SA_DIR%\cleo\CLEO_PLUGINS\" /K /D /H /Y
>>>>>>> d1a2aba3c3bc211b773c9c44dee2eeb385378b81
5 changes: 0 additions & 5 deletions tools/ReleaseCleo.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,5 @@ echo --------------------------------------------------
echo[
call "tools\Setup.bat"
MsBuild ImGuiRedux.sln /property:Configuration=ReleaseCleo /t:ImGuiRedux /property:Platform=Win32
<<<<<<< HEAD
del "%GTA_SA_DIR%\cleo\CLEO_PLUGINS\ImGuiReduxWin32.cleo" /Q
xcopy /s "bin\ImGuiReduxWin32.cleo" "%GTA_SA_DIR%\cleo\CLEO_PLUGINS\" /K /D /H /Y
=======
del "%SA_DIR%\cleo\CLEO_PLUGINS\ImGuiReduxWin32.cleo" /Q
xcopy /s "bin\ImGuiReduxWin32.cleo" "%SA_DIR%\cleo\CLEO_PLUGINS\" /K /D /H /Y
>>>>>>> d1a2aba3c3bc211b773c9c44dee2eeb385378b81
8 changes: 0 additions & 8 deletions tools/Setup.bat
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
@echo off
<<<<<<< HEAD
set "projectdir=%CD%"
set "PLUGIN_NAME="GrinchTrainer""

Expand Down Expand Up @@ -46,10 +45,3 @@ premake5.exe vs2022
:run_dev
cd "%projectdir%\build"
call %vs_path%"\Common7\Tools\VsDevCmd.bat"
=======
rem Set game paths below
cd tools
premake5.exe vs2022
cd ../build
call "C:\Program Files\Microsoft Visual Studio\2022\Preview\Common7\Tools\VsDevCmd.bat"
>>>>>>> d1a2aba3c3bc211b773c9c44dee2eeb385378b81

0 comments on commit 8c24fd1

Please sign in to comment.