VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
Messages.cpp
Go to the documentation of this file.
3
4#include <igameevents.h>
5
14#include <engine2/igameeventsystem.h>
15#include <networksystem/inetworkmessages.h>
16#include <recipientfilter.h>
17#include <shareddefs.h>
18#include <usermessages.pb.h>
19
20namespace VoltMod
21{
22
23// Prepend a space to preserve a leading color, or Default to prevent color carryover.
24static std::string EnsureColorPrefix(std::string_view message)
25{
26 std::string_view prefix =
27 (!message.empty() && static_cast<unsigned char>(message.front()) <= 0x10) // 0x01-0x10: color escape bytes
28 ? " "
30
31 std::string out;
32 out.reserve(prefix.size() + message.size());
33 out.append(prefix);
34 out.append(message);
35 return out;
36}
37
38static std::string Render(std::string_view message, MessageKind kind)
39{
40 return kind == MessageKind::Chat ? EnsureColorPrefix(message) : std::string(message);
41}
42
44{
45 switch (kind)
46 {
48 return HUD_PRINTCENTER;
50 return HUD_PRINTALERT;
51 default:
52 return HUD_PRINTTALK;
53 }
54}
55
56Messages::Messages(Interfaces& interfaces, GameEvents& events, Translations& translations)
57 : _interfaces(interfaces), _events(events), _translations(translations)
58{}
59
61{
62 if (!_interfaces.GameEventSystem)
63 {
64 return std::unexpected(Error::NotReady("IGameEventSystem not available"));
65 }
66
67 if (!_interfaces.NetworkMessages)
68 {
69 return std::unexpected(Error::NotReady("INetworkMessages not available"));
70 }
71 return {};
72}
73
74void Messages::SendCenterHtml(int slot, const std::string& html)
75{
76 auto* gameEventManager = _interfaces.GameEventManager;
77 if (!gameEventManager || !IsValidSlot(slot))
78 {
79 return;
80 }
81
82 IGameEvent* pEvent = _events.CreateEvent(ShowSurvivalRespawnStatus::EventName);
83 if (!pEvent)
84 {
85 return;
86 }
87
88 pEvent->SetString("loc_token", html.c_str());
89 pEvent->SetInt("userid", slot);
90 pEvent->SetInt("duration", 5);
91
92 // Use the client's listener when available; otherwise the event broadcasts to every client.
93 if (IGameEventListener2* listener = _events.GetClientLegacyListener(slot))
94 {
95 listener->FireGameEvent(pEvent);
96 gameEventManager->FreeEvent(pEvent);
97 return;
98 }
99
100 gameEventManager->FireEvent(pEvent);
101}
102
103void Messages::Send(int slot, std::string_view message, MessageKind kind)
104{
105 if (kind == MessageKind::CenterHtml)
106 {
107 SendCenterHtml(slot, std::string(message));
108 return;
109 }
110
111 SendTextMsg(slot, HudDestination(kind), Render(message, kind));
112}
113
114void Messages::Broadcast(std::string_view message, MessageKind kind)
115{
116 auto rendered = Render(message, kind);
117
118 if (kind == MessageKind::CenterHtml)
119 {
120 // Each panel write targets one client's listener; null means the slot is empty.
121 for (int slot = 0; slot < MaxPlayers; ++slot)
122 {
123 if (_events.GetClientLegacyListener(slot))
124 {
125 SendCenterHtml(slot, rendered);
126 }
127 }
128 return;
129 }
130
131 // One event lets the engine remove empty slots from recipient bits without a roster scan.
132 CRecipientFilter filter;
133 filter.AddAllPlayers();
134 filter.MakeReliable();
135
136 PostTextMsg(filter, HudDestination(kind), rendered);
137}
138
139void Messages::SendKey(int slot, std::string_view key, const Tokens& tokens, MessageKind kind)
140{
141 Send(slot, _translations.Get(key, slot, tokens), kind);
142}
143
144void Messages::BroadcastKey(std::string_view key, const Tokens& tokens, MessageKind kind)
145{
146 for (int slot = 0; slot < MaxPlayers; ++slot)
147 {
148 if (_events.GetClientLegacyListener(slot))
149 {
150 SendKey(slot, key, tokens, kind);
151 }
152 }
153}
154
155void Messages::SendTextMsg(int slot, int destination, const std::string& message)
156{
157 if (!IsValidSlot(slot))
158 {
159 return;
160 }
161
162 CReliableSingleUserRecipientFilter filter(slot);
163 PostTextMsg(filter, destination, message);
164}
165
166void Messages::PostTextMsg(IRecipientFilter& filter, int destination, const std::string& message)
167{
168 // Server chat uses TextMsg with HUD_PRINTTALK; SayText2 requires a connected source player.
169 PostUserMessage(_interfaces, _textMsgType, "TextMsg", filter, [&](CNetMessage* raw) {
170 auto* textMsg = raw->ToPB<CUserMessageTextMsg>();
171 if (!textMsg)
172 {
173 return false;
174 }
175 textMsg->set_dest(destination);
176 textMsg->add_param(message.c_str());
177 return true;
178 });
179}
180
181void Messages::Shake(int slot, float durationSec, float frequency, float amplitude)
182{
183 CReliableSingleUserRecipientFilter filter(slot);
184 PostUserMessage(_interfaces, _shakeType, "Shake", filter, [&](CNetMessage* raw) {
185 auto* shake = raw->ToPB<CUserMessageShake>();
186 if (!shake)
187 {
188 return false;
189 }
190 shake->set_duration(durationSec);
191 shake->set_frequency(frequency);
192 shake->set_amplitude(amplitude);
193 shake->set_command(0); // SHAKE_START
194 return true;
195 });
196}
197
199{
200 SendCenterHtml(slot, " ");
201}
202
203} // namespace VoltMod
Creates, fires and listens to game events.
void BroadcastKey(std::string_view key, const Tokens &tokens={}, MessageKind kind=MessageKind::Chat)
Definition Messages.cpp:144
Status Available() const
Definition Messages.cpp:60
void ClearCenterHtml(int slot)
Definition Messages.cpp:198
void SendCenterHtml(int slot, const std::string &html)
Definition Messages.cpp:74
void Send(int slot, std::string_view message, MessageKind kind=MessageKind::Chat)
Definition Messages.cpp:103
void Broadcast(std::string_view message, MessageKind kind=MessageKind::Chat)
Definition Messages.cpp:114
Messages(Interfaces &interfaces, GameEvents &events, Translations &translations)
Definition Messages.cpp:56
void SendKey(int slot, std::string_view key, const Tokens &tokens={}, MessageKind kind=MessageKind::Chat)
Definition Messages.cpp:139
void Shake(int slot, float durationSec, float frequency, float amplitude)
Definition Messages.cpp:181
Localization system. Loads one JSON file per language; nested objects flatten into dotted keys (categ...
constexpr std::string_view Default
@ Center
Plain center-screen print.
Definition Messages.hpp:18
@ Chat
Chat box line (color escapes honored).
Definition Messages.hpp:17
@ CenterHtml
Center HTML panel (same channel the menu system renders into).
Definition Messages.hpp:19
@ Alert
Top-center alert bar.
Definition Messages.hpp:20
bool PostUserMessage(Interfaces &interfaces, NetMessageInfo_t *&cached, std::string_view partialName, IRecipientFilter &filter, const std::function< bool(CNetMessage *)> &fill)
static std::string EnsureColorPrefix(std::string_view message)
Definition Messages.cpp:24
std::expected< void, Error > Status
Definition Result.hpp:67
constexpr bool IsValidSlot(int slot)
Definition Slot.hpp:13
std::map< std::string, std::string > Tokens
Definition Strings.hpp:14
static std::string Render(std::string_view message, MessageKind kind)
Definition Messages.cpp:38
static int HudDestination(MessageKind kind)
Definition Messages.cpp:43
constexpr int MaxPlayers
Definition Slot.hpp:7
static Error NotReady(std::string detail)
Definition Result.hpp:50
Centralized holder for all SDK interface pointers.
static constexpr std::string_view EventName