Serialization Strategy - BeardedManStudios/ForgeNetworkingRemastered GitHub Wiki

Directory Previous Next
Directory Message Pools Engine Facade

The serialization strategy is a way of being able to describe how to serialize any type to a byte[] and back from a byte[] back to it's type. There are plenty of standard C# ways to do this kind of thing; however, we often want to use the minimal amount of bandwidth to send the same information which can be re-constructed on the other side of the network. For this, the serialization strategy allows you to swap and add new ways of serializing specific types to a byte[].

Default serializers

Forge has a few default types serilizers that you can use right away. Below is a list of serializers that are available to you. If you try to serialize a type that is either not in this list, or ones that you've created/downloaded, then you not be able to serialize that type.

Forge default serializers

Unity default serializers

Creating a type serializer

There are a couple things to know about creating type serializers.

  1. Your class must implement ITypeSerializer
  2. Pack your binary data into a BMSByte
  3. Implement the Serialize function
  4. Implement the Deserialize function

Below is an example of a custom serializer for a Vector3. Since a Vector3 just has 3 variables that describe it float x, float y, and float z we will serialize and deserialize it by those. It is important to note that we serialize into a BMSByte and you pull your binary representation of the data from a BMSByte.

public class Vector3Serializer : ITypeSerializer
{
	public object Deserialize(BMSByte buffer)
	{
		return new Vector3(buffer.GetBasicType<float>(),
			buffer.GetBasicType<float>(),
			buffer.GetBasicType<float>());
	}

	public void Serialize(object val, BMSByte buffer)
	{
		var vec = (Vector3)val;
		buffer.Append(BitConverter.GetBytes(vec.x));
		buffer.Append(BitConverter.GetBytes(vec.y));
		buffer.Append(BitConverter.GetBytes(vec.z));
	}
}

How to register your serializer

You can check out the ForgeRegistrations class to see how the Vector3Serializer and the QuaternionSerializer are registered. You can either choose to modify this class to add your registrations or make a call to ForgeSerializer.Instance.AddSerializer to add your serializer when you want to add it.

After adding your serializer to the ForgeSerializer you can call ForgeSerializer.Instance.Serialize and pass in an object that matches the serialized type to be serialized. You can also call ForgeSerializer.Instance.Deserialize to deserialize bytes into the type you have registered. You must register your type before you can call these methods using your type.

A note about compression

There are 2 places where you can apply compression to your types, the first one being here in the serializer, this will make it so that the compression you apply is global to your program whenever you use the ForgeSerializer to serialize a type. The second place is directly inside of a message when you do serialize & deserialize which will be for that specific message.


Directory Previous Next
Directory Message Pools Engine Facade
⚠️ **GitHub.com Fallback** ⚠️