Explorar el Código

Initial commit.

I actually had this last week, but I forgot to commit it.  Whoops.
nosoop hace 8 años
padre
commit
f0172f7e2a
Se han modificado 1 ficheros con 270 adiciones y 0 borrados
  1. 270 0
      scripting/simple-chatprocessor.sp

+ 270 - 0
scripting/simple-chatprocessor.sp

@@ -0,0 +1,270 @@
+/**
+ * [CSRD] Simple Chat Processor
+ * 
+ * Simple Chat Processor almost-compatible library for TF2.
+ * Attempts to fix the one-recipient SayText2 messages, and does not depend on server-side
+ * localization nonsense (i.e., everyone sees messages in their language).
+ */
+#pragma semicolon 1
+#include <sourcemod>
+
+#include <sdkhooks>
+
+#pragma newdecls required
+
+#define PLUGIN_VERSION "0.0.0"
+public Plugin myinfo = {
+	name = "[CSRD] Simple Chat Processor",
+	author = "nosoop (based off of Simple Plugins' implementation)",
+	description = "Simple Chat Processor almost-compatible library for TF2-specific fixes.",
+	version = PLUGIN_VERSION,
+	url = "https://git.csrd.science/"
+}
+
+#define PACKED_TOKEN_DELIMITER ":"
+
+#define CHATFLAGS_INVALID		0b0000
+#define CHATFLAGS_ALL			0b0001
+#define CHATFLAGS_TEAM			0b0010
+#define CHATFLAGS_SPEC			0b0100
+#define CHATFLAGS_DEAD			0b1000
+
+public APLRes AskPluginLoad2(Handle hPluginSelf, bool late, char[] error, int maxlen) {
+	MarkNativeAsOptional("GetUserMessageType");
+	CreateNative("GetMessageFlags", Native_GetMessageFlags);
+	RegPluginLibrary("scp");
+	
+	return APLRes_Success;
+}
+
+// Holds player-unique messages sent in the current frame.
+StringMap g_QueuedMessages[MAXPLAYERS+1];
+
+Handle g_fwdOnChatMessage, g_fwdOnChatMessagePost;
+
+int g_ChatFlags;
+
+public void OnPluginStart() {
+	UserMsg umSayText2 = GetUserMessageId("SayText2");
+	if (umSayText2 != INVALID_MESSAGE_ID) {
+		HookUserMessage(umSayText2, OnSayText2, true);
+	} else {
+		SetFailState("Game does not use SayText2.");
+	}
+	
+	g_fwdOnChatMessage = CreateGlobalForward("OnChatMessage", ET_Hook, Param_CellByRef,
+			Param_Cell, Param_String, Param_String);
+	g_fwdOnChatMessagePost = CreateGlobalForward("OnChatMessage_Post", ET_Ignore, Param_Cell,
+			Param_Cell, Param_String, Param_String);
+	
+	for (int i = 1; i < MaxClients; i++) {
+		if (IsClientInGame(i)) {
+			OnClientPutInServer(i);
+		}
+	}
+}
+
+public void OnClientPutInServer(int client) {
+	g_QueuedMessages[client] = new StringMap();
+	SDKHook(client, SDKHook_PostThink, OnClientThinkPost);
+}
+
+public void OnClientDisconnect(int client) {
+	if (g_QueuedMessages[client]) {
+		if (g_QueuedMessages[client].Size > 0) {
+			// delete remaining queued messages, don't bother sending
+			StringMapSnapshot messages = g_QueuedMessages[client].Snapshot();
+			
+			for (int m = 0; m < messages.Length; m++) {
+				char packedMessage[192];
+				messages.GetKey(m, packedMessage, sizeof(packedMessage));
+				
+				ArrayList clientList;
+				g_QueuedMessages[client].GetValue(packedMessage, clientList);
+				
+				delete clientList;
+			}
+			g_QueuedMessages[client].Clear();
+		}
+		delete g_QueuedMessages[client];
+	}
+}
+
+public Action OnSayText2(UserMsg id, Handle buffer, const int[] clients, int nClients,
+		bool reliable, bool init) {
+	BfRead bitbuf = view_as<BfRead>(buffer);
+	
+	int author = bitbuf.ReadByte();
+	
+	if (!author) {
+		return Plugin_Continue;
+	}
+	
+	bool bChat = bitbuf.ReadByte() != 0;
+	
+	char localizationToken[32];
+	bitbuf.ReadString(localizationToken, sizeof(localizationToken));
+	
+	if (StrContains(localizationToken, "TF_Chat_") == -1) {
+		return Plugin_Continue;
+	}
+	
+	if (!GetChatMessageFlags(localizationToken)) {
+		return Plugin_Continue;
+	}
+	
+	char name[MAX_NAME_LENGTH];
+	bitbuf.ReadString(name, sizeof(name));
+	
+	char message[128];
+	bitbuf.ReadString(message, sizeof(message));
+	
+	/**
+	 * Pack messages based on localization token and message.
+	 * Any new similar usermessages in the same frame (matching message and flags) get their
+	 * recipients added to the same entry.
+	 */
+	char packedMessage[192];
+	Format(packedMessage, sizeof(packedMessage), "%s" ... PACKED_TOKEN_DELIMITER ... "%s",
+			localizationToken, message);
+	
+	ArrayList recipients;
+	
+	if (!g_QueuedMessages[author].GetValue(packedMessage, recipients)) {
+		recipients = new ArrayList();
+		g_QueuedMessages[author].SetValue(packedMessage, recipients);
+	}
+	
+	for (int i = 0; i < nClients; i++) {
+		recipients.Push(clients[i]);
+	}
+	
+	return Plugin_Handled;
+}
+
+public void OnClientThinkPost(int client) {
+	/**
+	 * Iterate through all queued messages from OnSayText2
+	 */
+	if (g_QueuedMessages[client].Size > 0) {
+		StringMapSnapshot messages = g_QueuedMessages[client].Snapshot();
+		
+		for (int m = 0; m < messages.Length; m++) {
+			char packedMessage[192], localizationToken[32], message[128];
+			messages.GetKey(m, packedMessage, sizeof(packedMessage));
+			
+			ArrayList clientList;
+			
+			g_QueuedMessages[client].GetValue(packedMessage, clientList);
+			
+			// unpack localization and message from key
+			strcopy(localizationToken, StrContains(packedMessage, PACKED_TOKEN_DELIMITER) + 1,
+					packedMessage);
+			strcopy(message, sizeof(message), packedMessage[strlen(localizationToken) + 1]);
+			
+			char name[MAX_NAME_LENGTH + 1];
+			GetClientName(client, name, sizeof(name));
+			
+			// Prepare chat message flags.
+			g_ChatFlags = GetChatMessageFlags(localizationToken);
+			
+			// Forward call.
+			Action forwardResult = ForwardOnChatMessage(client, clientList, name, sizeof(name),
+					message, sizeof(message));
+			
+			// Proceed to display message on continue or changed, else drop message.
+			if (forwardResult < Plugin_Handled) {
+				// convert ArrayList to client array
+				int clients[MAXPLAYERS + 1], nClients;
+				for (int i = 0; i < clientList.Length; i++) {
+					clients[nClients++] = clientList.Get(i);
+				}
+				
+				SayText(client, clients, nClients, localizationToken, name, message);
+				
+				ForwardOnChatMessagePost(client, clientList, name, message);
+			}
+			delete clientList;
+			
+			g_ChatFlags = CHATFLAGS_INVALID;
+		}
+		delete messages;
+		
+		g_QueuedMessages[client].Clear();
+	}
+}
+
+Action ForwardOnChatMessage(int &author, ArrayList clientList, char[] name, int nameLength,
+		char[] message, int messageLength) {
+	Action forwardResult;
+	Call_StartForward(g_fwdOnChatMessage);
+	Call_PushCellRef(author);
+	Call_PushCell(clientList);
+	Call_PushStringEx(name, nameLength,
+			SM_PARAM_STRING_UTF8 | SM_PARAM_STRING_COPY, SM_PARAM_COPYBACK);
+	Call_PushStringEx(message, messageLength,
+			SM_PARAM_STRING_UTF8 | SM_PARAM_STRING_COPY, SM_PARAM_COPYBACK);
+	
+	int error = Call_Finish(forwardResult);
+	
+	if (error) {
+		ThrowNativeError(error, "Forward failed");
+		return Plugin_Stop;
+	}
+	return forwardResult;
+}
+
+void ForwardOnChatMessagePost(int author, ArrayList clientList, const char[] name,
+		const char[] message) {
+	Call_StartForward(g_fwdOnChatMessagePost);
+	Call_PushCell(author);
+	Call_PushCell(clientList);
+	Call_PushString(name);
+	Call_PushString(message);
+	
+	int error = Call_Finish();
+	if (error) {
+		ThrowNativeError(error, "Forward failed");
+	}
+}
+
+public int Native_GetMessageFlags(Handle hPlugin, int argc) {
+	return g_ChatFlags;
+}
+
+int GetChatMessageFlags(const char[] localizationToken) {
+	int chatFlags;
+	if (StrContains(localizationToken, "all", false) != -1) {
+		// send to all players, living and dead
+		chatFlags |= CHATFLAGS_ALL;
+	}
+	if (StrContains(localizationToken, "team", false) != -1) {
+		// send only to players on the same team
+		chatFlags |= CHATFLAGS_TEAM;
+	}
+	if (StrContains(localizationToken, "spec", false) != -1) {
+		// send only to players in spec
+		chatFlags |= CHATFLAGS_SPEC;
+	}
+	if (StrContains(localizationToken, "dead", false) != -1) {
+		// send to dead players and team members only
+		chatFlags |= CHATFLAGS_DEAD;
+	}
+	return chatFlags;
+}
+
+void SayText(int author, int[] clients, int nClients,
+		const char[] localizationToken, const char[] name, const char[] message) {
+	Handle buffer = StartMessage("SayText2", clients, nClients,
+			USERMSG_RELIABLE | USERMSG_BLOCKHOOKS);
+	
+	BfWrite bitbuf = view_as<BfWrite>(buffer);
+	
+	bitbuf.WriteByte(author);
+	bitbuf.WriteByte(true);
+	bitbuf.WriteString(localizationToken);
+	bitbuf.WriteString(name);
+	bitbuf.WriteString(message);
+	
+	EndMessage();
+}