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"
]
}
]
}
```
This commit is contained in:
parent
cb66bef0e5
commit
9afa4e4cab
61 changed files with 3203 additions and 142 deletions
81
Projects/SerializationGenerator/EntitySerializationGenerator.cs
Executable file
81
Projects/SerializationGenerator/EntitySerializationGenerator.cs
Executable file
|
|
@ -0,0 +1,81 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright (C) 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: EntityJsonGenerator.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.Collections.Immutable;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.Text;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
[Generator]
|
||||
public class EntitySerializationGenerator : ISourceGenerator
|
||||
{
|
||||
public void Initialize(GeneratorInitializationContext context)
|
||||
{
|
||||
#if DEBUG
|
||||
if (!Debugger.IsAttached)
|
||||
{
|
||||
Debugger.Launch();
|
||||
}
|
||||
#endif
|
||||
|
||||
context.RegisterForPostInitialization(i =>
|
||||
{
|
||||
SerializerSyntaxReceiver.AttributeTypes.Add("Server.SerializableAttribute");
|
||||
SerializerSyntaxReceiver.AttributeTypes.Add("Server.SerializableFieldAttribute");
|
||||
});
|
||||
|
||||
context.RegisterForSyntaxNotifications(() => new SerializerSyntaxReceiver());
|
||||
}
|
||||
|
||||
public void Execute(GeneratorExecutionContext context)
|
||||
{
|
||||
if (context.SyntaxContextReceiver is not SerializerSyntaxReceiver receiver)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var migrationPath = SerializableMigration.GetMigrationPath(context);
|
||||
var jsonOptions = SerializableMigration.GetJsonSerializerOptions(context.Compilation);
|
||||
// List of types that _will_ become ISerializable
|
||||
var serializableList = receiver
|
||||
.Fields
|
||||
.GroupBy(f => f.ContainingType, SymbolEqualityComparer.Default)
|
||||
.Select(g => g.Key as INamedTypeSymbol)
|
||||
.Where(t => t.WillBeSerializable(context))
|
||||
.ToImmutableArray();
|
||||
|
||||
foreach (IGrouping<ISymbol, IFieldSymbol> group in receiver.Fields.GroupBy(f => f.ContainingType, SymbolEqualityComparer.Default))
|
||||
{
|
||||
string classSource = SerializableEntityGeneration.GenerateSerializationPartialClass(
|
||||
group.Key as INamedTypeSymbol,
|
||||
group.ToList(),
|
||||
context,
|
||||
migrationPath,
|
||||
jsonOptions,
|
||||
serializableList
|
||||
);
|
||||
|
||||
if (classSource != null)
|
||||
{
|
||||
context.AddSource($"{group.Key.Name}.Serialization.cs", SourceText.From(classSource, Encoding.UTF8));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Projects/SerializationGenerator/ExampleSerialization.json
Normal file
12
Projects/SerializationGenerator/ExampleSerialization.json
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
"type": "Server.Items.TestItem",
|
||||
"version": 1,
|
||||
"properties": [
|
||||
{
|
||||
"name": "SomeProperty",
|
||||
"type": "Server.Item",
|
||||
"rule": "SerializableInterfaceMigrationRule",
|
||||
"ruleArguments": ["Server.Items.Item"]
|
||||
}
|
||||
]
|
||||
}
|
||||
4
Projects/SerializationGenerator/IsExternalInit.cs
Normal file
4
Projects/SerializationGenerator/IsExternalInit.cs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
namespace System.Runtime.CompilerServices
|
||||
{
|
||||
internal static class IsExternalInit {}
|
||||
}
|
||||
|
|
@ -0,0 +1,262 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: SerializableEntityGeneration.Class.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.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public static partial class SerializableEntityGeneration
|
||||
{
|
||||
public static bool WillBeSerializable(this INamedTypeSymbol classSymbol, GeneratorExecutionContext context)
|
||||
{
|
||||
var compilation = context.Compilation;
|
||||
|
||||
var serializableEntityAttribute =
|
||||
compilation.GetTypeByMetadataName(SERIALIZABLE_ATTRIBUTE);
|
||||
var serializableInterface = compilation.GetTypeByMetadataName(SERIALIZABLE_INTERFACE);
|
||||
|
||||
if (!classSymbol.ContainingSymbol.Equals(classSymbol.ContainingNamespace, SymbolEqualityComparer.Default))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!classSymbol.ContainsInterface(serializableInterface))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var versionValue = classSymbol.GetAttributes()
|
||||
.FirstOrDefault(
|
||||
attr => attr.AttributeClass?.Equals(serializableEntityAttribute, SymbolEqualityComparer.Default) ?? false
|
||||
)?.ConstructorArguments.FirstOrDefault().Value;
|
||||
|
||||
return versionValue != null;
|
||||
}
|
||||
|
||||
public static string GenerateSerializationPartialClass(
|
||||
INamedTypeSymbol classSymbol,
|
||||
IList<IFieldSymbol> fields,
|
||||
GeneratorExecutionContext context,
|
||||
string migrationPath,
|
||||
JsonSerializerOptions jsonSerializerOptions,
|
||||
ImmutableArray<INamedTypeSymbol> serializableTypes
|
||||
)
|
||||
{
|
||||
var compilation = context.Compilation;
|
||||
|
||||
var serializableEntityAttribute =
|
||||
compilation.GetTypeByMetadataName(SERIALIZABLE_ATTRIBUTE);
|
||||
var serializableFieldAttribute =
|
||||
compilation.GetTypeByMetadataName(SERIALIZABLE_FIELD_ATTRIBUTE);
|
||||
var serializableFieldAttrAttribute =
|
||||
compilation.GetTypeByMetadataName(SERIALIZABLE_FIELD_ATTR_ATTRIBUTE);
|
||||
var serializableInterface = compilation.GetTypeByMetadataName(SERIALIZABLE_INTERFACE);
|
||||
|
||||
// This is a class symbol if the containing symbol is the namespace
|
||||
if (!classSymbol.ContainingSymbol.Equals(classSymbol.ContainingNamespace, SymbolEqualityComparer.Default))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// If we have a parent that is or derives from ISerializable, then we are in override
|
||||
var isOverride = classSymbol.BaseType.ContainsInterface(serializableInterface);
|
||||
|
||||
if (!isOverride && !classSymbol.ContainsInterface(serializableInterface))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var version = classSymbol.GetAttributes()
|
||||
.FirstOrDefault(
|
||||
attr => attr.AttributeClass?.Equals(serializableEntityAttribute, SymbolEqualityComparer.Default) ?? false
|
||||
)?.ConstructorArguments.FirstOrDefault().Value?.ToString();
|
||||
|
||||
if (version == null)
|
||||
{
|
||||
return null; // We don't have the attribute
|
||||
}
|
||||
|
||||
var namespaceName = classSymbol.ContainingNamespace.ToDisplayString();
|
||||
var className = classSymbol.Name;
|
||||
|
||||
StringBuilder source = new StringBuilder();
|
||||
|
||||
source.GenerateNamespaceStart(namespaceName);
|
||||
|
||||
source.GenerateClassStart(
|
||||
className,
|
||||
isOverride ?
|
||||
ImmutableArray<ITypeSymbol>.Empty :
|
||||
ImmutableArray.Create<ITypeSymbol>(serializableInterface)
|
||||
);
|
||||
|
||||
source.GenerateClassField(
|
||||
AccessModifier.Private,
|
||||
InstanceModifier.Const,
|
||||
"int",
|
||||
"_version",
|
||||
version,
|
||||
true
|
||||
);
|
||||
source.AppendLine();
|
||||
|
||||
var serializableProperties = new List<SerializableProperty>();
|
||||
|
||||
foreach (IFieldSymbol fieldSymbol in fields)
|
||||
{
|
||||
var allAttributes = fieldSymbol.GetAttributes();
|
||||
|
||||
var hasAttribute = allAttributes
|
||||
.Any(
|
||||
attr =>
|
||||
SymbolEqualityComparer.Default.Equals(attr.AttributeClass, serializableFieldAttribute)
|
||||
);
|
||||
|
||||
if (hasAttribute)
|
||||
{
|
||||
foreach (var attr in allAttributes)
|
||||
{
|
||||
if (!SymbolEqualityComparer.Default.Equals(attr.AttributeClass, serializableFieldAttrAttribute))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (attr.AttributeClass == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var ctorArgs = attr.ConstructorArguments;
|
||||
var attrTypeArg = ctorArgs[0];
|
||||
|
||||
if (attrTypeArg.Kind == TypedConstantKind.Primitive && attrTypeArg.Value is string attrStr)
|
||||
{
|
||||
source.AppendLine($" {attrStr}");
|
||||
}
|
||||
else
|
||||
{
|
||||
var attrType = (ITypeSymbol)attrTypeArg.Value;
|
||||
source.GenerateAttribute(attrType.Name, ctorArgs[1].Values);
|
||||
}
|
||||
}
|
||||
|
||||
source.GenerateSerializableProperty(fieldSymbol);
|
||||
source.AppendLine();
|
||||
|
||||
var serializableProperty = SerializableMigrationRulesEngine.GenerateSerializableProperty(
|
||||
compilation,
|
||||
fieldSymbol.GetPropertyName(),
|
||||
fieldSymbol.Type,
|
||||
allAttributes,
|
||||
serializableTypes
|
||||
);
|
||||
|
||||
serializableProperties.Add(serializableProperty);
|
||||
}
|
||||
}
|
||||
|
||||
// If we are not inheriting ISerializable, then we need to define some stuff
|
||||
if (!isOverride)
|
||||
{
|
||||
// long ISerializable.SavePosition { get; set; }
|
||||
source.GenerateAutoProperty(
|
||||
AccessModifier.None,
|
||||
"long",
|
||||
"ISerializable.SavePosition",
|
||||
AccessModifier.None,
|
||||
AccessModifier.None
|
||||
);
|
||||
source.AppendLine();
|
||||
|
||||
// BufferWriter ISerializable.SaveBuffer { get; set; }
|
||||
source.GenerateAutoProperty(
|
||||
AccessModifier.None,
|
||||
"BufferWriter",
|
||||
"ISerializable.SaveBuffer",
|
||||
AccessModifier.None,
|
||||
AccessModifier.None
|
||||
);
|
||||
source.AppendLine();
|
||||
}
|
||||
|
||||
// Serial constructor
|
||||
source.GenerateSerialCtor(context, className, isOverride);
|
||||
source.AppendLine();
|
||||
|
||||
var versionValue = int.Parse(version);
|
||||
List<SerializableMetadata> migrations;
|
||||
|
||||
if (versionValue > 0)
|
||||
{
|
||||
migrations = SerializableMigration.GetMigrations(
|
||||
migrationPath,
|
||||
classSymbol,
|
||||
versionValue,
|
||||
jsonSerializerOptions
|
||||
);
|
||||
|
||||
for (var i = 0; i < migrations.Count; i++)
|
||||
{
|
||||
var migration = migrations[i];
|
||||
if (migration.Version < versionValue)
|
||||
{
|
||||
source.GenerateMigrationContentStruct(migration);
|
||||
source.AppendLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
migrations = new List<SerializableMetadata>();
|
||||
}
|
||||
|
||||
// Serialize Method
|
||||
source.GenerateSerializeMethod(
|
||||
compilation,
|
||||
isOverride,
|
||||
serializableProperties
|
||||
);
|
||||
source.AppendLine();
|
||||
|
||||
// Deserialize Method
|
||||
source.GenerateDeserializeMethod(
|
||||
compilation,
|
||||
isOverride,
|
||||
versionValue,
|
||||
migrations,
|
||||
serializableProperties
|
||||
);
|
||||
|
||||
source.GenerateClassEnd();
|
||||
source.GenerateNamespaceEnd();
|
||||
|
||||
// Write the migration file
|
||||
var newMigration = new SerializableMetadata
|
||||
{
|
||||
Version = versionValue,
|
||||
Type = classSymbol.ToDisplayString(),
|
||||
Properties = serializableProperties
|
||||
};
|
||||
SerializableMigration.WriteMigration(migrationPath, newMigration, jsonSerializerOptions);
|
||||
|
||||
return source.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,95 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: SerializableEntityGeneration.DeserializeMethod.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.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public static partial class SerializableEntityGeneration
|
||||
{
|
||||
public static void GenerateDeserializeMethod(
|
||||
this StringBuilder source,
|
||||
Compilation compilation,
|
||||
bool isOverride,
|
||||
int version,
|
||||
List<SerializableMetadata> migrations,
|
||||
List<SerializableProperty> properties
|
||||
)
|
||||
{
|
||||
var genericReaderInterface = compilation.GetTypeByMetadataName(GENERIC_READER_INTERFACE);
|
||||
|
||||
source.GenerateMethodStart(
|
||||
"Deserialize",
|
||||
AccessModifier.Public,
|
||||
isOverride,
|
||||
"void",
|
||||
ImmutableArray.Create<(ITypeSymbol, string)>((genericReaderInterface, "reader"))
|
||||
);
|
||||
|
||||
const string indent = " ";
|
||||
|
||||
// Version
|
||||
source.AppendLine($"{indent}var version = reader.ReadEncodedInt();");
|
||||
|
||||
if (version > 0)
|
||||
{
|
||||
var nextVersion = 0;
|
||||
|
||||
for (var i = 0; i < migrations.Count; i++)
|
||||
{
|
||||
var migrationVersion = migrations[i].Version;
|
||||
if (migrationVersion == nextVersion)
|
||||
{
|
||||
nextVersion++;
|
||||
}
|
||||
|
||||
source.AppendLine();
|
||||
source.AppendLine($"{indent}if (version == {migrationVersion})");
|
||||
source.AppendLine($"{indent}{{");
|
||||
source.AppendLine($"{indent} MigrateFrom(new V{migrationVersion}Content(reader));");
|
||||
source.AppendLine($"{indent} ((Server.ISerializable)this).MarkDirty();");
|
||||
source.AppendLine($"{indent} return;");
|
||||
source.AppendLine($"{indent}}}");
|
||||
}
|
||||
|
||||
if (nextVersion < version)
|
||||
{
|
||||
source.AppendLine();
|
||||
source.AppendLine($"{indent}if (version < _version)");
|
||||
source.AppendLine($"{indent}{{");
|
||||
source.AppendLine($"{indent} Deserialize(reader, version);");
|
||||
source.AppendLine($"{indent} ((Server.ISerializable)this).MarkDirty();");
|
||||
source.AppendLine($"{indent} return;");
|
||||
source.AppendLine($"{indent}}}");
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var property in properties)
|
||||
{
|
||||
source.AppendLine();
|
||||
SerializableMigrationRulesEngine.Rules[property.Rule].GenerateDeserializationMethod(
|
||||
source,
|
||||
indent,
|
||||
property
|
||||
);
|
||||
}
|
||||
|
||||
source.GenerateMethodEnd();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,161 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: SerializableEntityGeneration.MetadataTypes.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.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public static partial class SerializableEntityGeneration
|
||||
{
|
||||
public const string LIST_CLASS = "System.Collections.Generic.List`1";
|
||||
public const string HASHSET_CLASS = "System.Collections.Generic.HashSet`1";
|
||||
public const string IP_CLASS = "System.Net.IPAddress";
|
||||
public const string KEYVALUEPAIR_STRUCT = "System.Collections.Generic.KeyValuePair";
|
||||
|
||||
public const string SERIALIZABLE_ATTRIBUTE = "Server.SerializableAttribute";
|
||||
public const string SERIALIZABLE_FIELD_ATTRIBUTE = "Server.SerializableFieldAttribute";
|
||||
public const string SERIALIZABLE_FIELD_ATTR_ATTRIBUTE = "Server.SerializableFieldAttrAttribute";
|
||||
public const string SERIALIZABLE_INTERFACE = "Server.ISerializable";
|
||||
public const string GENERIC_WRITER_INTERFACE = "Server.IGenericWriter";
|
||||
public const string GENERIC_READER_INTERFACE = "Server.IGenericReader";
|
||||
public const string DELTA_DATE_TIME_ATTRIBUTE = "Server.DeltaDateTimeAttribute";
|
||||
public const string POINT2D_STRUCT = "Server.Point2D";
|
||||
public const string POINT3D_STRUCT = "Server.Point3D";
|
||||
public const string RECTANGLE2D_STRUCT = "Server.Rectangle2D";
|
||||
public const string RECTANGLE3D_STRUCT = "Server.Rectangle3D";
|
||||
public const string RACE_CLASS = "Server.Race";
|
||||
public const string MAP_CLASS = "Server.Map";
|
||||
|
||||
public static bool IsDeltaDateTime(this AttributeData attr, Compilation compilation) =>
|
||||
attr?.IsAttribute(compilation.GetTypeByMetadataName(DELTA_DATE_TIME_ATTRIBUTE)) == true;
|
||||
|
||||
public static bool IsAttribute(this AttributeData attr, ISymbol symbol) =>
|
||||
attr?.AttributeClass?.Equals(symbol, SymbolEqualityComparer.Default) == true;
|
||||
|
||||
public static bool IsEnum(this ITypeSymbol symbol) =>
|
||||
symbol.SpecialType == SpecialType.System_Enum || symbol.TypeKind == TypeKind.Enum;
|
||||
|
||||
public static bool HasSerializableInterface(
|
||||
this ITypeSymbol symbol,
|
||||
Compilation compilation,
|
||||
ImmutableArray<INamedTypeSymbol> serializableTypes
|
||||
) =>
|
||||
symbol.ContainsInterface(compilation.GetTypeByMetadataName(SERIALIZABLE_INTERFACE)) ||
|
||||
serializableTypes.Contains(symbol);
|
||||
|
||||
public static bool Contains(this ImmutableArray<INamedTypeSymbol> symbols, ITypeSymbol symbol) =>
|
||||
symbol is INamedTypeSymbol namedSymbol &&
|
||||
symbols.Contains(namedSymbol, SymbolEqualityComparer.Default);
|
||||
|
||||
public static bool HasGenericReaderCtor(this INamedTypeSymbol symbol, Compilation compilation, out bool requiresParent)
|
||||
{
|
||||
var genericReaderInterface = compilation.GetTypeByMetadataName(GENERIC_READER_INTERFACE);
|
||||
var genericCtor = symbol.Constructors.FirstOrDefault(
|
||||
m => !m.IsStatic &&
|
||||
m.MethodKind == MethodKind.Constructor &&
|
||||
m.Parameters.Length <= 2 &&
|
||||
m.Parameters[0].Equals(genericReaderInterface, SymbolEqualityComparer.Default)
|
||||
);
|
||||
|
||||
requiresParent = genericCtor?.Parameters.Length == 2 && genericCtor.Parameters[1].Equals(symbol, SymbolEqualityComparer.Default);
|
||||
return genericCtor != null;
|
||||
}
|
||||
|
||||
public static bool HasPublicSerializeMethod(
|
||||
this ITypeSymbol symbol,
|
||||
Compilation compilation,
|
||||
ImmutableArray<INamedTypeSymbol> serializableTypes
|
||||
)
|
||||
{
|
||||
if (symbol.HasSerializableInterface(compilation, serializableTypes))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
var genericWriterInterface = compilation.GetTypeByMetadataName(GENERIC_WRITER_INTERFACE);
|
||||
|
||||
return symbol.GetAllMethods("Serialize")
|
||||
.Any(
|
||||
m => !m.IsStatic &&
|
||||
m.ReturnsVoid &&
|
||||
m.Parameters.Length == 1 &&
|
||||
m.Parameters[0].Equals(genericWriterInterface, SymbolEqualityComparer.Default) &&
|
||||
m.DeclaredAccessibility == Accessibility.Public
|
||||
);
|
||||
}
|
||||
|
||||
public static bool IsPoint2D(this ISymbol symbol, Compilation compilation) =>
|
||||
symbol.Equals(
|
||||
compilation.GetTypeByMetadataName(POINT2D_STRUCT),
|
||||
SymbolEqualityComparer.Default
|
||||
);
|
||||
|
||||
public static bool IsPoint3D(this ISymbol symbol, Compilation compilation) =>
|
||||
symbol.Equals(
|
||||
compilation.GetTypeByMetadataName(POINT3D_STRUCT),
|
||||
SymbolEqualityComparer.Default
|
||||
);
|
||||
|
||||
public static bool IsRectangle2D(this ISymbol symbol, Compilation compilation) =>
|
||||
symbol.Equals(
|
||||
compilation.GetTypeByMetadataName(RECTANGLE2D_STRUCT),
|
||||
SymbolEqualityComparer.Default
|
||||
);
|
||||
|
||||
public static bool IsRectangle3D(this ISymbol symbol, Compilation compilation) =>
|
||||
symbol.Equals(
|
||||
compilation.GetTypeByMetadataName(RECTANGLE3D_STRUCT),
|
||||
SymbolEqualityComparer.Default
|
||||
);
|
||||
|
||||
public static bool IsIpAddress(this ISymbol symbol, Compilation compilation) =>
|
||||
symbol.Equals(
|
||||
compilation.GetTypeByMetadataName(IP_CLASS),
|
||||
SymbolEqualityComparer.Default
|
||||
);
|
||||
|
||||
public static bool IsRace(this ISymbol symbol, Compilation compilation) =>
|
||||
symbol.Equals(
|
||||
compilation.GetTypeByMetadataName(RACE_CLASS),
|
||||
SymbolEqualityComparer.Default
|
||||
);
|
||||
|
||||
public static bool IsMap(this ISymbol symbol, Compilation compilation) =>
|
||||
symbol.Equals(
|
||||
compilation.GetTypeByMetadataName(MAP_CLASS),
|
||||
SymbolEqualityComparer.Default
|
||||
);
|
||||
|
||||
public static bool IsKeyValuePair(this ISymbol symbol, Compilation compilation) =>
|
||||
(symbol as INamedTypeSymbol)?.ConstructedFrom.Equals(
|
||||
compilation.GetTypeByMetadataName(KEYVALUEPAIR_STRUCT),
|
||||
SymbolEqualityComparer.Default
|
||||
) == true;
|
||||
|
||||
public static bool IsList(this ISymbol symbol, Compilation compilation) =>
|
||||
(symbol as INamedTypeSymbol)?.ConstructedFrom.Equals(
|
||||
compilation.GetTypeByMetadataName(LIST_CLASS),
|
||||
SymbolEqualityComparer.Default
|
||||
) == true;
|
||||
|
||||
public static bool IsHashSet(this ISymbol symbol, Compilation compilation) =>
|
||||
(symbol as INamedTypeSymbol)?.ConstructedFrom.Equals(
|
||||
compilation.GetTypeByMetadataName(HASHSET_CLASS),
|
||||
SymbolEqualityComparer.Default
|
||||
) == true;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: SerializableEntityGeneration.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.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public static partial class SerializableEntityGeneration
|
||||
{
|
||||
public static void GenerateSerializableProperty(
|
||||
this StringBuilder source,
|
||||
IFieldSymbol fieldSymbol
|
||||
)
|
||||
{
|
||||
var fieldName = fieldSymbol.Name;
|
||||
|
||||
source.GeneratePropertyStart(AccessModifier.Public, fieldSymbol);
|
||||
|
||||
// Getter
|
||||
source.GeneratePropertyGetterReturnsField(fieldSymbol);
|
||||
|
||||
// Setter
|
||||
source.GeneratePropertySetterStart(false);
|
||||
source.AppendLine(
|
||||
$@" if (value != {fieldName})
|
||||
{{
|
||||
((ISerializable)this).MarkDirty();
|
||||
{fieldName} = value;
|
||||
}}"
|
||||
);
|
||||
source.GeneratePropertyGetSetEnd(false);
|
||||
|
||||
source.GeneratePropertyEnd();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: SerializableEntityGeneration.SerialCtor.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.Collections.Immutable;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public static partial class SerializableEntityGeneration
|
||||
{
|
||||
private static readonly ImmutableArray<string> _baseParameters = new[] { "serial" }.ToImmutableArray();
|
||||
public static void GenerateSerialCtor(
|
||||
this StringBuilder source,
|
||||
GeneratorExecutionContext context,
|
||||
string className,
|
||||
bool isOverride
|
||||
)
|
||||
{
|
||||
var serialType = (ITypeSymbol)context.Compilation.GetTypeByMetadataName("Server.Serial");
|
||||
|
||||
source.GenerateConstructorStart(
|
||||
className,
|
||||
AccessModifier.Public,
|
||||
new []{ (serialType, "serial") }.ToImmutableArray(),
|
||||
isOverride ? _baseParameters : ImmutableArray<string>.Empty
|
||||
);
|
||||
|
||||
if (!isOverride)
|
||||
{
|
||||
source.Append(@$" Serial = serial;
|
||||
SetTypeRef(typeof({className}));");
|
||||
}
|
||||
|
||||
source.GenerateMethodEnd();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: SerializableEntityGeneration.SerializeMethod.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.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public static partial class SerializableEntityGeneration
|
||||
{
|
||||
public static void GenerateSerializeMethod(
|
||||
this StringBuilder source,
|
||||
Compilation compilation,
|
||||
bool isOverride,
|
||||
List<SerializableProperty> properties
|
||||
)
|
||||
{
|
||||
var genericWriterInterface = compilation.GetTypeByMetadataName(GENERIC_WRITER_INTERFACE);
|
||||
|
||||
source.GenerateMethodStart(
|
||||
"Serialize",
|
||||
AccessModifier.Public,
|
||||
isOverride,
|
||||
"void",
|
||||
ImmutableArray.Create<(ITypeSymbol, string)>((genericWriterInterface, "writer"))
|
||||
);
|
||||
|
||||
const string indent = " ";
|
||||
|
||||
source.AppendLine($"{indent}var savePosition = ((Server.ISerializable)this).SavePosition;");
|
||||
source.AppendLine(@$"{indent}if (savePosition > -1)
|
||||
{indent}{{
|
||||
{indent} writer.Seek(savePosition, System.IO.SeekOrigin.Begin);
|
||||
{indent} return;
|
||||
{indent}}}");
|
||||
|
||||
// Version
|
||||
source.AppendLine();
|
||||
source.AppendLine($"{indent}writer.WriteEncodedInt(_version);");
|
||||
|
||||
foreach (var property in properties)
|
||||
{
|
||||
source.AppendLine();
|
||||
SerializableMigrationRulesEngine.Rules[property.Rule].GenerateSerializationMethod(
|
||||
source,
|
||||
indent,
|
||||
property
|
||||
);
|
||||
}
|
||||
|
||||
source.GenerateMethodEnd();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: SerializableMigrationRule.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.Collections.Immutable;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public interface ISerializableMigrationRule
|
||||
{
|
||||
string RuleName { get; }
|
||||
|
||||
bool GenerateRuleState(
|
||||
Compilation compilation,
|
||||
ISymbol symbol,
|
||||
ImmutableArray<AttributeData> attributes,
|
||||
ImmutableArray<INamedTypeSymbol> serializableTypes,
|
||||
out string[] ruleArguments
|
||||
);
|
||||
|
||||
void GenerateDeserializationMethod(
|
||||
StringBuilder source,
|
||||
string indent,
|
||||
SerializableProperty property
|
||||
);
|
||||
|
||||
void GenerateSerializationMethod(
|
||||
StringBuilder source,
|
||||
string indent,
|
||||
SerializableProperty property
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,121 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: ArrayMigrationRule.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.Collections.Immutable;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public class ArrayMigrationRule : ISerializableMigrationRule
|
||||
{
|
||||
public string RuleName => nameof(ArrayMigrationRule);
|
||||
|
||||
public bool GenerateRuleState(
|
||||
Compilation compilation,
|
||||
ISymbol symbol,
|
||||
ImmutableArray<AttributeData> attributes,
|
||||
ImmutableArray<INamedTypeSymbol> serializableTypes,
|
||||
out string[] ruleArguments
|
||||
)
|
||||
{
|
||||
if (symbol is not IArrayTypeSymbol arrayTypeSymbol)
|
||||
{
|
||||
ruleArguments = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
var serializableArrayType = SerializableMigrationRulesEngine.GenerateSerializableProperty(
|
||||
compilation,
|
||||
"ArrayEntry",
|
||||
arrayTypeSymbol.ElementType,
|
||||
attributes,
|
||||
serializableTypes
|
||||
);
|
||||
|
||||
var length = serializableArrayType.RuleArguments.Length;
|
||||
ruleArguments = new string[length + 2];
|
||||
ruleArguments[0] = arrayTypeSymbol.ElementType.ToDisplayString();
|
||||
ruleArguments[1] = serializableArrayType.Rule;
|
||||
Array.Copy(serializableArrayType.RuleArguments, 0, ruleArguments, 2, length);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||
{
|
||||
const string expectedRule = nameof(ArrayMigrationRule);
|
||||
var ruleName = property.Rule;
|
||||
if (expectedRule != ruleName)
|
||||
{
|
||||
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
|
||||
}
|
||||
var ruleArguments = property.RuleArguments;
|
||||
var arrayElementRule = SerializableMigrationRulesEngine.Rules[ruleArguments[1]];
|
||||
var arrayElementRuleArguments = new string[ruleArguments.Length - 2];
|
||||
Array.Copy(ruleArguments, 2, arrayElementRuleArguments, 0, ruleArguments.Length - 2);
|
||||
|
||||
var propertyIndex = $"{property.Name}Index";
|
||||
source.AppendLine($"{indent}{property.Name} = new {ruleArguments[0]}[reader.ReadEncodedInt()];");
|
||||
source.AppendLine($"{indent}for (var {propertyIndex} = 0; {propertyIndex} < {property.Name}.Length; {propertyIndex}++)");
|
||||
source.AppendLine($"{indent}{{");
|
||||
|
||||
var serializableArrayElement = new SerializableProperty
|
||||
{
|
||||
Name = $"{property.Name}[{propertyIndex}]",
|
||||
Type = ruleArguments[0],
|
||||
Rule = arrayElementRule.RuleName,
|
||||
RuleArguments = arrayElementRuleArguments
|
||||
};
|
||||
|
||||
arrayElementRule.GenerateDeserializationMethod(source, $"{indent} ", serializableArrayElement);
|
||||
|
||||
source.AppendLine($"{indent}}}");
|
||||
}
|
||||
|
||||
public void GenerateSerializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||
{
|
||||
const string expectedRule = nameof(ArrayMigrationRule);
|
||||
var ruleName = property.Rule;
|
||||
if (expectedRule != ruleName)
|
||||
{
|
||||
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
|
||||
}
|
||||
|
||||
var ruleArguments = property.RuleArguments;
|
||||
var arrayElementRule = SerializableMigrationRulesEngine.Rules[ruleArguments[1]];
|
||||
var arrayElementRuleArguments = new string[ruleArguments.Length - 2];
|
||||
Array.Copy(ruleArguments, 2, arrayElementRuleArguments, 0, ruleArguments.Length - 2);
|
||||
|
||||
var propertyIndex = $"{property.Name}Index";
|
||||
source.AppendLine($"{indent}writer.WriteEncodedInt({property.Name}.Length);");
|
||||
source.AppendLine($"{indent}for (var {propertyIndex} = 0; {propertyIndex} < {property.Name}.Length; {propertyIndex}++)");
|
||||
source.AppendLine($"{indent}{{");
|
||||
|
||||
var serializableArrayElement = new SerializableProperty
|
||||
{
|
||||
Name = $"{property.Name}[{propertyIndex}]",
|
||||
Type = ruleArguments[0],
|
||||
Rule = arrayElementRule.RuleName,
|
||||
RuleArguments = arrayElementRuleArguments
|
||||
};
|
||||
|
||||
arrayElementRule.GenerateSerializationMethod(source, $"{indent} ", serializableArrayElement);
|
||||
|
||||
source.AppendLine($"{indent}}}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,134 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: HashSetMigrationRule.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.Collections.Immutable;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public class HashSetMigrationRule : ISerializableMigrationRule
|
||||
{
|
||||
public string RuleName => nameof(HashSetMigrationRule);
|
||||
|
||||
public bool GenerateRuleState(
|
||||
Compilation compilation,
|
||||
ISymbol symbol,
|
||||
ImmutableArray<AttributeData> attributes,
|
||||
ImmutableArray<INamedTypeSymbol> serializableTypes,
|
||||
out string[] ruleArguments
|
||||
)
|
||||
{
|
||||
if (symbol is not INamedTypeSymbol namedTypeSymbol || !symbol.IsHashSet(compilation))
|
||||
{
|
||||
ruleArguments = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
var setTypeSymbol = namedTypeSymbol.TypeArguments[0];
|
||||
|
||||
var serializableSetType = SerializableMigrationRulesEngine.GenerateSerializableProperty(
|
||||
compilation,
|
||||
"SetEntry",
|
||||
setTypeSymbol,
|
||||
attributes,
|
||||
serializableTypes
|
||||
);
|
||||
|
||||
var length = serializableSetType.RuleArguments.Length;
|
||||
ruleArguments = new string[length + 2];
|
||||
ruleArguments[0] = setTypeSymbol.ToDisplayString();
|
||||
ruleArguments[1] = serializableSetType.Rule;
|
||||
Array.Copy(serializableSetType.RuleArguments, 0, ruleArguments, 2, length);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||
{
|
||||
const string expectedRule = nameof(HashSetMigrationRule);
|
||||
var ruleName = property.Rule;
|
||||
if (expectedRule != ruleName)
|
||||
{
|
||||
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
|
||||
}
|
||||
|
||||
var ruleArguments = property.RuleArguments;
|
||||
var setElementRule = SerializableMigrationRulesEngine.Rules[ruleArguments[1]];
|
||||
var setElementRuleArguments = new string[ruleArguments.Length - 2];
|
||||
Array.Copy(ruleArguments, 2, setElementRuleArguments, 0, ruleArguments.Length - 2);
|
||||
|
||||
var propertyName = property.Name;
|
||||
var propertyVarPrefix = $"{char.ToLower(propertyName[0])}{propertyName.Substring(1, propertyName.Length - 1)}";
|
||||
var propertyIndex = $"{propertyVarPrefix}Index";
|
||||
var propertyEntry = $"{propertyVarPrefix}Entry";
|
||||
var propertyCount = $"{propertyVarPrefix}Count";
|
||||
|
||||
source.AppendLine($"{indent}{ruleArguments[0]} {propertyEntry};");
|
||||
source.AppendLine($"{indent}var {propertyCount} = reader.ReadEncodedInt();");
|
||||
source.AppendLine($"{indent}{property.Name} = new System.Collections.Generic.HashSet<{ruleArguments[0]}>({propertyCount});");
|
||||
source.AppendLine($"{indent}for (var {propertyIndex} = 0; i < {propertyCount}; {propertyIndex}++)");
|
||||
source.AppendLine($"{indent}{{");
|
||||
|
||||
var serializableSetElement = new SerializableProperty
|
||||
{
|
||||
Name = propertyEntry,
|
||||
Type = ruleArguments[0],
|
||||
Rule = setElementRule.RuleName,
|
||||
RuleArguments = setElementRuleArguments
|
||||
};
|
||||
|
||||
setElementRule.GenerateDeserializationMethod(source, $"{indent} ", serializableSetElement);
|
||||
source.AppendLine($"{indent} {property.Name}.Add({propertyEntry});");
|
||||
|
||||
source.AppendLine($"{indent}}}");
|
||||
}
|
||||
|
||||
public void GenerateSerializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||
{
|
||||
const string expectedRule = nameof(HashSetMigrationRule);
|
||||
var ruleName = property.Rule;
|
||||
if (expectedRule != ruleName)
|
||||
{
|
||||
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
|
||||
}
|
||||
|
||||
var ruleArguments = property.RuleArguments;
|
||||
var setElementRule = SerializableMigrationRulesEngine.Rules[ruleArguments[1]];
|
||||
var setElementRuleArguments = new string[ruleArguments.Length - 2];
|
||||
Array.Copy(ruleArguments, 2, setElementRuleArguments, 0, ruleArguments.Length - 2);
|
||||
|
||||
var propertyName = property.Name;
|
||||
var propertyVarPrefix = $"{char.ToLower(propertyName[0])}{propertyName.Substring(1, propertyName.Length - 1)}";
|
||||
var propertyEntry = $"{propertyVarPrefix}Entry";
|
||||
source.AppendLine($"{indent}writer.WriteEncodedInt({property.Name}.Count);");
|
||||
source.AppendLine($"{indent}foreach (var {propertyEntry} in {property.Name});");
|
||||
source.AppendLine($"{indent}{{");
|
||||
|
||||
var serializableSetElement = new SerializableProperty
|
||||
{
|
||||
Name = propertyEntry,
|
||||
Type = ruleArguments[0],
|
||||
Rule = setElementRule.RuleName,
|
||||
RuleArguments = setElementRuleArguments
|
||||
};
|
||||
|
||||
setElementRule.GenerateSerializationMethod(source, $"{indent} ", serializableSetElement);
|
||||
|
||||
source.AppendLine($"{indent}}}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,179 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: KeyValuePairMigrationRule.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.Collections.Immutable;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public class KeyValuePairMigrationRule : ISerializableMigrationRule
|
||||
{
|
||||
public string RuleName => nameof(KeyValuePairMigrationRule);
|
||||
|
||||
public bool GenerateRuleState(
|
||||
Compilation compilation,
|
||||
ISymbol symbol,
|
||||
ImmutableArray<AttributeData> attributes,
|
||||
ImmutableArray<INamedTypeSymbol> serializableTypes,
|
||||
out string[] ruleArguments
|
||||
)
|
||||
{
|
||||
if (symbol is not INamedTypeSymbol namedTypeSymbol || !symbol.IsKeyValuePair(compilation))
|
||||
{
|
||||
ruleArguments = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
var typeArguments = namedTypeSymbol.TypeArguments;
|
||||
|
||||
var keySerializedProperty = SerializableMigrationRulesEngine.GenerateSerializableProperty(
|
||||
compilation,
|
||||
"key",
|
||||
typeArguments[0],
|
||||
attributes,
|
||||
serializableTypes
|
||||
);
|
||||
|
||||
var valueSerializedProperty = SerializableMigrationRulesEngine.GenerateSerializableProperty(
|
||||
compilation,
|
||||
"value",
|
||||
typeArguments[1],
|
||||
attributes,
|
||||
serializableTypes
|
||||
);
|
||||
|
||||
// Key
|
||||
ruleArguments = new string[5 + keySerializedProperty.RuleArguments.Length + valueSerializedProperty.RuleArguments.Length];
|
||||
ruleArguments[0] = typeArguments[0].ToDisplayString();
|
||||
ruleArguments[1] = keySerializedProperty.Rule;
|
||||
ruleArguments[2] = keySerializedProperty.RuleArguments.Length.ToString();
|
||||
Array.Copy(keySerializedProperty.RuleArguments, 0, ruleArguments, 2, keySerializedProperty.RuleArguments.Length);
|
||||
|
||||
// Value
|
||||
var valueIndex = 3 + keySerializedProperty.RuleArguments.Length;
|
||||
ruleArguments[valueIndex++] = typeArguments[1].ToDisplayString();
|
||||
ruleArguments[valueIndex++] = valueSerializedProperty.Rule;
|
||||
Array.Copy(valueSerializedProperty.RuleArguments, 0, ruleArguments, valueIndex, valueSerializedProperty.RuleArguments.Length);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||
{
|
||||
const string expectedRule = nameof(KeyValuePairMigrationRule);
|
||||
var ruleName = property.Rule;
|
||||
if (expectedRule != ruleName)
|
||||
{
|
||||
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
|
||||
}
|
||||
|
||||
var ruleArguments = property.RuleArguments;
|
||||
var keyType = ruleArguments[0];
|
||||
var keyRule = SerializableMigrationRulesEngine.Rules[ruleArguments[1]];
|
||||
var keyRuleArguments = new string[int.Parse(ruleArguments[2])];
|
||||
Array.Copy(ruleArguments, 3, keyRuleArguments, 0, keyRuleArguments.Length);
|
||||
|
||||
var serializableKeyProperty = new SerializableProperty
|
||||
{
|
||||
Name = "key",
|
||||
Type = keyType,
|
||||
Rule = keyRule.RuleName,
|
||||
RuleArguments = keyRuleArguments
|
||||
};
|
||||
|
||||
keyRule.GenerateDeserializationMethod(
|
||||
source,
|
||||
indent,
|
||||
serializableKeyProperty
|
||||
);
|
||||
|
||||
var valueIndex = 3 + keyRuleArguments.Length;
|
||||
var valueType = ruleArguments[valueIndex++];
|
||||
var valueRule = SerializableMigrationRulesEngine.Rules[ruleArguments[valueIndex++]];
|
||||
var valueRuleArguments = new string[ruleArguments.Length - valueIndex];
|
||||
Array.Copy(ruleArguments, valueIndex, valueRuleArguments, 0, valueRuleArguments.Length);
|
||||
|
||||
var serializableValueProperty = new SerializableProperty
|
||||
{
|
||||
Name = "value",
|
||||
Type = valueType,
|
||||
Rule = valueRule.RuleName,
|
||||
RuleArguments = valueRuleArguments
|
||||
};
|
||||
|
||||
keyRule.GenerateDeserializationMethod(
|
||||
source,
|
||||
indent,
|
||||
serializableValueProperty
|
||||
);
|
||||
|
||||
source.AppendLine(
|
||||
$"{indent}{property.Name} = new {SerializableEntityGeneration.KEYVALUEPAIR_STRUCT}<{keyType}, {valueType}>(key, value);"
|
||||
);
|
||||
}
|
||||
|
||||
public void GenerateSerializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||
{
|
||||
const string expectedRule = nameof(KeyValuePairMigrationRule);
|
||||
var ruleName = property.Rule;
|
||||
if (expectedRule != ruleName)
|
||||
{
|
||||
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
|
||||
}
|
||||
|
||||
var ruleArguments = property.RuleArguments;
|
||||
var keyType = ruleArguments[0];
|
||||
var keyRule = SerializableMigrationRulesEngine.Rules[ruleArguments[1]];
|
||||
var keyRuleArguments = new string[int.Parse(ruleArguments[2])];
|
||||
Array.Copy(ruleArguments, 3, keyRuleArguments, 0, keyRuleArguments.Length);
|
||||
|
||||
var serializableKeyProperty = new SerializableProperty
|
||||
{
|
||||
Name = $"{property.Name}.Key",
|
||||
Type = keyType,
|
||||
Rule = keyRule.RuleName,
|
||||
RuleArguments = keyRuleArguments
|
||||
};
|
||||
|
||||
keyRule.GenerateSerializationMethod(
|
||||
source,
|
||||
indent,
|
||||
serializableKeyProperty
|
||||
);
|
||||
|
||||
var valueIndex = 3 + keyRuleArguments.Length;
|
||||
var valueType = ruleArguments[valueIndex++];
|
||||
var valueRule = SerializableMigrationRulesEngine.Rules[ruleArguments[valueIndex++]];
|
||||
var valueRuleArguments = new string[ruleArguments.Length - valueIndex];
|
||||
Array.Copy(ruleArguments, valueIndex, valueRuleArguments, 0, valueRuleArguments.Length);
|
||||
|
||||
var serializableValueProperty = new SerializableProperty
|
||||
{
|
||||
Name = $"{property.Name}.Value",
|
||||
Type = valueType,
|
||||
Rule = valueRule.RuleName,
|
||||
RuleArguments = valueRuleArguments
|
||||
};
|
||||
|
||||
keyRule.GenerateSerializationMethod(
|
||||
source,
|
||||
indent,
|
||||
serializableValueProperty
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,133 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: ListMigrationRule.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.Collections.Immutable;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public class ListMigrationRule : ISerializableMigrationRule
|
||||
{
|
||||
public string RuleName => nameof(ListMigrationRule);
|
||||
|
||||
public bool GenerateRuleState(
|
||||
Compilation compilation,
|
||||
ISymbol symbol,
|
||||
ImmutableArray<AttributeData> attributes,
|
||||
ImmutableArray<INamedTypeSymbol> serializableTypes,
|
||||
out string[] ruleArguments
|
||||
)
|
||||
{
|
||||
if (symbol is not INamedTypeSymbol namedTypeSymbol || !symbol.IsList(compilation))
|
||||
{
|
||||
ruleArguments = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
var listTypeSymbol = namedTypeSymbol.TypeArguments[0];
|
||||
|
||||
var serializableListType = SerializableMigrationRulesEngine.GenerateSerializableProperty(
|
||||
compilation,
|
||||
"ListEntry",
|
||||
listTypeSymbol,
|
||||
attributes,
|
||||
serializableTypes
|
||||
);
|
||||
|
||||
var length = serializableListType.RuleArguments.Length;
|
||||
ruleArguments = new string[length + 2];
|
||||
ruleArguments[0] = listTypeSymbol.ToDisplayString();
|
||||
ruleArguments[1] = serializableListType.Rule;
|
||||
Array.Copy(serializableListType.RuleArguments, 0, ruleArguments, 2, length);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||
{
|
||||
const string expectedRule = nameof(ListMigrationRule);
|
||||
var ruleName = property.Rule;
|
||||
if (expectedRule != ruleName)
|
||||
{
|
||||
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
|
||||
}
|
||||
|
||||
var ruleArguments = property.RuleArguments;
|
||||
var listElementRule = SerializableMigrationRulesEngine.Rules[ruleArguments[1]];
|
||||
var listElementRuleArguments = new string[ruleArguments.Length - 2];
|
||||
Array.Copy(ruleArguments, 2, listElementRuleArguments, 0, ruleArguments.Length - 2);
|
||||
|
||||
var propertyName = property.Name;
|
||||
var propertyVarPrefix = $"{char.ToLower(propertyName[0])}{propertyName.Substring(1, propertyName.Length - 1)}";
|
||||
var propertyIndex = $"{propertyVarPrefix}Index";
|
||||
var propertyEntry = $"{propertyVarPrefix}Entry";
|
||||
var propertyCount = $"{propertyVarPrefix}Count";
|
||||
|
||||
source.AppendLine($"{indent}{ruleArguments[0]} {propertyEntry};");
|
||||
source.AppendLine($"{indent}var {propertyCount} = reader.ReadEncodedInt();");
|
||||
source.AppendLine($"{indent}{propertyName} = new System.Collections.Generic.List<{ruleArguments[0]}>({propertyCount});");
|
||||
source.AppendLine($"{indent}for (var {propertyIndex} = 0; {propertyIndex} < {propertyCount}; {propertyIndex}++)");
|
||||
source.AppendLine($"{indent}{{");
|
||||
|
||||
var serializableListElement = new SerializableProperty
|
||||
{
|
||||
Name = propertyEntry,
|
||||
Type = ruleArguments[0],
|
||||
Rule = listElementRule.RuleName,
|
||||
RuleArguments = listElementRuleArguments
|
||||
};
|
||||
|
||||
listElementRule.GenerateDeserializationMethod(source, $"{indent} ", serializableListElement);
|
||||
source.AppendLine($"{indent} {propertyName}.Add({propertyEntry});");
|
||||
|
||||
source.AppendLine($"{indent}}}");
|
||||
}
|
||||
|
||||
public void GenerateSerializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||
{
|
||||
const string expectedRule = nameof(ListMigrationRule);
|
||||
var ruleName = property.Rule;
|
||||
if (expectedRule != ruleName)
|
||||
{
|
||||
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
|
||||
}
|
||||
|
||||
var ruleArguments = property.RuleArguments;
|
||||
var listElementRule = SerializableMigrationRulesEngine.Rules[ruleArguments[1]];
|
||||
var listElementRuleArguments = new string[ruleArguments.Length - 2];
|
||||
Array.Copy(ruleArguments, 2, listElementRuleArguments, 0, ruleArguments.Length - 2);
|
||||
|
||||
var propertyName = property.Name;
|
||||
var propertyEntry = $"{char.ToLower(propertyName[0])}{propertyName.Substring(1, propertyName.Length - 1)}Entry";
|
||||
source.AppendLine($"{indent}writer.WriteEncodedInt({propertyName}.Count);");
|
||||
source.AppendLine($"{indent}foreach (var {propertyEntry} in {propertyName})");
|
||||
source.AppendLine($"{indent}{{");
|
||||
|
||||
var serializableListElement = new SerializableProperty
|
||||
{
|
||||
Name = propertyEntry,
|
||||
Type = ruleArguments[0],
|
||||
Rule = listElementRule.RuleName,
|
||||
RuleArguments = listElementRuleArguments
|
||||
};
|
||||
|
||||
listElementRule.GenerateSerializationMethod(source, $"{indent} ", serializableListElement);
|
||||
|
||||
source.AppendLine($"{indent}}}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,159 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: PrimitiveTypeMigrationRule.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.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public class PrimitiveTypeMigrationRule : ISerializableMigrationRule
|
||||
{
|
||||
public string RuleName => nameof(PrimitiveTypeMigrationRule);
|
||||
|
||||
public bool GenerateRuleState(
|
||||
Compilation compilation,
|
||||
ISymbol symbol,
|
||||
ImmutableArray<AttributeData> attributes,
|
||||
ImmutableArray<INamedTypeSymbol> serializableTypes,
|
||||
out string[] ruleArguments
|
||||
)
|
||||
{
|
||||
if (symbol.IsIpAddress(compilation))
|
||||
{
|
||||
ruleArguments = new[] { "IPAddress" };
|
||||
return true;
|
||||
}
|
||||
|
||||
if (symbol is not ITypeSymbol typeSymbol)
|
||||
{
|
||||
ruleArguments = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (
|
||||
typeSymbol.SpecialType is
|
||||
SpecialType.System_Boolean or
|
||||
SpecialType.System_SByte or
|
||||
SpecialType.System_Int16 or
|
||||
SpecialType.System_Int32 or
|
||||
SpecialType.System_Int64 or
|
||||
SpecialType.System_Byte or
|
||||
SpecialType.System_UInt16 or
|
||||
SpecialType.System_UInt32 or
|
||||
SpecialType.System_UInt64 or
|
||||
SpecialType.System_Single or
|
||||
SpecialType.System_Double or
|
||||
SpecialType.System_String or
|
||||
SpecialType.System_Decimal or
|
||||
SpecialType.System_DateTime
|
||||
)
|
||||
{
|
||||
ruleArguments = new[] { typeSymbol.SpecialType.ToString() };
|
||||
return true;
|
||||
}
|
||||
|
||||
if (typeSymbol.SpecialType == SpecialType.System_DateTime)
|
||||
{
|
||||
ruleArguments = attributes.Any(a => a.IsDeltaDateTime(compilation))
|
||||
? new[] { typeSymbol.SpecialType.ToString(), "DeltaTime" }
|
||||
: new[] { typeSymbol.SpecialType.ToString() };
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
ruleArguments = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||
{
|
||||
const string expectedRule = nameof(PrimitiveTypeMigrationRule);
|
||||
var ruleName = property.Rule;
|
||||
if (expectedRule != ruleName)
|
||||
{
|
||||
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
|
||||
}
|
||||
|
||||
var propertyName = property.Name;
|
||||
var ruleType = property.RuleArguments[0];
|
||||
string readMethod;
|
||||
|
||||
if (ruleType == "IPAddress")
|
||||
{
|
||||
readMethod = "ReadIPAddress";
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!Enum.TryParse<SpecialType>(ruleType, out var specialType))
|
||||
{
|
||||
throw new ArgumentException($"Invalid rule state for property {propertyName} ({ruleType})");
|
||||
}
|
||||
|
||||
readMethod = specialType switch
|
||||
{
|
||||
SpecialType.System_Boolean => "ReadBool",
|
||||
SpecialType.System_SByte => "ReadSByte",
|
||||
SpecialType.System_Int16 => "ReadShort",
|
||||
SpecialType.System_Int32 => "ReadInt",
|
||||
SpecialType.System_Int64 => "ReadLong",
|
||||
SpecialType.System_Byte => "ReadByte",
|
||||
SpecialType.System_UInt16 => "ReadUShort",
|
||||
SpecialType.System_UInt32 => "ReadUInt",
|
||||
SpecialType.System_UInt64 => "ReadULong",
|
||||
SpecialType.System_Single => "ReadFloat",
|
||||
SpecialType.System_Double => "ReadDouble",
|
||||
SpecialType.System_String => "ReadString",
|
||||
SpecialType.System_Decimal => "ReadDecimal",
|
||||
SpecialType.System_DateTime => property.RuleArguments.Length >= 2 &&
|
||||
property.RuleArguments[1] == "DeltaTime" ?
|
||||
"ReadDeltaTime" :
|
||||
"ReadDateTime"
|
||||
};
|
||||
}
|
||||
|
||||
source.AppendLine($"{indent}{propertyName} = reader.{readMethod}()");
|
||||
}
|
||||
|
||||
public void GenerateSerializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||
{
|
||||
const string expectedRule = nameof(PrimitiveTypeMigrationRule);
|
||||
var ruleName = property.Rule;
|
||||
if (expectedRule != ruleName)
|
||||
{
|
||||
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
|
||||
}
|
||||
|
||||
var propertyName = property.Name;
|
||||
var ruleType = property.RuleArguments[0];
|
||||
|
||||
if (!Enum.TryParse<SpecialType>(ruleType, out var specialType))
|
||||
{
|
||||
throw new ArgumentException($"Invalid rule state for property {propertyName} ({ruleType})");
|
||||
}
|
||||
|
||||
if (specialType == SpecialType.System_DateTime && property.RuleArguments[1] == "DeltaTime")
|
||||
{
|
||||
source.AppendLine($"{indent}writer.WriteDeltaTime({propertyName});");
|
||||
}
|
||||
else
|
||||
{
|
||||
source.AppendLine($"{indent}writer.Write({propertyName});");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,75 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: PrimitiveUOTypeMigrationRule.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.Collections.Immutable;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public class PrimitiveUOTypeMigrationRule : ISerializableMigrationRule
|
||||
{
|
||||
public string RuleName => nameof(PrimitiveUOTypeMigrationRule);
|
||||
|
||||
public bool GenerateRuleState(
|
||||
Compilation compilation,
|
||||
ISymbol symbol,
|
||||
ImmutableArray<AttributeData> attributes,
|
||||
ImmutableArray<INamedTypeSymbol> serializableTypes,
|
||||
out string[] ruleArguments
|
||||
)
|
||||
{
|
||||
ruleArguments = symbol switch
|
||||
{
|
||||
_ when symbol.IsPoint2D(compilation) => new[] { "Point2D" },
|
||||
_ when symbol.IsPoint3D(compilation) => new[] { "Point3D" },
|
||||
_ when symbol.IsRectangle2D(compilation) => new[] { "Rect2D" },
|
||||
_ when symbol.IsRectangle3D(compilation) => new[] { "Rect3D" },
|
||||
_ when symbol.IsRace(compilation) => new[] { "Race" },
|
||||
_ when symbol.IsMap(compilation) => new[] { "Map" },
|
||||
_ => null
|
||||
};
|
||||
|
||||
return ruleArguments != null;
|
||||
}
|
||||
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||
{
|
||||
const string expectedRule = nameof(PrimitiveUOTypeMigrationRule);
|
||||
var ruleName = property.Rule;
|
||||
if (expectedRule != ruleName)
|
||||
{
|
||||
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
|
||||
}
|
||||
|
||||
var propertyName = property.Name;
|
||||
source.AppendLine($"{indent}{propertyName} = reader.Read{property.RuleArguments[0]}()");
|
||||
}
|
||||
|
||||
public void GenerateSerializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||
{
|
||||
const string expectedRule = nameof(PrimitiveUOTypeMigrationRule);
|
||||
var ruleName = property.Rule;
|
||||
if (expectedRule != ruleName)
|
||||
{
|
||||
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
|
||||
}
|
||||
|
||||
var propertyName = property.Name;
|
||||
source.AppendLine($"{indent}writer.Write({propertyName});");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,71 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: SerializableInterfaceMigrationRule.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.Collections.Immutable;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public class SerializableInterfaceMigrationRule : ISerializableMigrationRule
|
||||
{
|
||||
public string RuleName => nameof(SerializableInterfaceMigrationRule);
|
||||
|
||||
public bool GenerateRuleState(
|
||||
Compilation compilation,
|
||||
ISymbol symbol,
|
||||
ImmutableArray<AttributeData> attributes,
|
||||
ImmutableArray<INamedTypeSymbol> serializableTypes,
|
||||
out string[] ruleArguments
|
||||
)
|
||||
{
|
||||
if (symbol is ITypeSymbol typeSymbol && typeSymbol.HasSerializableInterface(compilation, serializableTypes))
|
||||
{
|
||||
ruleArguments = Array.Empty<string>();
|
||||
return true;
|
||||
}
|
||||
|
||||
ruleArguments = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||
{
|
||||
const string expectedRule = nameof(SerializableInterfaceMigrationRule);
|
||||
var ruleName = property.Rule;
|
||||
if (expectedRule != ruleName)
|
||||
{
|
||||
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
|
||||
}
|
||||
|
||||
var propertyName = property.Name;
|
||||
source.AppendLine($"{indent}{propertyName} = reader.ReadEntity<{property.Type}>();");
|
||||
}
|
||||
|
||||
public void GenerateSerializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||
{
|
||||
const string expectedRule = nameof(SerializableInterfaceMigrationRule);
|
||||
var ruleName = property.Rule;
|
||||
if (expectedRule != ruleName)
|
||||
{
|
||||
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
|
||||
}
|
||||
|
||||
var propertyName = property.Name;
|
||||
source.AppendLine($"{indent}writer.Write({propertyName});");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,81 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: SerializationMethodSignatureMigrationRule.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.Collections.Immutable;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public class SerializationMethodSignatureMigrationRule : ISerializableMigrationRule
|
||||
{
|
||||
public string RuleName => nameof(SerializationMethodSignatureMigrationRule);
|
||||
|
||||
public bool GenerateRuleState(
|
||||
Compilation compilation,
|
||||
ISymbol symbol,
|
||||
ImmutableArray<AttributeData> attributes,
|
||||
ImmutableArray<INamedTypeSymbol> serializableTypes,
|
||||
out string[] ruleArguments
|
||||
)
|
||||
{
|
||||
if ((symbol as ITypeSymbol)?.HasPublicSerializeMethod(compilation, serializableTypes) != true)
|
||||
{
|
||||
ruleArguments = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (symbol is not INamedTypeSymbol namedTypeSymbol ||
|
||||
!namedTypeSymbol.HasGenericReaderCtor(compilation, out var requiresParent))
|
||||
{
|
||||
ruleArguments = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
ruleArguments = requiresParent ? new[] { "DeserializationRequiresParent" } : Array.Empty<string>();
|
||||
return true;
|
||||
}
|
||||
|
||||
public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||
{
|
||||
const string expectedRule = nameof(SerializationMethodSignatureMigrationRule);
|
||||
var ruleName = property.Rule;
|
||||
if (expectedRule != ruleName)
|
||||
{
|
||||
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
|
||||
}
|
||||
|
||||
var propertyName = property.Name;
|
||||
var argument = property.RuleArguments.Length >= 1 &&
|
||||
property.RuleArguments[0] == "DeserializationRequiresParent" ? ", this" : "";
|
||||
|
||||
source.AppendLine($"{indent}{propertyName} = new {property.Type}(reader{argument})");
|
||||
}
|
||||
|
||||
public void GenerateSerializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||
{
|
||||
const string expectedRule = nameof(SerializationMethodSignatureMigrationRule);
|
||||
var ruleName = property.Rule;
|
||||
if (expectedRule != ruleName)
|
||||
{
|
||||
throw new ArgumentException($"Invalid rule applied to property {ruleName}. Expecting {expectedRule}, but received {ruleName}.");
|
||||
}
|
||||
|
||||
var propertyName = property.Name;
|
||||
source.AppendLine($"{indent}{propertyName}.Serialize(writer);");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: SerializableMigration.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.Collections.Generic;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public class SerializableMetadata
|
||||
{
|
||||
[JsonPropertyName("version")]
|
||||
public int Version { get; set; }
|
||||
|
||||
[JsonPropertyName("type")]
|
||||
public string Type { get; set; }
|
||||
|
||||
[JsonPropertyName("properties")]
|
||||
public List<SerializableProperty> Properties { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public class SerializableMetadataComparer : IComparer<SerializableMetadata>
|
||||
{
|
||||
public int Compare(SerializableMetadata x, SerializableMetadata y)
|
||||
{
|
||||
if (ReferenceEquals(x, y))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ReferenceEquals(null, y))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (ReferenceEquals(null, x))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return x.Version.CompareTo(y.Version);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: SerializableMigration.ContentStruct.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.Text;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public static partial class SerializableMigration
|
||||
{
|
||||
public static void GenerateMigrationContentStruct(
|
||||
this StringBuilder source,
|
||||
SerializableMetadata migration
|
||||
)
|
||||
{
|
||||
const string indent = " ";
|
||||
|
||||
source.AppendLine($"{indent}ref struct V{migration.Version}Content");
|
||||
source.AppendLine($"{indent}{{");
|
||||
foreach (var serializableProperty in migration.Properties)
|
||||
{
|
||||
source.AppendLine($"{indent} internal readonly {serializableProperty.Type} {serializableProperty.Name};");
|
||||
}
|
||||
|
||||
source.AppendLine($"{indent} internal V{migration.Version}Content(IGenericReader reader)");
|
||||
source.AppendLine($"{indent} {{");
|
||||
|
||||
foreach (var serializableProperty in migration.Properties)
|
||||
{
|
||||
SerializableMigrationRulesEngine.Rules[serializableProperty.Rule].GenerateDeserializationMethod(
|
||||
source,
|
||||
$"{indent} ",
|
||||
serializableProperty
|
||||
);
|
||||
}
|
||||
|
||||
source.AppendLine($"{indent} }}");
|
||||
|
||||
source.AppendLine($"{indent}}}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,78 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: SerializableMigration.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.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public static partial class SerializableMigration
|
||||
{
|
||||
public static JsonSerializerOptions GetJsonSerializerOptions(Compilation compilation) =>
|
||||
new()
|
||||
{
|
||||
WriteIndented = true,
|
||||
AllowTrailingCommas = true,
|
||||
IgnoreNullValues = true,
|
||||
ReadCommentHandling = JsonCommentHandling.Skip
|
||||
};
|
||||
|
||||
public static string GetMigrationPath(GeneratorExecutionContext context)
|
||||
{
|
||||
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue(
|
||||
"build_property.SerializableMigrationPath",
|
||||
out var migrationPath
|
||||
);
|
||||
|
||||
return migrationPath;
|
||||
}
|
||||
|
||||
public static List<SerializableMetadata> GetMigrations(
|
||||
string migrationPath,
|
||||
INamedTypeSymbol typeSymbol,
|
||||
int version,
|
||||
JsonSerializerOptions options
|
||||
)
|
||||
{
|
||||
var typeName = typeSymbol.ToDisplayString();
|
||||
|
||||
var migrations = new SortedSet<SerializableMetadata>(new SerializableMetadataComparer());
|
||||
var migrationFiles = Directory.GetFiles(migrationPath, $"{typeName}.v*.json");
|
||||
|
||||
foreach (var migrationFile in migrationFiles)
|
||||
{
|
||||
var text = File.ReadAllText(migrationFile, Encoding.UTF8);
|
||||
var migration = JsonSerializer.Deserialize<SerializableMetadata>(text, options);
|
||||
if (typeName == migration!.Type && version > migration.Version)
|
||||
{
|
||||
migrations.Add(migration);
|
||||
}
|
||||
}
|
||||
|
||||
return migrations.ToList();
|
||||
}
|
||||
|
||||
public static void WriteMigration(string migrationPath, SerializableMetadata metadata, JsonSerializerOptions options)
|
||||
{
|
||||
Directory.CreateDirectory(migrationPath);
|
||||
var filePath = Path.Combine(migrationPath, $"{metadata.Type}.v{metadata.Version}.json");
|
||||
File.WriteAllText(filePath, JsonSerializer.Serialize(metadata, options));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,78 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: SerializableMigrationRulesEngine.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.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public static class SerializableMigrationRulesEngine
|
||||
{
|
||||
public static readonly Dictionary<string, ISerializableMigrationRule> Rules = new();
|
||||
|
||||
static SerializableMigrationRulesEngine()
|
||||
{
|
||||
var rules = new ISerializableMigrationRule[]
|
||||
{
|
||||
new ArrayMigrationRule(),
|
||||
new HashSetMigrationRule(),
|
||||
new KeyValuePairMigrationRule(),
|
||||
new ListMigrationRule(),
|
||||
new PrimitiveTypeMigrationRule(),
|
||||
new PrimitiveUOTypeMigrationRule(),
|
||||
new SerializableInterfaceMigrationRule(),
|
||||
new SerializationMethodSignatureMigrationRule()
|
||||
};
|
||||
|
||||
foreach (var rule in rules)
|
||||
{
|
||||
Rules.Add(rule.RuleName, rule);
|
||||
}
|
||||
}
|
||||
|
||||
public static SerializableProperty GenerateSerializableProperty(
|
||||
Compilation compilation,
|
||||
string propertyName,
|
||||
ISymbol propertyType,
|
||||
ImmutableArray<AttributeData> attributes,
|
||||
ImmutableArray<INamedTypeSymbol> serializableTypes
|
||||
)
|
||||
{
|
||||
foreach (var rule in Rules.Values)
|
||||
{
|
||||
if (rule.GenerateRuleState(
|
||||
compilation,
|
||||
propertyType,
|
||||
attributes,
|
||||
serializableTypes,
|
||||
out var ruleArguments
|
||||
))
|
||||
{
|
||||
return new SerializableProperty
|
||||
{
|
||||
Name = propertyName,
|
||||
Type = propertyType.ToDisplayString(),
|
||||
Rule = rule.RuleName,
|
||||
RuleArguments = ruleArguments
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
throw new Exception($"No rule found for property {propertyName} of type {propertyType} ({Rules.Count})");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: SerializableProperty.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.Text.Json.Serialization;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public class SerializableProperty
|
||||
{
|
||||
[JsonPropertyName("name")]
|
||||
public string Name { get; init; }
|
||||
|
||||
[JsonPropertyName("type")]
|
||||
public string Type { get; init; }
|
||||
|
||||
[JsonPropertyName("rule")]
|
||||
public string Rule { get; init; }
|
||||
|
||||
[JsonPropertyName("ruleArguments")]
|
||||
public string[] RuleArguments { get; init; }
|
||||
}
|
||||
}
|
||||
25
Projects/SerializationGenerator/SerializationGenerator.csproj
Executable file
25
Projects/SerializationGenerator/SerializationGenerator.csproj
Executable file
|
|
@ -0,0 +1,25 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<LangVersion>preview</LangVersion>
|
||||
<BuildOutputTargetFolder>analyzers</BuildOutputTargetFolder>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2" PrivateAssets="all" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.9.0" />
|
||||
<PackageReference Include="Humanizer.Core" Version="2.10.1" GeneratePathProperty="true" PrivateAssets="all" />
|
||||
<PackageReference Include="System.Text.Json" Version="4.7.2" GeneratePathProperty="true" PrivateAssets="all" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn>
|
||||
</PropertyGroup>
|
||||
|
||||
<Target Name="GetDependencyTargetPaths">
|
||||
<ItemGroup>
|
||||
<TargetPathWithTargetPlatformMoniker Include="$(PKGHumanizer_Core)\lib\netstandard2.0\*.dll" IncludeRuntimeDependency="false" />
|
||||
<TargetPathWithTargetPlatformMoniker Include="$(PKGSystem_Text_Json)\lib\netstandard2.0\*.dll" IncludeRuntimeDependency="false" />
|
||||
</ItemGroup>
|
||||
</Target>
|
||||
</Project>
|
||||
48
Projects/SerializationGenerator/SerializerSyntaxReceiver.cs
Executable file
48
Projects/SerializationGenerator/SerializerSyntaxReceiver.cs
Executable file
|
|
@ -0,0 +1,48 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright (C) 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: SyntaxReceiver.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.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public class SerializerSyntaxReceiver : ISyntaxContextReceiver
|
||||
{
|
||||
public List<IFieldSymbol> Fields { get; } = new();
|
||||
|
||||
public static HashSet<string> AttributeTypes { get; } = new();
|
||||
|
||||
public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
|
||||
{
|
||||
if (context.Node is FieldDeclarationSyntax { AttributeLists: { Count: > 0 } } fieldDeclarationSyntax)
|
||||
{
|
||||
foreach (VariableDeclaratorSyntax variable in fieldDeclarationSyntax.Declaration.Variables)
|
||||
{
|
||||
if (context.SemanticModel.GetDeclaredSymbol(variable) is not IFieldSymbol fieldSymbol)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (fieldSymbol.GetAttributes().Any(ad => AttributeTypes.Contains(ad.AttributeClass?.ToDisplayString())))
|
||||
{
|
||||
Fields.Add(fieldSymbol);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
44
Projects/SerializationGenerator/SourceGeneration/Helpers.cs
Normal file
44
Projects/SerializationGenerator/SourceGeneration/Helpers.cs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: Helpers.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.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public static class Helpers
|
||||
{
|
||||
public static bool ContainsInterface(this ITypeSymbol symbol, ISymbol interfaceSymbol) =>
|
||||
symbol.Interfaces.Any(i => i.ConstructedFrom.Equals(interfaceSymbol, SymbolEqualityComparer.Default)) ||
|
||||
symbol.AllInterfaces.Any(i => i.ConstructedFrom.Equals(interfaceSymbol, SymbolEqualityComparer.Default));
|
||||
|
||||
public static ImmutableArray<IMethodSymbol> GetAllMethods(this ITypeSymbol symbol, string name)
|
||||
{
|
||||
var methods = symbol.GetMembers(name).OfType<IMethodSymbol>().ToImmutableArray();
|
||||
if (symbol.ContainingSymbol is not ITypeSymbol typeSymbol)
|
||||
{
|
||||
return methods;
|
||||
}
|
||||
|
||||
var list = new List<IMethodSymbol>();
|
||||
list.AddRange(methods.ToList());
|
||||
list.AddRange(GetAllMethods(typeSymbol, name).ToList());
|
||||
|
||||
return list.ToImmutableArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright (C) 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: SourceGeneration.AccessModifier.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/>. *
|
||||
*************************************************************************/
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public enum AccessModifier
|
||||
{
|
||||
None,
|
||||
Public,
|
||||
Private,
|
||||
Protected,
|
||||
Internal,
|
||||
ProtectedInternal,
|
||||
PrivateProtected
|
||||
}
|
||||
|
||||
public static partial class SourceGeneration
|
||||
{
|
||||
public static string ToFriendlyString(this AccessModifier modifier) =>
|
||||
modifier switch
|
||||
{
|
||||
AccessModifier.Public => "public",
|
||||
AccessModifier.Private => "private",
|
||||
AccessModifier.Protected => "protected",
|
||||
AccessModifier.Internal => "internal",
|
||||
AccessModifier.ProtectedInternal => "protected internal",
|
||||
AccessModifier.PrivateProtected => "private protected",
|
||||
_ => ""
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,125 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright (C) 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: SourceGeneration.Arguments.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.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public static partial class SourceGeneration
|
||||
{
|
||||
public static void GetTypesFromTypedConstant(TypedConstant arg, List<ITypeSymbol> list)
|
||||
{
|
||||
if (arg.Kind == TypedConstantKind.Type)
|
||||
{
|
||||
list.Add((ITypeSymbol)arg.Value);
|
||||
}
|
||||
else if (arg.Kind == TypedConstantKind.Array)
|
||||
{
|
||||
for (var i = 0; i < arg.Values.Length; i++)
|
||||
{
|
||||
GetTypesFromTypedConstant(arg.Values[i], list);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void GenerateSignatureArguments(this StringBuilder source, ImmutableArray<(ITypeSymbol, string)> parameters)
|
||||
{
|
||||
for (var i = 0; i < parameters.Length; i++)
|
||||
{
|
||||
var (t, v) = parameters[i];
|
||||
source.AppendFormat("{0} {1}", t.Name, v);
|
||||
if (i < parameters.Length - 1)
|
||||
{
|
||||
source.Append(", ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void GenerateNamedArgument(this StringBuilder source, KeyValuePair<string, TypedConstant> namedArg)
|
||||
{
|
||||
source.AppendFormat("{0} = ", namedArg.Key);
|
||||
source.GenerateTypedConstant(namedArg.Value);
|
||||
}
|
||||
|
||||
public static void GenerateTypedConstants(this StringBuilder source, ImmutableArray<TypedConstant> args)
|
||||
{
|
||||
source.Append("new []{");
|
||||
for (var i = 0; i < args.Length; i++)
|
||||
{
|
||||
source.GenerateTypedConstant(args[i]);
|
||||
if (i < args.Length - 1)
|
||||
{
|
||||
source.Append(", ");
|
||||
}
|
||||
}
|
||||
source.Append('}');
|
||||
}
|
||||
|
||||
public static void GenerateTypedConstant(this StringBuilder source, TypedConstant arg)
|
||||
{
|
||||
if (arg.IsNull)
|
||||
{
|
||||
source.Append("null");
|
||||
return;
|
||||
}
|
||||
|
||||
switch (arg.Kind)
|
||||
{
|
||||
default:
|
||||
{
|
||||
return;
|
||||
}
|
||||
case TypedConstantKind.Primitive:
|
||||
{
|
||||
|
||||
if (arg.Value is string str)
|
||||
{
|
||||
source.AppendFormat("\"{0}\"", str);
|
||||
}
|
||||
else
|
||||
{
|
||||
source.Append(arg.Value);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case TypedConstantKind.Enum:
|
||||
{
|
||||
if (arg.Type == null || arg.Value == null)
|
||||
{
|
||||
source.Append("null");
|
||||
}
|
||||
else
|
||||
{
|
||||
source.AppendFormat("({0}){1}", arg.Type.ToDisplayString(), arg.Value);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case TypedConstantKind.Type:
|
||||
{
|
||||
source.AppendFormat("typeof({0})", ((ITypeSymbol)arg.Value)?.Name);
|
||||
break;
|
||||
}
|
||||
case TypedConstantKind.Array:
|
||||
{
|
||||
source.GenerateTypedConstants(arg.Values);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,92 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright (C) 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: SourceGeneration.Attribute.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.Collections.Immutable;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public static partial class SourceGeneration
|
||||
{
|
||||
public static void GenerateAttribute(this StringBuilder source, string attrClassName, ImmutableArray<TypedConstant> args)
|
||||
{
|
||||
source.Append($" [{attrClassName}");
|
||||
var hasArgs = args.Length > 0;
|
||||
|
||||
if (hasArgs)
|
||||
{
|
||||
source.Append("(");
|
||||
}
|
||||
|
||||
for (var i = 0; i < args.Length; i++)
|
||||
{
|
||||
var arg = args[i];
|
||||
source.GenerateTypedConstant(arg);
|
||||
if (i < args.Length - 1)
|
||||
{
|
||||
source.Append(", ");
|
||||
}
|
||||
}
|
||||
|
||||
if (hasArgs)
|
||||
{
|
||||
source.Append(")");
|
||||
}
|
||||
|
||||
source.AppendLine("]");
|
||||
}
|
||||
|
||||
public static void GenerateAttribute(this StringBuilder source, AttributeData attr)
|
||||
{
|
||||
source.Append($" [{attr.AttributeClass?.Name}");
|
||||
var ctorArgs = attr.ConstructorArguments;
|
||||
var namedArgs = attr.NamedArguments;
|
||||
var hasArgs = ctorArgs.Length + namedArgs.Length > 0;
|
||||
|
||||
if (hasArgs)
|
||||
{
|
||||
source.Append("(");
|
||||
}
|
||||
|
||||
for (var i = 0; i < ctorArgs.Length; i++)
|
||||
{
|
||||
var arg = ctorArgs[i];
|
||||
source.GenerateTypedConstant(arg);
|
||||
if (i < ctorArgs.Length - 1)
|
||||
{
|
||||
source.Append(", ");
|
||||
}
|
||||
}
|
||||
|
||||
for (var i = 0; i < namedArgs.Length; i++)
|
||||
{
|
||||
var arg = namedArgs[i];
|
||||
source.GenerateNamedArgument(arg);
|
||||
if (i < namedArgs.Length - 1)
|
||||
{
|
||||
source.Append(", ");
|
||||
}
|
||||
}
|
||||
|
||||
if (hasArgs)
|
||||
{
|
||||
source.Append(")");
|
||||
}
|
||||
|
||||
source.AppendLine("]");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright (C) 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: SourceGeneration.Class.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.Collections.Immutable;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public static partial class SourceGeneration
|
||||
{
|
||||
public static void GenerateClassStart(this StringBuilder source, string className, ImmutableArray<ITypeSymbol> interfaces)
|
||||
{
|
||||
source.Append($" public partial class {className}");
|
||||
if (!interfaces.IsEmpty)
|
||||
{
|
||||
source.Append(" : ");
|
||||
for (var i = 0; i < interfaces.Length; i++)
|
||||
{
|
||||
source.Append(interfaces[i].ToDisplayString());
|
||||
if (i < interfaces.Length - 1)
|
||||
{
|
||||
source.Append(", ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
source.AppendLine(@"
|
||||
{");
|
||||
}
|
||||
|
||||
public static void GenerateClassEnd(this StringBuilder source)
|
||||
{
|
||||
source.AppendLine(" }");
|
||||
}
|
||||
|
||||
// TODO: Generalize this to any field using dynamic indentation
|
||||
public static void GenerateClassField(
|
||||
this StringBuilder source,
|
||||
AccessModifier accessors,
|
||||
InstanceModifier instance,
|
||||
string type,
|
||||
string variableName,
|
||||
string value,
|
||||
bool unusedPragma = false
|
||||
)
|
||||
{
|
||||
if (unusedPragma)
|
||||
{
|
||||
source.AppendLine("#pragma warning disable 0414"); // assigned, but never used
|
||||
}
|
||||
|
||||
var instanceStr = instance == InstanceModifier.None ? "" : $"{instance.ToFriendlyString()} ";
|
||||
var accessorStr = accessors == AccessModifier.None ? "" : $"{accessors.ToFriendlyString()} ";
|
||||
var valueStr = value == null ? "" : $" = {value}";
|
||||
source.AppendLine($" {accessorStr}{instanceStr}{type} {variableName}{valueStr};");
|
||||
|
||||
if (unusedPragma)
|
||||
{
|
||||
source.AppendLine("#pragma warning restore 0414");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: SourceGeneration.InstanceModifier.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/>. *
|
||||
*************************************************************************/
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public enum InstanceModifier
|
||||
{
|
||||
None,
|
||||
Const,
|
||||
ReadOnly,
|
||||
Static,
|
||||
StaticReadOnly
|
||||
}
|
||||
|
||||
public static partial class SourceGeneration
|
||||
{
|
||||
public static string ToFriendlyString(this InstanceModifier modifier) =>
|
||||
modifier switch
|
||||
{
|
||||
InstanceModifier.Const => "const",
|
||||
InstanceModifier.ReadOnly => "readonly",
|
||||
InstanceModifier.Static => "static",
|
||||
InstanceModifier.StaticReadOnly => "static readonly",
|
||||
_ => ""
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright (C) 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: SourceGeneration.Method.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.Collections.Immutable;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public static partial class SourceGeneration
|
||||
{
|
||||
public static void GenerateMethodStart(this StringBuilder source, string methodName, AccessModifier accessors, bool isOverride, string returnType, ImmutableArray<(ITypeSymbol, string)> parameters)
|
||||
{
|
||||
source.Append($" {accessors.ToFriendlyString()}{(isOverride ? " override" : "")} {returnType} {methodName}(");
|
||||
source.GenerateSignatureArguments(parameters);
|
||||
source.AppendLine(@")
|
||||
{");
|
||||
}
|
||||
|
||||
public static void GenerateMethodEnd(this StringBuilder source) => source.AppendLine(@" }");
|
||||
|
||||
public static void GenerateConstructorStart(
|
||||
this StringBuilder source, string className, AccessModifier accessors, ImmutableArray<(ITypeSymbol, string)> parameters,
|
||||
ImmutableArray<string> baseParameters, bool isOverload = false
|
||||
)
|
||||
{
|
||||
source.Append($" {accessors.ToFriendlyString()} {className}(");
|
||||
source.GenerateSignatureArguments(parameters);
|
||||
source.Append(')');
|
||||
bool hasBaseParams = baseParameters.Length > 0;
|
||||
if (hasBaseParams)
|
||||
{
|
||||
source.AppendFormat(" : {0}(", isOverload ? "this" : "base");
|
||||
for (int i = 0; i < baseParameters.Length; i++)
|
||||
{
|
||||
source.Append(baseParameters[i]);
|
||||
if (i < baseParameters.Length - 1)
|
||||
{
|
||||
source.Append(',');
|
||||
}
|
||||
}
|
||||
source.Append(')');
|
||||
}
|
||||
|
||||
source.AppendLine("\n {");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright (C) 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: SourceGeneration.Namespace.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.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public static partial class SourceGeneration
|
||||
{
|
||||
public static void GenerateUsings(this StringBuilder source, IImmutableList<ITypeSymbol> typesUsed)
|
||||
{
|
||||
var enumerable = typesUsed
|
||||
.Select(t => t.ContainingNamespace.Name)
|
||||
.Distinct()
|
||||
.OrderByDescending(t => t);
|
||||
|
||||
foreach (var t in enumerable)
|
||||
{
|
||||
source.Insert(0, $"using {t}{Environment.NewLine}");
|
||||
}
|
||||
}
|
||||
|
||||
public static void GenerateNamespaceStart(this StringBuilder source, string namespaceName)
|
||||
{
|
||||
source.AppendLine($@"namespace {namespaceName}
|
||||
{{");
|
||||
}
|
||||
|
||||
public static void GenerateNamespaceEnd(this StringBuilder source)
|
||||
{
|
||||
source.AppendLine("}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,107 @@
|
|||
/*************************************************************************
|
||||
* 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,
|
||||
bool useInit = 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 != AccessModifier.None ? $"{setAccessor?.ToFriendlyString() ?? ""} " : "";
|
||||
var setter = setterAccessor == "" ? "" : $"{getterSpace}{setterAccessor}{setOrInit}";
|
||||
|
||||
var propertyAccessor = accessors == AccessModifier.None ? "" : $"{accessors.ToFriendlyString()} ";
|
||||
|
||||
source.AppendLine($"{propertyAccessor}{type} {propertyName} {{ {getter}{setter} }}");
|
||||
}
|
||||
|
||||
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 {")}");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue