simple-chatprocessor.sp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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.1.4"
  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. }
  54. public void OnClientPutInServer(int client) {
  55. g_QueuedMessages[client] = new StringMap();
  56. SDKHook(client, SDKHook_PostThink, OnClientThinkPost);
  57. }
  58. public void OnClientDisconnect(int client) {
  59. if (g_QueuedMessages[client] && g_QueuedMessages[client].Size > 0) {
  60. // delete remaining queued messages, don't bother sending
  61. StringMapSnapshot messages = g_QueuedMessages[client].Snapshot();
  62. for (int m = 0; m < messages.Length; m++) {
  63. char packedMessage[192];
  64. messages.GetKey(m, packedMessage, sizeof(packedMessage));
  65. ArrayList clientList;
  66. g_QueuedMessages[client].GetValue(packedMessage, clientList);
  67. delete clientList;
  68. }
  69. delete messages;
  70. g_QueuedMessages[client].Clear();
  71. }
  72. delete g_QueuedMessages[client];
  73. }
  74. /**
  75. * Collects previously fired UTIL_SayText2Filter events and holds them in our own internal
  76. * buffer to be manipulated at a later time. Each usermessage is fired separately.
  77. *
  78. * This is handled by game/server/client.cpp::Host_Say
  79. */
  80. Action OnSayText2(UserMsg id, Handle buffer, const int[] clients, int nClients,
  81. bool reliable, bool init) {
  82. BfRead bitbuf = view_as<BfRead>(buffer);
  83. int author = bitbuf.ReadByte();
  84. if (!author) {
  85. return Plugin_Continue;
  86. }
  87. bitbuf.ReadByte(); // bChat, unused?
  88. char localizationToken[32];
  89. bitbuf.ReadString(localizationToken, sizeof(localizationToken));
  90. if (StrContains(localizationToken, "TF_Chat_") == -1) {
  91. return Plugin_Continue;
  92. }
  93. if (!ParseChatMessageFlags(localizationToken)) {
  94. return Plugin_Continue;
  95. }
  96. char name[MAX_NAME_LENGTH];
  97. bitbuf.ReadString(name, sizeof(name));
  98. char message[128];
  99. bitbuf.ReadString(message, sizeof(message));
  100. /**
  101. * Pack messages based on localization token and message.
  102. * Any new similar usermessages in the same frame (matching message and flags) get their
  103. * recipients added to the same entry.
  104. */
  105. char packedMessage[192];
  106. Format(packedMessage, sizeof(packedMessage), "%s" ... PACKED_TOKEN_DELIMITER ... "%s",
  107. localizationToken, message);
  108. ArrayList recipients;
  109. if (!g_QueuedMessages[author].GetValue(packedMessage, recipients)) {
  110. recipients = new ArrayList();
  111. g_QueuedMessages[author].SetValue(packedMessage, recipients);
  112. }
  113. for (int i = 0; i < nClients; i++) {
  114. recipients.Push(clients[i]);
  115. }
  116. return Plugin_Handled;
  117. }
  118. public void OnClientThinkPost(int author) {
  119. /**
  120. * Iterate through all queued messages from OnSayText2
  121. */
  122. if (g_QueuedMessages[author].Size == 0) {
  123. return;
  124. }
  125. StringMapSnapshot messages = g_QueuedMessages[author].Snapshot();
  126. for (int m = 0; m < messages.Length; m++) {
  127. char packedMessage[192], localizationToken[32], message[128];
  128. messages.GetKey(m, packedMessage, sizeof(packedMessage));
  129. ArrayList clientList;
  130. g_QueuedMessages[author].GetValue(packedMessage, clientList);
  131. // unpack localization and message from key
  132. int d = SplitString(packedMessage, PACKED_TOKEN_DELIMITER, localizationToken,
  133. sizeof(localizationToken));
  134. strcopy(message, sizeof(message), packedMessage[d]);
  135. char name[MAX_NAME_LENGTH + 1];
  136. GetClientName(author, name, sizeof(name));
  137. // Prepare chat message flags.
  138. g_ChatFlags = ParseChatMessageFlags(localizationToken);
  139. // Forward call.
  140. Action forwardResult = ForwardOnChatMessage(author, clientList, name, sizeof(name),
  141. message, sizeof(message));
  142. // Proceed to display message on continue or changed, else drop message.
  143. if (forwardResult < Plugin_Handled) {
  144. // convert ArrayList to client array
  145. int clients[MAXPLAYERS + 1], nClients;
  146. for (int i = 0; i < clientList.Length; i++) {
  147. int recipient = clientList.Get(i);
  148. // display to author and players that did not mute the author
  149. if (ShouldTransmitMessage(author, recipient)) {
  150. clients[nClients++] = recipient;
  151. }
  152. }
  153. SayText(author, clients, nClients, localizationToken, name, message);
  154. ForwardOnChatMessagePost(author, clientList, name, message);
  155. }
  156. delete clientList;
  157. g_ChatFlags = CHATFLAGS_INVALID;
  158. }
  159. delete messages;
  160. g_QueuedMessages[author].Clear();
  161. }
  162. Action ForwardOnChatMessage(int &author, ArrayList clientList, char[] name, int nameLength,
  163. char[] message, int messageLength) {
  164. Action forwardResult;
  165. Call_StartForward(g_fwdOnChatMessage);
  166. Call_PushCellRef(author);
  167. Call_PushCell(clientList);
  168. Call_PushStringEx(name, nameLength,
  169. SM_PARAM_STRING_UTF8 | SM_PARAM_STRING_COPY, SM_PARAM_COPYBACK);
  170. Call_PushStringEx(message, messageLength,
  171. SM_PARAM_STRING_UTF8 | SM_PARAM_STRING_COPY, SM_PARAM_COPYBACK);
  172. int error = Call_Finish(forwardResult);
  173. if (error) {
  174. ThrowNativeError(error, "Forward failed");
  175. return Plugin_Stop;
  176. }
  177. return forwardResult;
  178. }
  179. bool ShouldTransmitMessage(int author, int recipient) {
  180. return recipient == author || !IsClientMuted(recipient, author);
  181. }
  182. void ForwardOnChatMessagePost(int author, ArrayList clientList, const char[] name,
  183. const char[] message) {
  184. Call_StartForward(g_fwdOnChatMessagePost);
  185. Call_PushCell(author);
  186. Call_PushCell(clientList);
  187. Call_PushString(name);
  188. Call_PushString(message);
  189. int error = Call_Finish();
  190. if (error) {
  191. ThrowNativeError(error, "Forward failed");
  192. }
  193. }
  194. int Native_GetMessageFlags(Handle hPlugin, int argc) {
  195. return g_ChatFlags;
  196. }
  197. int ParseChatMessageFlags(const char[] localizationToken) {
  198. int chatFlags;
  199. if (StrContains(localizationToken, "all", false) != -1) {
  200. // send to all players, living and dead
  201. chatFlags |= CHATFLAGS_ALL;
  202. }
  203. if (StrContains(localizationToken, "team", false) != -1) {
  204. // send only to players on the same team
  205. chatFlags |= CHATFLAGS_TEAM;
  206. }
  207. if (StrContains(localizationToken, "spec", false) != -1) {
  208. // send only to players in spec
  209. chatFlags |= CHATFLAGS_SPEC;
  210. }
  211. if (StrContains(localizationToken, "dead", false) != -1) {
  212. // send to dead players and team members only
  213. chatFlags |= CHATFLAGS_DEAD;
  214. }
  215. return chatFlags;
  216. }
  217. void SayText(int author, int[] clients, int nClients,
  218. const char[] localizationToken, const char[] name, const char[] message) {
  219. Handle buffer = StartMessage("SayText2", clients, nClients,
  220. USERMSG_RELIABLE | USERMSG_BLOCKHOOKS);
  221. BfWrite bitbuf = view_as<BfWrite>(buffer);
  222. bitbuf.WriteByte(author);
  223. bitbuf.WriteByte(true);
  224. bitbuf.WriteString(localizationToken);
  225. bitbuf.WriteString(name);
  226. bitbuf.WriteString(message);
  227. EndMessage();
  228. }