com.wallstop-studios.dxmessaging 1.0.5 → 2.0.0-rc01
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Analyzers/Microsoft.CodeAnalysis.dll +0 -0
- package/Analyzers/Microsoft.CodeAnalysis.dll.meta +33 -0
- package/Analyzers/Microsoft.CodeAnalysis.pdb.meta +7 -0
- package/Analyzers/Microsoft.CodeAnalysis.xml +38823 -0
- package/Analyzers/Microsoft.CodeAnalysis.xml.meta +7 -0
- package/Analyzers/System.Reflection.Metadata.dll +0 -0
- package/Analyzers/System.Reflection.Metadata.dll.meta +33 -0
- package/Analyzers/System.Reflection.Metadata.xml +8202 -0
- package/Analyzers/System.Reflection.Metadata.xml.meta +7 -0
- package/Analyzers/WallstopStudios.DxMessaging.SourceGenerators.deps.json +319 -0
- package/Analyzers/WallstopStudios.DxMessaging.SourceGenerators.deps.json.meta +7 -0
- package/Analyzers/WallstopStudios.DxMessaging.SourceGenerators.dll +0 -0
- package/Analyzers/WallstopStudios.DxMessaging.SourceGenerators.dll.meta +69 -0
- package/Analyzers/WallstopStudios.DxMessaging.SourceGenerators.pdb.meta +7 -0
- package/Analyzers.meta +8 -0
- package/Editor/SetupCscRsp.cs +67 -0
- package/Editor/SetupCscRsp.cs.meta +3 -0
- package/Editor/WallstopStudios.DxMessaging.Editor.asmdef +21 -0
- package/{Tests/Editor/WallstopStudios.DxMessaging.Tests.Editor.asmdef.meta → Editor/WallstopStudios.DxMessaging.Editor.asmdef.meta} +7 -7
- package/README.md +43 -6
- package/Runtime/Core/Attributes/DxAutoMessageTypeAttribute.cs +9 -0
- package/Runtime/Core/Attributes/DxAutoMessageTypeAttribute.cs.meta +3 -0
- package/Runtime/Core/Attributes/DxBroadcastMessageAttribute.cs +10 -0
- package/Runtime/Core/Attributes/DxBroadcastMessageAttribute.cs.meta +3 -0
- package/Runtime/Core/Attributes/DxTargetedMessageAttribute.cs +10 -0
- package/Runtime/Core/Attributes/DxTargetedMessageAttribute.cs.meta +3 -0
- package/Runtime/Core/Attributes/DxUntargetedMessageAttribute.cs +10 -0
- package/Runtime/Core/Attributes/DxUntargetedMessageAttribute.cs.meta +3 -0
- package/Runtime/Core/Attributes.meta +3 -0
- package/Runtime/Unity/MessagingComponent.cs +21 -10
- package/Runtime/WallstopStudios.DxMessaging.asmdef +15 -17
- package/SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators/DxAutoMessageTypeGenerator.cs +75 -0
- package/SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators/DxAutoMessageTypeGenerator.cs.meta +11 -0
- package/SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators/DxBroadcastMessageGenerator.cs +77 -0
- package/SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators/DxBroadcastMessageGenerator.cs.meta +3 -0
- package/SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators/DxTargetedMessageGenerator.cs +77 -0
- package/SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators/DxTargetedMessageGenerator.cs.meta +3 -0
- package/SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators/DxUntargetedMessageGenerator.cs +78 -0
- package/SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators/DxUntargetedMessageGenerator.cs.meta +3 -0
- package/SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators.csproj +21 -0
- package/SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators.csproj.meta +7 -0
- package/SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators.meta +8 -0
- package/SourceGenerators.meta +8 -0
- package/Tests/Runtime/Core/NominalTests.cs +127 -77
- package/Tests/Runtime/Core/RegistrationTests.cs +56 -106
- package/Tests/Runtime/Scripts/Messages/ComplexTargetedMessage.cs +4 -2
- package/Tests/Runtime/Scripts/Messages/SimpleBroadcastMessage.cs +3 -3
- package/Tests/Runtime/Scripts/Messages/SimpleTargetedMessage.cs +4 -4
- package/Tests/Runtime/Scripts/Messages/SimpleUntargetedMessage.cs +3 -3
- package/Tests/Runtime/WallstopStudios.DxMessaging.Tests.Runtime.asmdef +1 -3
- package/package.json +3 -3
- package/Tests/Editor/WallstopStudios.DxMessaging.Tests.Editor.asmdef +0 -23
|
@@ -4,12 +4,17 @@
|
|
|
4
4
|
using System;
|
|
5
5
|
using System.Collections.Generic;
|
|
6
6
|
using UnityEngine;
|
|
7
|
-
|
|
7
|
+
|
|
8
8
|
[DisallowMultipleComponent]
|
|
9
9
|
public sealed class MessagingComponent : MonoBehaviour
|
|
10
10
|
{
|
|
11
|
+
[SerializeField]
|
|
12
|
+
private bool _emitMessagesWhenDisabled;
|
|
13
|
+
|
|
11
14
|
private MessageHandler _messageHandler;
|
|
12
|
-
|
|
15
|
+
|
|
16
|
+
private readonly Dictionary<MonoBehaviour, MessageRegistrationToken> _registeredListeners =
|
|
17
|
+
new Dictionary<MonoBehaviour, MessageRegistrationToken>();
|
|
13
18
|
|
|
14
19
|
public MessageRegistrationToken Create(MonoBehaviour listener)
|
|
15
20
|
{
|
|
@@ -20,9 +25,7 @@
|
|
|
20
25
|
|
|
21
26
|
if (gameObject.GetInstanceID() != listener.gameObject.GetInstanceID())
|
|
22
27
|
{
|
|
23
|
-
throw new ArgumentException(
|
|
24
|
-
"Cannot create a RegistrationToken without an valid owner. {0}.",
|
|
25
|
-
listener.gameObject.GetInstanceID()));
|
|
28
|
+
throw new ArgumentException($"Cannot create a RegistrationToken without an valid owner. {listener.gameObject.GetInstanceID()}.");
|
|
26
29
|
}
|
|
27
30
|
|
|
28
31
|
if (_registeredListeners.TryGetValue(listener, out MessageRegistrationToken createdToken))
|
|
@@ -37,14 +40,17 @@
|
|
|
37
40
|
{
|
|
38
41
|
active = true
|
|
39
42
|
};
|
|
40
|
-
MessagingDebug.Log(
|
|
41
|
-
|
|
43
|
+
MessagingDebug.Log(
|
|
44
|
+
LogLevel.Debug,
|
|
45
|
+
"Creating MessageHandler for componentType {0}, GameObject name: {1}, InstanceId: {2}.",
|
|
46
|
+
listener.GetType(), listener.gameObject.name, (InstanceId)gameObject);
|
|
42
47
|
}
|
|
43
48
|
else
|
|
44
49
|
{
|
|
45
|
-
MessagingDebug.Log(
|
|
50
|
+
MessagingDebug.Log(
|
|
51
|
+
LogLevel.Debug,
|
|
46
52
|
"Using existing MessageHandler for componentType {0}, GameObject name: {1}, InstanceId: {2}.",
|
|
47
|
-
listener.GetType(), listener.gameObject.name, (InstanceId)
|
|
53
|
+
listener.GetType(), listener.gameObject.name, (InstanceId)gameObject);
|
|
48
54
|
}
|
|
49
55
|
|
|
50
56
|
createdToken = MessageRegistrationToken.Create(_messageHandler);
|
|
@@ -64,10 +70,15 @@
|
|
|
64
70
|
|
|
65
71
|
private void ToggleMessageHandler(bool newActive)
|
|
66
72
|
{
|
|
73
|
+
if (!newActive && _emitMessagesWhenDisabled)
|
|
74
|
+
{
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
67
78
|
if (_messageHandler != null && _messageHandler.active != newActive)
|
|
68
79
|
{
|
|
69
80
|
_messageHandler.active = newActive;
|
|
70
81
|
}
|
|
71
82
|
}
|
|
72
83
|
}
|
|
73
|
-
}
|
|
84
|
+
}
|
|
@@ -1,18 +1,16 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "WallstopStudios.DxMessaging",
|
|
3
|
-
"rootNamespace": "DxMessaging",
|
|
4
|
-
"references": [],
|
|
5
|
-
"includePlatforms": [],
|
|
6
|
-
"excludePlatforms": [],
|
|
7
|
-
"allowUnsafeCode": false,
|
|
8
|
-
"overrideReferences": true,
|
|
9
|
-
"precompiledReferences": [
|
|
10
|
-
"System.Runtime.CompilerServices.Unsafe.dll"
|
|
11
|
-
],
|
|
12
|
-
"autoReferenced": true,
|
|
13
|
-
"defineConstraints": [
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
"versionDefines": [],
|
|
17
|
-
"noEngineReferences": false
|
|
1
|
+
{
|
|
2
|
+
"name": "WallstopStudios.DxMessaging",
|
|
3
|
+
"rootNamespace": "DxMessaging",
|
|
4
|
+
"references": [],
|
|
5
|
+
"includePlatforms": [],
|
|
6
|
+
"excludePlatforms": [],
|
|
7
|
+
"allowUnsafeCode": false,
|
|
8
|
+
"overrideReferences": true,
|
|
9
|
+
"precompiledReferences": [
|
|
10
|
+
"System.Runtime.CompilerServices.Unsafe.dll"
|
|
11
|
+
],
|
|
12
|
+
"autoReferenced": true,
|
|
13
|
+
"defineConstraints": [],
|
|
14
|
+
"versionDefines": [],
|
|
15
|
+
"noEngineReferences": false
|
|
18
16
|
}
|
package/SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators/DxAutoMessageTypeGenerator.cs
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
namespace WallstopStudios.DxMessaging.SourceGenerators;
|
|
2
|
+
|
|
3
|
+
using System.Collections.Generic;
|
|
4
|
+
using System.Linq;
|
|
5
|
+
using System.Text;
|
|
6
|
+
using Microsoft.CodeAnalysis;
|
|
7
|
+
using Microsoft.CodeAnalysis.CSharp;
|
|
8
|
+
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
9
|
+
using Microsoft.CodeAnalysis.Text;
|
|
10
|
+
|
|
11
|
+
[Generator]
|
|
12
|
+
public sealed class DxAutoMessageTypeGenerator : ISourceGenerator
|
|
13
|
+
{
|
|
14
|
+
public void Initialize(GeneratorInitializationContext context)
|
|
15
|
+
{
|
|
16
|
+
context.RegisterForSyntaxNotifications(() => new SyntaxReceiver());
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public void Execute(GeneratorExecutionContext context)
|
|
20
|
+
{
|
|
21
|
+
if (context.SyntaxReceiver is not SyntaxReceiver receiver)
|
|
22
|
+
{
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
INamedTypeSymbol attributeSymbol = context.Compilation.GetTypeByMetadataName("DxMessaging.Core.Attributes.DxAutoMessageTypeAttribute");
|
|
27
|
+
|
|
28
|
+
foreach (TypeDeclarationSyntax classDeclaration in receiver.CandidateClasses)
|
|
29
|
+
{
|
|
30
|
+
SemanticModel model = context.Compilation.GetSemanticModel(classDeclaration.SyntaxTree);
|
|
31
|
+
ISymbol classSymbol = ModelExtensions.GetDeclaredSymbol(model, classDeclaration);
|
|
32
|
+
|
|
33
|
+
if (classSymbol.GetAttributes().Any(
|
|
34
|
+
attributeData => attributeData.AttributeClass.Equals(
|
|
35
|
+
attributeSymbol, SymbolEqualityComparer.Default)))
|
|
36
|
+
{
|
|
37
|
+
string namespaceName = classSymbol.ContainingNamespace.ToDisplayString();
|
|
38
|
+
string className = classSymbol.Name;
|
|
39
|
+
string typeKind = classDeclaration.Kind() == SyntaxKind.ClassDeclaration ? "class" : "struct";
|
|
40
|
+
|
|
41
|
+
string source = $$"""
|
|
42
|
+
|
|
43
|
+
namespace {{namespaceName}}
|
|
44
|
+
{
|
|
45
|
+
public partial {{typeKind}} {{className}}
|
|
46
|
+
{
|
|
47
|
+
public System.Type MessageType => typeof({{className}});
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
""";
|
|
52
|
+
|
|
53
|
+
context.AddSource($"{className}_DxAutoMessageType.g.cs", SourceText.From(source, Encoding.UTF8));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private sealed class SyntaxReceiver : ISyntaxReceiver
|
|
59
|
+
{
|
|
60
|
+
public List<TypeDeclarationSyntax> CandidateClasses { get; } = [];
|
|
61
|
+
|
|
62
|
+
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
|
|
63
|
+
{
|
|
64
|
+
if (syntaxNode is TypeDeclarationSyntax typeDeclarationSyntax)
|
|
65
|
+
{
|
|
66
|
+
if (typeDeclarationSyntax.AttributeLists.Count > 0 &&
|
|
67
|
+
(typeDeclarationSyntax.Kind() == SyntaxKind.ClassDeclaration ||
|
|
68
|
+
typeDeclarationSyntax.Kind() == SyntaxKind.StructDeclaration))
|
|
69
|
+
{
|
|
70
|
+
CandidateClasses.Add(typeDeclarationSyntax);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
package/SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators/DxBroadcastMessageGenerator.cs
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
namespace WallstopStudios.DxMessaging.SourceGenerators;
|
|
2
|
+
|
|
3
|
+
using System.Collections.Generic;
|
|
4
|
+
using System.Linq;
|
|
5
|
+
using System.Text;
|
|
6
|
+
using Microsoft.CodeAnalysis;
|
|
7
|
+
using Microsoft.CodeAnalysis.CSharp;
|
|
8
|
+
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
9
|
+
using Microsoft.CodeAnalysis.Text;
|
|
10
|
+
|
|
11
|
+
[Generator]
|
|
12
|
+
public sealed class DxBroadcastMessageGenerator : ISourceGenerator
|
|
13
|
+
{
|
|
14
|
+
public void Initialize(GeneratorInitializationContext context)
|
|
15
|
+
{
|
|
16
|
+
context.RegisterForSyntaxNotifications(() => new SyntaxReceiver());
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public void Execute(GeneratorExecutionContext context)
|
|
20
|
+
{
|
|
21
|
+
if (context.SyntaxReceiver is not SyntaxReceiver receiver)
|
|
22
|
+
{
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
INamedTypeSymbol attributeSymbol = context.Compilation.GetTypeByMetadataName("DxMessaging.Core.Attributes.DxBroadcastMessageAttribute");
|
|
27
|
+
|
|
28
|
+
foreach (TypeDeclarationSyntax classDeclaration in receiver.CandidateClasses)
|
|
29
|
+
{
|
|
30
|
+
SemanticModel model = context.Compilation.GetSemanticModel(classDeclaration.SyntaxTree);
|
|
31
|
+
ISymbol classSymbol = ModelExtensions.GetDeclaredSymbol(model, classDeclaration);
|
|
32
|
+
|
|
33
|
+
if (classSymbol.GetAttributes().Any(
|
|
34
|
+
attributeData => attributeData.AttributeClass.Equals(
|
|
35
|
+
attributeSymbol, SymbolEqualityComparer.Default)))
|
|
36
|
+
{
|
|
37
|
+
string namespaceName = classSymbol.ContainingNamespace.ToDisplayString();
|
|
38
|
+
string className = classSymbol.Name;
|
|
39
|
+
string typeKind = classDeclaration.Kind() == SyntaxKind.ClassDeclaration ? "class" : "struct";
|
|
40
|
+
|
|
41
|
+
string source = $$"""
|
|
42
|
+
|
|
43
|
+
namespace {{namespaceName}}
|
|
44
|
+
{
|
|
45
|
+
using DxMessaging.Core.Messages;
|
|
46
|
+
|
|
47
|
+
public partial {{typeKind}} {{className}} : IBroadcastMessage
|
|
48
|
+
{
|
|
49
|
+
public System.Type MessageType => typeof({{className}});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
""";
|
|
54
|
+
|
|
55
|
+
context.AddSource($"{className}_DxBroadcastMessage.g.cs", SourceText.From(source, Encoding.UTF8));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
private sealed class SyntaxReceiver : ISyntaxReceiver
|
|
61
|
+
{
|
|
62
|
+
public List<TypeDeclarationSyntax> CandidateClasses { get; } = [];
|
|
63
|
+
|
|
64
|
+
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
|
|
65
|
+
{
|
|
66
|
+
if (syntaxNode is TypeDeclarationSyntax typeDeclarationSyntax)
|
|
67
|
+
{
|
|
68
|
+
if (typeDeclarationSyntax.AttributeLists.Count > 0 &&
|
|
69
|
+
(typeDeclarationSyntax.Kind() == SyntaxKind.ClassDeclaration ||
|
|
70
|
+
typeDeclarationSyntax.Kind() == SyntaxKind.StructDeclaration))
|
|
71
|
+
{
|
|
72
|
+
CandidateClasses.Add(typeDeclarationSyntax);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
package/SourceGenerators/WallstopStudios.DxMessaging.SourceGenerators/DxTargetedMessageGenerator.cs
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
namespace WallstopStudios.DxMessaging.SourceGenerators;
|
|
2
|
+
|
|
3
|
+
using System.Collections.Generic;
|
|
4
|
+
using System.Linq;
|
|
5
|
+
using System.Text;
|
|
6
|
+
using Microsoft.CodeAnalysis;
|
|
7
|
+
using Microsoft.CodeAnalysis.CSharp;
|
|
8
|
+
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
9
|
+
using Microsoft.CodeAnalysis.Text;
|
|
10
|
+
|
|
11
|
+
[Generator]
|
|
12
|
+
public sealed class DxTargetedMessageGenerator : ISourceGenerator
|
|
13
|
+
{
|
|
14
|
+
public void Initialize(GeneratorInitializationContext context)
|
|
15
|
+
{
|
|
16
|
+
context.RegisterForSyntaxNotifications(() => new SyntaxReceiver());
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public void Execute(GeneratorExecutionContext context)
|
|
20
|
+
{
|
|
21
|
+
if (context.SyntaxReceiver is not SyntaxReceiver receiver)
|
|
22
|
+
{
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
INamedTypeSymbol attributeSymbol = context.Compilation.GetTypeByMetadataName("DxMessaging.Core.Attributes.DxTargetedMessageAttribute");
|
|
27
|
+
|
|
28
|
+
foreach (TypeDeclarationSyntax classDeclaration in receiver.CandidateClasses)
|
|
29
|
+
{
|
|
30
|
+
SemanticModel model = context.Compilation.GetSemanticModel(classDeclaration.SyntaxTree);
|
|
31
|
+
ISymbol classSymbol = ModelExtensions.GetDeclaredSymbol(model, classDeclaration);
|
|
32
|
+
|
|
33
|
+
if (classSymbol.GetAttributes().Any(
|
|
34
|
+
attributeData => attributeData.AttributeClass.Equals(
|
|
35
|
+
attributeSymbol, SymbolEqualityComparer.Default)))
|
|
36
|
+
{
|
|
37
|
+
string namespaceName = classSymbol.ContainingNamespace.ToDisplayString();
|
|
38
|
+
string className = classSymbol.Name;
|
|
39
|
+
string typeKind = classDeclaration.Kind() == SyntaxKind.ClassDeclaration ? "class" : "struct";
|
|
40
|
+
|
|
41
|
+
string source = $$"""
|
|
42
|
+
|
|
43
|
+
namespace {{namespaceName}}
|
|
44
|
+
{
|
|
45
|
+
using DxMessaging.Core.Messages;
|
|
46
|
+
|
|
47
|
+
public partial {{typeKind}} {{className}} : ITargetedMessage
|
|
48
|
+
{
|
|
49
|
+
public System.Type MessageType => typeof({{className}});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
""";
|
|
54
|
+
|
|
55
|
+
context.AddSource($"{className}_DxTargetedMessage.g.cs", SourceText.From(source, Encoding.UTF8));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
private sealed class SyntaxReceiver : ISyntaxReceiver
|
|
61
|
+
{
|
|
62
|
+
public List<TypeDeclarationSyntax> CandidateClasses { get; } = [];
|
|
63
|
+
|
|
64
|
+
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
|
|
65
|
+
{
|
|
66
|
+
if (syntaxNode is TypeDeclarationSyntax typeDeclarationSyntax)
|
|
67
|
+
{
|
|
68
|
+
if (typeDeclarationSyntax.AttributeLists.Count > 0 &&
|
|
69
|
+
(typeDeclarationSyntax.Kind() == SyntaxKind.ClassDeclaration ||
|
|
70
|
+
typeDeclarationSyntax.Kind() == SyntaxKind.StructDeclaration))
|
|
71
|
+
{
|
|
72
|
+
CandidateClasses.Add(typeDeclarationSyntax);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
namespace WallstopStudios.DxMessaging.SourceGenerators;
|
|
2
|
+
|
|
3
|
+
using System.Collections.Generic;
|
|
4
|
+
using System.Linq;
|
|
5
|
+
using System.Text;
|
|
6
|
+
using Microsoft.CodeAnalysis;
|
|
7
|
+
using Microsoft.CodeAnalysis.CSharp;
|
|
8
|
+
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
9
|
+
using Microsoft.CodeAnalysis.Text;
|
|
10
|
+
|
|
11
|
+
[Generator]
|
|
12
|
+
public sealed class DxUntargetedMessageGenerator : ISourceGenerator
|
|
13
|
+
{
|
|
14
|
+
public void Initialize(GeneratorInitializationContext context)
|
|
15
|
+
{
|
|
16
|
+
context.RegisterForSyntaxNotifications(() => new SyntaxReceiver());
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public void Execute(GeneratorExecutionContext context)
|
|
20
|
+
{
|
|
21
|
+
if (context.SyntaxReceiver is not SyntaxReceiver receiver)
|
|
22
|
+
{
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
INamedTypeSymbol attributeSymbol = context.Compilation.GetTypeByMetadataName("DxMessaging.Core.Attributes.DxUntargetedMessageAttribute");
|
|
27
|
+
|
|
28
|
+
foreach (TypeDeclarationSyntax classDeclaration in receiver.CandidateClasses)
|
|
29
|
+
{
|
|
30
|
+
SemanticModel model = context.Compilation.GetSemanticModel(classDeclaration.SyntaxTree);
|
|
31
|
+
ISymbol classSymbol = ModelExtensions.GetDeclaredSymbol(model, classDeclaration);
|
|
32
|
+
|
|
33
|
+
if (classSymbol.GetAttributes().Any(
|
|
34
|
+
attributeData => attributeData.AttributeClass.Equals(
|
|
35
|
+
attributeSymbol, SymbolEqualityComparer.Default)))
|
|
36
|
+
{
|
|
37
|
+
string namespaceName = classSymbol.ContainingNamespace.ToDisplayString();
|
|
38
|
+
string className = classSymbol.Name;
|
|
39
|
+
string typeKind = classDeclaration.Kind() == SyntaxKind.ClassDeclaration ? "class" : "struct";
|
|
40
|
+
|
|
41
|
+
string source = $$"""
|
|
42
|
+
|
|
43
|
+
namespace {{namespaceName}}
|
|
44
|
+
{
|
|
45
|
+
using DxMessaging.Core.Messages;
|
|
46
|
+
|
|
47
|
+
public partial {{typeKind}} {{className}} : IUntargetedMessage
|
|
48
|
+
{
|
|
49
|
+
public System.Type MessageType => typeof({{className}});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
""";
|
|
54
|
+
|
|
55
|
+
context.AddSource($"{className}_DxUntargetedMessage.g.cs", SourceText.From(source, Encoding.UTF8));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
private sealed class SyntaxReceiver : ISyntaxReceiver
|
|
61
|
+
{
|
|
62
|
+
public List<TypeDeclarationSyntax> CandidateClasses { get; } = [];
|
|
63
|
+
|
|
64
|
+
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
|
|
65
|
+
{
|
|
66
|
+
if (syntaxNode is TypeDeclarationSyntax typeDeclarationSyntax)
|
|
67
|
+
{
|
|
68
|
+
if (typeDeclarationSyntax.AttributeLists.Count > 0 &&
|
|
69
|
+
(typeDeclarationSyntax.Kind() == SyntaxKind.ClassDeclaration ||
|
|
70
|
+
typeDeclarationSyntax.Kind() == SyntaxKind.StructDeclaration))
|
|
71
|
+
{
|
|
72
|
+
CandidateClasses.Add(typeDeclarationSyntax);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
<Project Sdk="Microsoft.NET.Sdk">
|
|
2
|
+
|
|
3
|
+
<PropertyGroup>
|
|
4
|
+
<TargetFramework>netstandard2.0</TargetFramework>
|
|
5
|
+
<LangVersion>latest</LangVersion>
|
|
6
|
+
</PropertyGroup>
|
|
7
|
+
|
|
8
|
+
<ItemGroup>
|
|
9
|
+
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.1">
|
|
10
|
+
<PrivateAssets>all</PrivateAssets>
|
|
11
|
+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
|
12
|
+
</PackageReference>
|
|
13
|
+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0">
|
|
14
|
+
<PrivateAssets>all</PrivateAssets>
|
|
15
|
+
</PackageReference>
|
|
16
|
+
<PackageReference Include="System.Collections.Immutable" Version="5.0.0">
|
|
17
|
+
<PrivateAssets>all</PrivateAssets>
|
|
18
|
+
</PackageReference>
|
|
19
|
+
</ItemGroup>
|
|
20
|
+
|
|
21
|
+
</Project>
|