Added character limit for chat (#1586)

* Added character limit for chat

* Changed buffer reading from Int16 to Int32

Co-authored-by: Clément <clement.orlandini@gmail.com>
This commit is contained in:
Clement-O
2020-08-17 14:45:02 +02:00
committed by GitHub
parent b051261485
commit 0e6f55a23d
4 changed files with 151 additions and 3 deletions

View File

@@ -35,6 +35,8 @@ namespace Content.Client.Chat
public bool ReleaseFocusOnEnter { get; set; } = true; public bool ReleaseFocusOnEnter { get; set; } = true;
public bool ClearOnEnter { get; set; } = true;
public ChatBox() public ChatBox()
{ {
/*MarginLeft = -475.0f; /*MarginLeft = -475.0f;
@@ -166,12 +168,18 @@ namespace Content.Client.Chat
private void Input_OnTextEntered(LineEdit.LineEditEventArgs args) private void Input_OnTextEntered(LineEdit.LineEditEventArgs args)
{ {
// We set it there to true so it's set to false by TextSubmitted.Invoke if necessary
ClearOnEnter = true;
if (!string.IsNullOrWhiteSpace(args.Text)) if (!string.IsNullOrWhiteSpace(args.Text))
{ {
TextSubmitted?.Invoke(this, args.Text); TextSubmitted?.Invoke(this, args.Text);
} }
Input.Clear(); if (ClearOnEnter)
{
Input.Clear();
}
if (ReleaseFocusOnEnter) if (ReleaseFocusOnEnter)
{ {

View File

@@ -1,17 +1,21 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using Content.Client.Interfaces.Chat; using Content.Client.Interfaces.Chat;
using Content.Shared.Chat; using Content.Shared.Chat;
using Robust.Client.Console; using Robust.Client.Console;
using Robust.Client.Interfaces.Graphics.ClientEye; using Robust.Client.Interfaces.Graphics.ClientEye;
using Robust.Client.Interfaces.UserInterface; using Robust.Client.Interfaces.UserInterface;
using Robust.Client.Player;
using Robust.Client.UserInterface; using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls; using Robust.Client.UserInterface.Controls;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Network; using Robust.Shared.Interfaces.Network;
using Robust.Shared.IoC; using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Log; using Robust.Shared.Log;
using Robust.Shared.Maths; using Robust.Shared.Maths;
using Robust.Shared.Network;
using Robust.Shared.Timing; using Robust.Shared.Timing;
using Robust.Shared.Utility; using Robust.Shared.Utility;
@@ -45,6 +49,11 @@ namespace Content.Client.Chat
/// </summary> /// </summary>
private const int SpeechBubbleCap = 4; private const int SpeechBubbleCap = 4;
/// <summary>
/// The max amount of characters an entity can send in one message
/// </summary>
private int _maxMessageLength = 1000;
private const char ConCmdSlash = '/'; private const char ConCmdSlash = '/';
private const char OOCAlias = '['; private const char OOCAlias = '[';
private const char MeAlias = '@'; private const char MeAlias = '@';
@@ -89,11 +98,15 @@ namespace Content.Client.Chat
public void Initialize() public void Initialize()
{ {
_netManager.RegisterNetMessage<MsgChatMessage>(MsgChatMessage.NAME, _onChatMessage); _netManager.RegisterNetMessage<MsgChatMessage>(MsgChatMessage.NAME, _onChatMessage);
_netManager.RegisterNetMessage<ChatMaxMsgLengthMessage>(ChatMaxMsgLengthMessage.NAME, _onMaxLengthReceived);
_speechBubbleRoot = new LayoutContainer(); _speechBubbleRoot = new LayoutContainer();
LayoutContainer.SetAnchorPreset(_speechBubbleRoot, LayoutContainer.LayoutPreset.Wide); LayoutContainer.SetAnchorPreset(_speechBubbleRoot, LayoutContainer.LayoutPreset.Wide);
_userInterfaceManager.StateRoot.AddChild(_speechBubbleRoot); _userInterfaceManager.StateRoot.AddChild(_speechBubbleRoot);
_speechBubbleRoot.SetPositionFirst(); _speechBubbleRoot.SetPositionFirst();
// When connexion is achieved, request the max chat message length
_netManager.Connected += new EventHandler<NetChannelArgs>(RequestMaxLength);
} }
public void FrameUpdate(FrameEventArgs delta) public void FrameUpdate(FrameEventArgs delta)
@@ -213,6 +226,15 @@ namespace Content.Client.Chat
if (string.IsNullOrWhiteSpace(text)) if (string.IsNullOrWhiteSpace(text))
return; return;
// Check if message is longer than the character limit
if (text.Length > _maxMessageLength)
{
string locWarning = Loc.GetString("Your message exceeds {0} character limit", _maxMessageLength);
_currentChatBox?.AddLine(locWarning, ChatChannel.Server, Color.Orange);
_currentChatBox.ClearOnEnter = false; // The text shouldn't be cleared if it hasn't been sent
return;
}
switch (text[0]) switch (text[0])
{ {
case ConCmdSlash: case ConCmdSlash:
@@ -345,6 +367,17 @@ namespace Content.Client.Chat
} }
} }
private void _onMaxLengthReceived(ChatMaxMsgLengthMessage msg)
{
_maxMessageLength = msg.MaxMessageLength;
}
private void RequestMaxLength(object sender, NetChannelArgs args)
{
ChatMaxMsgLengthMessage msg = _netManager.CreateNetMessage<ChatMaxMsgLengthMessage>();
_netManager.ClientSendMessage(msg);
}
private void AddSpeechBubble(MsgChatMessage msg, SpeechBubble.SpeechType speechType) private void AddSpeechBubble(MsgChatMessage msg, SpeechBubble.SpeechType speechType)
{ {
if (!_entityManager.TryGetEntity(msg.SenderEntity, out var entity)) if (!_entityManager.TryGetEntity(msg.SenderEntity, out var entity))

View File

@@ -5,7 +5,9 @@ using Content.Server.Interfaces;
using Content.Server.Interfaces.Chat; using Content.Server.Interfaces.Chat;
using Content.Shared.Chat; using Content.Shared.Chat;
using Content.Shared.GameObjects.EntitySystems; using Content.Shared.GameObjects.EntitySystems;
using NFluidsynth;
using Robust.Server.Console; using Robust.Server.Console;
using Robust.Server.Interfaces.GameObjects;
using Robust.Server.Interfaces.Player; using Robust.Server.Interfaces.Player;
using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Network; using Robust.Shared.Interfaces.Network;
@@ -19,8 +21,18 @@ namespace Content.Server.Chat
/// </summary> /// </summary>
internal sealed class ChatManager : IChatManager internal sealed class ChatManager : IChatManager
{ {
/// <summary>
/// The maximum length a player-sent message can be sent
/// </summary>
public int MaxMessageLength = 1000;
private const int VoiceRange = 7; // how far voice goes in world units private const int VoiceRange = 7; // how far voice goes in world units
/// <summary>
/// The message displayed to the player when it exceeds the chat character limit
/// </summary>
private const string MaxLengthExceededMessage = "Your message exceeded {0} character limit";
#pragma warning disable 649 #pragma warning disable 649
[Dependency] private readonly IEntitySystemManager _entitySystemManager; [Dependency] private readonly IEntitySystemManager _entitySystemManager;
[Dependency] private readonly IServerNetManager _netManager; [Dependency] private readonly IServerNetManager _netManager;
@@ -33,6 +45,12 @@ namespace Content.Server.Chat
public void Initialize() public void Initialize()
{ {
_netManager.RegisterNetMessage<MsgChatMessage>(MsgChatMessage.NAME); _netManager.RegisterNetMessage<MsgChatMessage>(MsgChatMessage.NAME);
_netManager.RegisterNetMessage<ChatMaxMsgLengthMessage>(ChatMaxMsgLengthMessage.NAME, _onMaxLengthRequest);
// Tell all the connected players the chat's character limit
var msg = _netManager.CreateNetMessage<ChatMaxMsgLengthMessage>();
msg.MaxMessageLength = MaxMessageLength;
_netManager.ServerSendToAll(msg);
} }
public void DispatchServerAnnouncement(string message) public void DispatchServerAnnouncement(string message)
@@ -69,6 +87,17 @@ namespace Content.Server.Chat
return; return;
} }
// Get entity's PlayerSession
IPlayerSession playerSession = source.GetComponent<IActorComponent>().playerSession;
// Check if message exceeds the character limit if the sender is a player
if (playerSession != null)
if (message.Length > MaxMessageLength)
{
DispatchServerMessage(playerSession, Loc.GetString(MaxLengthExceededMessage, MaxMessageLength));
return;
}
var pos = source.Transform.GridPosition; var pos = source.Transform.GridPosition;
var clients = _playerManager.GetPlayersInRange(pos, VoiceRange).Select(p => p.ConnectedClient); var clients = _playerManager.GetPlayersInRange(pos, VoiceRange).Select(p => p.ConnectedClient);
@@ -90,6 +119,17 @@ namespace Content.Server.Chat
return; return;
} }
// Check if entity is a player
IPlayerSession playerSession = source.GetComponent<IActorComponent>().playerSession;
// Check if message exceeds the character limit
if (playerSession != null)
if (action.Length > MaxMessageLength)
{
DispatchServerMessage(playerSession, Loc.GetString(MaxLengthExceededMessage, MaxMessageLength));
return;
}
var pos = source.Transform.GridPosition; var pos = source.Transform.GridPosition;
var clients = _playerManager.GetPlayersInRange(pos, VoiceRange).Select(p => p.ConnectedClient); var clients = _playerManager.GetPlayersInRange(pos, VoiceRange).Select(p => p.ConnectedClient);
@@ -103,6 +143,13 @@ namespace Content.Server.Chat
public void SendOOC(IPlayerSession player, string message) public void SendOOC(IPlayerSession player, string message)
{ {
// Check if message exceeds the character limi
if (message.Length > MaxMessageLength)
{
DispatchServerMessage(player, Loc.GetString(MaxLengthExceededMessage, MaxMessageLength));
return;
}
var msg = _netManager.CreateNetMessage<MsgChatMessage>(); var msg = _netManager.CreateNetMessage<MsgChatMessage>();
msg.Channel = ChatChannel.OOC; msg.Channel = ChatChannel.OOC;
msg.Message = message; msg.Message = message;
@@ -114,6 +161,13 @@ namespace Content.Server.Chat
public void SendDeadChat(IPlayerSession player, string message) public void SendDeadChat(IPlayerSession player, string message)
{ {
// Check if message exceeds the character limit
if (message.Length > MaxMessageLength)
{
DispatchServerMessage(player, Loc.GetString(MaxLengthExceededMessage, MaxMessageLength));
return;
}
var clients = _playerManager.GetPlayersBy(x => x.AttachedEntity != null && x.AttachedEntity.HasComponent<GhostComponent>()).Select(p => p.ConnectedClient);; var clients = _playerManager.GetPlayersBy(x => x.AttachedEntity != null && x.AttachedEntity.HasComponent<GhostComponent>()).Select(p => p.ConnectedClient);;
var msg = _netManager.CreateNetMessage<MsgChatMessage>(); var msg = _netManager.CreateNetMessage<MsgChatMessage>();
@@ -126,7 +180,14 @@ namespace Content.Server.Chat
public void SendAdminChat(IPlayerSession player, string message) public void SendAdminChat(IPlayerSession player, string message)
{ {
if(!_conGroupController.CanCommand(player, "asay")) // Check if message exceeds the character limit
if (message.Length > MaxMessageLength)
{
DispatchServerMessage(player, Loc.GetString(MaxLengthExceededMessage, MaxMessageLength));
return;
}
if (!_conGroupController.CanCommand(player, "asay"))
{ {
SendOOC(player, message); SendOOC(player, message);
return; return;
@@ -149,5 +210,12 @@ namespace Content.Server.Chat
msg.MessageWrap = $"OOC: (D){sender}: {{0}}"; msg.MessageWrap = $"OOC: (D){sender}: {{0}}";
_netManager.ServerSendToAll(msg); _netManager.ServerSendToAll(msg);
} }
private void _onMaxLengthRequest(ChatMaxMsgLengthMessage msg)
{
var response = _netManager.CreateNetMessage<ChatMaxMsgLengthMessage>();
response.MaxMessageLength = MaxMessageLength;
_netManager.ServerSendMessage(response, msg.MsgChannel);
}
} }
} }

View File

@@ -0,0 +1,39 @@
using Lidgren.Network;
using Robust.Shared.Interfaces.Network;
using Robust.Shared.Network;
using System;
using System.Collections.Generic;
using System.Text;
namespace Content.Shared.Chat
{
/// <summary>
/// This message is sent by the server to let clients know what is the chat's character limit for this server.
/// It is first sent by the client as a request
/// </summary>
public sealed class ChatMaxMsgLengthMessage : NetMessage
{
#region REQUIRED
public const MsgGroups GROUP = MsgGroups.Command;
public const string NAME = nameof(ChatMaxMsgLengthMessage);
public ChatMaxMsgLengthMessage(INetChannel channel) : base(NAME, GROUP) { }
#endregion
/// <summary>
/// The max length a player-sent message can get
/// </summary>
public int MaxMessageLength { get; set; }
public override void ReadFromBuffer(NetIncomingMessage buffer)
{
MaxMessageLength = buffer.ReadInt32();
}
public override void WriteToBuffer(NetOutgoingMessage buffer)
{
buffer.Write(MaxMessageLength);
}
}
}