Skip to content

Commit

Permalink
Changed chat message type to Binary instead of unreadable string
Browse files Browse the repository at this point in the history
  • Loading branch information
sven-n committed Oct 9, 2023
1 parent 26f6919 commit 6c874bf
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 42 deletions.
2 changes: 1 addition & 1 deletion docs/Packets/C1-04-ChatMessage.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ The client will show the message.
| 2 | 1 | Byte | 0x04 | Packet header - packet type identifier |
| 3 | 1 | Byte | | SenderIndex |
| 4 | 1 | Byte | | MessageLength |
| 5 | | String | | Message; The message. It's "encrypted" with the 3-byte XOR key (FC CF AB). |
| 5 | | Binary | | Message; The message. It's "encrypted" with the 3-byte XOR key (FC CF AB). |
9 changes: 5 additions & 4 deletions src/ChatServer/ChatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,12 @@ public async ValueTask SendMessageAsync(byte senderId, string message)

int WritePacket()
{
var length = ChatMessageRef.GetRequiredSize(message);
var messageLength = (byte)Encoding.UTF8.GetByteCount(message);
var length = ChatMessageRef.GetRequiredSize(messageLength);
var packet = new ChatMessageRef(connection.Output.GetSpan(length)[..length]);
packet.SenderIndex = senderId;
packet.MessageLength = (byte)Encoding.UTF8.GetByteCount(message);
packet.Message = message;
packet.MessageLength = messageLength;
Encoding.UTF8.GetBytes(message, packet.Message);
MessageEncryptor.Encrypt(packet);
return length;
}
Expand Down Expand Up @@ -246,7 +247,7 @@ private async ValueTask ReadPacketAsync(ReadOnlySequence<byte> sequence)

private bool CheckMessage(Memory<byte> packet)
{
return packet.Length > 4 && (packet.Span[4] + 5) == packet.Length;
return packet.Length > 4 && (packet.Span[4] + 5) <= packet.Length;
}

private async ValueTask AuthenticateAsync(Memory<byte> packet)
Expand Down
4 changes: 2 additions & 2 deletions src/Network/MUnique.OpenMU.Network.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
MUnique.OpenMU.Network contains all what's required to connect from and to a MU Online game, connect or chat server. It implements the MU Online network protocol.
It also contains several the encryption algorithms and keys which were used until a few years ago up to Season 6 Episode 3.
</Description>
<PackageVersion>0.7.24</PackageVersion>
<Version>0.7.24</Version>
<PackageVersion>0.7.25</PackageVersion>
<Version>0.7.25</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
18 changes: 6 additions & 12 deletions src/Network/Packets/ChatServer/ChatServerPackets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -559,10 +559,9 @@ public byte MessageLength
/// <summary>
/// Gets or sets the message. It's "encrypted" with the 3-byte XOR key (FC CF AB).
/// </summary>
public string Message
public Span<byte> Message
{
get => this._data.Span.ExtractString(5, this._data.Length - 5, System.Text.Encoding.UTF8);
set => this._data.Slice(5).Span.WriteString(value, System.Text.Encoding.UTF8);
get => this._data.Slice(5).Span;
}

/// <summary>
Expand All @@ -580,16 +579,11 @@ public string Message
public static implicit operator Memory<byte>(ChatMessage packet) => packet._data;

/// <summary>
/// Calculates the size of the packet for the specified field content.
/// Calculates the size of the packet for the specified length of <see cref="Message"/>.
/// </summary>
/// <param name="content">The content of the variable 'Message' field from which the size will be calculated.</param>
public static int GetRequiredSize(string content) => System.Text.Encoding.UTF8.GetByteCount(content) + 1 + 5;

/// <summary>
/// Calculates the size of the packet for the specified field content.
/// </summary>
/// <param name="contentLength">The content length in bytes of the variable 'Message' field from which the size will be calculated.</param>
public static int GetRequiredSize(int contentLength) => contentLength + 1 + 5;
/// <param name="messageLength">The length in bytes of <see cref="Message"/> on which the required size depends.</param>

public static int GetRequiredSize(int messageLength) => messageLength + 5;
}


