BurntToast/BurntToast/Filter.cs

77 lines
2.5 KiB
C#
Raw Normal View History

2021-04-06 14:47:32 +00:00
using System;
using System.Linq;
2021-09-20 18:26:42 +00:00
using Dalamud.Game.Gui.Toast;
2021-04-12 16:51:46 +00:00
using Dalamud.Game.Text;
2021-04-06 14:47:32 +00:00
using Dalamud.Game.Text.SeStringHandling;
2021-04-12 16:51:46 +00:00
using XivCommon.Functions;
2021-04-06 14:47:32 +00:00
namespace BurntToast {
2021-04-06 14:47:32 +00:00
public class Filter : IDisposable {
private BurntToast Plugin { get; }
internal Filter(BurntToast plugin) {
this.Plugin = plugin;
2021-09-20 18:26:42 +00:00
this.Plugin.ToastGui.Toast += this.OnToast;
this.Plugin.ToastGui.QuestToast += this.OnQuestToast;
this.Plugin.ToastGui.ErrorToast += this.OnErrorToast;
2021-04-12 16:51:46 +00:00
this.Plugin.Common.Functions.BattleTalk.OnBattleTalk += this.OnBattleTalk;
2021-04-06 14:47:32 +00:00
}
public void Dispose() {
2021-04-12 16:51:46 +00:00
this.Plugin.Common.Functions.BattleTalk.OnBattleTalk -= this.OnBattleTalk;
2021-09-20 18:26:42 +00:00
this.Plugin.ToastGui.ErrorToast -= this.OnErrorToast;
this.Plugin.ToastGui.QuestToast -= this.OnQuestToast;
this.Plugin.ToastGui.Toast -= this.OnToast;
2021-04-06 14:47:32 +00:00
}
2021-04-12 16:51:46 +00:00
private bool AnyMatches(string text) {
return this.Plugin.Config.Patterns.Any(regex => regex.IsMatch(text));
}
private void OnToast(ref SeString message, ref ToastOptions options, ref bool isHandled) {
this.DoFilter(message, ref isHandled);
}
private void OnErrorToast(ref SeString message, ref bool isHandled) {
this.DoFilter(message, ref isHandled);
}
private void OnQuestToast(ref SeString message, ref QuestToastOptions options, ref bool isHandled) {
this.DoFilter(message, ref isHandled);
}
private void DoFilter(SeString message, ref bool isHandled) {
2021-04-06 14:47:32 +00:00
if (isHandled) {
return;
}
2021-04-12 16:51:46 +00:00
if (this.AnyMatches(message.TextValue)) {
2021-04-06 14:47:32 +00:00
isHandled = true;
}
}
2021-04-12 16:51:46 +00:00
private void OnBattleTalk(ref SeString sender, ref SeString message, ref BattleTalkOptions options, ref bool isHandled) {
if (isHandled) {
return;
}
var text = message.TextValue;
var pattern = this.Plugin.Config.BattleTalkPatterns.Find(pattern => pattern.Pattern.IsMatch(text));
if (pattern == null) {
return;
}
isHandled = true;
if (pattern.ShowMessage) {
2023-09-29 01:13:37 +00:00
this.Plugin.ChatGui.Print(new XivChatEntry {
2021-04-12 16:51:46 +00:00
Type = (XivChatType) 68,
Name = sender.TextValue,
2021-09-20 18:26:42 +00:00
Message = message,
2021-04-12 16:51:46 +00:00
});
}
}
2021-04-06 14:47:32 +00:00
}
}