Commit graph

7 commits

Author SHA1 Message Date
Kamron Batman
1ac91c3998
feat(codegen): Adds InvalidateProperties (#616)
Adds invalidate properties to code genned fields.

Example:
```cs
[InvalidateProperties]
[EncodedInt]
private int _number; // Cliloc
```

Generates:
```cs
public int Number
{
    get => _number;
    set
    {
        if (value != _number)
        {
            _number = value;
            ((ISerializable)this).MarkDirty();
            InvalidateProperties();
        }
    }
}
```
2021-05-24 22:49:00 -07:00
Kamron Batman
b20f3df595
feat(codegen): Adds AfterDeserialization support (#615)
Added the ability to execute arbitrary code after deserialization.

Example, let's say you want to delete an item after you deserialize it:
```cs
        [AfterDeserialization]
        private void OnAfterDeserialization()
        {
            Delete();
        }
```

Generates this:
```cs
        public override void Deserialize(IGenericReader reader)
        {
            base.Deserialize(reader);

            var version = reader.ReadEncodedInt();

            Timer.DelayCall(OnAfterDeserialization);
        }
```
2021-05-24 22:30:13 -07:00
Kamron Batman
ca5a06e9a2
fix(codegen): Adds string intern, encoded int, enum and legacy version support (#613)
- [X] Adds Enum migration rule
- [X] Adds legacy version (writing full int for version field)
- [X] Adds encoded int attribute
- [X] Adds intern string attribute
- [X] Fixes missing base deserialize/serialize
2021-05-24 01:01:56 -07:00
Kamron Batman
7084c6c24f
fix(codegen): Fixes primitive check (#612) 2021-05-23 23:54:45 -07:00
Kamron Batman
99c46f1b11
fix(codegen): Removes extra rule arg for primitive serialization (#611) 2021-05-23 23:47:12 -07:00
Kamron Batman
82b0d03e19
fix(codegen): Fixes serializing classes with no fields (#609)
Fixes code genning classes with no fields.
Fixes code genning primitives.
FIxes code genning uo types.
2021-05-23 21:47:18 -07:00
Kamron Batman
9afa4e4cab
feat: Source generated Serialization/Deserialization (#550)
### Features
* Fully abstracts serialization by using compile-time attributes.
* Supports serializing the following:
  - Primitives (integers, strings, etc)
  - IP Addresses
  - BigDecimal
  - DateTime, Delta DateTimes
  - TimeSpan
  - Server.Race
  - Server.Map
  - Point2D, Point3D, Rect2D, Rect3D
  - Existing/New `ISerializable` references
  - Lists/Sets of serializable types
  - Type with a `Serialize` method and constructor that takes an `IGenericReader`
* Supports forward-only migration
* Supports existing RunUO deserialization for older versions by changing to the following signature:
  - `public void OldDeserialize(IGenericReader reader, int version)`
  - Must remove deserializing the version since this is already done
* Supports serializing from private fields or custom made properties.
* Types do not require inheriting Item/Mobile. Code gen will fully create `ISerializable` information.
  - This is not recommended yet, since it requires wiring to `Persistence` which will cause lots of unresolved symbol errors until code gen is built.

### Example
```cs
using System.Collections.Generic;

namespace Server.Items
{
    [Serializable(1)]
    public partial class TestItem1 : Item
    {
        [SerializableField(1)]
        [SerializableFieldAttr("[CommandProperty(AccessLevel.Administrator)]")]
        private List<Item> _someProperty;

        private void Deserialize(IGenericReader reader, int version)
        {
        }
    }
}
```

Generates this:
```cs
namespace Server.Items
{
    public partial class TestItem1
    {
#pragma warning disable 0414
        private const int _version = 1;
#pragma warning restore 0414

        [CommandProperty(AccessLevel.Administrator)]
        public System.Collections.Generic.List<Server.Item> SomeProperty
        {
            get => _someProperty;
            set
            {
                if (value != _someProperty)
                {
                    ((ISerializable)this).MarkDirty();
                    _someProperty = value;
                }
            }
        }

        public TestItem1(Serial serial) : base(serial)
        {
        }

        public override void Serialize(IGenericWriter writer)
        {
            var savePosition = ((Server.ISerializable)this).SavePosition;
            if (savePosition > -1)
            {
                writer.Seek(savePosition, System.IO.SeekOrigin.Begin);
                return;
            }
            writer.WriteEncodedInt(_version);
            writer.Write(_someProperty);
        }

        public override void Deserialize(IGenericReader reader)
        {
            var version = reader.ReadEncodedInt();
            if (version < 1)
            {
                OldDeserialize(reader, version);
                ((Server.ISerializable)this).MarkDirty();
                return;
            }
            SomeProperty = reader.ReadEntityList<Server.Item>();
        }
    }
}
```

And this:
```json
{
  "version": 1,
  "type": "TestItem1",
  "properties": [
    {
      "name": "SomeProperty",
      "type": "System.Collections.Generic.List\u003CServer.Item\u003E",
      "rule": "ListMigrationRule",
      "ruleArguments": [
        "Server.Item",
        "SerializableInterfaceMigrationRule"
      ]
    }
  ]
}
```
2021-05-23 21:06:23 -07:00