Merge remote-tracking branch 'origin/alchemy-serialize-with-inheritance' into alchemy-serialize-with-inheritance

# Conflicts:
#	Alchemy.SourceGenerator/AlchemySerializeGenerator.cs
This commit is contained in:
Akeit0 2024-03-03 13:12:04 +09:00
commit cb32709324

View File

@ -26,22 +26,26 @@ namespace Alchemy.SourceGenerator
{ {
foreach (var typeSyntax in receiver.TargetTypes) foreach (var typeSyntax in receiver.TargetTypes)
{ {
var typeSymbol = context.Compilation.GetSemanticModel(typeSyntax.SyntaxTree).GetDeclaredSymbol(typeSyntax); var typeSymbol = context.Compilation.GetSemanticModel(typeSyntax.SyntaxTree)
.GetDeclaredSymbol(typeSyntax);
if (!IsPartial(typeSyntax)) if (!IsPartial(typeSyntax))
{ {
context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.MustBePartial, typeSyntax.Identifier.GetLocation(), typeSymbol.Name)); context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.MustBePartial,
typeSyntax.Identifier.GetLocation(), typeSymbol.Name));
continue; continue;
} }
if (IsNested(typeSyntax)) if (IsNested(typeSyntax))
{ {
context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.NestedNotAllow, typeSyntax.Identifier.GetLocation(), typeSymbol.Name)); context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.NestedNotAllow,
typeSyntax.Identifier.GetLocation(), typeSymbol.Name));
continue; continue;
} }
var fieldSymbols = new List<IFieldSymbol>(); var fieldSymbols = new List<IFieldSymbol>();
var fields = typeSyntax.Members.OfType<FieldDeclarationSyntax>(); var fields = typeSyntax.Members
.OfType<FieldDeclarationSyntax>();
foreach (var field in fields) foreach (var field in fields)
{ {
var model = context.Compilation.GetSemanticModel(field.SyntaxTree); var model = context.Compilation.GetSemanticModel(field.SyntaxTree);
@ -66,7 +70,9 @@ namespace Alchemy.SourceGenerator
{ {
if (nonSerializedAttribute == null) if (nonSerializedAttribute == null)
{ {
context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.ShouldBeNonSerialized, variable.Identifier.GetLocation(), fieldSymbol.Name)); context.ReportDiagnostic(Diagnostic.Create(
DiagnosticDescriptors.ShouldBeNonSerialized, variable.Identifier.GetLocation(),
fieldSymbol.Name));
} }
fieldSymbols.Add(fieldSymbol); fieldSymbols.Add(fieldSymbol);
@ -85,11 +91,13 @@ namespace Alchemy.SourceGenerator
} }
catch (Exception ex) catch (Exception ex)
{ {
var diagnosticDescriptor = new DiagnosticDescriptor("AlchemySerializeGeneratorError", "AlchemySerializeGeneratorError", $"Generation failed with:\n {ex}", "AlchemySerializeGeneratorError", DiagnosticSeverity.Error, true); var diagnosticDescriptor = new DiagnosticDescriptor("AlchemySerializeGeneratorError",
context.ReportDiagnostic(Diagnostic.Create(diagnosticDescriptor, Location.None, DiagnosticSeverity.Error)); "AlchemySerializeGeneratorError", $"Generation failed with:\n {ex}",
"AlchemySerializeGeneratorError", DiagnosticSeverity.Error, true);
context.ReportDiagnostic(Diagnostic.Create(diagnosticDescriptor, Location.None,
DiagnosticSeverity.Error));
} }
} }
static string ReplaceGenericsToCount( string typeName,int count) static string ReplaceGenericsToCount( string typeName,int count)
{ {
if(count == 0) return typeName; if(count == 0) return typeName;
@ -111,7 +119,6 @@ namespace Alchemy.SourceGenerator
builder.Append(c); builder.Append(c);
} }
} }
return builder.ToString(); return builder.ToString();
} }
@ -124,8 +131,8 @@ namespace Alchemy.SourceGenerator
var baseType = typeSymbol.BaseType; var baseType = typeSymbol.BaseType;
while (baseType != null) while (baseType != null)
{ {
if (baseType.GetAttributes().Any(x => if (baseType.GetAttributes().Any(x => x.AttributeClass!.Name
x.AttributeClass!.Name is "AlchemySerialize" is "AlchemySerialize"
or "AlchemySerializeAttribute" or "AlchemySerializeAttribute"
or "Alchemy.Serialization.AlchemySerialize" or "Alchemy.Serialization.AlchemySerialize"
or "Alchemy.Serialization.AlchemySerializeAttribute")) or "Alchemy.Serialization.AlchemySerializeAttribute"))
@ -141,27 +148,34 @@ namespace Alchemy.SourceGenerator
if (typeSymbol.IsGenericType) if (typeSymbol.IsGenericType)
{ {
genericsCount = typeSymbol.TypeParameters.Length; genericsCount = typeSymbol.TypeParameters.Length;
}
}
var typeGenerics = typeSymbol.IsGenericType var typeGenerics = typeSymbol.IsGenericType
? "<" + string.Join(", ", typeSymbol.TypeParameters.Select(x => x.Name)) + ">" ? "<" + string.Join(", ", typeSymbol.TypeParameters.Select(x => x.Name)) + ">"
: ""; : "";
var alchemySerializationDataName = typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat).Replace("global::", "").Replace(".", "_"); var alchemySerializationDataName = typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)
.Replace("global::", "").Replace(".", "_");
alchemySerializationDataName = ReplaceGenericsToCount(alchemySerializationDataName,genericsCount) + "_alchemySerializationData"; alchemySerializationDataName = ReplaceGenericsToCount(alchemySerializationDataName,genericsCount) + "_alchemySerializationData";
var inheritedSerializationCallback = hasInheritedImplementation ? "base.SerializationCallback_AlchemyImpl(isBeforeSerialize);" : string.Empty; var inheritedSerializationCallback = hasInheritedImplementation
? "base.SerializationCallback_AlchemyImpl(isBeforeSerialize);"
: string.Empty;
var hasShowSerializationData = typeSymbol.GetAttributes().Any(x => var hasShowSerializationData = typeSymbol.GetAttributes().Any(x => x.AttributeClass.Name
x.AttributeClass.Name is "ShowAlchemySerializationData" is "ShowAlchemySerializationData"
or "ShowAlchemySerializationDataAttribute" or "ShowAlchemySerializationDataAttribute"
or "Alchemy.Serialization.ShowAlchemySerializationData" or "Alchemy.Serialization.ShowAlchemySerializationData"
or "Alchemy.Serialization.ShowAlchemySerializationDataAttribute"); or "Alchemy.Serialization.ShowAlchemySerializationDataAttribute");
var serializationDataAttributesCode = hasShowSerializationData ? "[global::Alchemy.Inspector.ReadOnly, global::UnityEngine.TextArea(3, 999), global::UnityEngine.SerializeField]" : "[global::UnityEngine.HideInInspector, global::UnityEngine.SerializeField]"; var serializationDataAttributesCode = hasShowSerializationData
? "[global::Alchemy.Inspector.ReadOnly, global::UnityEngine.TextArea(3, 999), global::UnityEngine.SerializeField]"
: "[global::UnityEngine.HideInInspector, global::UnityEngine.SerializeField]";
// target class namespace // target class namespace
var ns = typeSymbol.ContainingNamespace.IsGlobalNamespace ? string.Empty : $"namespace {typeSymbol.ContainingNamespace} {{"; var ns = typeSymbol.ContainingNamespace.IsGlobalNamespace
? string.Empty
: $"namespace {typeSymbol.ContainingNamespace} {{";
foreach (var field in fieldSymbols) foreach (var field in fieldSymbols)
{ {