Skip to content

Commit 993ecb3

Browse files
committed
Stop re-encoding Text for chat gateway chatevents
1 parent 81e8257 commit 993ecb3

1 file changed

Lines changed: 44 additions & 24 deletions

File tree

src/Atlasd/Battlenet/Protocols/Game/ChatEvent.cs

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -176,75 +176,95 @@ public byte[] ToByteArray(ProtocolType.Types protocolType)
176176
case ProtocolType.Types.Chat_Alt1:
177177
case ProtocolType.Types.Chat_Alt2:
178178
{
179-
var buf = $"{1000 + EventId} ";
180179
var product = new byte[4];
181-
182180
Buffer.BlockCopy(Text, 0, product, 0, Math.Min(4, Text.Length));
183-
Array.Reverse(product); // "RATS" becomes "STAR", "STAR" becomes "RATS", etc.
181+
Array.Reverse(product); // "RATS" becomes "STAR", etc.
182+
183+
using var m = new MemoryStream(); // Expandable buffer
184+
using var w = new BinaryWriter(m);
185+
186+
w.Write($"{1000 + EventId} ");
184187

185188
switch (EventId)
186189
{
187190
case EventIds.EID_USERSHOW:
188-
case EventIds.EID_USERUPDATE:
189-
{
190-
buf += $"USER {Username} {Flags:X4} [{Encoding.UTF8.GetString(product)}]";
191-
break;
192-
}
193191
case EventIds.EID_USERJOIN:
192+
case EventIds.EID_USERUPDATE:
194193
{
195-
buf += $"JOIN {Username} {Flags:X4} [{Encoding.UTF8.GetString(product)}]";
194+
w.Write(EventId == EventIds.EID_USERJOIN ? "JOIN " : "USER ");
195+
w.Write(Username);
196+
w.Write($" {Flags:X4} ");
197+
w.Write(product);
196198
break;
197199
}
198200
case EventIds.EID_USERLEAVE:
199201
{
200-
buf += $"LEAVE {Username} {Flags:X4}";
202+
w.Write("LEAVE ");
203+
w.Write(Username);
204+
w.Write($" {Flags:X4}");
201205
break;
202206
}
203207
case EventIds.EID_WHISPERFROM:
204208
case EventIds.EID_WHISPERTO:
205209
{
206-
buf += $"WHISPER {Username} {Flags:X4} \"{Encoding.UTF8.GetString(Text)}\"";
210+
w.Write("WHISPER ");
211+
w.Write(Username);
212+
w.Write($" {Flags:X4} \"");
213+
w.Write(Text);
214+
w.Write('"');
207215
break;
208216
}
209217
case EventIds.EID_TALK:
218+
case EventIds.EID_EMOTE:
210219
{
211-
buf += $"TALK {Username} {Flags:X4} \"{Encoding.UTF8.GetString(Text)}\"";
220+
w.Write(EventId == EventIds.EID_EMOTE ? "EMOTE " : "TALK ");
221+
w.Write(Username);
222+
w.Write($" {Flags:X4} \"");
223+
w.Write(Text);
224+
w.Write('"');
212225
break;
213226
}
214227
case EventIds.EID_BROADCAST:
215228
{
216-
buf += $"BROADCAST \"{Encoding.UTF8.GetString(Text)}\"";
229+
w.Write("BROADCAST \"");
230+
w.Write(Text);
231+
w.Write('"');
217232
break;
218233
}
219234
case EventIds.EID_CHANNELJOIN:
220235
{
221-
buf += $"CHANNEL \"{Encoding.UTF8.GetString(Text)}\"";
236+
w.Write("CHANNEL \"");
237+
w.Write(Text);
238+
w.Write('"');
222239
break;
223240
}
224241
case EventIds.EID_INFO:
225242
{
226-
buf += $"INFO \"{Encoding.UTF8.GetString(Text)}\"";
243+
w.Write("INFO \"");
244+
w.Write(Text);
245+
w.Write('"');
227246
break;
228247
}
229248
case EventIds.EID_ERROR:
230249
{
231-
buf += $"ERROR \"{Encoding.UTF8.GetString(Text)}\"";
232-
break;
233-
}
234-
case EventIds.EID_EMOTE:
235-
{
236-
buf += $"EMOTE {Username} {Flags:X4} \"{Encoding.UTF8.GetString(Text)}\"";
250+
w.Write("ERROR \"");
251+
w.Write(Text);
252+
w.Write('"');
237253
break;
238254
}
239255
default:
240256
{
241-
buf += $"UNKNOWN {Username} {Flags:X4} \"{Encoding.UTF8.GetString(Text)}\"";
257+
w.Write("UNKNOWN ");
258+
w.Write(Username);
259+
w.Write($" {Flags:X4} \"");
260+
w.Write(Text);
261+
w.Write('"');
242262
break;
243263
}
244264
}
245265

246-
buf += Battlenet.Common.NewLine;
247-
return Encoding.UTF8.GetBytes(buf);
266+
w.Write(Battlenet.Common.NewLine);
267+
return m.GetBuffer();
248268
}
249269
default:
250270
throw new ProtocolNotSupportedException(protocolType, null, $"Unsupported protocol type [0x{(byte)protocolType:X2}]");

0 commit comments

Comments
 (0)