Skip to content

Commit

Permalink
Fix bugs read packet
Browse files Browse the repository at this point in the history
  • Loading branch information
TitleHHHH authored and TitleHHHH committed Sep 20, 2024
1 parent 4cb59e0 commit f6dc65e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/McProtoNet/McProtoNet.Abstractions/InputPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace McProtoNet.Abstractions;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public InputPacket(MemoryOwner<byte> owner)
{
Id = ReadVarInt(this.owner.Span, out int offset);
this.owner = owner;
Id = ReadVarInt(owner.Span, out int offset);
Data = this.owner.Memory.Slice(offset);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Reactive;
using System.Reactive.Subjects;
using McProtoNet.Abstractions;
using McProtoNet.NBT;
using McProtoNet.Serialization;

namespace McProtoNet.MultiVersionProtocol;
Expand Down Expand Up @@ -74,7 +75,12 @@ protected override void OnPacketReceived(InputPacket packet)
scoped var reader = new MinecraftPrimitiveReaderSlim(packet.Data);
if (ProtocolVersion >= 765)
{
var reason = reader.ReadNbt();
// var reason = reader.ReadNbt();
byte[] data = packet.Data.ToArray();
MemoryStream ms = new MemoryStream(data);
ms.Position = 0;
NbtReader nbtReader = new NbtReader(ms);
var nbt = nbtReader.ReadAsTag();
}
else
{
Expand Down
24 changes: 15 additions & 9 deletions src/McProtoNet/McProtoNet/Client/MinecraftClientLogin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,10 @@ public async Task<LoginizationResult> Login(Stream source, LoginOptions options,
while (true)
{
var inputPacket = await reader.ReadNextPacketAsync(cancellationToken).ConfigureAwait(false);
scoped MinecraftPrimitiveReaderSlim p_reader =
new MinecraftPrimitiveReaderSlim(inputPacket.Data);

try
{
var needBreak = false;


switch (inputPacket.Id)
{
case 0x00: // Login Disconnect
Expand Down Expand Up @@ -127,7 +124,7 @@ public async Task<LoginizationResult> Login(Stream source, LoginOptions options,
//if (!inputPacket.Data.TryReadVarInt(out threshold, out _))


threshold = p_reader.ReadVarInt();
threshold = ReadTreshold(inputPacket);
reader.SwitchCompression(threshold);
sender.SwitchCompression(threshold);

Expand Down Expand Up @@ -158,12 +155,12 @@ public async Task<LoginizationResult> Login(Stream source, LoginOptions options,
while (true)
{
var inputPacket = await reader.ReadNextPacketAsync(cancellationToken).ConfigureAwait(false);
scoped var p_reader = new MinecraftPrimitiveReaderSlim(inputPacket.Data);

try
{
var needBreak = false;


switch (inputPacket.Id)
{
case 0x00: // Cookie Request
Expand All @@ -189,7 +186,7 @@ public async Task<LoginizationResult> Login(Stream source, LoginOptions options,
break; // Finish
case 0x04: // KeepAlive

long id = p_reader.ReadSignedLong();
long id = ReadKeepAlive(inputPacket);

var outP = CreateKeepAlive(id);
try
Expand Down Expand Up @@ -263,7 +260,16 @@ private static OutputPacket CreateKeepAlive(long id)
return new OutputPacket(writer.GetWrittenMemory());
}


private static int ReadTreshold(InputPacket p)
{
scoped MinecraftPrimitiveReaderSlim r = new MinecraftPrimitiveReaderSlim(p.Data);
return r.ReadVarInt();
}
private static long ReadKeepAlive(InputPacket p)
{
scoped MinecraftPrimitiveReaderSlim r = new MinecraftPrimitiveReaderSlim(p.Data);
return r.ReadSignedLong();
}
private static ClientboundConfigurationPluginMessagePacket ReadConfigPluginMessagePacket(InputPacket packet)
{
scoped var reader = new MinecraftPrimitiveReaderSlim(packet.Data);
Expand Down
15 changes: 1 addition & 14 deletions src/McProtoNet/McProtoNet/Protocol/MinecraftPacketReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,10 @@ public async ValueTask<InputPacket> ReadNextPacketAsync(CancellationToken token
var len = await BaseStream.ReadVarIntAsync(token);
if (_compressionThreshold <= 0)
{
var id = await BaseStream.ReadVarIntAsync(token);
len -= id.GetVarIntLength();


var buffer = memoryAllocator.AllocateExactly(len);

try
{
await BaseStream.ReadExactlyAsync(buffer.Memory, token);

return new InputPacket(buffer);
}
catch
Expand Down Expand Up @@ -89,16 +83,10 @@ public async ValueTask<InputPacket> ReadNextPacketAsync(CancellationToken token
if (sizeUncompressed != 0)
throw new Exception("size incorrect");

var id = await BaseStream.ReadVarIntAsync(token);
len -= id.GetVarIntLength() + 1;


var buffer = memoryAllocator.AllocateExactly(len);

var buffer = memoryAllocator.AllocateExactly(len - 1); // -1 is sizeUncompressed length !!!
try
{
await BaseStream.ReadExactlyAsync(buffer.Memory, token);

return new InputPacket(buffer);
}
catch
Expand All @@ -109,7 +97,6 @@ public async ValueTask<InputPacket> ReadNextPacketAsync(CancellationToken token
}
}



private static void DecompressCore(ReadOnlySpan<byte> buffer_compress, Span<byte> uncompress)
{
Expand Down

0 comments on commit f6dc65e

Please sign in to comment.