-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexportfuncs.cpp
More file actions
197 lines (170 loc) · 6.68 KB
/
exportfuncs.cpp
File metadata and controls
197 lines (170 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// exportfuncs.cpp
#define _WIN32_WINNT 0x0600
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include "plugins.h"
#include "HLSDK/common/parsemsg.h"
#include <string>
#include <cstring>
#include <new>
#include <chrono>
#include <thread>
#include <vector>
#pragma comment(lib, "Ws2_32.lib")
std::string CleanMessage(const char* input) {
std::string out;
if (!input) return out;
while (*input) {
unsigned char c = (unsigned char)*input;
// Allow printable characters, color codes (1-4), \r, \n, \t. Skip bell (7).
if (c >= 0x20 || (c >= 0x01 && c <= 0x04) || c == '\r' || c == '\n' || c == '\t') {
out += *input;
}
input++;
}
return out;
}
pfnUserMsgHook g_pfnSayText = NULL;
fn_parsefunc g_pfnCL_ParsePrint = NULL;
fn_parsefunc g_pfnCL_ParseStuffText = NULL;
void QueueTask(char tag, const std::string& msg) {
if (msg.empty()) return;
// Cvars may not be registered yet (e.g., called before HUD_Init completes)
if (!IsCvarValid(cf_server_ip) || !IsCvarValid(cf_server_port)) return;
std::string fullMsg;
fullMsg += tag;
fullMsg += msg;
SendTask task;
strncpy_s(task.message, fullMsg.c_str(), sizeof(task.message) - 1);
strncpy_s(task.server_ip, cf_server_ip->string, sizeof(task.server_ip) - 1);
task.port = atoi(cf_server_port->string);
g_sendQueue.push(task);
}
void __MsgFunc_Print(void) {
char* psz = READ_STRING();
if (psz && psz[0]) {
std::string cleanMsg = CleanMessage(psz);
if (!cleanMsg.empty()) {
if (IsCvarValid(cf_debug) && atoi(cf_debug->string) > 0) {
gEngfuncs.Con_Printf("[ChatForwarder][NET] %s", cleanMsg.c_str());
}
QueueTask(MSG_TYPE_NET, cleanMsg);
}
}
if (g_pfnCL_ParsePrint) g_pfnCL_ParsePrint();
}
void __MsgFunc_StuffText(void) {
char* psz = READ_STRING();
if (psz && psz[0]) {
std::string cleanMsg = CleanMessage(psz);
if (!cleanMsg.empty()) {
if (IsCvarValid(cf_debug) && atoi(cf_debug->string) > 0) {
gEngfuncs.Con_Printf("[ChatForwarder][STUFF] %s", cleanMsg.c_str());
}
QueueTask(MSG_TYPE_STUFF, cleanMsg);
}
}
if (g_pfnCL_ParseStuffText) g_pfnCL_ParseStuffText();
}
void HUD_Init(void) {
ChatForwarder_Init();
if (g_pfnHUD_Init) {
g_pfnHUD_Init();
}
// HUD_Init is called on every map load / reconnect.
// HookUserMsg and HookCLParseFuncByName must only be called once:
// calling them again would cause the returned "previous" pointer to point
// back to our own hook, creating infinite recursion.
static bool bHooksInstalled = false;
if (!bHooksInstalled) {
g_pfnSayText = g_pMetaHookAPI->HookUserMsg("SayText", __MsgFunc_SayText);
g_pfnTextMsg = g_pMetaHookAPI->HookUserMsg("TextMsg", __MsgFunc_TextMsg);
g_pfnCL_ParsePrint = g_pMetaHookAPI->HookCLParseFuncByName("print", __MsgFunc_Print);
g_pfnCL_ParseStuffText = g_pMetaHookAPI->HookCLParseFuncByName("stufftext", __MsgFunc_StuffText);
bHooksInstalled = true;
}
}
void HUD_Frame(double time) {
auto now = std::chrono::steady_clock::now();
double delay = IsCvarValid(cf_command_delay) ? (double)cf_command_delay->value : 0.0;
double elapsed = std::chrono::duration<double>(now - g_lastCommandTime).count();
if (elapsed >= delay) {
std::string message;
if (g_messageQueue.pop(message, 0)) {
if (!message.empty()) {
while (!message.empty() && (unsigned char)message.back() <= 32) {
message.pop_back();
}
if (!message.empty()) {
if (IsCvarValid(cf_debug) && atoi(cf_debug->string) > 0) {
gEngfuncs.Con_Printf("[ChatForwarder] Executing: %s\n", message.c_str());
}
message += '\n';
gEngfuncs.pfnClientCmd(message.c_str());
g_lastCommandTime = now;
}
}
}
}
if (g_pfnHUD_Frame) g_pfnHUD_Frame(time);
}
int __MsgFunc_SayText(const char* pszName, int iSize, void* pbuf) {
// Null-safe passthrough: HookUserMsg may return NULL if message wasn't registered
auto callOriginal = [&]() -> int {
return g_pfnSayText ? g_pfnSayText(pszName, iSize, pbuf) : 1;
};
if (!IsCvarValid(cf_enabled) || atoi(cf_enabled->string) == 0) return callOriginal();
char temp_buf[1024];
if (iSize >= sizeof(temp_buf)) return callOriginal();
memcpy(temp_buf, pbuf, iSize);
BEGIN_READ(temp_buf, iSize);
READ_BYTE(); // client index
char* msg_base = READ_STRING();
std::string fullMsg = msg_base ? CleanMessage(msg_base) : "";
for (int i = 0; i < 4; i++) {
char* arg = READ_STRING();
if (arg && arg[0]) {
std::string cleanArg = CleanMessage(arg);
size_t pos = fullMsg.find("%s");
if (pos != std::string::npos) fullMsg.replace(pos, 2, cleanArg);
else { if (!fullMsg.empty()) fullMsg += " "; fullMsg += cleanArg; }
}
}
if (!fullMsg.empty()) {
if (IsCvarValid(cf_debug) && atoi(cf_debug->string) > 0) {
gEngfuncs.Con_Printf("[ChatForwarder][CHAT] %s\n", fullMsg.c_str());
}
QueueTask(MSG_TYPE_CHAT, fullMsg);
}
return callOriginal();
}
int __MsgFunc_TextMsg(const char* pszName, int iSize, void* pbuf) {
// Null-safe passthrough: HookUserMsg may return NULL if message wasn't registered
auto callOriginal = [&]() -> int {
return g_pfnTextMsg ? g_pfnTextMsg(pszName, iSize, pbuf) : 1;
};
char temp_buf[1024];
if (iSize >= sizeof(temp_buf)) return callOriginal();
memcpy(temp_buf, pbuf, iSize);
BEGIN_READ(temp_buf, iSize);
int msg_dest = READ_BYTE();
char* msg_text = READ_STRING();
if (msg_dest >= 1 && msg_dest <= 4 && IsCvarValid(cf_enabled) && atoi(cf_enabled->string) == 1) {
std::string fullMsg = msg_text ? CleanMessage(msg_text) : "";
for (int i = 0; i < 4; i++) {
char* arg = READ_STRING();
if (arg && arg[0]) {
std::string cleanArg = CleanMessage(arg);
size_t pos = fullMsg.find("%s");
if (pos != std::string::npos) fullMsg.replace(pos, 2, cleanArg);
else { if (!fullMsg.empty()) fullMsg += " "; fullMsg += cleanArg; }
}
}
if (!fullMsg.empty()) {
if (IsCvarValid(cf_debug) && atoi(cf_debug->string) > 0) {
gEngfuncs.Con_Printf("[ChatForwarder][GAME] %s\n", fullMsg.c_str());
}
QueueTask(MSG_TYPE_GAME, fullMsg);
}
}
return callOriginal();
}