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{
16 Text,
17 Class
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 /** True when @p shown differs from the cursor state @p slot was last sent, remembering it. */
33 bool CursorChanged(int slot, bool shown);
34
35 /** True the first time it is asked for @p slot, so a failure that repeats every frame logs once. */
36 bool IsFirstFailure(int slot);
37
38 /** After a failed write, so the next one goes through. The failure still logs once. */
39 void Invalidate(int slot);
40
41 /** Another player took @p slot. */
42 void RemoveSlot(int slot);
43
44 /** A new entity has been told nothing. */
45 void Clear();
46
47private:
48 struct Sent
49 {
50 std::unordered_map<std::string, std::string> Values;
51 std::optional<bool> Cursor;
52 bool Failed = false;
53 };
54
55 /** @p slot's record, made on first use; the shared one for @ref EveryoneSlot; null for neither. */
56 Sent* For(int slot);
57
58 /** (kind, element, name) joined into one key, in a buffer reused across calls. */
59 const std::string& KeyFor(WriteKind kind, std::string_view elementId, std::string_view name);
60
61 std::string _key;
62 /** Made on first use: a player screen only ever writes for its owner. */
64 Sent _everyone;
65};
66
67} // 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)
bool CursorChanged(int slot, bool shown)
void RemoveSlot(int slot)
@ Text
Not selectable; a caption or a summary line.
@ Failed
Anything else; the default so a value-initialized Error is never a false success.
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56