SanAndreasUnity/Assets/Scripts/Chat/ChatManager.cs

182 lines
4.4 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 ; }
public string serverChatNick = "<color=green>Server</color>";
public static event System.Action<ChatMessage> onChatMessage = delegate {};
2021-02-20 20:57:10 +00:00
List<ChatPreprocessor> m_chatPreprocessors = new List<ChatPreprocessor>();
2019-11-15 11:37:37 +00:00
void Awake ()
{
singleton = this;
onChatMessage += (ChatMessage chatMsg) => Debug.Log ("<color=blue>" + chatMsg.sender + "</color> : " + chatMsg.msg);
2021-02-20 20:57:10 +00:00
ChatSync.onChatMessageReceivedOnServer += OnChatMessageReceivedOnServer;
2021-02-27 22:25:07 +00:00
ChatSync.onChatMessageReceivedOnLocalPlayer += (ChatMessage chatMsg) => F.InvokeEventExceptionSafe(onChatMessage, chatMsg);
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-02-20 20:57:10 +00:00
private void OnChatMessageReceivedOnServer(Player player, string msg)
{
foreach (var chatPreprocessor in m_chatPreprocessors)
{
ChatPreprocessorResult result = null;
F.RunExceptionSafe(() => result = chatPreprocessor.processCallback(player, msg));
if (null == result || result.shouldBeDiscarded || null == result.finalChatMessage)
return;
msg = result.finalChatMessage;
}
SendChatMessageToAllPlayers(msg, "player " + player.netId);
}
public static string RemoveInvalidCharacters(string chatMessage)
{
if (chatMessage == null)
return string.Empty;
StringBuilder sb = chatMessage.Length > 50 ? new StringBuilder(chatMessage, 0, 50, 50) : new StringBuilder(chatMessage);
// Remove tags.
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();
}
2019-11-15 11:37:37 +00:00
public static void SendChatMessageToAllPlayersAsServer( string msg ) {
if (NetStatus.IsServerStarted) {
SendChatMessageToAllPlayers (msg, singleton.serverChatNick);
}
}
public static void SendChatMessageToAllPlayersAsLocalPlayer( string msg ) {
if (null == Player.Local) {
return;
}
var chatSync = Player.Local.GetComponent<ChatSync> ();
if (chatSync != null) {
chatSync.SendChatMsgToServer (msg);
}
}
/// <summary> Use only on server. </summary>
public static void SendChatMessageToAllPlayers( string msg, string sender ) {
if (!NetStatus.IsServerStarted)
return;
2021-02-20 20:57:52 +00:00
msg = ChatManager.RemoveInvalidCharacters(msg);
if (string.IsNullOrEmpty(msg))
return;
2019-11-15 11:37:37 +00:00
foreach (var player in Player.AllPlayers) {
SendChatMessageToPlayer ( player, msg, sender );
}
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
}
}
/// <summary> Use only on server. </summary>
public static void SendChatMessageToPlayer( Player player, string msg ) {
if (!NetStatus.IsServerStarted)
return;
msg = ChatManager.RemoveInvalidCharacters(msg);
if (string.IsNullOrEmpty(msg))
return;
2019-11-15 11:37:37 +00:00
SendChatMessageToPlayer (player, msg, singleton.serverChatNick);
}
private static void SendChatMessageToPlayer( Player player, string msg, string sender ) {
if (!NetStatus.IsServerStarted)
return;
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
}
}