simple-chatprocessor.sp 8.5 KB

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