CS2-Kit
C++23 library for CS2 Metamod:Source plugin development
Loading...
Searching...
No Matches
Chat.cpp
Go to the documentation of this file.
1#include <CS2Kit/Players/PlayerManager.hpp>
2#include <CS2Kit/Sdk/UserMessage.hpp>
3#include <CS2Kit/Utils/Chat.hpp>
4#include <CS2Kit/Utils/ChatColors.hpp>
5#include <string>
6
8{
9
10namespace
11{
12
13// CS2 strips leading color escapes until a non-color byte. Prepend a space to
14// preserve a leading color, or Default to avoid color carryover from the prior line.
15std::string EnsureColorPrefix(std::string_view message)
16{
17 std::string_view prefix =
18 (!message.empty() && static_cast<unsigned char>(message.front()) <= 0x10) ? " " : ChatColors::Default;
19
20 std::string out;
21 out.reserve(prefix.size() + message.size());
22 out.append(prefix);
23 out.append(message);
24 return out;
25}
26} // namespace
27
28void Print(int slot, std::string_view message)
29{
30 Sdk::MessageSystem::Instance().SendChatMessage(slot, EnsureColorPrefix(message));
31}
32
33void PrintAll(std::string_view message)
34{
35 auto rendered = EnsureColorPrefix(message);
36 auto& mgr = Players::PlayerManager::Instance();
37 for (auto* p : mgr.GetAllPlayers())
38 {
39 if (!p)
40 continue;
41 Sdk::MessageSystem::Instance().SendChatMessage(p->GetSlot(), rendered);
42 }
43}
44
45void PrintFiltered(std::string_view message, const std::function<bool(const Players::Player*)>& filter)
46{
47 if (!filter)
48 return;
49
50 auto rendered = EnsureColorPrefix(message);
51 auto& mgr = Players::PlayerManager::Instance();
52 for (auto* p : mgr.GetAllPlayers())
53 {
54 if (!p || !filter(p))
55 continue;
56 Sdk::MessageSystem::Instance().SendChatMessage(p->GetSlot(), rendered);
57 }
58}
59
60} // namespace CS2Kit::Utils::Chat
void Print(int slot, std::string_view message)
Definition Chat.cpp:28
void PrintAll(std::string_view message)
Definition Chat.cpp:33
void PrintFiltered(std::string_view message, const std::function< bool(const Players::Player *)> &filter)
Definition Chat.cpp:45