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();
}
}
}
```
This commit is contained in:
parent
b20f3df595
commit
1ac91c3998
4 changed files with 54 additions and 9 deletions
|
|
@ -155,7 +155,7 @@ namespace SerializationGenerator
|
|||
}
|
||||
}
|
||||
|
||||
source.GenerateSerializableProperty(fieldSymbol);
|
||||
source.GenerateSerializableProperty(fieldSymbol, compilation);
|
||||
source.AppendLine();
|
||||
|
||||
var serializableProperty = SerializableMigrationRulesEngine.GenerateSerializableProperty(
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ namespace SerializationGenerator
|
|||
public const string IPADDRESS_CLASS = "System.Net.IPAddress";
|
||||
public const string KEYVALUEPAIR_STRUCT = "System.Collections.Generic.KeyValuePair";
|
||||
|
||||
public const string INVALIDATEPROPERTIES_ATTRIBUTE = "Server.InvalidatePropertiesAttribute";
|
||||
public const string AFTERDESERIALIZATION_ATTRIBUTE = "Server.AfterDeserializationAttribute";
|
||||
public const string SERIALIZABLE_ATTRIBUTE = "Server.SerializableAttribute";
|
||||
public const string SERIALIZABLE_FIELD_ATTRIBUTE = "Server.SerializableFieldAttribute";
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
*************************************************************************/
|
||||
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
|
|
@ -22,11 +23,22 @@ namespace SerializationGenerator
|
|||
{
|
||||
public static void GenerateSerializableProperty(
|
||||
this StringBuilder source,
|
||||
IFieldSymbol fieldSymbol
|
||||
IFieldSymbol fieldSymbol,
|
||||
Compilation compilation
|
||||
)
|
||||
{
|
||||
var fieldName = fieldSymbol.Name;
|
||||
|
||||
var invalidatePropertiesAttribute = fieldSymbol
|
||||
.GetAttributes()
|
||||
.OfType<AttributeData>()
|
||||
.FirstOrDefault(
|
||||
attr => attr.AttributeClass?.Equals(
|
||||
compilation.GetTypeByMetadataName(INVALIDATEPROPERTIES_ATTRIBUTE),
|
||||
SymbolEqualityComparer.Default
|
||||
) ?? false
|
||||
);
|
||||
|
||||
source.GeneratePropertyStart(AccessModifier.Public, fieldSymbol);
|
||||
|
||||
// Getter
|
||||
|
|
@ -34,13 +46,17 @@ namespace SerializationGenerator
|
|||
|
||||
// Setter
|
||||
source.GeneratePropertySetterStart(false);
|
||||
source.AppendLine(
|
||||
$@" if (value != {fieldName})
|
||||
{{
|
||||
((ISerializable)this).MarkDirty();
|
||||
{fieldName} = value;
|
||||
}}"
|
||||
);
|
||||
const string indent = " ";
|
||||
source.AppendLine($"{indent}if(value != {fieldName})");
|
||||
source.AppendLine($"{indent}{{");
|
||||
source.AppendLine($"{indent} {fieldName} = value;");
|
||||
source.AppendLine($"{indent} ((ISerializable)this).MarkDirty();");
|
||||
|
||||
if (invalidatePropertiesAttribute != null)
|
||||
{
|
||||
source.AppendLine($"{indent} InvalidateProperties();");
|
||||
}
|
||||
source.AppendLine($"{indent}}}");
|
||||
source.GeneratePropertyGetSetEnd(false);
|
||||
|
||||
source.GeneratePropertyEnd();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,28 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: InvalidatePropertiesAttribute.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;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
/// <summary>
|
||||
/// Hints to the source generator that this field will execute an InvalidateProperties when the value is set
|
||||
/// and the value is different from the current value.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Field)]
|
||||
public class InvalidatePropertiesAttribute : Attribute
|
||||
{
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue