VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
ScreenEntitySetters.cpp
Go to the documentation of this file.
1#include "Ui/ScreenEntity.hpp"
2
6#include <format>
7#include <networksystem/networkvar.h>
8#include <tier0/utlstring.h>
9#include <utility>
10
11namespace VoltMod
12{
13
14using StringTable = CNetworkUtlVectorBase<CUtlString>;
15
16/** EHudPanelClassStatus_t values. */
17static constexpr int32_t ClassAbsent = 0;
18static constexpr int32_t ClassPresent = 1;
19
20/** CS2 refuses to intern past this many entries per table; stop short so a runaway caller cannot fill them. */
21static constexpr int StringTableCap = 1024;
22static constexpr int StringTableHeadroom = 64;
23
24/** Assigned in place: CUtlString allocates inside tier0, the allocator the engine frees with. */
25static void SetStr(CUtlString& out, std::string_view text)
26{
27 out.SetDirect(text.data(), static_cast<int>(text.size()));
28}
29
30int ScreenEntity::PlayerStateCount() const
31{
32 // The embedded vector keeps its count first, which is what the engine itself reads before indexing.
33 const Schema::CCSCustomHudLayout layout{_entities.Get(_entity).Raw()};
34 return layout ? layout.PlayerLayoutStates() : -1;
35}
36
37Result<CEntityInstance*> ScreenEntity::ResolveWritable() const
38{
39 Entity entity = _entities.Get(_entity);
40 if (!entity)
41 {
42 return std::unexpected(Error::NotFound("the custom_hud_layout entity no longer exists"));
43 }
44
45 const Schema::CCSCustomHudLayout layout{entity.Raw()};
46 for (const auto& [name, table] :
47 {std::pair{"element id", layout.PanelIds()}, std::pair{"class name", layout.ClassNames()},
48 std::pair{"text variable", layout.DialogVariableNames()}})
49 {
50 const int count = table ? static_cast<const StringTable*>(table)->Count() : -1;
52 {
53 return std::unexpected(
54 Error::Failed(std::format("the {} table is nearly full ({}/{})", name, count, StringTableCap)));
55 }
56 }
57
58 return entity.Raw();
59}
60
61Status ScreenEntity::SendText(std::string_view variable, std::string_view value)
62{
63 auto entity = ResolveWritable();
64 if (!entity)
65 {
66 return std::unexpected(entity.error());
67 }
68
69 const auto& set = _entities.Bindings().CustomHudSetDialogVariable;
70 if (!set)
71 {
72 return std::unexpected(Error::Unsupported("the custom HUD text setter did not bind"));
73 }
74
75 CUtlString root, name, text;
76 SetStr(root, _layout.Name());
77 SetStr(name, variable);
78 SetStr(text, value);
79 set(*entity, &root, &name, &text);
80 return {};
81}
82
83Status ScreenEntity::SendClass(std::string_view elementId, std::string_view className, bool on)
84{
85 auto entity = ResolveWritable();
86 if (!entity)
87 {
88 return std::unexpected(entity.error());
89 }
90
91 const auto& set = _entities.Bindings().CustomHudSetHasClass;
92 if (!set)
93 {
94 return std::unexpected(Error::Unsupported("the custom HUD class setter did not bind"));
95 }
96
97 CUtlString element, name;
98 SetStr(element, elementId);
99 SetStr(name, className);
100 set(*entity, &element, &name, on ? ClassPresent : ClassAbsent);
101 return {};
102}
103
104Status ScreenEntity::SendCursor(bool shown)
105{
106 auto entity = ResolveWritable();
107 if (!entity)
108 {
109 return std::unexpected(entity.error());
110 }
111
112 // The engine's per-player setter returns silently for a slot past the state count.
113 if (const int states = PlayerStateCount(); states <= _owner)
114 {
115 return std::unexpected(
116 Error::Failed(std::format("slot {} has no per-player layout state (the entity holds {})", _owner, states)));
117 }
118
119 const auto& set = _entities.Bindings().CustomHudSetInputCapture;
120 if (!set)
121 {
122 return std::unexpected(Error::Unsupported("the custom HUD input capture setter did not bind"));
123 }
124 set(*entity, _owner, shown);
125 return {};
126}
127
128} // namespace VoltMod
Entity Get(EntityRef ref)
CEntityInstance * Raw() const noexcept
Definition Entity.hpp:58
static constexpr int32_t ClassAbsent
CNetworkUtlVectorBase< CUtlString > StringTable
std::expected< void, Error > Status
Definition Result.hpp:67
static constexpr int32_t ClassPresent
std::expected< T, Error > Result
Definition Result.hpp:64
static constexpr int StringTableCap
static void SetStr(CUtlString &out, std::string_view text)
static constexpr int StringTableHeadroom
static Error Failed(std::string detail)
Definition Result.hpp:59
static Error Unsupported(std::string detail)
Definition Result.hpp:57
static Error NotFound(std::string detail)
Definition Result.hpp:49