VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
WriteCache.cpp
Go to the documentation of this file.
1#include "Ui/WriteCache.hpp"
2
3namespace VoltMod
4{
5
6/** Separates the parts of a key; not legal in an element id or a variable name. */
7static constexpr char KeySeparator = '\x1f';
8
9/** The value a class write is cached under. */
10static std::string_view ClassValue(bool on)
11{
12 return on ? "1" : "0";
13}
14
15WriteCache::Sent* WriteCache::For(int slot)
16{
17 if (slot == EveryoneSlot)
18 {
19 return &_everyone;
20 }
21 if (!IsValidSlot(slot))
22 {
23 return nullptr;
24 }
25
26 std::optional<Sent>& sent = _slots[slot];
27 if (!sent)
28 {
29 sent.emplace();
30 }
31 return &*sent;
32}
33
34bool WriteCache::Changed(int slot, WriteKind kind, std::string_view elementId, std::string_view name,
35 std::string_view value)
36{
37 Sent* sent = For(slot);
38 if (!sent)
39 {
40 return false;
41 }
42
43 const std::string& key = BuildKey(kind, elementId, name);
44 if (auto it = sent->Values.find(key); it != sent->Values.end())
45 {
46 if (it->second == value)
47 {
48 return false;
49 }
50
51 it->second = value;
52 return true;
53 }
54
55 sent->Values.emplace(key, value);
56 return true;
57}
58
59bool WriteCache::ClassChanged(int slot, std::string_view elementId, std::string_view className, bool on)
60{
61 return Changed(slot, WriteKind::Class, elementId, className, ClassValue(on));
62}
63
64bool WriteCache::IsClassOn(int slot, std::string_view elementId, std::string_view className)
65{
66 Sent* sent = For(slot);
67 if (!sent)
68 {
69 return false;
70 }
71
72 const auto it = sent->Values.find(BuildKey(WriteKind::Class, elementId, className));
73 return it != sent->Values.end() && it->second == ClassValue(true);
74}
75
76bool WriteCache::CursorChanged(int slot, bool shown)
77{
78 Sent* sent = For(slot);
79 if (!sent || sent->Cursor == shown)
80 {
81 return false;
82 }
83
84 sent->Cursor = shown;
85 return true;
86}
87
89{
90 Sent* sent = For(slot);
91 if (!sent || sent->Failed)
92 {
93 return false;
94 }
95
96 sent->Failed = true;
97 return true;
98}
99
101{
102 Sent* sent = For(slot);
103 if (!sent)
104 {
105 return;
106 }
107
108 sent->Values.clear();
109 sent->Cursor.reset();
110}
111
113{
114 _slots.Reset(slot);
115}
116
118{
119 if (Sent* sent = For(slot))
120 {
121 sent->Cursor.reset();
122 }
123}
124
126{
127 _slots.ResetAll();
128 _everyone = {};
129}
130
131const std::string& WriteCache::BuildKey(WriteKind kind, std::string_view elementId, std::string_view name)
132{
133 _key.assign(1, static_cast<char>(kind));
134 _key += KeySeparator;
135 _key += elementId;
136 _key += KeySeparator;
137 _key += name;
138 return _key;
139}
140
141} // namespace VoltMod
void Invalidate(int slot)
bool IsFirstFailure(int slot)
bool Changed(int slot, WriteKind kind, std::string_view elementId, std::string_view name, std::string_view value)
void ResetCursor(int slot)
bool CursorChanged(int slot, bool shown)
void RemoveSlot(int slot)
bool IsClassOn(int slot, std::string_view elementId, std::string_view className)
bool ClassChanged(int slot, std::string_view elementId, std::string_view className, bool on)
static std::string_view ClassValue(bool on)
constexpr bool IsValidSlot(int slot)
Definition Slot.hpp:13
constexpr int EveryoneSlot
Definition Slot.hpp:10
static constexpr char KeySeparator
Definition WriteCache.cpp:7