Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Strafe HUD #364

Merged
merged 2 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions spt.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,7 @@ del "$(OutDir)spt-version.obj"</Command>
<ClCompile Include="spt\features\restart.cpp" />
<ClCompile Include="spt\features\saveloads.cpp" />
<ClCompile Include="spt\features\shadow.cpp" />
<ClCompile Include="spt\features\strafehud.cpp" />
<ClCompile Include="spt\features\stucksave.cpp" />
<ClCompile Include="spt\features\tas.cpp" />
<ClCompile Include="spt\features\taslogging.cpp" />
Expand Down
3 changes: 3 additions & 0 deletions spt.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,9 @@
<ClCompile Include="spt\features\grass.cpp">
<Filter>spt\features</Filter>
</ClCompile>
<ClCompile Include="spt\features\strafehud.cpp">
<Filter>spt\features</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\public\tier0\basetypes.h">
Expand Down
3 changes: 2 additions & 1 deletion spt/features/aim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "ent_utils.hpp"
#include "math.hpp"
#include "playerio.hpp"
#include "signals.hpp"

#undef min
#undef max
Expand Down Expand Up @@ -330,7 +331,7 @@ CON_COMMAND(_y_spt_setangle,

void AimFeature::LoadFeature()
{
if (spt_generic.ORIG_ControllerMove && spt_playerio.ORIG_CreateMove)
if (spt_generic.ORIG_ControllerMove && CreateMoveSignal.Works)
{
InitCommand(tas_aim_reset);
InitCommand(tas_aim);
Expand Down
73 changes: 17 additions & 56 deletions spt/features/ihud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,10 @@ class InputHud : public FeatureWrapper<InputHud>

virtual void LoadFeature() override;

virtual void PreHook() override;

virtual void UnloadFeature() override;

private:
DECL_HOOK_THISCALL(void, DecodeUserCmdFromBuffer, void*, bf_read& buf, int sequence_number);
void CreateMove(uintptr_t pCmd);
void SetData(uintptr_t pCmd);
void DrawRectAndCenterTxt(Color buttonColor,
int x0,
int y0,
Expand All @@ -101,8 +98,6 @@ class InputHud : public FeatureWrapper<InputHud>
int buttonBits = 0;

bool awaitingFrameDraw = false;

bool loadingSuccessful = false;
};

InputHud spt_ihud;
Expand Down Expand Up @@ -564,22 +559,7 @@ CON_COMMAND(y_spt_ihud_add_key, "Add custom key to ihud.")
}
#endif

namespace patterns
{
PATTERNS(
DecodeUserCmdFromBuffer,
"5135",
"83 EC 54 33 C0 D9 EE 89 44 24 ?? D9 54 24 ?? 89 44 24 ??",
"7197370",
"55 8B EC 83 EC 54 56 8B F1 C7 45 ?? ?? ?? ?? ?? 8D 4D ?? C7 45 ?? 00 00 00 00 C7 45 ?? 00 00 00 00 C7 45 ?? 00 00 00 00 C7 45 ?? 00 00 00 00 C7 45 ?? 00 00 00 00 E8 ?? ?? ?? ?? 8B 4D ??",
"4044",
"83 EC 54 53 57 8D 44 24 ?? 50 8B 44 24 ?? 99");
}

void InputHud::InitHooks()
{
HOOK_FUNCTION(client, DecodeUserCmdFromBuffer);
}
void InputHud::InitHooks() {}

bool InputHud::ShouldLoadFeature()
{
Expand All @@ -588,10 +568,11 @@ bool InputHud::ShouldLoadFeature()

void InputHud::LoadFeature()
{
if (!loadingSuccessful)
if (!(CreateMoveSignal.Works && DecodeUserCmdFromBufferSignal.Works))
return;
if (CreateMoveSignal.Works)
CreateMoveSignal.Connect(this, &InputHud::CreateMove);

CreateMoveSignal.Connect(this, &InputHud::SetData);
DecodeUserCmdFromBufferSignal.Connect(this, &InputHud::SetData);

bool result = spt_hud_feat.AddHudDefaultGroup(HudCallback(
std::bind(&InputHud::DrawInputHud, this), []() { return y_spt_ihud.GetBool(); }, false));
Expand Down Expand Up @@ -664,27 +645,8 @@ void InputHud::LoadFeature()
anglesSetting = {true, false, L"", font, 4, 1, 0, 0, background, highlight, textcolor, texthighlight, 0};
}

void InputHud::PreHook()
{
loadingSuccessful = !!(ORIG_DecodeUserCmdFromBuffer);
}

void InputHud::UnloadFeature() {}

void InputHud::SetInputInfo(int button, Vector movement)
{
if (awaitingFrameDraw)
{
buttonBits |= button;
}
else
{
buttonBits = button;
}
inputMovement = movement;
awaitingFrameDraw = true;
}

bool InputHud::ModifySetting(const char* element, const char* param, const char* value)
{
InputHud::Button* target;
Expand Down Expand Up @@ -1057,21 +1019,20 @@ void InputHud::DrawButton(Button button)
button.text.c_str());
}

IMPL_HOOK_THISCALL(InputHud, void, DecodeUserCmdFromBuffer, void*, bf_read& buf, int sequence_number)
void InputHud::SetData(uintptr_t pCmd)
{
spt_ihud.ORIG_DecodeUserCmdFromBuffer(thisptr, buf, sequence_number);

auto m_pCommands =
*reinterpret_cast<uintptr_t*>(reinterpret_cast<uintptr_t>(thisptr) + spt_playerio.offM_pCommands);
auto pCmd = m_pCommands + spt_playerio.sizeofCUserCmd * (sequence_number % 90);
auto cmd = reinterpret_cast<CUserCmd*>(pCmd);
spt_ihud.SetInputInfo(cmd->buttons, Vector(cmd->sidemove, cmd->forwardmove, cmd->upmove));
}

void InputHud::CreateMove(uintptr_t pCmd)
{
auto cmd = reinterpret_cast<CUserCmd*>(pCmd);
spt_ihud.SetInputInfo(cmd->buttons, Vector(cmd->sidemove, cmd->forwardmove, cmd->upmove));
if (awaitingFrameDraw)
{
buttonBits |= cmd->buttons;
}
else
{
buttonBits = cmd->buttons;
}
inputMovement = Vector(cmd->sidemove, cmd->forwardmove, cmd->upmove);
awaitingFrameDraw = true;
}

std::string InputHud::Button::ToString() const
Expand Down
67 changes: 43 additions & 24 deletions spt/features/playerio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ namespace patterns
"8B 44 24 ?? 83 EC 1C 53 55 56 57",
"dmomm",
"83 EC 10 53 8B 5C 24 ?? 55 56 8B F1");
PATTERNS(
DecodeUserCmdFromBuffer,
"5135",
"83 EC 54 33 C0 D9 EE 89 44 24 ?? D9 54 24 ?? 89 44 24 ??",
"7197370",
"55 8B EC 83 EC 54 56 8B F1 C7 45 ?? ?? ?? ?? ?? 8D 4D ?? C7 45 ?? 00 00 00 00 C7 45 ?? 00 00 00 00 C7 45 ?? 00 00 00 00 C7 45 ?? 00 00 00 00 C7 45 ?? 00 00 00 00 E8 ?? ?? ?? ?? 8B 4D ??",
"4044",
"83 EC 54 53 57 8D 44 24 ?? 50 8B 44 24 ?? 99");
PATTERNS(
GetGroundEntity,
"5135",
Expand All @@ -114,6 +122,7 @@ namespace patterns
void PlayerIOFeature::InitHooks()
{
HOOK_FUNCTION(client, CreateMove);
HOOK_FUNCTION(client, DecodeUserCmdFromBuffer);
HOOK_FUNCTION(client, GetButtonBits);
FIND_PATTERN(client, GetGroundEntity);
}
Expand Down Expand Up @@ -171,6 +180,11 @@ void PlayerIOFeature::PreHook()
offSidemove = 28;
}

if (ORIG_DecodeUserCmdFromBuffer)
{
DecodeUserCmdFromBufferSignal.Works = true;
}

GetPlayerFields();
}

