Skip to content

Commit

Permalink
添加try-catch捕获解包错误
Browse files Browse the repository at this point in the history
  • Loading branch information
ACaiCat committed Jan 27, 2025
1 parent ad0543e commit 8b18d63
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/CaiPacketDebug/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,18 @@ private void NetMessageOnInvokeSendBytes(Hooks.NetMessage.orig_InvokeSendBytes o
{
using MemoryStream memoryStream = new(data);
using BinaryReader reader = new(memoryStream);
var packet = this._clientPacketSerializer.Deserialize(reader);

Packet packet = null;

Check warning on line 90 in src/CaiPacketDebug/Plugin.cs

View workflow job for this annotation

GitHub Actions / 构建插件

Converting null literal or possible null value to non-nullable type.

Check warning on line 90 in src/CaiPacketDebug/Plugin.cs

View workflow job for this annotation

GitHub Actions / 构建插件

Converting null literal or possible null value to non-nullable type.

Check warning on line 90 in src/CaiPacketDebug/Plugin.cs

View workflow job for this annotation

GitHub Actions / 构建插件

Converting null literal or possible null value to non-nullable type.

Check warning on line 90 in src/CaiPacketDebug/Plugin.cs

View workflow job for this annotation

GitHub Actions / 构建插件

Converting null literal or possible null value to non-nullable type.
try
{
packet = this._clientPacketSerializer.Deserialize(reader);
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write($"[S->C({remoteclient})] 解析数据包时出错:" + ex);
Console.ResetColor();
return;
}
if (Config.Instance.ServerToClient.ExcludePackets.Contains((int) packet.Type))
{
return;
Expand All @@ -113,9 +123,19 @@ private bool MessageBufferOnInvokeGetData(Hooks.MessageBuffer.orig_InvokeGetData
if (this._clientToServerDebug)
{
instance.ResetReader();

var packet = this._serverPacketSerializer.Deserialize(instance.reader);

Packet packet = null;

Check warning on line 126 in src/CaiPacketDebug/Plugin.cs

View workflow job for this annotation

GitHub Actions / 构建插件

Converting null literal or possible null value to non-nullable type.

Check warning on line 126 in src/CaiPacketDebug/Plugin.cs

View workflow job for this annotation

GitHub Actions / 构建插件

Converting null literal or possible null value to non-nullable type.

Check warning on line 126 in src/CaiPacketDebug/Plugin.cs

View workflow job for this annotation

GitHub Actions / 构建插件

Converting null literal or possible null value to non-nullable type.

Check warning on line 126 in src/CaiPacketDebug/Plugin.cs

View workflow job for this annotation

GitHub Actions / 构建插件

Converting null literal or possible null value to non-nullable type.
try
{
packet = this._serverPacketSerializer.Deserialize(instance.reader);
}

catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write($"[C({instance.whoAmI})->S] 解析数据包时出错:" + ex);
Console.ResetColor();
return result;
}
if (Config.Instance.ClientToServer.ExcludePackets.Contains((int) packet.Type))
{
return result;
Expand Down

0 comments on commit 8b18d63

Please sign in to comment.