Skip to content

Commit

Permalink
Fix for inbound byte attributes getting corrupted by UTF8 Encoding.
Browse files Browse the repository at this point in the history
  • Loading branch information
desukuran committed Apr 23, 2024
1 parent a723411 commit 1c4abfa
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions src/PFire.Core/Protocol/MessageSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,26 @@ public static IMessage Deserialize(BinaryReader reader, IMessage messageBase)

var value = XFireAttributeFactory.Instance.GetAttribute(attributeType).ReadValue(reader);

var field = fieldInfo.Where(a => a.GetCustomAttribute<XMessageField>() != null)
.FirstOrDefault(a => a.GetCustomAttribute<XMessageField>()?.Name == attributeName);
var field = fieldInfo
.Where(a => a.GetCustomAttribute<XMessageField>() != null)
.FirstOrDefault(a =>
{
var attribute = a.GetCustomAttribute<XMessageField>();
if (attribute != null)
{
if (attributeName.Length == 1)
{
// If attributeName is a single byte, compare with the attribute's NameAsBytes
return attribute.NameAsBytes.SequenceEqual(attributeName);
}
else
{
// If attributeName is a byte array with length > 1, compare with the attribute's Name converted to string
return Encoding.UTF8.GetString(attributeName) == attribute.Name;
}
}
return false;
});

if (field != null)
{
Expand All @@ -59,7 +77,7 @@ public static IMessage Deserialize(BinaryReader reader, IMessage messageBase)
return messageBase;
}

private static string GetAttributeName(BinaryReader reader, Type messageType)
private static byte[] GetAttributeName(BinaryReader reader, Type messageType)
{
HashSet<Type> messageTypeSet = new HashSet<Type>
{
Expand All @@ -70,14 +88,25 @@ private static string GetAttributeName(BinaryReader reader, Type messageType)
typeof(GroupMemberRemove),
typeof(GroupRemove),
typeof(GroupRename),
typeof(GameClientData)
typeof(GameClientData),
typeof(UserRequestAdvancedInfo)

Check failure on line 92 in src/PFire.Core/Protocol/MessageSerializer.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'UserRequestAdvancedInfo' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 92 in src/PFire.Core/Protocol/MessageSerializer.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'UserRequestAdvancedInfo' could not be found (are you missing a using directive or an assembly reference?)
};

byte count = messageTypeSet.Contains(messageType) ? (byte)1 : reader.ReadByte();

// Read the bytes for the attribute name
var readBytes = reader.ReadBytes(count);
return Encoding.UTF8.GetString(readBytes);
// Check if count is 1, indicating a single byte
if (count == 1)
{
// Read the single byte and treat it as a numeric value
byte numericValue = reader.ReadByte();
return [numericValue];
}
else
{
// Read the bytes for the attribute name
var readBytes = reader.ReadBytes(count);
return readBytes;
}
}

public static byte[] Serialize(IMessage message)
Expand Down

0 comments on commit 1c4abfa

Please sign in to comment.