VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
ButtonPresses.cpp
Go to the documentation of this file.
2
8#include <cstrike15_usermessages.pb.h>
9#include <engine2/serversideclient.h>
10#include <format>
11#include <netmessages.pb.h>
12#include <networksystem/inetchannel.h>
13#include <networksystem/inetworkmessages.h>
14#include <string_view>
15#include <utility>
16
17namespace VoltMod
18{
19
20// A press arrives as a CustomHudClicked body inside the generic user message.
21static constexpr const char* UserMessageName = "CSVCMsg_UserMessage";
22static constexpr int64_t WarningIntervalSeconds = 10;
23
24ButtonPresses::ButtonPresses(const Bindings& bindings, const EngineInterfaces& engine, PluginRegistry& registry)
25 : _registry(registry), _bindings(bindings), _warnings(WarningIntervalSeconds)
26{
27 if (!engine.NetworkMessages)
28 {
29 return;
30 }
31 if (Status installed = Install(engine); !installed)
32 {
33 Log::Warn("Button presses: {}; presses will not arrive.", installed.error().Detail);
34 }
35}
36
37Status ButtonPresses::Install(const EngineInterfaces& engine)
38{
39 if (const auto* type = engine.NetworkMessages->FindNetworkMessagePartial(UserMessageName))
40 {
41 _messageId = type->GetMessageId();
42 }
43 if (_messageId < 0)
44 {
45 return std::unexpected(Error::Engine(std::format("the engine does not know {}", UserMessageName)));
46 }
47
48 const auto onMessage = [this](INetworkMessageProcessingPreFilter& filter, const CNetMessage* message,
49 INetChannel*) { Queue(message, filter); };
50 auto hook = HookTable("Custom HUD button presses", &INetworkMessageProcessingPreFilter::FilterMessage,
51 _bindings.ClientFilterTable, onMessage);
52 if (!hook)
53 {
54 return std::unexpected(hook.error());
55 }
56 _hook = std::move(*hook);
57 return {};
58}
59
61{
62 // Swapped out first: a handler may cause another press to queue.
63 for (const Press& press : std::exchange(_queued, {}))
64 {
65 _registry.RaiseButtonPress(press.Slot, press.ButtonId);
66 }
67}
68
69void ButtonPresses::Queue(const CNetMessage* message, const INetworkMessageProcessingPreFilter& filter)
70{
71 // Every inbound message passes here; the id rules out nearly all before any parsing.
72 NetMessageInfo_t* info = message ? message->GetNetMessage() : nullptr;
73 if (!info || info->GetMessageId() != _messageId)
74 {
75 return;
76 }
77
78 const auto* user = message->ToPB<CSVCMsg_UserMessage>();
79 if (user->msg_type() != CS_UM_CustomHudClicked)
80 {
81 return;
82 }
83
84 // The sending connection's slot, so a spectator's press stays theirs.
85 const int slot = static_cast<const CServerSideClientBase&>(filter).GetPlayerSlot().Get();
86 if (!IsValidSlot(slot))
87 {
88 return;
89 }
90
91 // Parsed into our own message: the bytes come from the client, not the engine.
92 CCSUsrMsg_CustomHudClicked click;
93 if (!click.ParseFromString(user->msg_data()) || !click.has_button_id())
94 {
95 WarnMalformed(slot);
96 return;
97 }
98 if (click.button_id().find('\0') != std::string::npos)
99 {
100 return;
101 }
102
103 _queued.push_back({.Slot = slot, .ButtonId = std::move(*click.mutable_button_id())});
104}
105
106void ButtonPresses::WarnMalformed(int slot)
107{
108 if (!_warnings.TryStart(slot, static_cast<int64_t>(Time::MonotonicSeconds())))
109 {
110 return;
111 }
112 Log::Warn("Button presses: a press from slot {} is not a CustomHudClicked message with a button id.", slot);
113}
114
115} // namespace VoltMod
ButtonPresses(const Bindings &bindings, const EngineInterfaces &engine, PluginRegistry &registry)
The loaded plugins and the engine events raised on them, one per process.
static double MonotonicSeconds()
Definition Durations.hpp:29
void Warn(std::format_string< Args... > fmt, Args &&... args)
Definition Log.hpp:88
Result< Subscription > HookTable(std::string_view name, Ret(Object::*method)(Args...), const ClassTable &table, Before &&before, After &&after=nullptr)
Hook an SDK-declared virtual on every object sharing a gamedata-found class table.
Definition Hook.hpp:200
std::expected< void, Error > Status
Definition Result.hpp:67
constexpr bool IsValidSlot(int slot)
Definition Slot.hpp:13
static constexpr const char * UserMessageName
static constexpr int64_t WarningIntervalSeconds
INetworkMessages * NetworkMessages
static Error Engine(std::string detail)
Definition Result.hpp:58