-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBetterChatBoxPro.cs
More file actions
103 lines (101 loc) · 3.5 KB
/
BetterChatBoxPro.cs
File metadata and controls
103 lines (101 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.Xna.Framework;
using MonoMod.RuntimeDetour;
using Terraria;
using Terraria.GameContent.UI.Chat;
using Terraria.ModLoader;
using Terraria.Localization;
using Terraria.UI.Chat;
using System.Linq;
using BetterChatBoxPro.System;
namespace BetterChatBoxPro
{
public class BetterChatBoxPro : Mod
{
public static int tab = 0;
public static List<ChatMessageContainer> ALL = null;
public static List<ChatMessageContainer> PLAYER = null;
public static List<ChatMessageContainer> NOPLAYER = null;
public static KeywordFilterConfig KeywordConfig = null;
private Hook addNewMessageHook;
private Hook newTextHook;
public override void Load()
{
var configType = Type.GetType("ChatImproverConfig");
if (configType != null)
{
var instanceField = configType.GetField("instance", BindingFlags.Static | BindingFlags.NonPublic);
var configInstance = instanceField?.GetValue(null);
var showCountProp = configType.GetProperty("showCount");
if (configInstance != null && showCountProp != null)
{
showCountProp.SetValue(configInstance, 30);
}
}
ALL = new List<ChatMessageContainer>();
PLAYER = new List<ChatMessageContainer>();
NOPLAYER = new List<ChatMessageContainer>();
KeywordConfig = new KeywordFilterConfig(this);
KeywordConfig.Initialize();
Logger.Info($"BetterChatBoxPro keyword file: {KeywordConfig.FilePath}");
var method = typeof(RemadeChatMonitor).GetMethod(
"AddNewMessage",
BindingFlags.Public | BindingFlags.Instance,
null,
new[] { typeof(string), typeof(Microsoft.Xna.Framework.Color), typeof(int) },
null
);
addNewMessageHook = new Hook(method, MyAddNewMessage);
method = typeof(Main).GetMethod("NewText", BindingFlags.Public | BindingFlags.Static, null, new[] { typeof(string), typeof(byte), typeof(byte), typeof(byte) }, null);
newTextHook = new Hook(method, MyNewText);
Logger.Info(Language.GetTextValue("Mods.BetterChatBoxPro.Log.Loaded"));
}
private void MyNewText(Action<string, byte, byte, byte> orig, string newText, byte R, byte G, byte B)
{
orig("[SYSTEM]" + newText, R, G, B);
}
private void MyAddNewMessage(Action<RemadeChatMonitor, string, Color, int> orig, RemadeChatMonitor self, string text, Color color, int widthLimitInPixels)
{
bool ifSystem = false;
string actualText = text;
if (text.StartsWith("[SYSTEM]"))
{
ifSystem = true;
actualText = text.Substring(8);
}
try
{
if (KeywordConfig != null && KeywordConfig.Matches(actualText))
{
var logContainer = new ChatMessageContainer();
logContainer.SetContents(actualText, color, widthLimitInPixels);
NOPLAYER.Insert(0, logContainer);
return;
}
var container = new ChatMessageContainer();
container.SetContents(actualText, color, widthLimitInPixels);
if (ifSystem)
NOPLAYER.Insert(0, container);
else
PLAYER.Insert(0, container);
ALL.Insert(0, container);
}
catch
{
// Ignore errors during container creation (e.g. during load)
}
if (tab == 0 || (tab == 1 && !ifSystem) || (tab == 2 && ifSystem))
orig(self, actualText, color, widthLimitInPixels);
}
public override void Unload()
{
newTextHook?.Dispose();
addNewMessageHook?.Dispose();
KeywordConfig?.Dispose();
KeywordConfig = null;
Logger.Info(Language.GetTextValue("Mods.BetterChatBoxPro.Log.Unloaded"));
}
}
}