modernuo/docs/scripting-guide/serialization.md

199 lines
6.8 KiB
Markdown
Raw Permalink Normal View History

2022-04-04 22:39:16 +02:00
---
title: Serialization
---
# Serialization / Savings
=== "Generic persistence"
```Persistence.Serialize``` and ```Persistence.Deserialize``` is replaced with GenericPersistence class
Example of how to persist a custom system.
```cs
namespace Server.ExampleSystem
{
public static class ExampleSerialization
{
public static void Configure()
{
GenericPersistence.Register("ExampleSystem", Serialize, Deserialize);
}
public static void Serialize(IGenericWriter writer)
{
// Do serialization here
writer.WriteEncodedInt(0); // version
}
public static void Deserialize(IGenericReader reader)
{
// Do deserialization here
var version = reader.ReadEncodedInt();
}
}
}
```
=== "Codegen"
### Basic info
ModernUO can programatically generate migrations. This feature is based on internal C# Source generators [More info](https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/)
Old way of serializing objects:
```cs
public class ExampleItem : Item
{
private string _exampleText;
2022-04-04 22:39:16 +02:00
[CommandProperty(AccessLevel.GameMaster)]
public string ExampleText
{
get => _exampleText;
set
{
if (value != _exampleText)
{
_exampleText = value;
this.MarkDirty();
}
}
}
[Constructible]
public ExampleItem(string text) : base(0)
{
Example = text;
}
public ExampleItem(Serial serial) : base(serial)
{
}
2022-04-04 22:39:16 +02:00
public override void Serialize(IGenericWriter writer)
{
base.Serialize(writer);
2022-04-04 22:39:16 +02:00
writer.WriteEncodedInt(0); //Version
writer.Write(_exampleText);
}
2022-04-04 22:39:16 +02:00
public override void Deserialize(IGenericReader reader)
{
base.Deserialize(reader);
2022-04-04 22:39:16 +02:00
var version = reader.ReadEncodedInt();
2022-04-04 22:39:16 +02:00
// version 0
_exampleText = reader.ReadString();
}
2022-04-04 22:39:16 +02:00
}
```
Same class serialized with codegen
```cs
[SerializationGenerator(0)]
2022-04-04 22:39:16 +02:00
public partial class ExampleItem : Item
{
[SerializableField(0)]
[SerializedCommandProperty(AccessLevel.GameMaster)]
private string _exampleText;
2022-04-04 22:39:16 +02:00
[Constructible]
public ExampleItem(string text) : base(0)
{
_exampleText = text;
}
2022-04-04 22:39:16 +02:00
}
```
### Step by step
1. Add ```SerializationGenerator(versionNumber)``` to your class and make the class ```partial```
2022-04-04 22:39:16 +02:00
```cs
[SerializationGenerator(0)]
2022-04-04 22:39:16 +02:00
public partial class ExampleItem : Item
```
1. Delete the constructor with ```Serial serial```
1. Delete the ```Serialize``` and ```Deserialize``` methods.
2022-04-04 22:39:16 +02:00
1. Add ```SerializableField(fieldOrder)``` attribute to all field you want to serialize.
```cs
[SerializableField(0)]
private string _exampleText;
2022-04-04 22:39:16 +02:00
```
1. Run `publish.cmd`.
ModernUO will create migration files for you. In this case "Server.Items.ExampleItem.v0.json" and "Server.Items.ExampleItem.Serialization.cs".
These files contains all information and classes needed for ModernUO to serialize/deserialize your objects.
2022-04-04 22:39:16 +02:00
### Migrations
When new field is added to the serialization, you need to increment `versionNumber` and run `publish.cmd` again to generate a migration file for the new version.
Here is little example.
2022-04-04 22:39:16 +02:00
New class code will look like this:
```cs
[SerializationGenerator(1)]
2022-04-04 22:39:16 +02:00
public partial class ExampleItem : Item
{
[SerializableField(0)]
private string _exampleText;
2022-04-04 22:39:16 +02:00
[SerializableField(1)]
[SerializedCommandProperty(AccessLevel.GameMaster)]
private string _addedExampleTest;
2022-04-04 22:39:16 +02:00
[Constructible]
public ExampleItem(string text, string addedText) : base(0)
{
_exampleText = text;
_addedExampleTest = addedText;
}
2022-04-04 22:39:16 +02:00
}
```
Your IDE (Visual Studio, Rider, or VSCode), will show errors and the ModernUO will not compile.
This happens because the code generator builds a migration from V0 to V1 and a new struct `V0Content` with all of the V0 fields is generated.
ModernUO is expecting the developer to create a migration from the old version to the new.
Now create a `MigrateFrom` method for each of the older versions, in this case V0, to the new version.
2022-04-04 22:39:16 +02:00
```cs
private void MigrateFrom(V0Content content)
{
_exampleText = content.ExampleText;
2022-04-04 22:39:16 +02:00
}
```
!!! Tip
Since the class is `partial`, you can create a standalone file for migrations to keep them organized. For example "ExampleItem.Migrations.cs"
Your migration is now complete.
2022-04-04 22:39:16 +02:00
### Migrating from pre-codegen
To migrate from pre-codegen serialization, change the old Deserialize method to this:
2022-04-04 22:39:16 +02:00
```cs
private void Deserialize(IGenericReader reader, int version)
```
This method will be automatically called when the serialization generator doesn't have a migration for for that older version.
In this method you can make old fashioned deserializations that are mostly compatible with RunUO.
2022-04-04 22:39:16 +02:00
### After Deserialization
To execute code after the deserialization, add the `AfterDeserialization()` attribute to a method.
2022-04-04 22:39:16 +02:00
```cs
[AfterDeserialization]
private void AfterDeserialization()
{
// Some code here
}
```
!!! Tip
By default, `AfterDeserialization` is executed synchronously right after the actual deserialization. Passing `false` to the attribute will make it execute after all deserializations.
### Serializing Non-Entity Classes
Sometimes you might need to serialize a nested object that is not an `ISerializable`. The syntax is largely the same except you must also mark the *parent* object for dirty tracking by using the `DirtyTrackingEntity` attribute.
A good example of this is ["AquariumState"](https://github.com/modernuo/ModernUO/blob/main/Projects/UOContent/Items/Aquarium/AquariumState.cs).
!!! Note
Non-entity classes cannot be serialized by reference since that would require a reference Serial or ID. This means if the object is a serializable property on multiple objects, it will be serialized
multiple times and will effectively get duplicated on world load. Consider either making it an `ISerializable` type and building world load/save mechanisms, or do not serialize the nested object directly.
Instead opt for a global lookup, serialize with generic persistence, and then reattach to the objects using the `WorldLoad` event sink.