VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
ScreenEntity.cpp
Go to the documentation of this file.
1#include "Ui/ScreenEntity.hpp"
2
6#include <algorithm>
7#include <string>
8#include <utility>
9
10namespace VoltMod
11{
12
13/** `<block>--<name>`, built in @p out. */
14static std::string_view ModifierClass(std::string& out, std::string_view block, std::string_view name)
15{
16 out.assign(block).append("--").append(name);
17 return out;
18}
19
21 const NetChannels& clients, std::vector<const ScreenEntity*>& live, LayoutPath layout,
22 int owner)
23 : _entities(entities),
24 _visibility(visibility),
25 _clients(clients),
26 _live(live),
27 _layout(std::move(layout)),
28 _owner(owner)
29{
30 _live.push_back(this);
31 _slotChanges = slots.Changed += [this](int slot) {
32 _playersChangedSinceSpawn = true;
33 if (slot == _owner) // the screen leaves with its owner
34 {
35 Remove();
36 }
37 };
38 // The game resets the owner's per-player state, the cursor, as their client finishes loading.
39 _fullConnects = slots.FullyConnected += [this](int slot) {
40 _playersChangedSinceSpawn = true;
41 if (slot == _owner)
42 {
43 _written.ResetCursor(_owner);
44 }
45 };
46}
47
49{
50 Remove();
51 std::erase(_live, this);
52}
53
55{
56 return static_cast<bool>(_entities.Get(_entity));
57}
58
60{
61 return _cursor && Exists();
62}
63
65{
66 // A bot has no client to draw on.
67 if (!Accepts(slot) || !_clients.HasClient(_owner))
68 {
69 return false;
70 }
71
72 if (!Exists() && !SpawnOrWarn())
73 {
74 return false;
75 }
76
77 if (Covers())
78 {
79 return true;
80 }
81
82 // Respawning for any other reason would retry a hopeless spawn every frame.
83 if (!_playersChangedSinceSpawn)
84 {
85 return false;
86 }
87
88 return SpawnOrWarn() && Covers();
89}
90
92{
93 _visibility.ShowToEveryone(_entity);
94 _entities.Get(_entity).Remove();
95
96 _entity = {};
97 _written.Clear();
98 _cursor = false;
99}
100
101void ScreenEntity::WriteText(int slot, std::string_view variable, std::string_view value)
102{
103 // One entity holds one layout, so the variable alone names the text.
104 if (!Accepts(slot) || !_written.Changed(_owner, WriteKind::Text, {}, variable, value))
105 {
106 return;
107 }
108
109 CheckWrite(SendText(variable, value), variable);
110}
111
112void ScreenEntity::WriteClass(int slot, std::string_view elementId, std::string_view className, bool on)
113{
114 if (!Accepts(slot) || !_written.ClassChanged(_owner, elementId, className, on))
115 {
116 return;
117 }
118
119 CheckWrite(SendClass(elementId, className, on), elementId);
120}
121
122void ScreenEntity::WriteModifier(int slot, std::string_view elementId, std::string_view block,
123 std::span<const std::string_view> names, std::string_view name)
124{
125 if (!Accepts(slot))
126 {
127 return;
128 }
129 std::string className;
130 // One modifier is on at a time, so finding the wanted one on leaves nothing to send.
131 if (_written.IsClassOn(_owner, elementId, ModifierClass(className, block, name)))
132 {
133 return;
134 }
135 for (std::string_view each : names)
136 {
137 const bool on = each == name;
138 const std::string_view eachClass = ModifierClass(className, block, each);
139 // Every off write is a networked entry, so only one that changes something is sent.
140 if (on || _written.IsClassOn(_owner, elementId, eachClass))
141 {
142 WriteClass(_owner, elementId, eachClass, on);
143 }
144 }
145}
146
147void ScreenEntity::WriteCursor(int slot, bool shown)
148{
149 if (!Accepts(slot))
150 {
151 return;
152 }
153 _cursor = shown;
154 if (!_written.CursorChanged(_owner, shown))
155 {
156 return;
157 }
158
159 CheckWrite(SendCursor(shown), "the cursor");
160}
161
162Status ScreenEntity::Spawn()
163{
164 _playersChangedSinceSpawn = false;
165 Remove();
166
167 if (Status available = _entities.Available(); !available)
168 {
169 return available;
170 }
171
172 KeyValues values;
173 values.Set("layout", _layout.Resource());
174
175 const Entity spawned = _entities.Spawn("custom_hud_layout", values);
176 if (!spawned)
177 {
178 return std::unexpected(Error::Engine("the engine refused to spawn custom_hud_layout"));
179 }
180
181 _entity = spawned.Ref();
182 _visibility.ShowOnlyTo(_entity, _owner);
183 return {};
184}
185
186bool ScreenEntity::SpawnOrWarn()
187{
188 const Status spawned = Spawn();
189 if (!spawned)
190 {
191 Log::Warn("Screen '{}': spawn failed ({}).", _layout.Name(), spawned.error().Detail);
192 }
193 return spawned.has_value();
194}
195
196bool ScreenEntity::Covers() const
197{
198 return PlayerStateCount() > _owner;
199}
200
201void ScreenEntity::CheckWrite(const Status& status, std::string_view what)
202{
203 if (status)
204 {
205 return;
206 }
207
208 _written.Invalidate(_owner);
209 if (_written.IsFirstFailure(_owner))
210 {
211 Log::Warn("Screen '{}': writing {} for slot {} failed ({}).", _layout.Name(), what, _owner,
212 status.error().Detail);
213 }
214}
215
216} // namespace VoltMod
runtime.Entities: finds and creates entities. Everything it returns is valid for this frame only; see...
Entity Spawn(std::string_view className, KeyValues &values)
EntityRef Ref() const
Definition Entity.cpp:70
A layout resource the client will accept, checked before anything is spawned.
const std::string & Resource() const noexcept
Per-client network access: latency, userids, usercmd status and the client's replicated userinfo cvar...
void WriteText(int slot, std::string_view variable, std::string_view value)
void WriteClass(int slot, std::string_view elementId, std::string_view className, bool on)
bool EnsureSpawned(int slot)
void WriteModifier(int slot, std::string_view elementId, std::string_view block, std::span< const std::string_view > names, std::string_view name)
ScreenEntity(EntitySystem &entities, SlotEvents &slots, Visibility &visibility, const NetChannels &clients, std::vector< const ScreenEntity * > &live, LayoutPath layout, int owner)
void WriteCursor(int slot, bool shown)
"The occupant of this slot changed" - raised by PlayerManager, consumed below it.
Event< int > FullyConnected
Event< int > Changed
Which clients receive which entities, filtered after CheckTransmit.
void Warn(std::format_string< Args... > fmt, Args &&... args)
Definition Log.hpp:88
std::expected< void, Error > Status
Definition Result.hpp:67
static std::string_view ModifierClass(std::string &out, std::string_view block, std::string_view name)
static Error Engine(std::string detail)
Definition Result.hpp:58