9#include <cs_usercmd.pb.h>
10#include <entityhandle.h>
25 const int slot = CEntityHandle(controller.
Handle).GetEntryIndex() - 1;
30 : _registry(registry), _bindings(bindings)
32 if (
Status installed = Install(); !installed)
34 Log::Warn(
"Player commands: {}; Movement is off.", installed.error().Detail);
37 if (!_bindings.UserCmdNumber)
39 Log::Warn(
"Player commands: no 'CUserCmdBase::cmdNum' offset; command numbers read 0.");
43Status PlayerCommands::Install()
47 return std::unexpected(
Error::Unsupported(
"the RunCommand slot or the usercmd offset did not bind"));
50 const auto beforeCommand = [
this](EngineMovementServices& services,
void* userCmd) { Before(&services, userCmd); };
51 const auto afterCommand = [
this](EngineMovementServices&,
void*) { After(); };
52 auto hook =
HookVirtual(
"Player commands", _bindings.RunCommand, beforeCommand, afterCommand);
55 return std::unexpected(hook.error());
57 _hook = std::move(*hook);
61void PlayerCommands::Before(
void* movementServices,
const void* userCmd)
63 _slot = _registry.HasCommandListeners() ?
OwnerSlot(movementServices) : -1;
69 _registry.RaisePlayerCommand(_slot, _input);
72void PlayerCommands::After()
76 _registry.RaisePlayerCommandDone(_slot, _input);
80void PlayerCommands::Decode(
const void* userCmd)
88 const auto* pb =
static_cast<const CSGOUserCmdPB*
>(_bindings.UserCmdProto.Ptr(userCmd));
89 const auto& base = pb->base();
92 _input.ClientTick = base.client_tick();
94 _input.CommandNumber =
95 _bindings.UserCmdNumber ? _bindings.UserCmdNumber.Read(userCmd) : base.legacy_command_number();
96 _input.HasViewAngles = base.has_viewangles();
97 if (_input.HasViewAngles)
99 _input.ViewPitch = base.viewangles().x();
100 _input.ViewYaw = base.viewangles().y();
101 _input.ViewRoll = base.viewangles().z();
103 _input.ForwardMove = base.forwardmove();
104 _input.LeftMove = base.leftmove();
105 if (base.has_buttons_pb())
107 _input.ButtonsHeld = base.buttons_pb().buttonstate1();
108 _input.ButtonsChanged = base.buttons_pb().buttonstate2();
110 _input.MouseDx = base.mousedx();
111 _input.MouseDy = base.mousedy();
112 _input.Attack1StartHistoryIndex = pb->attack1_start_history_index();
113 _input.Attack2StartHistoryIndex = pb->attack2_start_history_index();
116 for (
int i = 0; i < _input.SubtickMoveCount; ++i)
118 const auto& move = base.subtick_moves(i);
119 _input.SubtickMoves[i] = {
120 .Button = move.button(),
121 .Pressed = move.pressed(),
123 .PitchDelta = move.pitch_delta(),
124 .YawDelta = move.yaw_delta(),
128 _input.InputHistorySent = pb->input_history_size();
130 for (
int i = 0; i < _input.InputHistoryCount; ++i)
132 const auto& entry = pb->input_history(i);
133 auto& sample = _input.InputHistorySamples[i];
134 sample.TargetEntIndex = entry.target_ent_index();
135 if (entry.has_view_angles())
137 sample.HasViewAngles =
true;
138 sample.ViewPitch = entry.view_angles().x();
139 sample.ViewYaw = entry.view_angles().y();
PlayerCommands(const Bindings &bindings, PluginRegistry ®istry)
The loaded plugins and the engine events raised on them, one per process.
EntityRef ControllerRef() const
::CEntityInstance * OwnerEntity() const
void Warn(std::format_string< Args... > fmt, Args &&... args)
Result< Subscription > HookVirtual(std::string_view name, const VirtualFn< Ret(Object *, Args...)> &function, Before &&before, After &&after=nullptr)
Hook a gamedata-bound virtual function on objects sharing its class vtable.
std::expected< void, Error > Status
constexpr bool IsValidSlot(int slot)
static int OwnerSlot(void *movementServices)
Offset< void > UserCmdProto
VirtualFn< void *(EngineMovementServices *, void *)> RunCommand
static Error Unsupported(std::string detail)