Skip to content

Commit 356152a

Browse files
committed
fix: revert chat command hooking back to detour of Host_Say
1 parent 31e12ba commit 356152a

File tree

4 files changed

+38
-37
lines changed

4 files changed

+38
-37
lines changed

configs/addons/counterstrikesharp/gamedata/gamedata.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,5 +277,12 @@
277277
"windows": 12,
278278
"linux": 13
279279
}
280+
},
281+
"Host_Say": {
282+
"signatures": {
283+
"library": "server",
284+
"windows": "44 89 4C 24 20 44 88 44 24 18",
285+
"linux": "55 48 89 E5 41 57 49 89 F7 41 56 41 55 41 54 4D 89 C4"
286+
}
280287
}
281288
}

src/core/managers/chat_manager.cpp

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,24 @@ ChatManager::ChatManager() {}
3535

3636
ChatManager::~ChatManager() {}
3737

38-
void ChatManager::OnAllInitialized() {}
38+
void ChatManager::OnAllInitialized()
39+
{
40+
m_pHostSay = reinterpret_cast<HostSay>(modules::server->FindSignature(globals::gameConfig->GetSignature("Host_Say")));
41+
42+
if (m_pHostSay == nullptr)
43+
{
44+
CSSHARP_CORE_ERROR("Failed to find signature for \'Host_Say\'");
45+
return;
46+
}
47+
48+
auto m_hook = funchook_create();
49+
funchook_prepare(m_hook, (void**)&m_pHostSay, (void*)&DetourHostSay);
50+
funchook_install(m_hook, 0);
51+
}
3952

4053
void ChatManager::OnShutdown() {}
4154

42-
bool ChatManager::OnSayCommand(CEntityInstance* pController, const CCommand& args, bool teamonly)
55+
void DetourHostSay(CEntityInstance* pController, CCommand& args, bool teamonly, int unk1, const char* unk2)
4356
{
4457
if (pController)
4558
{
@@ -58,35 +71,32 @@ bool ChatManager::OnSayCommand(CEntityInstance* pController, const CCommand& arg
5871
bool bSilent = globals::coreConfig->IsSilentChatTrigger(args[1], prefix);
5972
bool bCommand = globals::coreConfig->IsPublicChatTrigger(args[1], prefix) || bSilent;
6073

74+
if (!bSilent)
75+
{
76+
m_pHostSay(pController, args, teamonly, unk1, unk2);
77+
}
78+
6179
if (bCommand)
6280
{
63-
auto message = std::string(args.ArgS());
81+
char* pszMessage = (char*)(args.ArgS() + prefix.length() + 1);
6482

65-
// trim quotes off message if they appear, then trim the prefix
66-
// "!foobar" -> foobar
67-
// !foobar -> foobar
68-
if (message.size() >= 2 && message.front() == '"' && message.back() == '"')
69-
{
70-
message = message.substr(1, message.size() - 2);
71-
}
72-
message = message.substr(prefix.size());
83+
// Trailing slashes are only removed if Host_Say has been called.
84+
if (bSilent) pszMessage[V_strlen(pszMessage) - 1] = 0;
7385

74-
CCommand newArgs;
75-
newArgs.Tokenize(message.c_str());
86+
CCommand args;
87+
args.Tokenize(pszMessage);
7688

77-
auto prefixedPhrase = std::string("css_") + newArgs.Arg(0);
89+
auto prefixedPhrase = std::string("css_") + args.Arg(0);
7890
auto bValidWithPrefix = globals::conCommandManager.IsValidValveCommand(prefixedPhrase.c_str());
7991

8092
if (bValidWithPrefix)
8193
{
8294
// Re-tokenize with a `css_` prefix if we have found that its a valid command.
83-
newArgs.Tokenize(("css_" + std::string(message)).c_str());
95+
args.Tokenize(("css_" + std::string(pszMessage)).c_str());
8496
}
8597

86-
globals::chatManager.OnSayCommandPost(pController, newArgs);
98+
globals::chatManager.OnSayCommandPost(pController, args);
8799
}
88-
89-
return bSilent;
90100
}
91101

92102
bool ChatManager::OnSayCommandPre(CEntityInstance* pController, CCommand& command) { return false; }

src/core/managers/chat_manager.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ class ChatManager : public GlobalClass
5454

5555
bool OnSayCommandPre(CEntityInstance* pController, CCommand& args);
5656
void OnSayCommandPost(CEntityInstance* pController, CCommand& args);
57-
static bool OnSayCommand(CEntityInstance* pController, const CCommand& args, bool teamonly);
5857

5958
private:
6059
void InternalDispatch(CEntityInstance* pPlayerController, const char* szTriggerPhrase, CCommand& pFullCommand);
@@ -63,4 +62,7 @@ class ChatManager : public GlobalClass
6362
std::map<std::string, ChatCommandInfo*> m_cmd_lookup;
6463
};
6564

65+
static void DetourHostSay(CEntityInstance* pController, CCommand& args, bool teamonly, int unk1, const char* unk2);
66+
static HostSay m_pHostSay = nullptr;
67+
6668
} // namespace counterstrikesharp

src/core/managers/con_command_manager.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -467,24 +467,6 @@ void ConCommandManager::Hook_DispatchConCommand(ConCommandRef cmd, const CComman
467467

468468
CSSHARP_CORE_TRACE("[ConCommandManager::Hook_DispatchConCommand]: {}", name);
469469

470-
auto slot = ctx.GetPlayerSlot();
471-
bool isSay = V_strcmp(name, "say");
472-
bool isTeamSay = V_strcmp(name, "say_team");
473-
474-
if (isSay || isTeamSay)
475-
{
476-
CEntityInstance* entityInstance = nullptr;
477-
if (globals::entitySystem && (slot != -1))
478-
{
479-
entityInstance = globals::entitySystem->GetEntityInstance(CEntityIndex(slot.Get() + 1));
480-
}
481-
482-
if (globals::chatManager.OnSayCommand(entityInstance, args, isTeamSay))
483-
{
484-
RETURN_META(MRES_SUPERCEDE);
485-
}
486-
}
487-
488470
auto result = ExecuteCommandCallbacks(name, ctx, args, HookMode::Pre, CommandCallingContext::Console);
489471
if (result >= HookResult::Handled)
490472
{

0 commit comments

Comments
 (0)