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
9WriteCache::Sent* WriteCache::For(int slot)
10{
11 if (slot == EveryoneSlot)
12 {
13 return &_everyone;
14 }
15 if (!IsValidSlot(slot))
16 {
17 return nullptr;
18 }
19
20 std::optional<Sent>& sent = _slots[slot];
21 if (!sent)
22 {
23 sent.emplace();
24 }
25 return &*sent;
26}
27
28bool WriteCache::Changed(int slot, WriteKind kind, std::string_view elementId, std::string_view name,
29 std::string_view value)
30{
31 Sent* sent = For(slot);
32 if (!sent)
33 {
34 return false;
35 }
36
37 const std::string& key = KeyFor(kind, elementId, name);
38 if (auto it = sent->Values.find(key); it != sent->Values.end())
39 {
40 if (it->second == value)
41 {
42 return false;
43 }
44
45 it->second = value;
46 return true;
47 }
48
49 sent->Values.emplace(key, value);
50 return true;
51}
52
54{
55 Sent* sent = For(slot);
56 if (!sent || sent->Cursor == shown)
57 {
58 return false;
59 }
60
61 sent->Cursor = shown;
62 return true;
63}
64
66{
67 Sent* sent = For(slot);
68 if (!sent || sent->Failed)
69 {
70 return false;
71 }
72
73 sent->Failed = true;
74 return true;
75}
76
78{
79 Sent* sent = For(slot);
80 if (!sent)
81 {
82 return;
83 }
84
85 sent->Values.clear();
86 sent->Cursor.reset();
87}
88
90{
91 _slots.Reset(slot);
92}
93
95{
96 _slots.ResetAll();
97 _everyone = {};
98}
99
100const std::string& WriteCache::KeyFor(WriteKind kind, std::string_view elementId, std::string_view name)
101{
102 _key.assign(1, static_cast<char>(kind));
103 _key += KeySeparator;
104 _key += elementId;
105 _key += KeySeparator;
106 _key += name;
107 return _key;
108}
109
110} // namespace VoltMod
void Reset(int slot)
Definition PerSlot.hpp:48
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)
bool CursorChanged(int slot, bool shown)
void RemoveSlot(int slot)
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56
constexpr bool IsValidSlot(int slot)
Definition Slot.hpp:13
constexpr int EveryoneSlot
Definition Slot.hpp:10
static constexpr char KeySeparator
Definition WriteCache.cpp:7