Skip to content

Commit

Permalink
fix(network): fix Examine packet parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
zhyupe committed Dec 5, 2023
1 parent 963ce7b commit 14a9833
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cafe.Matcha/Constant/LogType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public enum LogType
Telemetry,
ActorControlSelf,
InvalidPacket,

RawPacket,
Debug1,
#endif
}
Expand Down
5 changes: 3 additions & 2 deletions Cafe.Matcha/Network/NetworkMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ private void HandleMessage(byte[] message)
if (ToMatchaOpcode(BitConverter.ToUInt16(message, 18), out var opcode))
{
LogIncorrectPacketSize(opcode, message.Length);
Log.Packet(message);
}
#endif

Expand Down Expand Up @@ -634,12 +635,12 @@ private bool HandleMessageByOpcode(byte[] message)
}
else if (opcode == MatchaOpcode.Examine)
{
if (message.Length != 1016)
if (message.Length != 1032)
{
return false;
}

const int offset = 0x40;
const int offset = 0x50;
const int length = 0x28;
for (int slot = 0; slot < 14; ++slot)
{
Expand Down
30 changes: 28 additions & 2 deletions Cafe.Matcha/Utils/Log.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright (c) FFCafe. All rights reserved.
// Licensed under the AGPL-3.0 license. See LICENSE file in the project root for full license information.

using Cafe.Matcha.Constant;

namespace Cafe.Matcha.Utils
{
using System.Text;
using Cafe.Matcha.Constant;

internal class Log
{
public delegate void EventHandler(LogType type, char level, string message);
Expand Down Expand Up @@ -34,6 +35,31 @@ public static void Debug(LogType type, string message)
{
Add(type, 'D', message);
}

public static void Packet(byte[] byteArray) {
StringBuilder hexDump = new StringBuilder();
const int lineLength = 16;

for (int i = 0; i < byteArray.Length; i += lineLength)
{
hexDump.AppendFormat("{0:X8}: ", i);
for (int j = 0; j < lineLength; j++)
{
if (i + j >= byteArray.Length)
{
break;
}

byte b = byteArray[i + j];
hexDump.Append(b.ToString("X2"));
hexDump.Append(" ");
}

hexDump.AppendLine();
}

Add(LogType.RawPacket, 'D', hexDump.ToString());
}
#endif
}
}

0 comments on commit 14a9833

Please sign in to comment.