SanAndreasUnity/Assets/Scripts/Chat/ChatManager.cs

209 lines
5.1 KiB
C#
Raw Normal View History

2021-02-20 20:57:10 +00:00
using System.Collections.Generic;
using System.Text;
2021-02-20 20:57:10 +00:00
using UnityEngine;
2019-11-15 11:37:37 +00:00
using SanAndreasUnity.Net;
2021-02-20 20:57:10 +00:00
using SanAndreasUnity.Utilities;
2019-11-15 11:37:37 +00:00
namespace SanAndreasUnity.Chat
{
public class ChatMessage
{
public ChatMessage (string msg, string sender)
{
this.msg = msg;
this.sender = sender;
}
public string msg = "";
public string sender = "";
}
2021-02-20 20:57:10 +00:00
public class ChatPreprocessorResult
{
public bool shouldBeDiscarded;
public string finalChatMessage;
}
public class ChatPreprocessor
{
public System.Func<Player, string, ChatPreprocessorResult> processCallback;
}
2019-11-15 11:37:37 +00:00
public class ChatManager : MonoBehaviour
{
public static ChatManager singleton { get ; private set ; }
2019-11-15 11:37:37 +00:00
public string serverChatNick = "<color=green>Server</color>";
public int maxChatMessageLength = 50;
2019-11-15 11:37:37 +00:00
public static event System.Action<ChatMessage> onChatMessage = delegate {};
2021-02-20 20:57:10 +00:00
List<ChatPreprocessor> m_chatPreprocessors = new List<ChatPreprocessor>();
static StringBuilder _stringBuilderForMessageProcessing = new StringBuilder();
2019-11-15 11:37:37 +00:00
void Awake ()
{
singleton = this;
onChatMessage += (ChatMessage chatMsg) => Debug.Log ("<color=blue>" + chatMsg.sender + "</color> : " + chatMsg.msg);
}
void OnSceneChanged( SanAndreasUnity.Behaviours.SceneChangedMessage info ) {
if (NetStatus.IsServer) {
SendChatMessageToAllPlayersAsServer ("Map changed to " + info.s2.name + ".");
}
}
internal void OnChatMessageReceivedOnServer(Player player, string msg)
2021-02-20 20:57:10 +00:00
{
msg = ChatManager.ProcessChatMessage(msg, false);
if (string.IsNullOrEmpty(msg))
return;
2021-02-28 20:28:57 +00:00
if (!FilterWithPreprocessors(player, ref msg))
return;
2021-02-28 23:58:50 +00:00
SendChatMessageToAllPlayersAsServer(msg, "player " + player.netId);
2021-02-28 20:28:57 +00:00
}
internal void OnChatMessageReceivedOnLocalPlayer(ChatMessage chatMsg)
{
chatMsg.msg = ChatManager.ProcessChatMessage(chatMsg.msg, true);
if (string.IsNullOrEmpty(chatMsg.msg))
return;
F.InvokeEventExceptionSafe(onChatMessage, chatMsg);
}
2021-02-28 20:28:57 +00:00
private bool FilterWithPreprocessors(Player player, ref string chatMessageToFilter)
{
string finalMsg = chatMessageToFilter;
2021-02-20 20:57:10 +00:00
foreach (var chatPreprocessor in m_chatPreprocessors)
{
ChatPreprocessorResult result = null;
2021-02-28 20:28:57 +00:00
F.RunExceptionSafe(() => result = chatPreprocessor.processCallback(player, finalMsg));
2021-02-20 20:57:10 +00:00
if (null == result || result.shouldBeDiscarded || null == result.finalChatMessage)
2021-02-28 20:28:57 +00:00
return false;
2021-02-20 20:57:10 +00:00
2021-02-28 20:28:57 +00:00
finalMsg = result.finalChatMessage;
2021-02-20 20:57:10 +00:00
}
2021-02-28 20:28:57 +00:00
chatMessageToFilter = finalMsg;
return true;
2021-02-20 20:57:10 +00:00
}
2021-02-28 21:53:16 +00:00
public static string ProcessChatMessage(string chatMessage, bool allowTags)
{
if (chatMessage == null)
return string.Empty;
if (chatMessage.Length > 2000)
return string.Empty;
var sb = _stringBuilderForMessageProcessing;
sb.Clear();
sb.Append(allowTags ? chatMessage : (chatMessage.Length > singleton.maxChatMessageLength ? chatMessage.Substring(0, singleton.maxChatMessageLength) : chatMessage));
// Remove tags.
if (!allowTags)
{
sb.Replace('<', ' '); // the only easy way :D
sb.Replace('>', ' ');
// msg = msg.Replace ("<color", "color");
// msg = msg.Replace ("<size", "size");
// msg = msg.Replace ("<b>", "");
// msg = msg.Replace ("<i>", "");
// msg = msg.Replace (">", "\\>");
}
sb.Replace('\r', ' ');
sb.Replace('\n', ' ');
sb.Replace('\t', ' ');
return sb.ToString().Trim();
}
2021-02-28 22:07:26 +00:00
public static void SendChatMessageToAllPlayersAsServer( string msg )
{
NetStatus.ThrowIfNotOnServer();
2019-11-15 11:37:37 +00:00
2021-02-28 23:58:50 +00:00
SendChatMessageToAllPlayersAsServer (msg, singleton.serverChatNick);
2019-11-15 11:37:37 +00:00
}
public static void SendChatMessageToAllPlayersAsLocalPlayer( string msg ) {
if (null == Player.Local) {
return;
}
var chatSync = Player.Local.GetComponent<ChatSync> ();
if (chatSync != null) {
chatSync.SendChatMsgToServer (msg);
}
}
2021-02-28 23:58:50 +00:00
public static void SendChatMessageToAllPlayersAsServer( string msg, string sender ) {
2019-11-15 11:37:37 +00:00
2021-02-28 22:07:26 +00:00
NetStatus.ThrowIfNotOnServer();
2021-02-20 20:57:52 +00:00
2021-02-28 21:53:16 +00:00
msg = ChatManager.ProcessChatMessage(msg, true);
if (string.IsNullOrEmpty(msg))
return;
2019-11-15 11:37:37 +00:00
foreach (var player in Player.AllPlayers) {
2021-02-28 23:58:50 +00:00
SendChatMessageToPlayerAsServer ( player, msg, sender );
2019-11-15 11:37:37 +00:00
}
if (!NetStatus.IsHost ()) {
// running as dedicated server
// we should invoke the event here, because there is no local player to receive the chat message
2021-02-27 22:25:07 +00:00
F.InvokeEventExceptionSafe(onChatMessage, new ChatMessage(msg, sender));
2019-11-15 11:37:37 +00:00
}
}
2021-02-28 23:58:50 +00:00
public static void SendChatMessageToPlayerAsServer( Player player, string msg, bool useServerNick ) {
2019-11-15 11:37:37 +00:00
2021-02-28 22:07:26 +00:00
NetStatus.ThrowIfNotOnServer();
2019-11-15 11:37:37 +00:00
2021-02-28 21:53:16 +00:00
msg = ChatManager.ProcessChatMessage(msg, true);
if (string.IsNullOrEmpty(msg))
return;
2021-02-28 23:58:50 +00:00
SendChatMessageToPlayerAsServer (player, msg, useServerNick ? singleton.serverChatNick : "");
2019-11-15 11:37:37 +00:00
}
2021-02-28 23:58:50 +00:00
private static void SendChatMessageToPlayerAsServer( Player player, string msg, string sender ) {
2019-11-15 11:37:37 +00:00
2021-02-28 22:07:26 +00:00
NetStatus.ThrowIfNotOnServer();
2019-11-15 11:37:37 +00:00
var chatSync = player.GetComponent<ChatSync> ();
if (chatSync != null) {
chatSync.SendChatMsgToClient (player.connectionToClient, msg, sender);
}
}
2021-02-20 20:57:10 +00:00
public void RegisterChatPreprocessor(ChatPreprocessor chatPreprocessor)
{
m_chatPreprocessors.Add(chatPreprocessor);
}
2019-11-15 11:37:37 +00:00
}
}