VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
NetChannel.cpp
Go to the documentation of this file.
4#include <engine2/isource2server.h>
5#include <inetchannelinfo.h>
6#include <string>
7
8namespace VoltMod
9{
10
11INetChannelInfo* NetChannels::GetNetInfo(int slot) const
12{
13 auto* engine = IsValidSlot(slot) ? _interfaces.Engine : nullptr;
14 return engine ? engine->GetPlayerNetInfo(CPlayerSlot(slot)) : nullptr;
15}
16
17float NetChannels::EngineLatency(int slot) const
18{
19 auto* info = GetNetInfo(slot);
20 return info ? info->GetEngineLatency() : 0.0f;
21}
22
23int NetChannels::GetUserId(int slot) const
24{
25 auto* engine = IsValidSlot(slot) ? _interfaces.Engine : nullptr;
26 if (!engine)
27 {
28 return -1;
29 }
30 // The engine keeps userids in 16 bits and answers an empty slot with all of them set.
31 const int id = engine->GetPlayerUserId(CPlayerSlot(slot)).Get();
32 return id == 0xFFFF ? -1 : id;
33}
34
35std::optional<CommandStatus> NetChannels::GetCommandStatus(int slot) const
36{
37 auto* clients = IsValidSlot(slot) ? _interfaces.ServerGameClients : nullptr;
38 if (!clients || !clients->IsPlayerSlotOccupied(CPlayerSlot(slot)))
39 {
40 return std::nullopt;
41 }
42
43 PlayerCommandStatus_t status{};
44 clients->GetPlayerCommandStatus(CPlayerSlot(slot), &status);
45 return CommandStatus{.LastExecuted = status.m_nLastCmdNumberExecuted,
46 .LastReceived = status.m_nLastCmdNumberReceived,
47 .ReceiveMargin = status.m_flLastCmdRecvMargin,
48 .StarvedTicks = status.m_nStarvedCommandTicks,
49 .DiscardedTicks = status.m_nDiscardedCommandTicks,
50 .HasQueueStats = status.m_bHasCommandQueueStats};
51}
52
53std::string_view NetChannels::GetUserInfoCvar(int slot, std::string_view name) const
54{
55 auto* engine = IsValidSlot(slot) ? _interfaces.Engine : nullptr;
56 if (!engine || name.empty())
57 {
58 return {};
59 }
60
61 const char* value = engine->GetClientConVarValue(CPlayerSlot(slot), std::string(name).c_str());
62 return value ? std::string_view(value) : std::string_view{};
63}
64
65} // namespace VoltMod
std::optional< CommandStatus > GetCommandStatus(int slot) const
std::string_view GetUserInfoCvar(int slot, std::string_view name) const
float EngineLatency(int slot) const
INetChannelInfo * GetNetInfo(int slot) const
int GetUserId(int slot) const
constexpr bool IsValidSlot(int slot)
Definition Slot.hpp:13