VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
TextMessages.cpp
Go to the documentation of this file.
3#include <engine/igameeventsystem.h>
4#include <networksystem/inetworkserializer.h>
5#include <networksystem/netmessage.h>
6#include <usermessages.pb.h>
7
8namespace VoltMod
9{
10
13
15 const CNetMessage* data)
16{
17 if (!kind || !data)
18 {
19 return {};
20 }
21 if (kind->GetNetMessageInfo()->m_MessageId != UM_TextMsg)
22 {
23 return {};
24 }
25 const auto* text = const_cast<CNetMessage*>(data)->ToPB<CUserMessageTextMsg>();
26 if (!text || text->param_size() == 0)
27 {
28 return {};
29 }
30 TextMessage message{.Text = text->param(0)};
31 before.Raise(message);
33}
34
36 : _hook("TextMessages", [this] { return Install(); }), Before(_hook.ForEvent()), _interfaces(interfaces)
37{}
38
39Result<Subscription> TextMessages::Install()
40{
41 if (auto available = Available(); !available)
42 {
43 return std::unexpected(available.error());
44 }
45 const auto onPost = [this](IGameEventSystem&, CSplitScreenSlot, bool, int, const uint64*,
48 // The filter overload posts through this one, so it sees every message.
49 return HookInterface(static_cast<PostEvent>(&IGameEventSystem::PostEventAbstract), _interfaces.GameEventSystem,
50 onPost);
51}
52
54{
55 if (!_interfaces.GameEventSystem)
56 {
57 return std::unexpected(Error::NotReady("IGameEventSystem not available"));
58 }
59 return {};
60}
61
62} // namespace VoltMod
A multicast signal with a fixed handler signature: the one way to subscribe in VoltMod.
Definition Event.hpp:54
A hook before-handler's verdict.
static HookResult Block(Ret value)
Status Available() const
Event< TextMessage & > Before
TextMessages(Interfaces &interfaces)
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56
std::expected< void, Error > Status
Definition Result.hpp:67
void(IGameEventSystem::*)(CSplitScreenSlot, bool, int, const uint64 *, INetworkMessageInternal *, const CNetMessage *, unsigned long, NetChannelBufType_t) PostEvent
Subscription HookInterface(Ret(Iface::*method)(Args...), Iface *instance, Before &&before, After &&after=nullptr)
Hook one virtual method of an engine interface for a subscription's lifetime.
Definition Hook.hpp:142
static HookResult< void > FilterTextMessage(Event< TextMessage & > &before, INetworkMessageInternal *kind, const CNetMessage *data)
static Error NotReady(std::string detail)
Definition Result.hpp:50
Centralized holder for all HL2SDK interface pointers.
IGameEventSystem * GameEventSystem
std::string_view Text