Skip to content

Commit

Permalink
Fix bug in data encoder where MSB of 128 and 0 value bytes was errone…
Browse files Browse the repository at this point in the history
…ously being flipped
  • Loading branch information
ethanmoffat committed Sep 20, 2022
1 parent 6676c84 commit bdb8ce4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
4 changes: 2 additions & 2 deletions EOLib.IO/NumericConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace EOLib.IO
public static class NumericConstants
{
public const int ONE_BYTE_MAX = 253;
public const int TWO_BYTE_MAX = 64009;
public const int THREE_BYTE_MAX = 16194277;
public const int TWO_BYTE_MAX = ONE_BYTE_MAX * ONE_BYTE_MAX;
public const int THREE_BYTE_MAX = ONE_BYTE_MAX * ONE_BYTE_MAX * ONE_BYTE_MAX;

public static readonly int[] NUMERIC_MAXIMUM = {ONE_BYTE_MAX, TWO_BYTE_MAX, THREE_BYTE_MAX };
}
Expand Down
2 changes: 1 addition & 1 deletion EOLib.IO/Services/DataEncoderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public List<byte> Deinterleave(IReadOnlyList<byte> data)

public List<byte> FlipMSB(IReadOnlyList<byte> data)
{
return data.Select(x => (byte)(x ^ 0x80u)).ToList();
return data.Select(x => (byte)(x == 128 || x == 0 ? x : x ^ 0x80u)).ToList();
}

public List<byte> SwapMultiples(IReadOnlyList<byte> data, int multi)
Expand Down
45 changes: 45 additions & 0 deletions EOLib.Test/Net/PacketEncoderServiceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using EOLib.Domain.Interact.Quest;
using EOLib.IO.Services;
using EOLib.Net;
using EOLib.Net.PacketProcessing;
using NUnit.Framework;

namespace EOLib.IO.Test.Services
{
[TestFixture]
public class PacketEncoderServiceTest
{
[Test]
public void PacketEncoderService_Encode_127Byte_DoesNotProduceAny0ValueBytes()
{
var svc = new PacketEncoderService(new NumberEncoderService(), new DataEncoderService());

var packet = new PacketBuilder(PacketFamily.Quest, PacketAction.Accept)
.AddShort(0)
.AddShort(0)
.AddShort(111)
.AddShort(127)
.AddChar((byte)DialogReply.Link)
.AddChar(1)
.Build();

var encoded = svc.Encode(packet, 5);

Assert.That(encoded, Has.All.Not.EqualTo(0));
}

[Test]
public void PacketEncoderService_Encode_0Byte_DoesNotProduceAny128ValueBytes()
{
var svc = new PacketEncoderService(new NumberEncoderService(), new DataEncoderService());

var packet = new PacketBuilder(PacketFamily.Quest, PacketAction.Accept)
.AddByte(0)
.Build();

var encoded = svc.Encode(packet, 5);

Assert.That(encoded, Has.All.Not.EqualTo(128));
}
}
}

0 comments on commit bdb8ce4

Please sign in to comment.