simple-chatprocessor.sp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /**
  2. * [CSRD] Simple Chat Processor
  3. *
  4. * Simple Chat Processor almost-compatible library for TF2.
  5. * Attempts to fix the one-recipient SayText2 messages, and does not depend on server-side
  6. * localization nonsense (everyone sees "(TEAM)" and "(DEAD)" in their language).
  7. */
  8. #pragma semicolon 1
  9. #include <sourcemod>
  10. #include <sdkhooks>
  11. #include <sdktools_voice>
  12. #pragma newdecls required
  13. #define PLUGIN_VERSION "0.2.0"
  14. public Plugin myinfo = {
  15. name = "[CSRD] Simple Chat Processor",
  16. author = "nosoop (based off of Simple Plugins' implementation)",
  17. description = "Simple Chat Processor almost-compatible library for TF2-specific fixes.",
  18. version = PLUGIN_VERSION,
  19. url = "https://git.csrd.science/"
  20. }
  21. #define PACKED_TOKEN_DELIMITER ":"
  22. #define CHATFLAGS_INVALID 0b0000
  23. #define CHATFLAGS_ALL 0b0001
  24. #define CHATFLAGS_TEAM 0b0010
  25. #define CHATFLAGS_SPEC 0b0100
  26. #define CHATFLAGS_DEAD 0b1000
  27. public APLRes AskPluginLoad2(Handle hPluginSelf, bool late, char[] error, int maxlen) {
  28. MarkNativeAsOptional("GetUserMessageType");
  29. CreateNative("GetMessageFlags", Native_GetMessageFlags);
  30. RegPluginLibrary("scp");
  31. return APLRes_Success;
  32. }
  33. // Holds player-unique messages sent in the current frame.
  34. StringMap g_QueuedMessages[MAXPLAYERS+1];
  35. Handle g_fwdOnChatMessage, g_fwdOnChatMessagePost;
  36. int g_ChatFlags;
  37. public void OnPluginStart() {
  38. UserMsg umSayText2 = GetUserMessageId("SayText2");
  39. if (umSayText2 != INVALID_MESSAGE_ID) {
  40. HookUserMessage(umSayText2, OnSayText2, true);
  41. } else {
  42. SetFailState("Game does not use SayText2.");
  43. }
  44. g_fwdOnChatMessage = CreateGlobalForward("OnChatMessage", ET_Hook, Param_CellByRef,
  45. Param_Cell, Param_String, Param_String);
  46. g_fwdOnChatMessagePost = CreateGlobalForward("OnChatMessage_Post", ET_Ignore, Param_Cell,
  47. Param_Cell, Param_String, Param_String);
  48. for (int i = 1; i < MaxClients; i++) {
  49. if (IsClientInGame(i)) {
  50. OnClientPutInServer(i);
  51. }
  52. }
  53. HookEvent("player_say", OnPlayerSayPost, EventHookMode_Post);
  54. }
  55. public void OnClientPutInServer(int client) {
  56. g_QueuedMessages[client] = new StringMap();
  57. SDKHook(client, SDKHook_PostThink, OnClientThinkPost);
  58. }
  59. public void OnClientDisconnect(int client) {
  60. if (g_QueuedMessages[client] && g_QueuedMessages[client].Size > 0) {
  61. // delete remaining queued messages, don't bother sending
  62. StringMapSnapshot messages = g_QueuedMessages[client].Snapshot();
  63. for (int m = 0; m < messages.Length; m++) {
  64. char packedMessage[192];
  65. messages.GetKey(m, packedMessage, sizeof(packedMessage));
  66. ArrayList clientList;
  67. g_QueuedMessages[client].GetValue(packedMessage, clientList);
  68. delete clientList;
  69. }
  70. delete messages;
  71. g_QueuedMessages[client].Clear();
  72. }
  73. delete g_QueuedMessages[client];
  74. }
  75. /**
  76. * Collects previously fired UTIL_SayText2Filter events and holds them in our own internal
  77. * buffer to be manipulated at a later time. Each usermessage is fired separately.
  78. *
  79. * This is handled by game/server/client.cpp::Host_Say
  80. */
  81. Action OnSayText2(UserMsg id, Handle buffer, const int[] clients, int nClients,
  82. bool reliable, bool init) {
  83. BfRead bitbuf = view_as<BfRead>(buffer);
  84. int author = bitbuf.ReadByte();
  85. if (!author) {
  86. return Plugin_Continue;
  87. }
  88. bitbuf.ReadByte(); // bChat, unused?
  89. char localizationToken[32];
  90. bitbuf.ReadString(localizationToken, sizeof(localizationToken));
  91. if (StrContains(localizationToken, "TF_Chat_") == -1) {
  92. return Plugin_Continue;
  93. }
  94. if (!ParseChatMessageFlags(localizationToken)) {
  95. return Plugin_Continue;
  96. }
  97. char name[MAX_NAME_LENGTH];
  98. bitbuf.ReadString(name, sizeof(name));
  99. char message[128];
  100. bitbuf.ReadString(message, sizeof(message));
  101. /**
  102. * Pack messages based on localization token and message.
  103. * Any new similar usermessages in the same frame (matching message and flags) get their
  104. * recipients added to the same entry.
  105. */
  106. char packedMessage[192];
  107. Format(packedMessage, sizeof(packedMessage), "%s" ... PACKED_TOKEN_DELIMITER ... "%s",
  108. localizationToken, message);
  109. ArrayList recipients;
  110. if (!g_QueuedMessages[author].GetValue(packedMessage, recipients)) {
  111. recipients = new ArrayList();
  112. g_QueuedMessages[author].SetValue(packedMessage, recipients);
  113. }
  114. for (int i = 0; i < nClients; i++) {
  115. recipients.Push(clients[i]);
  116. }
  117. return Plugin_Handled;
  118. }
  119. void OnPlayerSayPost(Event event, const char[] name, bool dontBroadcast) {
  120. int client = GetClientOfUserId(event.GetInt("userid"));
  121. FlushQueuedMessages(client);
  122. }
  123. void OnClientThinkPost(int client) {
  124. FlushQueuedMessages(client);
  125. }
  126. void FlushQueuedMessages(int author) {
  127. /**
  128. * Iterate through all queued messages from OnSayText2
  129. */
  130. if (!g_QueuedMessages[author] || g_QueuedMessages[author].Size == 0) {
  131. return;
  132. }
  133. StringMapSnapshot messages = g_QueuedMessages[author].Snapshot();
  134. for (int m = 0; m < messages.Length; m++) {
  135. char packedMessage[192], localizationToken[32], message[128];
  136. messages.GetKey(m, packedMessage, sizeof(packedMessage));
  137. ArrayList clientList;
  138. g_QueuedMessages[author].GetValue(packedMessage, clientList);
  139. // unpack localization and message from key
  140. int d = SplitString(packedMessage, PACKED_TOKEN_DELIMITER, localizationToken,
  141. sizeof(localizationToken));
  142. strcopy(message, sizeof(message), packedMessage[d]);
  143. char name[MAX_NAME_LENGTH + 1];
  144. GetClientName(author, name, sizeof(name));
  145. // Prepare chat message flags.
  146. g_ChatFlags = ParseChatMessageFlags(localizationToken);
  147. // Forward call.
  148. Action forwardResult = ForwardOnChatMessage(author, clientList, name, sizeof(name),
  149. message, sizeof(message));
  150. // Proceed to display message on continue or changed, else drop message.
  151. if (forwardResult < Plugin_Handled) {
  152. // convert ArrayList to client array
  153. int clients[MAXPLAYERS + 1], nClients;
  154. for (int i = 0; i < clientList.Length; i++) {
  155. int recipient = clientList.Get(i);
  156. // display to author and players that did not mute the author
  157. if (ShouldTransmitMessage(author, recipient)) {
  158. clients[nClients++] = recipient;
  159. }
  160. }
  161. // since it's not commented on in the SDK, we can only speculate on why the
  162. // developers decided to send SayText2 messages individually
  163. // probably cheat clients?
  164. SayText(author, clients, nClients, localizationToken, name, message);
  165. ForwardOnChatMessagePost(author, clientList, name, message);
  166. }
  167. delete clientList;
  168. g_ChatFlags = CHATFLAGS_INVALID;
  169. }
  170. delete messages;
  171. g_QueuedMessages[author].Clear();
  172. }
  173. Action ForwardOnChatMessage(int &author, ArrayList clientList, char[] name, int nameLength,
  174. char[] message, int messageLength) {
  175. Action forwardResult;
  176. Call_StartForward(g_fwdOnChatMessage);
  177. Call_PushCellRef(author);
  178. Call_PushCell(clientList);
  179. Call_PushStringEx(name, nameLength,
  180. SM_PARAM_STRING_UTF8 | SM_PARAM_STRING_COPY, SM_PARAM_COPYBACK);
  181. Call_PushStringEx(message, messageLength,
  182. SM_PARAM_STRING_UTF8 | SM_PARAM_STRING_COPY, SM_PARAM_COPYBACK);
  183. int error = Call_Finish(forwardResult);
  184. if (error) {
  185. ThrowNativeError(error, "Forward failed");
  186. return Plugin_Stop;
  187. }
  188. return forwardResult;
  189. }
  190. bool ShouldTransmitMessage(int author, int recipient) {
  191. return recipient == author || !IsClientMuted(recipient, author);
  192. }
  193. void ForwardOnChatMessagePost(int author, ArrayList clientList, const char[] name,
  194. const char[] message) {
  195. Call_StartForward(g_fwdOnChatMessagePost);
  196. Call_PushCell(author);
  197. Call_PushCell(clientList);
  198. Call_PushString(name);
  199. Call_PushString(message);
  200. int error = Call_Finish();
  201. if (error) {
  202. ThrowNativeError(error, "Forward failed");
  203. }
  204. }
  205. int Native_GetMessageFlags(Handle hPlugin, int argc) {
  206. return g_ChatFlags;
  207. }
  208. int ParseChatMessageFlags(const char[] localizationToken) {
  209. int chatFlags;
  210. if (StrContains(localizationToken, "all", false) != -1) {
  211. // send to all players, living and dead
  212. chatFlags |= CHATFLAGS_ALL;
  213. }
  214. if (StrContains(localizationToken, "team", false) != -1) {
  215. // send only to players on the same team
  216. chatFlags |= CHATFLAGS_TEAM;
  217. }
  218. if (StrContains(localizationToken, "spec", false) != -1) {
  219. // send only to players in spec
  220. chatFlags |= CHATFLAGS_SPEC;
  221. }
  222. if (StrContains(localizationToken, "dead", false) != -1) {
  223. // send to dead players and team members only
  224. chatFlags |= CHATFLAGS_DEAD;
  225. }
  226. return chatFlags;
  227. }
  228. void SayText(int author, int[] clients, int nClients,
  229. const char[] localizationToken, const char[] name, const char[] message) {
  230. Handle buffer = StartMessage("SayText2", clients, nClients,
  231. USERMSG_RELIABLE | USERMSG_BLOCKHOOKS);
  232. BfWrite bitbuf = view_as<BfWrite>(buffer);
  233. bitbuf.WriteByte(author);
  234. bitbuf.WriteByte(true);
  235. bitbuf.WriteString(localizationToken);
  236. bitbuf.WriteString(name);
  237. bitbuf.WriteString(message);
  238. EndMessage();
  239. }