Skip to content

Modder guide : Serialization

6opoDuJIo edited this page Nov 19, 2018 · 8 revisions

Serialization is the way to convert entities of different complexity degrees, into numbers, that can be sent over the network. RimAlong uses BinaryFormatter, which, obviously, can't convert EVERYTHING into sequence of bytes. Thus, it needs help. And we can help him, by providing him so ISerializationSurrogate implementation. This might sound confusing, but essentially, it's just a thing, that tells WHAT and HOW should be sent and taken from serialization stream. Still sounds confusing? Actually, it's pretty simple. Here's how IntVec3 serialized :

public class IntVec3Surrogate : ISerializationSurrogate
    {
        public void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
        {
            IntVec3 v = (IntVec3)obj;
            info.AddValue("xi", v.x);
            info.AddValue("yi", v.y);
            info.AddValue("zi", v.z);
        }

        public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
        {
            IntVec3 v = new IntVec3();
            v.x = info.GetInt32("xi");
            v.y = info.GetInt32("yi");
            v.z = info.GetInt32("zi");
            return v;
        }
    }