-
Notifications
You must be signed in to change notification settings - Fork 0
SerializationManager
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.
- Getting Started
- Configuring Types
- Serialization
- Deserialization
- Changing Serialization Model
- Additional Methods
To use the SerializationManager
, follow these steps:
-
Include the required namespaces at the beginning of your file:
using ScapeCore.Core.Serialization;
-
Ensure that the necessary dependencies are available in your project. Full list of dependencies available here.
-
Initialize the
SerializationManager
and configure types as needed.
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
}
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.
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.
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);
-
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.