Skip to content

SerializationManager

Daniel Molinero Lucas edited this page Dec 5, 2023 · 1 revision

Using inbuild serialization in ScapeCore [WIP]

Introduction

This guide outlines the usage of the SerializationManager class in the ScapeCore project for serialization and deserialization using Protocol Buffers. The SerializationManager provides a convenient way to serialize and deserialize objects, supporting GZip compression for efficient data storage, and easy to use runtime type serialization.

Table of Contents

  1. Getting Started
  2. Configuring Types
  3. Serialization
  4. Deserialization
  5. Changing Serialization Model
  6. Additional Methods

Getting Started

To use the SerializationManager, follow these steps:

  1. Include the required namespaces at the beginning of your file:

    using ScapeCore.Core.Serialization;
  2. Ensure that the necessary dependencies are available in your project. Full list of dependencies available here.

  3. Initialize the SerializationManager and configure types as needed.

Configuring Types

The SerializationManager requires the configuration of types that need to be serialized. The _types array in the SerializationManager class lists the default types. You can add more types using the AddType method at runtime. As an example, you can securely add any MonoBehaviour sub-type by overriding OnCreate(), and checking if the SerializationManager can serialize your custom type:

    protected override void OnCreate()
    {
        base.OnCreate();
        if (!SerializationManager.Model.CanSerialize(typeof(_YourCustomType_)))
            SerializationManager.AddType(typeof(_YourCustomType_));
    }

You can run into problems while creating cyclic dependencies if your types are complex enough, in this situation you can try to resolve the dependency by making the sub-type field in the parent type a property, so the SerializationManager stops serializing it. But in some situations you may need to use a more direct aproach interacting with the underlying protobuf-net API:

    [ProtoAfterDeserialization]
    private void OnAfterDeserialize()
    {
        //Resolve cyclic dependency 
    }

Serialization

To serialize an object, use the Serialize method:

YourObjectType obj = // create or obtain your object
string path = "path/to/save";
SerializationManager.SerializationOutput output = SerializationManager.Serialize(obj, path, compress: true);

This method returns a SerializationOutput record, containing information about the serialization process, including any errors that may have occurred.

Deserialization

To deserialize an object, use the Deserialize method:

string path = "path/to/load";
SerializationManager.DeserializationOutput<YourObjectType> output = SerializationManager.Deserialize<YourObjectType>(path, obj: null, decompress: true);

This method returns a DeserializationOutput record, containing information about the deserialization process, including any errors that may have occurred.

Changing Serialization Model

If you need to change the serialization model, use the ChangeModel method:

RuntimeTypeModel newModel = // create or obtain your new model
SerializationManager.ChangeModelOutput output = SerializationManager.ChangeModel(newModel);

Additional Methods

  • GetRuntimeClone: Returns a clone of the current runtime type model.

Feel free to explore and customize the SerializationManager class according to your project's specific needs.