This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
/
LeagueClientEvent.cs
43 lines (36 loc) · 2.03 KB
/
LeagueClientEvent.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace RiotGames.Client.CodeGeneration.LeagueClient;
internal static class LeagueClientEvent
{
public static MemberDeclarationSyntax[] RmsEvent(string topic, string identifier, string typeName)
{
identifier = identifier.RemoveStart("Get");
var privateEventIdentifier = '_' + identifier.ToCamelCase() + "Changed";
var publicEventIdentifier = identifier.ToPascalCase() + "Changed";
var privateEvent = EventFieldDeclaration(List<AttributeListSyntax>(), SyntaxKind.PrivateKeyword.ToTokenList(),
VariableDeclaration(
ParseTypeName($"LeagueClientEventHandler<{typeName}>"),
SeparatedList(new[]
{
VariableDeclarator(privateEventIdentifier)
}
)
));
var publicEvent = EventDeclaration(ParseTypeName($"LeagueClientEventHandler<{typeName}>"),
Identifier(publicEventIdentifier))
.WithModifiers(SyntaxKind.PublicKeyword.ToTokenList())
.WithAccessorList(AccessorList(new SyntaxList<AccessorDeclarationSyntax>(new[]
{
AccessorDeclaration(SyntaxKind.AddAccessorDeclaration, ParseStatement(
$"if ({privateEventIdentifier} == null) EventRouter.Subscribe(\"{topic}\", (RmsEventType eventType, {typeName} args) => {privateEventIdentifier}?.Invoke(this, eventType, args)); {privateEventIdentifier} += value;")
.ToBlock()),
AccessorDeclaration(SyntaxKind.RemoveAccessorDeclaration, ParseStatement(
$"{privateEventIdentifier} -= value; if ({privateEventIdentifier} == null) EventRouter.Unsubscribe(\"{topic}\"); ")
.ToBlock())
})));
return new MemberDeclarationSyntax[] {privateEvent, publicEvent};
}
}