ModernUO/Projects/SerializationGenerator/SourceGeneration/SourceGeneration.Property.cs
Kamron Batman 312e3873f1
fix(core): Fixes encoded int. Adds dirty checking opt-out (#619)
### Additions
- Automatically opts-out `Item/Mobile/Guild/Accounts` from dirty checking with a new property `UseDirtyChecking`
- Codegen now enables `UseDirtyChecking` via getter. This requires that the property is `virtual` for derived types.

### Fixes
- Fixes `EncodedInt` being broken
- Fixes issues with new custom serializable types that are not derived from Item/Mobile/etc.
- Removes double dirty checking.

### Example of a brand new serializable type that isn't an Item/Mobile/etc.
User created code:
```cs
using System;

namespace Server.Items
{
    [Serializable(0)]
    public partial class NewTestEntityObject : ISerializable
    {
        [EncodedInt]
        [SerializableField(0)]
        private int _someProperty;

        public NewTestEntityObject()
        {
            SetTypeRef(GetType());
            // Add to serial tracking like World.Item
            /*
            Serial = World.NewEntity;
            World.AddEntity(this);
            */
        }

        [AfterDeserialization]
        private void AfterDeserialization()
        {
            Console.WriteLine("This ran!");
        }

        public int TypeRef { get; }
        public Serial Serial { get; }
        public void Delete()
        {
        }

        public bool Deleted { get; set; }
        public void SetTypeRef(Type type)
        {
            // Type tracking for persistence goes here
            /*
            TypeRef = World.NewEntityTypes.IndexOf(type);
            if (TypeRef == -1)
            {
                World.NewEntityTypes.Add(type);
                TypeRef = World.NewEntityTypes.Count - 1;
            }
            */
        }
    }
}
```

Generated code:
```cs
namespace Server.Items
{
    public partial class NewTestEntityObject
    {
#pragma warning disable 0414
        private const int _version = 0;
#pragma warning restore 0414

        public int SomeProperty
        {
            get => _someProperty;
            set
            {
                if (value != _someProperty)
                {
                    _someProperty = value;
                    ((ISerializable)this).MarkDirty();
                }
            }
        }

        long ISerializable.SavePosition { get; set; } = -1;
        BufferWriter ISerializable.SaveBuffer { get; set; }
        bool ISerializable.UseDirtyChecking => true;

        public NewTestEntityObject(Serial serial)
        {
            Serial = serial;
            SetTypeRef(typeof(NewTestEntityObject));
        }

        public void Serialize(IGenericWriter writer)
        {

            writer.WriteEncodedInt(_version);

            writer.WriteEncodedInt(SomeProperty);
        }

        public void Deserialize(IGenericReader reader)
        {
            var version = reader.ReadEncodedInt();

            SomeProperty = reader.ReadEncodedInt();

            Timer.DelayCall(AfterDeserialization);
        }
    }
}
```
2021-05-26 19:25:05 -07:00

116 lines
5 KiB
C#

/*************************************************************************
* ModernUO *
* Copyright (C) 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SourceGeneration.Property.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.Text;
using Humanizer;
using Microsoft.CodeAnalysis;
namespace SerializationGenerator
{
public static partial class SourceGeneration
{
public static string GetPropertyName(this IFieldSymbol fieldSymbol)
{
var fieldName = fieldSymbol.Name;
var propertyName = fieldName;
if (propertyName.StartsWith("m_", StringComparison.OrdinalIgnoreCase))
{
propertyName = propertyName.Substring(2);
}
else if (propertyName.StartsWith("_", StringComparison.OrdinalIgnoreCase))
{
propertyName = propertyName.Substring(1);
}
return propertyName.Dehumanize();
}
public static void GeneratePropertyStart(
this StringBuilder source,
AccessModifier accessors,
IFieldSymbol fieldSymbol
)
{
var propertyName = fieldSymbol.GetPropertyName();
source.AppendLine($@" {accessors.ToFriendlyString()} {fieldSymbol.Type} {propertyName}
{{");
}
public static void GenerateAutoProperty(
this StringBuilder source,
AccessModifier accessors,
string type,
string propertyName,
AccessModifier? getAccessor,
AccessModifier? setAccessor,
string indent,
bool useInit = false,
string defaultValue = null,
bool isOverride = false
)
{
if (getAccessor == null && setAccessor == null)
{
throw new ArgumentNullException($"Must specify a {nameof(getAccessor)} or {nameof(setAccessor)} parameter");
}
var getter = getAccessor == null ?
"" :
$"{(getAccessor != AccessModifier.None ? $"{getAccessor.Value.ToFriendlyString()} " : "")}get;";
var getterSpace = getAccessor != null ? " " : "";
var setOrInit = useInit ? "init;" : "set;";
var setterAccessor = setAccessor is null or AccessModifier.None
? ""
: $"{setAccessor.Value.ToFriendlyString() ?? ""} ";
var setter = setAccessor == null ? "" : $"{getterSpace}{setterAccessor}{setOrInit}";
var propertyAccessor = accessors == AccessModifier.None ? "" : $"{accessors.ToFriendlyString()} ";
var printOverride = isOverride ? "override " : "";
var printDefaultValue = defaultValue != null ? $"{(setAccessor != null ? " =" : "")} {defaultValue};" : "";
var printGetterSetter = setAccessor == null ? "=>" : $"{{ {getter}{setter} }}";
source.AppendLine($"{indent}{propertyAccessor}{printOverride}{type} {propertyName} {printGetterSetter}{printDefaultValue}");
}
public static void GeneratePropertyEnd(this StringBuilder source) => source.AppendLine(" }");
public static void GeneratePropertyGetterReturnsField(this StringBuilder source, IFieldSymbol fieldSymbol) =>
source.AppendLine($" get => {fieldSymbol.Name};");
public static void GeneratePropertyGetterStart(this StringBuilder source, bool useExpression) =>
source.AppendLine($" get{(useExpression ? " => " : "\n {")}");
public static void GeneratePropertyGetSetEnd(this StringBuilder source, bool useExpression)
{
if (!useExpression)
{
source.AppendLine(" }");
}
}
public static void GeneratePropertySetterSetsValue(this StringBuilder source, IFieldSymbol fieldSymbol) =>
source.AppendLine($" set => {fieldSymbol.Name} = value;");
public static void GeneratePropertySetterStart(this StringBuilder source, bool useExpression, bool useInit = false) =>
source.AppendLine($" {(useInit ? "init" : "set")}{(useExpression ? " => " : "\n {")}");
}
}