VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
WriteCache.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <optional>
6#include <string>
7#include <string_view>
8#include <unordered_map>
9
10namespace VoltMod
11{
12
13/** Keeps a text and a class of the same name on the same element apart. */
14enum class WriteKind
15{
18};
19
20/**
21 * @brief What each player, or everyone, was last sent on one screen, so an unchanged write is skipped.
22 *
23 * A networked layout stays on screen without re-sending, so this turns a full redraw into writes
24 * for only what changed. SDK-free for its tests.
25 */
27{
28public:
29 /** True when @p value differs from what @p slot (or @ref EveryoneSlot) was last sent, remembering it. */
30 bool Changed(int slot, WriteKind kind, std::string_view elementId, std::string_view name, std::string_view value);
31
32 /** @ref Changed for @p className on @p elementId turned on or off. */
33 bool ClassChanged(int slot, std::string_view elementId, std::string_view className, bool on);
34
35 /** True when @p slot (or @ref EveryoneSlot) was last sent @p className on for @p elementId. */
36 bool IsClassOn(int slot, std::string_view elementId, std::string_view className);
37
38 /** True when @p shown differs from the cursor state @p slot was last sent, remembering it. */
39 bool CursorChanged(int slot, bool shown);
40
41 /** True the first time it is asked for @p slot, so a failure that repeats every frame logs once. */
42 bool IsFirstFailure(int slot);
43
44 /** After a failed write, so the next one goes through. The failure still logs once. */
45 void Invalidate(int slot);
46
47 /** Another player took @p slot. */
48 void RemoveSlot(int slot);
49
50 /** The game reset @p slot's cursor. */
51 void ResetCursor(int slot);
52
53 /** A new entity has been told nothing. */
54 void Clear();
55
56private:
57 struct Sent
58 {
59 std::unordered_map<std::string, std::string> Values;
60 std::optional<bool> Cursor;
61 bool Failed = false;
62 };
63
64 /** @p slot's record, made on first use; the shared one for @ref EveryoneSlot; null for neither. */
65 Sent* For(int slot);
66
67 /** (kind, element, name) joined into one key, in a buffer reused across calls. */
68 const std::string& BuildKey(WriteKind kind, std::string_view elementId, std::string_view name);
69
70 std::string _key;
71 /** Made on first use: a player screen only ever writes for its owner. */
73 Sent _everyone;
74};
75
76} // namespace VoltMod
Per-player-slot value store that never leaks state across occupants.
Definition PerSlot.hpp:25
What each player, or everyone, was last sent on one screen, so an unchanged write is skipped.
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 Text(const char *text)