Skip to content

Commit

Permalink
[fix] mouse issue with menu opened in game
Browse files Browse the repository at this point in the history
Merge pull request #97 from lagcompensation/master
  • Loading branch information
maecry authored Jul 3, 2024
2 parents 75d66f7 + e0ee02f commit b6fbac7
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 33 deletions.
2 changes: 1 addition & 1 deletion cstrike/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* - used to verify game version
*/

#define CS_PRODUCTSTRINGVERSION CS_XOR("1.40.1.7")
#define CS_PRODUCTSTRINGVERSION CS_XOR("1.40.2.0")

/*
* game's modules
Expand Down
16 changes: 16 additions & 0 deletions cstrike/core/hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ bool H::Setup()
return false;
L_PRINT(LOG_INFO) << CS_XOR("\"DrawObject\" hook has been created");

if (!hkIsRelativeMouseMode.Create(MEM::GetVFunc(I::InputSystem, VTABLE::SDL::ISRELATIVEMOUSEMODE), reinterpret_cast<void*>(&IsRelativeMouseMode)))
return false;
L_PRINT(LOG_INFO) << CS_XOR("\"IsRelativeMouseMode\" hook has been created");

return true;
}

Expand Down Expand Up @@ -281,3 +285,15 @@ void CS_FASTCALL H::DrawObject(void* pAnimatableSceneObjectDesc, void* pDx11, CM
if (!F::OnDrawObject(pAnimatableSceneObjectDesc, pDx11, arrMeshDraw, nDataCount, pSceneView, pSceneLayer, pUnk, pUnk2))
oDrawObject(pAnimatableSceneObjectDesc, pDx11, arrMeshDraw, nDataCount, pSceneView, pSceneLayer, pUnk, pUnk2);
}

void* H::IsRelativeMouseMode(void* pThisptr, bool bActive)
{
const auto oIsRelativeMouseMode = hkIsRelativeMouseMode.GetOriginal();

MENU::bMainActive = bActive;

if (MENU::bMainWindowOpened)
return oIsRelativeMouseMode(pThisptr, false);

return oIsRelativeMouseMode(pThisptr, bActive);
}
11 changes: 11 additions & 0 deletions cstrike/core/hooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ namespace VTABLE
FRAMESTAGENOTIFY = 36U,
};
}

namespace SDL
{
enum
{
ISRELATIVEMOUSEMODE = 78u,
};
}
}

class CRenderGameSystem;
Expand Down Expand Up @@ -69,6 +77,8 @@ namespace H
void CS_FASTCALL OverrideView(void* pClientModeCSNormal, CViewSetup* pSetup);
void CS_FASTCALL DrawObject(void* pAnimatableSceneObjectDesc, void* pDx11, CMeshData* arrMeshDraw, int nDataCount, void* pSceneView, void* pSceneLayer, void* pUnk, void* pUnk2);

// sdl functions
void* IsRelativeMouseMode(void* pThisptr, bool bActive);

/* @section: managers */
inline CBaseHookObject<decltype(&Present)> hkPresent = {};
Expand All @@ -79,6 +89,7 @@ namespace H
inline CBaseHookObject<decltype(&GetMatrixForView)> hkGetMatrixForView = {};
inline CBaseHookObject<decltype(&CreateMove)> hkCreateMove = {};
inline CBaseHookObject<decltype(&MouseInputEnabled)> hkMouseInputEnabled = {};
inline CBaseHookObject<decltype(&IsRelativeMouseMode)> hkIsRelativeMouseMode = {};
inline CBaseHookObject<decltype(&FrameStageNotify)> hkFrameStageNotify = {};
inline CBaseHookObject<decltype(&LevelInit)> hkLevelInit = {};
inline CBaseHookObject<decltype(&LevelShutdown)> hkLevelShutdown = {};
Expand Down
1 change: 1 addition & 0 deletions cstrike/core/menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ namespace MENU
};

inline bool bMainWindowOpened = false;
inline bool bMainActive = false;
inline int nCurrentMainTab = 0;
inline ParticleContext_t menuParticle = ParticleContext_t(MENU_MAX_BACKGROUND_PARTICLES);
inline AnimationHandler_t animMenuDimBackground;
Expand Down
2 changes: 1 addition & 1 deletion cstrike/sdk/interfaces/iinputsystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class IInputSystem
bool IsRelativeMouseMode()
{
// @ida: 'IInputSystem::SetRelativeMouseMode'.
return MEM::fnGetRelativeMouseMode();
return *reinterpret_cast<bool*>(reinterpret_cast<std::uintptr_t>(this) + 0x4D);
}

void* GetSDLWindow()
Expand Down
17 changes: 6 additions & 11 deletions cstrike/utilities/draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
// used: bMainWindowOpened
#include "../core/menu.h"

