Five sub-changes:
1. Windows-1252 codec switch (global). Every Encoding.ASCII call site
in src/AcDream.Core.Net/Messages/ -> Encoding.GetEncoding(1252).
Touched HearSpeech, ChatRequests, GameEvents, AppraiseInfoParser,
CharacterList, CreateObject, PlayerDescriptionParser, SocialActions.
New Encodings.cs module-init registers CodePagesEncodingProvider
(System.Text.Encoding.CodePages ships with .NET 10 SDK but isn't
auto-registered). Matches retail + holtburger; accented names
no longer round-trip-broken.
2. New parsers (opcodes confirmed against holtburger opcodes.rs):
- EmoteText (0x01E0) { u32 senderGuid, string16 senderName, string16 text }
- SoulEmote (0x01E2) same wire layout as EmoteText
- ServerMessage (0xF7E0) { string16 message, u32 chatType }
- PlayerKilled (0x019E) { string16 deathMessage, u32 victimGuid, u32 killerGuid }
Shared StringReader.cs has the CP1252 String16L primitive.
3. WorldSession dispatch. ProcessDatagram adds branches for the four
new top-level opcodes + fires session-level events (EmoteHeard,
SoulEmoteHeard, ServerMessageReceived, PlayerKilledReceived).
0x0295 SetTurbineChatChannels stubbed with TODO for parallel I.6.
4. GameEventWiring routes WeenieError + WeenieErrorWithString
(parsers existed but were unrouted) -> chat.OnWeenieError.
5. ChatLog adapters: Emote / SoulEmote ChatKind values, OnEmote,
OnSoulEmote, OnPlayerKilled, OnWeenieError. OnLocalSpeech now
substitutes empty sender -> "You" per holtburger client/messages.rs.
ChatVM.FormatEntry handles new kinds (asterisk + sender + text).
22 new tests covering parser round-trips + reject-bad-opcode +
ChatLog adapter coverage + Win-1252 round-trip with non-ASCII chars.
Solution total: 881 green (210->225 in Core.Net.Tests, 606->613 in Core.Tests).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
156 lines
6 KiB
C#
156 lines
6 KiB
C#
using System;
|
|
using System.Buffers.Binary;
|
|
using System.Text;
|
|
using AcDream.Core.Net.Messages;
|
|
using Xunit;
|
|
|
|
namespace AcDream.Core.Net.Tests.Messages;
|
|
|
|
public sealed class ChatTests
|
|
{
|
|
[Fact]
|
|
public void BuildTalk_EmitsOpcodeAndString16L()
|
|
{
|
|
byte[] body = ChatRequests.BuildTalk(gameActionSequence: 3, message: "hi");
|
|
|
|
Assert.Equal(ChatRequests.TalkOpcode,
|
|
BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(8)));
|
|
|
|
// Verify the string16L starts at offset 12.
|
|
ushort len = BinaryPrimitives.ReadUInt16LittleEndian(body.AsSpan(12));
|
|
Assert.Equal(2, len);
|
|
Assert.Equal("hi", Encoding.ASCII.GetString(body.AsSpan(14, 2)));
|
|
|
|
// Record size = 2+2 = 4, no padding needed.
|
|
Assert.Equal(16, body.Length);
|
|
}
|
|
|
|
[Fact]
|
|
public void BuildTalk_EmitsPadding_WhenMessageLengthRequiresIt()
|
|
{
|
|
byte[] body = ChatRequests.BuildTalk(gameActionSequence: 3, message: "h");
|
|
// 2+1=3 bytes record → pad 1 byte.
|
|
// Total body = 12 (envelope) + 4 (str16L aligned) = 16.
|
|
Assert.Equal(16, body.Length);
|
|
}
|
|
|
|
[Fact]
|
|
public void BuildTell_IncludesBothStrings()
|
|
{
|
|
byte[] body = ChatRequests.BuildTell(
|
|
gameActionSequence: 5, targetName: "Alice", message: "hey");
|
|
|
|
Assert.Equal(ChatRequests.TellOpcode,
|
|
BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(8)));
|
|
|
|
int pos = 12;
|
|
ushort len1 = BinaryPrimitives.ReadUInt16LittleEndian(body.AsSpan(pos));
|
|
Assert.Equal(5, len1);
|
|
Assert.Equal("Alice", Encoding.ASCII.GetString(body.AsSpan(pos + 2, 5)));
|
|
|
|
// "Alice" record = 2+5=7, pad 1 → advance by 8.
|
|
pos += 8;
|
|
ushort len2 = BinaryPrimitives.ReadUInt16LittleEndian(body.AsSpan(pos));
|
|
Assert.Equal(3, len2);
|
|
Assert.Equal("hey", Encoding.ASCII.GetString(body.AsSpan(pos + 2, 3)));
|
|
}
|
|
|
|
[Fact]
|
|
public void BuildChatChannel_IncludesChannelId()
|
|
{
|
|
byte[] body = ChatRequests.BuildChatChannel(
|
|
gameActionSequence: 1, channelId: 42, message: "tell me the good dungeons");
|
|
|
|
Assert.Equal(ChatRequests.ChatChannelOpcode,
|
|
BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(8)));
|
|
Assert.Equal(42u,
|
|
BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(12)));
|
|
}
|
|
|
|
[Fact]
|
|
public void HearSpeech_TryParse_LocalRoundTrip()
|
|
{
|
|
// Build a 0x02BB message and re-parse it.
|
|
byte[] talkBody = ChatRequests.BuildTalk(gameActionSequence: 0, message: "hello");
|
|
|
|
// Now synthesize the inbound HearSpeech format.
|
|
byte[] msg = PackString16L("hello");
|
|
byte[] sender = PackString16L("Alice");
|
|
byte[] inbound = new byte[4 + msg.Length + sender.Length + 8];
|
|
int pos = 0;
|
|
BinaryPrimitives.WriteUInt32LittleEndian(inbound, HearSpeech.LocalOpcode);
|
|
pos += 4;
|
|
Array.Copy(msg, 0, inbound, pos, msg.Length); pos += msg.Length;
|
|
Array.Copy(sender, 0, inbound, pos, sender.Length); pos += sender.Length;
|
|
BinaryPrimitives.WriteUInt32LittleEndian(inbound.AsSpan(pos), 0xCAFEu); pos += 4;
|
|
BinaryPrimitives.WriteUInt32LittleEndian(inbound.AsSpan(pos), 0x0B); pos += 4; // Speech
|
|
|
|
var parsed = HearSpeech.TryParse(inbound);
|
|
Assert.NotNull(parsed);
|
|
Assert.Equal("hello", parsed!.Value.Text);
|
|
Assert.Equal("Alice", parsed.Value.SenderName);
|
|
Assert.Equal(0xCAFEu, parsed.Value.SenderGuid);
|
|
Assert.Equal(0x0Bu, parsed.Value.ChatType);
|
|
Assert.False(parsed.Value.IsRanged);
|
|
}
|
|
|
|
[Fact]
|
|
public void HearSpeech_TryParse_RangedFlag()
|
|
{
|
|
byte[] msg = PackString16L("X");
|
|
byte[] sender = PackString16L("Y");
|
|
byte[] inbound = new byte[4 + msg.Length + sender.Length + 8];
|
|
BinaryPrimitives.WriteUInt32LittleEndian(inbound, HearSpeech.RangedOpcode);
|
|
int pos = 4;
|
|
Array.Copy(msg, 0, inbound, pos, msg.Length); pos += msg.Length;
|
|
Array.Copy(sender, 0, inbound, pos, sender.Length); pos += sender.Length;
|
|
BinaryPrimitives.WriteUInt32LittleEndian(inbound.AsSpan(pos), 0); pos += 4;
|
|
BinaryPrimitives.WriteUInt32LittleEndian(inbound.AsSpan(pos), 0); pos += 4;
|
|
|
|
var parsed = HearSpeech.TryParse(inbound);
|
|
Assert.NotNull(parsed);
|
|
Assert.True(parsed!.Value.IsRanged);
|
|
}
|
|
|
|
[Fact]
|
|
public void HearSpeech_TryParse_WrongOpcode_ReturnsNull()
|
|
{
|
|
byte[] body = new byte[16];
|
|
BinaryPrimitives.WriteUInt32LittleEndian(body, 0xDEADBEEFu);
|
|
Assert.Null(HearSpeech.TryParse(body));
|
|
}
|
|
|
|
[Fact]
|
|
public void HearSpeech_TryParse_PreservesWindows1252_RoundTrip()
|
|
{
|
|
// Phase I.5: ASCII would munge non-ASCII bytes into '?'. CP1252
|
|
// round-trips them. The high-byte 0xE9 = 'é' in Latin-1/CP1252.
|
|
byte[] msg = PackString16L("Café");
|
|
byte[] sender = PackString16L("Élise");
|
|
byte[] inbound = new byte[4 + msg.Length + sender.Length + 8];
|
|
int pos = 0;
|
|
BinaryPrimitives.WriteUInt32LittleEndian(inbound, HearSpeech.LocalOpcode);
|
|
pos += 4;
|
|
Array.Copy(msg, 0, inbound, pos, msg.Length); pos += msg.Length;
|
|
Array.Copy(sender, 0, inbound, pos, sender.Length); pos += sender.Length;
|
|
BinaryPrimitives.WriteUInt32LittleEndian(inbound.AsSpan(pos), 0u); pos += 4;
|
|
BinaryPrimitives.WriteUInt32LittleEndian(inbound.AsSpan(pos), 0u);
|
|
|
|
var parsed = HearSpeech.TryParse(inbound);
|
|
Assert.NotNull(parsed);
|
|
Assert.Equal("Café", parsed!.Value.Text);
|
|
Assert.Equal("Élise", parsed.Value.SenderName);
|
|
}
|
|
|
|
private static byte[] PackString16L(string s)
|
|
{
|
|
// Test helper now uses CP1252 to match the production codec.
|
|
byte[] data = Encoding.GetEncoding(1252).GetBytes(s);
|
|
int recordSize = 2 + data.Length;
|
|
int padding = (4 - (recordSize & 3)) & 3;
|
|
byte[] result = new byte[recordSize + padding];
|
|
BinaryPrimitives.WriteUInt16LittleEndian(result, (ushort)data.Length);
|
|
Array.Copy(data, 0, result, 2, data.Length);
|
|
return result;
|
|
}
|
|
}
|