Expand Down
2 changes: 1 addition & 1 deletion src/Network/Packets/ChatServer/ChatServerPackets.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
</Field>
<Field>
<Index>5</Index>
<Type>String</Type>
<Type>Binary</Type>
<Name>Message</Name>
<Description>The message. It's "encrypted" with the 3-byte XOR key (FC CF AB).</Description>
</Field>
Expand Down
18 changes: 6 additions & 12 deletions src/Network/Packets/ChatServer/ChatServerPacketsRef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -559,10 +559,9 @@ public byte MessageLength
/// <summary>
/// Gets or sets the message. It's "encrypted" with the 3-byte XOR key (FC CF AB).
/// </summary>
public string Message
public Span<byte> Message
{
get => this._data.ExtractString(5, this._data.Length - 5, System.Text.Encoding.UTF8);
set => this._data.Slice(5).WriteString(value, System.Text.Encoding.UTF8);
get => this._data.Slice(5);
}

/// <summary>
Expand All @@ -580,16 +579,11 @@ public string Message
public static implicit operator Span<byte>(ChatMessageRef packet) => packet._data;

/// <summary>
/// Calculates the size of the packet for the specified field content.
/// Calculates the size of the packet for the specified length of <see cref="Message"/>.
/// </summary>
/// <param name="content">The content of the variable 'Message' field from which the size will be calculated.</param>
public static int GetRequiredSize(string content) => System.Text.Encoding.UTF8.GetByteCount(content) + 1 + 5;

/// <summary>
/// Calculates the size of the packet for the specified field content.
/// </summary>
/// <param name="contentLength">The content length in bytes of the variable 'Message' field from which the size will be calculated.</param>
public static int GetRequiredSize(int contentLength) => contentLength + 1 + 5;
/// <param name="messageLength">The length in bytes of <see cref="Message"/> on which the required size depends.</param>

public static int GetRequiredSize(int messageLength) => messageLength + 5;
}


Expand Down
6 changes: 3 additions & 3 deletions src/Network/Packets/ChatServer/ConnectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ int WritePacket()
/// Is sent by the server when: This packet is sent by the server after another chat client sent a message to the current chat room.
/// Causes reaction on client side: The client will show the message.
/// </remarks>
public static async ValueTask SendChatMessageAsync(this IConnection? connection, byte @senderIndex, byte @messageLength, string @message)
public static async ValueTask SendChatMessageAsync(this IConnection? connection, byte @senderIndex, byte @messageLength, Memory<byte> @message)
{
if (connection is null)
{
Expand All @@ -163,11 +163,11 @@ public static async ValueTask SendChatMessageAsync(this IConnection? connection,

int WritePacket()
{
var length = ChatMessageRef.GetRequiredSize(message);
var length = ChatMessageRef.GetRequiredSize(message.Length);
var packet = new ChatMessageRef(connection.Output.GetSpan(length)[..length]);
packet.SenderIndex = @senderIndex;
packet.MessageLength = @messageLength;
packet.Message = @message;
@message.Span.CopyTo(packet.Message);

return packet.Header.Length;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Network/Packets/MUnique.OpenMU.Network.Packets.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
Expand All @@ -16,8 +16,8 @@
<PackageTags>MUnique OpenMU MUOnline Network Packets</PackageTags>
<PackageId>MUnique.OpenMU.Network.Packets</PackageId>
<Description>This package contains message structs for the MMORPG "MU Online", which are primarily focused on the english version of Season 6 Episode 3.</Description>
<PackageVersion>0.7.24</PackageVersion>
<Version>0.7.24</Version>
<PackageVersion>0.7.25</PackageVersion>
<Version>0.7.25</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
4 changes: 2 additions & 2 deletions src/PlugIns/MUnique.OpenMU.PlugIns.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
<Description>
MUnique.OpenMU.PlugIns contains all what's required to create plugins extension points and own plugins for dependent applications.
</Description>
<PackageVersion>0.7.24</PackageVersion>
<Version>0.7.24</Version>
<PackageVersion>0.7.25</PackageVersion>
<Version>0.7.25</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
4 changes: 2 additions & 2 deletions src/SharedAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("0.7.24.0")]
[assembly: AssemblyFileVersion("0.7.24.0")]
[assembly: AssemblyVersion("0.7.25.0")]
[assembly: AssemblyFileVersion("0.7.25.0")]

0 comments on commit 6c874bf

Please sign in to comment.