Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ChatMessage.cs
Go to the documentation of this file.
1using System;
2using System.IO;
3using System.Text;
5
6namespace Terraria.Chat;
7
8public sealed class ChatMessage
9{
10 public ChatCommandId CommandId { get; private set; }
11
12 public string Text { get; set; }
13
14 public bool IsConsumed { get; private set; }
15
16 public ChatMessage(string message)
17 {
19 Text = message;
20 IsConsumed = false;
21 }
22
23 private ChatMessage(string message, ChatCommandId commandId)
24 {
25 CommandId = commandId;
26 Text = message;
27 }
28
30 {
31 if (IsConsumed)
32 {
33 throw new InvalidOperationException("Message has already been consumed.");
34 }
36 writer.Write(Text);
37 }
38
40 {
41 if (IsConsumed)
42 {
43 throw new InvalidOperationException("Message has already been consumed.");
44 }
45 return 0 + CommandId.GetMaxSerializedSize() + (4 + Encoding.UTF8.GetByteCount(Text));
46 }
47
48 public static ChatMessage Deserialize(BinaryReader reader)
49 {
50 ChatCommandId commandId = ChatCommandId.Deserialize(reader);
51 return new ChatMessage(reader.ReadString(), commandId);
52 }
53
54 public void SetCommand(ChatCommandId commandId)
55 {
56 if (IsConsumed)
57 {
58 throw new InvalidOperationException("Message has already been consumed.");
59 }
60 CommandId = commandId;
61 }
62
63 public void SetCommand<T>() where T : IChatCommand
64 {
65 if (IsConsumed)
66 {
67 throw new InvalidOperationException("Message has already been consumed.");
68 }
69 CommandId = ChatCommandId.FromType<T>();
70 }
71
72 public void Consume()
73 {
74 IsConsumed = true;
75 }
76}
virtual string ReadString()
static Encoding UTF8
Definition Encoding.cs:526
ChatMessage(string message, ChatCommandId commandId)
static ChatMessage Deserialize(BinaryReader reader)
void Serialize(BinaryWriter writer)
void SetCommand(ChatCommandId commandId)
ChatMessage(string message)
void Serialize(BinaryWriter writer)
static ChatCommandId Deserialize(BinaryReader reader)