Skip to content

Commit

Permalink
Filter curses
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanmoffat committed May 11, 2022
1 parent b36d0e2 commit 4b1d2e6
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 25 deletions.
3 changes: 2 additions & 1 deletion EOLib.Localization/Services/EDFLoaderService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AutomaticTypeMapper;
using EOLib.IO.Services;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -68,7 +69,7 @@ private IEDFFile LoadCurseFilter(string fileName)
foreach (string encoded in lines)
{
string decoded = DecodeDatString(encoded, DataFiles.CurseFilter);
string[] curses = decoded.Split(':');
string[] curses = decoded.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string curse in curses)
data.Add(i++, curse);
}
Expand Down
20 changes: 15 additions & 5 deletions EOLib/Domain/Chat/ChatActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ public class ChatActions : IChatActions
private readonly IPacketSendService _packetSendService;
private readonly ILocalCommandHandler _localCommandHandler;
private readonly IChatProcessor _chatProcessor;
private readonly IConfigurationProvider _configurationProvider;

public ChatActions(IChatRepository chatRepository,
ICharacterProvider characterProvider,
IChatTypeCalculator chatTypeCalculator,
IChatPacketBuilder chatPacketBuilder,
IPacketSendService packetSendService,
ILocalCommandHandler localCommandHandler,
IChatProcessor chatProcessor)
IChatProcessor chatProcessor,
IConfigurationProvider configurationProvider)
{
_chatRepository = chatRepository;
_characterProvider = characterProvider;
Expand All @@ -35,16 +37,17 @@ public ChatActions(IChatRepository chatRepository,
_packetSendService = packetSendService;
_localCommandHandler = localCommandHandler;
_chatProcessor = chatProcessor;
_configurationProvider = configurationProvider;
}

public string SendChatToServer(string chat, string targetCharacter)
public (bool, string) SendChatToServer(string chat, string targetCharacter)
{
var chatType = _chatTypeCalculator.CalculateChatType(chat);

if (chatType == ChatType.Command)
{
if (HandleCommand(chat))
return chat;
return (true, chat);

//treat unhandled command as local chat
chatType = ChatType.Local;
Expand All @@ -59,6 +62,13 @@ public string SendChatToServer(string chat, string targetCharacter)
}

chat = _chatProcessor.RemoveFirstCharacterIfNeeded(chat, chatType, targetCharacter);
var (ok, filtered) = _chatProcessor.FilterCurses(chat);
if (!ok)
{
return (ok, filtered);
}

chat = filtered;

if (_characterProvider.MainCharacter.RenderProperties.IsDrunk)
chat = _chatProcessor.MakeDrunk(chat);
Expand All @@ -68,7 +78,7 @@ public string SendChatToServer(string chat, string targetCharacter)

AddChatForLocalDisplay(chatType, chat, targetCharacter);

return chat;
return (ok, chat);
}

public void SetHearWhispers(bool whispersEnabled)
Expand Down Expand Up @@ -147,7 +157,7 @@ private void AddChatForLocalDisplay(ChatType chatType, string chat, string targe

public interface IChatActions
{
string SendChatToServer(string chat, string targetCharacter);
(bool Ok, string Processed) SendChatToServer(string chat, string targetCharacter);

void SetHearWhispers(bool whispersEnabled);

Expand Down
32 changes: 19 additions & 13 deletions EOLib/Domain/Chat/ChatData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@ namespace EOLib.Domain.Chat
[Record(Features.ObjectEquals | Features.ToString)]
public sealed partial class ChatData
{
public ChatTab Tab { get; }
public ChatTab Tab { get; private set; }

public ChatIcon Icon { get; }
public ChatIcon Icon { get; private set; }

public string Who { get; }
public string Who { get; private set; }

public string Message { get; }
public string Message { get; } // making this get-only makes it the only property that generates a .With method

public ChatColor ChatColor { get; }
public ChatColor ChatColor { get; private set; }

public DateTime ChatTime { get; }
public DateTime ChatTime { get; private set; }

public bool Log { get; }
public bool Log { get; private set; }

public ChatData(ChatTab tab, string who,
string message,
ChatIcon icon = ChatIcon.None,
ChatColor color = ChatColor.Default,
bool log = true)
public ChatData(ChatTab tab,
string who,
string message,
ChatIcon icon = ChatIcon.None,
ChatColor chatColor = ChatColor.Default,
bool log = true)
{
if (who == null)
who = "";
Expand All @@ -38,10 +39,15 @@ public ChatData(ChatTab tab, string who,
Icon = icon;
Who = who;
Message = message;
ChatColor = color;
ChatColor = chatColor;
Log = log;

ChatTime = DateTime.Now;
}

public ChatData WithMessage(string message)
{
return new ChatData(Tab, Who, message, Icon, ChatColor, Log);
}
}
}
41 changes: 41 additions & 0 deletions EOLib/Domain/Chat/ChatProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Linq;
using System.Text;
using AutomaticTypeMapper;
using EOLib.Config;
using EOLib.Localization;

namespace EOLib.Domain.Chat
{
Expand All @@ -10,6 +12,16 @@ public class ChatProcessor : IChatProcessor
{
private readonly Random _random = new Random();

private readonly IDataFileProvider _dataFileProvider;
private readonly IConfigurationProvider _configurationProvider;

public ChatProcessor(IDataFileProvider dataFileProvider,
IConfigurationProvider configurationProvider)
{
_dataFileProvider = dataFileProvider;
_configurationProvider = configurationProvider;
}

public string RemoveFirstCharacterIfNeeded(string chat, ChatType chatType, string targetCharacter)
{
switch (chatType)
Expand Down Expand Up @@ -42,6 +54,8 @@ public string MakeDrunk(string input)
{
// implementation from Phorophor::notepad (thanks Blo)
// https://discord.com/channels/723989119503696013/785190349026492437/791376941822246953

// todo: make this use the authentic algorithm here: https://discord.com/channels/723989119503696013/787685796055482368/945700924536553544
var ret = new StringBuilder();

foreach (var c in input)
Expand All @@ -61,12 +75,39 @@ public string MakeDrunk(string input)

return ret.ToString();
}

public (bool, string) FilterCurses(string input)
{
if (_configurationProvider.CurseFilterEnabled || _configurationProvider.StrictFilterEnabled)
{
foreach (var curse in _dataFileProvider.DataFiles[DataFiles.CurseFilter].Data.Values)
{
var index = input.IndexOf(curse, StringComparison.OrdinalIgnoreCase);
if (index >= 0)
{
if (_configurationProvider.CurseFilterEnabled)
{
input = input.Remove(index, curse.Length);
input = input.Insert(index, "****");
}
else if (_configurationProvider.StrictFilterEnabled)
{
return (false, string.Empty);
}
}
}
}

return (true, input);
}
}

public interface IChatProcessor
{
string RemoveFirstCharacterIfNeeded(string input, ChatType chatType, string targetCharacter);

string MakeDrunk(string input);

(bool ShowChat, string FilteredMessage) FilterCurses(string input);
}
}
8 changes: 6 additions & 2 deletions EOLib/Domain/Chat/ChatRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class ChatRepository : IChatRepository, IChatProvider, IResettable
{
private readonly IConfigurationProvider _configurationProvider;
private readonly IChatLoggerProvider _chatLoggerProvider;
private readonly IChatProcessor _chatProcessor;

public IReadOnlyDictionary<ChatTab, IList<ChatData>> AllChat { get; private set; }

Expand All @@ -47,18 +48,21 @@ IReadOnlyDictionary<ChatTab, IReadOnlyList<ChatData>> IChatProvider.AllChat
public string PMTarget2 { get; set; }

public ChatRepository(IConfigurationProvider configurationProvider,
IChatLoggerProvider chatLoggerProvider)
IChatLoggerProvider chatLoggerProvider,
IChatProcessor chatProcessor)
{
_configurationProvider = configurationProvider;
_chatLoggerProvider = chatLoggerProvider;
_chatProcessor = chatProcessor;

ResetState();
}

public void ResetState()
{
var chat = new Dictionary<ChatTab, IList<ChatData>>();
foreach (var tab in (ChatTab[]) Enum.GetValues(typeof(ChatTab)))
chat.Add(tab, new LoggingList(_configurationProvider, _chatLoggerProvider));
chat.Add(tab, new LoggingList(_configurationProvider, _chatLoggerProvider, _chatProcessor));

AllChat = chat;
}
Expand Down
18 changes: 16 additions & 2 deletions EOLib/Domain/Chat/LoggingList.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EOLib.Config;
using EOLib.Localization;
using System;
using System.Collections;
using System.Collections.Generic;
Expand All @@ -9,28 +10,41 @@ internal class LoggingList : IList<ChatData>, IReadOnlyList<ChatData>
{
private readonly IConfigurationProvider _configurationProvider;
private readonly IChatLoggerProvider _chatLoggerProvider;
private readonly IChatProcessor _chatProcessor;

private readonly List<ChatData> _l;

public LoggingList(IConfigurationProvider configurationProvider,
IChatLoggerProvider chatLoggerProvider)
IChatLoggerProvider chatLoggerProvider,
IChatProcessor chatProcessor)
{
_configurationProvider = configurationProvider;
_chatLoggerProvider = chatLoggerProvider;
_chatProcessor = chatProcessor;

_l = new List<ChatData>();
}

public LoggingList(IConfigurationProvider configurationProvider,
IChatLoggerProvider chatLoggerProvider,
IChatProcessor chatProcessor,
IList<ChatData> source)
: this(configurationProvider, chatLoggerProvider)
: this(configurationProvider, chatLoggerProvider, chatProcessor)
{
_l = new List<ChatData>(source);
}

public void Add(ChatData item)
{
if (_configurationProvider.CurseFilterEnabled || _configurationProvider.StrictFilterEnabled)
{
var (ok, filtered) = _chatProcessor.FilterCurses(item.Message);
if (!ok)
return;

item = item.WithMessage(filtered);
}

((ICollection<ChatData>)_l).Add(item);

if (_configurationProvider.LogChatToFile && item.Log)
Expand Down
16 changes: 14 additions & 2 deletions EndlessClient/Controllers/ChatController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
using EndlessClient.ControlSets;
using EndlessClient.Dialogs.Actions;
using EndlessClient.GameExecution;
using EndlessClient.HUD;
using EndlessClient.HUD.Chat;
using EndlessClient.HUD.Controls;
using EndlessClient.UIControls;
using EOLib.Domain.Chat;
using EOLib.Localization;
using EOLib.Net;
using EOLib.Net.Communication;

Expand All @@ -19,18 +21,21 @@ public class ChatController : IChatController
private readonly IChatActions _chatActions;
private readonly IPrivateMessageActions _privateMessageActions;
private readonly IChatBubbleActions _chatBubbleActions;
private readonly IStatusLabelSetter _statusLabelSetter;
private readonly IHudControlProvider _hudControlProvider;

public ChatController(IChatTextBoxActions chatTextBoxActions,
IChatActions chatActions,
IPrivateMessageActions privateMessageActions,
IChatBubbleActions chatBubbleActions,
IStatusLabelSetter statusLabelSetter,
IHudControlProvider hudControlProvider)
{
_chatTextBoxActions = chatTextBoxActions;
_chatActions = chatActions;
_privateMessageActions = privateMessageActions;
_chatBubbleActions = chatBubbleActions;
_statusLabelSetter = statusLabelSetter;
_hudControlProvider = hudControlProvider;
}

Expand All @@ -39,11 +44,18 @@ public void SendChatAndClearTextBox()
var localTypedText = ChatTextBox.Text;
var targetCharacter = _privateMessageActions.GetTargetCharacter(localTypedText);

var updatedChat = _chatActions.SendChatToServer(localTypedText, targetCharacter);
var (ok, updatedChat) = _chatActions.SendChatToServer(localTypedText, targetCharacter);

_chatTextBoxActions.ClearChatText();

_chatBubbleActions.ShowChatBubbleForMainCharacter(updatedChat);
if (ok)
{
_chatBubbleActions.ShowChatBubbleForMainCharacter(updatedChat);
}
else
{
_statusLabelSetter.SetStatusLabel(EOResourceID.STATUS_LABEL_TYPE_WARNING, EOResourceID.YOUR_MIND_PREVENTS_YOU_TO_SAY);
}
}

public void SelectChatTextBox()
Expand Down

0 comments on commit 4b1d2e6

Please sign in to comment.