SanAndreasUnity/Assets/Scripts/Chat/ChatManager.cs

208 lines
5 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;
2021-03-01 00:24:21 +00:00
onChatMessage += LogChatMessage;
2019-11-15 11:37:37 +00:00
}
void OnSceneChanged( SanAndreasUnity.Behaviours.SceneChangedMessage info ) {
if (NetStatus.IsServer) {
SendChatMessageToAllPlayersAsServer ("Map changed to " + info.s2.name + ".");
}
}
2021-03-01 00:24:21 +00:00
void LogChatMessage(ChatMessage chatMessage)
{
string senderText = string.IsNullOrEmpty(chatMessage.sender) ? "" : "<color=blue>" + chatMessage.sender + "</color> : ";
Debug.Log(senderText + chatMessage.msg);
}
2019-11-15 11:37:37 +00:00
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;
SendChatMessageToAllPlayersAsServer(msg, player.PlayerName);
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 easiest way
}
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
2021-07-18 19:07:27 +00:00
foreach (var player in Player.AllPlayersCopy) {
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
}
}