Expand Down Expand Up @@ -284,38 +298,48 @@ Strafe::MovementVars PlayerIOFeature::GetMovementVars()
return vars;
}

void __fastcall PlayerIOFeature::HOOKED_CreateMove_Func(void* thisptr,
int edx,
int sequence_number,
float input_sample_frametime,
bool active)
IMPL_HOOK_THISCALL(PlayerIOFeature,
void,
CreateMove,
void*,
int sequence_number,
float input_sample_frametime,
bool active)
{
auto m_pCommands = *reinterpret_cast<uintptr_t*>(reinterpret_cast<uintptr_t>(thisptr) + offM_pCommands);
pCmd = m_pCommands + sizeofCUserCmd * (sequence_number % 90);
auto m_pCommands =
*reinterpret_cast<uintptr_t*>(reinterpret_cast<uintptr_t>(thisptr) + spt_playerio.offM_pCommands);
auto pCmd = m_pCommands + spt_playerio.sizeofCUserCmd * (sequence_number % 90);
spt_playerio.pCmd = pCmd;

ORIG_CreateMove(thisptr, edx, sequence_number, input_sample_frametime, active);
spt_playerio.ORIG_CreateMove(thisptr, sequence_number, input_sample_frametime, active);

CreateMoveSignal(pCmd);

pCmd = 0;
spt_playerio.pCmd = 0;
}