// used: hkIsRelativeMouseMode.GetOriginal();
#include "../core/hooks.h"

#pragma region imgui_extended
static constexpr const char* arrKeyNames[] = {
Expand Down Expand Up @@ -518,17 +520,10 @@ bool D::OnWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
MENU::bMainWindowOpened = !MENU::bMainWindowOpened;
// update animation
MENU::animMenuDimBackground.Switch();
// handle mouse input when menu is opened
if (I::InputSystem->IsRelativeMouseMode())
{
// set input system mouse mode
MEM::fnSetRelativeMouseMode(!MENU::bMainWindowOpened);
// set input system window grab state
MEM::fnSetWindowMouseGrab(I::InputSystem->GetSDLWindow(), !MENU::bMainWindowOpened);
// warp our cursor into middle of the screen
const ImVec2 vecScreenCenter = ImGui::GetIO().DisplaySize / 2.f;
MEM::fnWarpMouseInWindow(nullptr, vecScreenCenter.x, vecScreenCenter.y);
}

// handle IsRelativeMouseMode original
const auto oIsRelativeMouseMode = H::hkIsRelativeMouseMode.GetOriginal();
oIsRelativeMouseMode(I::InputSystem, MENU::bMainWindowOpened ? false : MENU::bMainActive);
}

// handle ImGui's window messages and block game's input if menu is opened
Expand Down
15 changes: 1 addition & 14 deletions cstrike/utilities/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,14 @@ bool MEM::Setup()
{
bool bSuccess = true;

const void* hSDL3 = GetModuleBaseHandle(SDL3_DLL);
const void* hDbgHelp = GetModuleBaseHandle(DBGHELP_DLL);
const void* hTier0 = GetModuleBaseHandle(TIER0_DLL);
if (hSDL3 == nullptr || hDbgHelp == nullptr)
if (hDbgHelp == nullptr || hTier0 == nullptr)
return false;

fnUnDecorateSymbolName = reinterpret_cast<decltype(fnUnDecorateSymbolName)>(GetExportAddress(hDbgHelp, CS_XOR("UnDecorateSymbolName")));
bSuccess &= (fnUnDecorateSymbolName != nullptr);

fnSetRelativeMouseMode = reinterpret_cast<decltype(fnSetRelativeMouseMode)>(GetExportAddress(hSDL3, CS_XOR("SDL_SetRelativeMouseMode")));
bSuccess &= (fnSetRelativeMouseMode != nullptr);

fnGetRelativeMouseMode = reinterpret_cast<decltype(fnGetRelativeMouseMode)>(GetExportAddress(hSDL3, CS_XOR("SDL_GetRelativeMouseMode")));
bSuccess &= (fnGetRelativeMouseMode != nullptr);

fnSetWindowMouseGrab = reinterpret_cast<decltype(fnSetWindowMouseGrab)>(GetExportAddress(hSDL3, CS_XOR("SDL_SetWindowMouseGrab")));
bSuccess &= (fnSetWindowMouseGrab != nullptr);

fnWarpMouseInWindow = reinterpret_cast<decltype(fnWarpMouseInWindow)>(GetExportAddress(hSDL3, CS_XOR("SDL_WarpMouseInWindow")));
bSuccess &= (fnWarpMouseInWindow != nullptr);

fnLoadKV3 = reinterpret_cast<decltype(fnLoadKV3)>(GetExportAddress(hTier0, CS_XOR("?LoadKV3@@YA_NPEAVKeyValues3@@PEAVCUtlString@@PEBDAEBUKV3ID_t@@2@Z")));
bSuccess &= (fnLoadKV3 != nullptr);

Expand Down
6 changes: 0 additions & 6 deletions cstrike/utilities/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,6 @@ namespace MEM
/* @section: game exports */
inline unsigned long(CS_STDCALL* fnUnDecorateSymbolName)(const char* szName, char* pszOutput, unsigned long nMaxStringLength, unsigned long dwFlags) = nullptr;

// SDL window stuffs
// @note: helpful for doing our mouse cursor like force show it when our menu is opened
inline int(CS_STDCALL* fnSetRelativeMouseMode)(int) = nullptr;
inline int(CS_STDCALL* fnSetWindowMouseGrab)(void*, int) = nullptr;
inline bool(CS_STDCALL* fnGetRelativeMouseMode)(void) = nullptr;
inline int(CS_STDCALL* fnWarpMouseInWindow)(void*, float, float) = nullptr;
inline bool(CS_FASTCALL* fnLoadKV3)(CKeyValues3*, void*, const char*, const KV3ID_t*, const char*);
inline std::int64_t(CS_FASTCALL* fnCreateMaterial)(void*, void*, const char*, void*, unsigned int, unsigned int);

Expand Down

0 comments on commit b6fbac7

Please sign in to comment.