VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
ChatInput.hpp
Go to the documentation of this file.
1#pragma once
2
7#include <array>
8#include <functional>
9#include <optional>
10#include <string>
11#include <string_view>
12
13namespace VoltMod
14{
15
16/**
17 * @brief Per-player pending-prompt registry for menu free-text input.
18 *
19 * The framework calls @ref TryConsume for every `say`/`say_team` message before
20 * command parsing. An active capture routes the message to its callback and
21 * suppresses the chat broadcast.
22 *
23 * This service does not install a separate chat hook: `player_say` runs after the
24 * broadcast. The consume call must remain in `Hook_DispatchConCommand`.
25 */
27{
28public:
29 /** Uses @p scheduler for timeouts and @p slots to clear recycled slots. Both must outlive this object. */
31 ChatInput(const ChatInput&) = delete;
32 ChatInput& operator=(const ChatInput&) = delete;
33
34 /** The callback returns true to accept and clear input, or false to keep capturing. */
35 using Callback = std::function<bool(int slot, std::string_view text)>;
36
37 /**
38 * Begin capturing the next chat line from @p slot. Replaces and cancels any
39 * existing capture. A positive @p timeoutMs cancels the capture without input.
40 */
41 void BeginCapture(int slot, std::string prompt, Callback callback, int timeoutMs = 60000);
42
43 bool IsCapturing(int slot) const;
44
45 /**
46 * @internal Called by the framework for every chat line. Returns true when the message was
47 * consumed, so the chat broadcast is suppressed. A rejected value restores the capture unless
48 * the callback installed a replacement.
49 */
50 bool TryConsume(int slot, std::string_view text);
51
52 /** Cancel without firing the callback. */
53 void CancelCapture(int slot);
54
55 /** Returns a copy of the active prompt, or nullopt. The copy remains valid if the capture changes. */
56 std::optional<std::string> GetPrompt(int slot) const;
57
58private:
59 struct Pending
60 {
61 std::string Prompt;
63 /** Owned by the capture, so replacing or dropping it cancels the timeout. */
64 Subscription Timeout;
65 };
66
67 Scheduler& _scheduler;
68 std::array<std::optional<Pending>, MaxPlayers> _pending{};
69 /** Declared after _pending so it unsubscribes before _pending is destroyed. */
70 Subscription _slotListener;
71};
72
73} // namespace VoltMod
Per-player pending-prompt registry for menu free-text input.
Definition ChatInput.hpp:27
ChatInput & operator=(const ChatInput &)=delete
std::function< bool(int slot, std::string_view text)> Callback
Definition ChatInput.hpp:35
void CancelCapture(int slot)
Definition ChatInput.cpp:70
bool TryConsume(int slot, std::string_view text)
Definition ChatInput.cpp:41
void BeginCapture(int slot, std::string prompt, Callback callback, int timeoutMs=60000)
Definition ChatInput.cpp:14
ChatInput(const ChatInput &)=delete
std::optional< std::string > GetPrompt(int slot) const
Definition ChatInput.cpp:80
bool IsCapturing(int slot) const
Definition ChatInput.cpp:32
One-shot delays and repeating timers, run on the game thread.
Definition Scheduler.hpp:18
"The occupant of this slot changed" - raised by PlayerManager, consumed below it.
Owns one registration and releases it on destruction.
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56
constexpr int MaxPlayers
Definition Slot.hpp:7