void __fastcall PlayerIOFeature::HOOKED_CreateMove(void* thisptr,
int edx,
int sequence_number,
float input_sample_frametime,
bool active)
IMPL_HOOK_THISCALL(PlayerIOFeature, void, DecodeUserCmdFromBuffer, void*, bf_read& buf, int sequence_number)
{
spt_playerio.HOOKED_CreateMove_Func(thisptr, edx, sequence_number, input_sample_frametime, active);
spt_playerio.ORIG_DecodeUserCmdFromBuffer(thisptr, buf, sequence_number);

auto m_pCommands =
*reinterpret_cast<uintptr_t*>(reinterpret_cast<uintptr_t>(thisptr) + spt_playerio.offM_pCommands);
auto pCmd = m_pCommands + spt_playerio.sizeofCUserCmd * (sequence_number % 90);

DecodeUserCmdFromBufferSignal(pCmd);
}

int __fastcall PlayerIOFeature::HOOKED_GetButtonBits_Func(void* thisptr, int edx, int bResetState)
IMPL_HOOK_THISCALL(PlayerIOFeature, int, GetButtonBits, void*, int bResetState)
{
int rv = ORIG_GetButtonBits(thisptr, edx, bResetState);
int rv = spt_playerio.ORIG_GetButtonBits(thisptr, bResetState);

if (bResetState == 1)
{
static int keyPressed = 0;
int spamButtons = spt_playerio.spamButtons;
bool& forceJump = spt_playerio.forceJump;
bool& forceUnduck = spt_playerio.forceUnduck;

keyPressed = (keyPressed ^ spamButtons) & spamButtons;
rv |= keyPressed;

Expand All @@ -335,11 +359,6 @@ int __fastcall PlayerIOFeature::HOOKED_GetButtonBits_Func(void* thisptr, int edx
return rv;
}

int __fastcall PlayerIOFeature::HOOKED_GetButtonBits(void* thisptr, int edx, int bResetState)
{
return spt_playerio.HOOKED_GetButtonBits_Func(thisptr, edx, bResetState);
}

bool PlayerIOFeature::GetFlagsDucking()
{
return m_fFlags.GetValue() & FL_DUCKING;
Expand All @@ -354,7 +373,7 @@ Strafe::PlayerData PlayerIOFeature::GetPlayerData()
const int IN_DUCK = 1 << 2;

data.Ducking = GetFlagsDucking();
data.DuckPressed = (ORIG_GetButtonBits(cinput_thisptr, 0, 0) & IN_DUCK);
data.DuckPressed = (ORIG_GetButtonBits(cinput_thisptr, 0) & IN_DUCK);
data.UnduckedOrigin = m_vecAbsOrigin.GetValue();
data.Velocity = GetPlayerVelocity();
data.Basevelocity = Vector();
Expand Down Expand Up @@ -454,7 +473,7 @@ bool PlayerIOFeature::IsGroundEntitySet()
bool PlayerIOFeature::TryJump()
{
const int IN_JUMP = (1 << 1);
return ORIG_GetButtonBits(cinput_thisptr, 0, 0) & IN_JUMP;
return ORIG_GetButtonBits(cinput_thisptr, 0) & IN_JUMP;
}

bool PlayerIOFeature::PlayerIOAddressesFound()
Expand Down
17 changes: 3 additions & 14 deletions spt/features/playerio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,9 @@ typedef void*(__cdecl* _GetLocalPlayer)();
class PlayerIOFeature : public FeatureWrapper<PlayerIOFeature>
{
private:
void __fastcall HOOKED_CreateMove_Func(void* thisptr,
int edx,
int sequence_number,
float input_sample_frametime,
bool active);
static void __fastcall HOOKED_CreateMove(void* thisptr,
int edx,
int sequence_number,
float input_sample_frametime,
bool active);
int __fastcall HOOKED_GetButtonBits_Func(void* thisptr, int edx, int bResetState);
static int __fastcall HOOKED_GetButtonBits(void* thisptr, int edx, int bResetState);
DECL_HOOK_THISCALL(void, CreateMove, void*, int sequence_number, float input_sample_frametime, bool active);
DECL_HOOK_THISCALL(void, DecodeUserCmdFromBuffer, void*, bf_read& buf, int sequence_number);
DECL_HOOK_THISCALL(int, GetButtonBits, void*, int bResetState);

public:
virtual bool ShouldLoadFeature() override;
Expand All @@ -60,10 +51,8 @@ class PlayerIOFeature : public FeatureWrapper<PlayerIOFeature>
int spamButtons = 0;
ptrdiff_t offServerAbsOrigin = 0;
uintptr_t pCmd = 0;
_CreateMove ORIG_CreateMove = nullptr;

_GetGroundEntity ORIG_GetGroundEntity = nullptr;
_GetButtonBits ORIG_GetButtonBits = nullptr;
Vector currentVelocity;
Vector previousVelocity;

Expand Down
Loading