I’ve built a serializer class with Reflection, but there is a bug.
It calls the OnSerialize
regardless of if its been overridden, and since its always “declared” in the abstract
class it always gets called, and returns true causing the serialization process to be halted.
private static bool InvokeOnSerializeIfExists(object packet, NetworkPacketWriter writer)
{
var onSerialize = packet.GetType().GetMethod("OnSerialize");
if (onSerialize == null)
{
return false;
}
if (onSerialize.GetBaseDefinition().DeclaringType != onSerialize.DeclaringType)
{
return false;
}
onSerialize.Invoke(packet, [writer]);
return true;
}
It is called here:
public static NetworkPacketWriter Serialize(object packet)
{
var writer = new NetworkPacketWriter();
writer.WriteShort(GetPacketIdentifierFromAttribute(packet));
if (InvokeOnSerializeIfExists(packet, writer))
{
return writer;
}
InvokeOnConfigureRules(packet);
AddObjectToWriter(packet, writer);
return writer;
}
New contributor
sam korbin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.