-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatMessage.cs
76 lines (64 loc) · 1.56 KB
/
ChatMessage.cs
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
using System;
using System.IO;
using System.Text;
using Terraria.Chat.Commands;
namespace Terraria.Chat;
public sealed class ChatMessage
{
public ChatCommandId CommandId { get; private set; }
public string Text { get; set; }
public bool IsConsumed { get; private set; }
public ChatMessage(string message)
{
CommandId = ChatCommandId.FromType<SayChatCommand>();
Text = message;
IsConsumed = false;
}
private ChatMessage(string message, ChatCommandId commandId)
{
CommandId = commandId;
Text = message;
}
public void Serialize(BinaryWriter writer)
{
if (IsConsumed)
{
throw new InvalidOperationException("Message has already been consumed.");
}
CommandId.Serialize(writer);
writer.Write(Text);
}
public int GetMaxSerializedSize()
{
if (IsConsumed)
{
throw new InvalidOperationException("Message has already been consumed.");
}
return 0 + CommandId.GetMaxSerializedSize() + (4 + Encoding.UTF8.GetByteCount(Text));
}
public static ChatMessage Deserialize(BinaryReader reader)
{
ChatCommandId commandId = ChatCommandId.Deserialize(reader);
return new ChatMessage(reader.ReadString(), commandId);
}
public void SetCommand(ChatCommandId commandId)
{
if (IsConsumed)
{
throw new InvalidOperationException("Message has already been consumed.");
}
CommandId = commandId;
}
public void SetCommand<T>() where T : IChatCommand
{
if (IsConsumed)
{
throw new InvalidOperationException("Message has already been consumed.");
}
CommandId = ChatCommandId.FromType<T>();
}
public void Consume()
{
IsConsumed = true;
}
}