Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
NetworkText.cs
Go to the documentation of this file.
1using System;
2using System.IO;
3using System.Text;
4
6
7public class NetworkText
8{
9 private enum Mode : byte
10 {
11 Literal,
14 }
15
16 public static readonly NetworkText Empty = FromLiteral("");
17
19
20 private string _text;
21
22 private Mode _mode;
23
24 private NetworkText(string text, Mode mode)
25 {
26 _text = text;
27 _mode = mode;
28 }
29
30 private static NetworkText[] ConvertSubstitutionsToNetworkText(object[] substitutions)
31 {
32 NetworkText[] array = new NetworkText[substitutions.Length];
33 for (int i = 0; i < substitutions.Length; i++)
34 {
35 NetworkText networkText = substitutions[i] as NetworkText;
36 if (networkText == null)
37 {
38 networkText = FromLiteral(substitutions[i].ToString());
39 }
40 array[i] = networkText;
41 }
42 return array;
43 }
44
45 public static NetworkText FromFormattable(string text, params object[] substitutions)
46 {
47 return new NetworkText(text, Mode.Formattable)
48 {
50 };
51 }
52
53 public static NetworkText FromLiteral(string text)
54 {
55 return new NetworkText(text, Mode.Literal);
56 }
57
58 public static NetworkText FromKey(string key, params object[] substitutions)
59 {
60 return new NetworkText(key, Mode.LocalizationKey)
61 {
63 };
64 }
65
67 {
68 int num = 0;
69 num++;
70 num += 4 + Encoding.UTF8.GetByteCount(_text);
71 if (_mode != 0)
72 {
73 num++;
74 for (int i = 0; i < _substitutions.Length; i++)
75 {
77 }
78 }
79 return num;
80 }
81
83 {
84 writer.Write((byte)_mode);
85 writer.Write(_text);
87 }
88
90 {
91 if (_mode != 0)
92 {
93 writer.Write((byte)_substitutions.Length);
94 for (int i = 0; i < (_substitutions.Length & 0xFF); i++)
95 {
97 }
98 }
99 }
100
102 {
103 Mode mode = (Mode)reader.ReadByte();
104 NetworkText networkText = new NetworkText(reader.ReadString(), mode);
105 networkText.DeserializeSubstitutionList(reader);
106 return networkText;
107 }
108
110 {
111 Mode mode = (Mode)reader.ReadByte();
112 NetworkText networkText = new NetworkText(reader.ReadString(), mode);
113 networkText.DeserializeSubstitutionList(reader);
114 if (mode != 0)
115 {
116 networkText.SetToEmptyLiteral();
117 }
118 return networkText;
119 }
120
122 {
123 if (_mode != 0)
124 {
125 _substitutions = new NetworkText[reader.ReadByte()];
126 for (int i = 0; i < _substitutions.Length; i++)
127 {
128 _substitutions[i] = Deserialize(reader);
129 }
130 }
131 }
132
133 private void SetToEmptyLiteral()
134 {
135 _mode = Mode.Literal;
136 _text = string.Empty;
137 _substitutions = null;
138 }
139
140 public override string ToString()
141 {
142 try
143 {
144 switch (_mode)
145 {
146 case Mode.Literal:
147 return _text;
148 case Mode.Formattable:
149 {
150 string text2 = _text;
151 object[] substitutions = _substitutions;
152 return string.Format(text2, substitutions);
153 }
154 case Mode.LocalizationKey:
155 {
156 string text = _text;
157 object[] substitutions = _substitutions;
158 return Language.GetTextValue(text, substitutions);
159 }
160 default:
161 return _text;
162 }
163 }
164 catch (Exception ex)
165 {
166 string.Concat(string.Concat("NetworkText.ToString() threw an exception.\n" + ToDebugInfoString(), "\n"), "Exception: ", ex);
168 }
169 return _text;
170 }
171
172 private string ToDebugInfoString(string linePrefix = "")
173 {
174 string text = string.Format("{0}Mode: {1}\n{0}Text: {2}\n", linePrefix, _mode, _text);
175 if (_mode == Mode.LocalizationKey)
176 {
177 text += $"{linePrefix}Localized Text: {Language.GetTextValue(_text)}\n";
178 }
179 if (_mode != 0)
180 {
181 for (int i = 0; i < _substitutions.Length; i++)
182 {
183 text += $"{linePrefix}Substitution {i}:\n";
184 text += _substitutions[i].ToDebugInfoString(linePrefix + "\t");
185 }
186 }
187 return text;
188 }
189}
virtual byte ReadByte()
virtual string ReadString()
static Encoding UTF8
Definition Encoding.cs:526
static string GetTextValue(string key)
Definition Language.cs:15
static NetworkText FromFormattable(string text, params object[] substitutions)
string ToDebugInfoString(string linePrefix="")
NetworkText(string text, Mode mode)
void SerializeSubstitutionList(BinaryWriter writer)
static NetworkText FromLiteral(string text)
void Serialize(BinaryWriter writer)
void DeserializeSubstitutionList(BinaryReader reader)
static NetworkText FromKey(string key, params object[] substitutions)
static NetworkText DeserializeLiteral(BinaryReader reader)
static NetworkText[] ConvertSubstitutionsToNetworkText(object[] substitutions)
static NetworkText Deserialize(BinaryReader reader)