public static CompilationUnitSyntax BuildPayloadClassSyntax(string className, NetworkOperationCode code) { return(CompilationUnit() .WithUsings ( List <UsingDirectiveSyntax> ( new UsingDirectiveSyntax[] { UsingDirective ( IdentifierName("System") ) .WithUsingKeyword ( Token ( TriviaList(), SyntaxKind.UsingKeyword, TriviaList ( Space ) ) ) .WithSemicolonToken ( Token ( TriviaList(), SyntaxKind.SemicolonToken, TriviaList ( CarriageReturnLineFeed ) ) ), UsingDirective ( QualifiedName ( IdentifierName("FreecraftCore"), IdentifierName("Serializer") ) ) .WithUsingKeyword ( Token ( TriviaList(), SyntaxKind.UsingKeyword, TriviaList ( Space ) ) ) .WithSemicolonToken ( Token ( TriviaList(), SyntaxKind.SemicolonToken, TriviaList ( CarriageReturnLineFeed ) ) ) } ) ) .WithMembers ( SingletonList <MemberDeclarationSyntax> ( NamespaceDeclaration ( IdentifierName ( Identifier ( TriviaList(), "FreecraftCore", TriviaList ( CarriageReturnLineFeed ) ) ) ) .WithNamespaceKeyword ( Token ( TriviaList ( CarriageReturnLineFeed ), SyntaxKind.NamespaceKeyword, TriviaList ( Space ) ) ) .WithOpenBraceToken ( Token ( TriviaList(), SyntaxKind.OpenBraceToken, TriviaList ( CarriageReturnLineFeed ) ) ) .WithMembers ( SingletonList <MemberDeclarationSyntax> ( ClassDeclaration ( Identifier ( TriviaList(), className, TriviaList ( Space ) ) ) .WithAttributeLists ( List <AttributeListSyntax> ( new AttributeListSyntax[] { AttributeList ( SingletonSeparatedList <AttributeSyntax> ( Attribute ( IdentifierName(nameof(WireDataContractAttribute).Replace("Attribute", "")) ) ) ) .WithOpenBracketToken ( Token ( TriviaList ( new[] { Tab, Trivia ( DocumentationCommentTrivia ( SyntaxKind.SingleLineDocumentationCommentTrivia, List <XmlNodeSyntax> ( new XmlNodeSyntax[] { XmlText() .WithTextTokens ( TokenList ( XmlTextLiteral ( TriviaList ( DocumentationCommentExterior("///") ), " ", " ", TriviaList() ) ) ), XmlExampleElement ( SingletonList <XmlNodeSyntax> ( XmlText() .WithTextTokens ( TokenList ( new[] { XmlTextNewLine ( TriviaList(), Environment.NewLine, Environment.NewLine, TriviaList() ), XmlTextLiteral ( TriviaList ( DocumentationCommentExterior(" ///") ), $" {code}:", $" {code}:", TriviaList() ), XmlTextNewLine ( TriviaList(), Environment.NewLine, Environment.NewLine, TriviaList() ), XmlTextLiteral ( TriviaList ( DocumentationCommentExterior(" ///") ), " ", " ", TriviaList() ) } ) ) ) ) .WithStartTag ( XmlElementStartTag ( XmlName ( Identifier("summary") ) ) ) .WithEndTag ( XmlElementEndTag ( XmlName ( Identifier("summary") ) ) ), XmlText() .WithTextTokens ( TokenList ( XmlTextNewLine ( TriviaList(), Environment.NewLine, Environment.NewLine, TriviaList() ) ) ) } ) ) ), Tab } ), SyntaxKind.OpenBracketToken, TriviaList() ) ) .WithCloseBracketToken ( Token ( TriviaList(), SyntaxKind.CloseBracketToken, TriviaList ( CarriageReturnLineFeed ) ) ), AttributeList ( SingletonSeparatedList <AttributeSyntax> ( Attribute ( IdentifierName(nameof(GamePayloadOperationCodeAttribute).Replace("Attribute", "")) ) .WithArgumentList ( AttributeArgumentList ( SingletonSeparatedList <AttributeArgumentSyntax> ( AttributeArgument ( MemberAccessExpression ( SyntaxKind.SimpleMemberAccessExpression, IdentifierName(nameof(NetworkOperationCode)), IdentifierName(code.ToString()) ) ) ) ) ) ) ) .WithOpenBracketToken ( Token ( TriviaList ( Tab ), SyntaxKind.OpenBracketToken, TriviaList() ) ) .WithCloseBracketToken ( Token ( TriviaList(), SyntaxKind.CloseBracketToken, TriviaList ( CarriageReturnLineFeed ) ) ) } ) ) .WithModifiers ( TokenList ( new[] { Token ( TriviaList ( Tab ), SyntaxKind.PublicKeyword, TriviaList ( Space ) ), Token ( TriviaList(), SyntaxKind.SealedKeyword, TriviaList ( Space ) ), Token ( TriviaList(), SyntaxKind.PartialKeyword, TriviaList ( Space ) ) } ) ) .WithKeyword ( Token ( TriviaList(), SyntaxKind.ClassKeyword, TriviaList ( Space ) ) ) .WithBaseList ( BaseList ( SingletonSeparatedList <BaseTypeSyntax> ( SimpleBaseType ( IdentifierName ( Identifier ( TriviaList(), nameof(GamePacketPayload), TriviaList ( CarriageReturnLineFeed ) ) ) ) ) ) .WithColonToken ( Token ( TriviaList(), SyntaxKind.ColonToken, TriviaList ( Space ) ) ) ) .WithOpenBraceToken ( Token ( TriviaList ( Tab ), SyntaxKind.OpenBraceToken, TriviaList ( CarriageReturnLineFeed ) ) ) .WithMembers ( List <MemberDeclarationSyntax> ( new MemberDeclarationSyntax[] { PropertyDeclaration ( ArrayType ( PredefinedType ( Token(SyntaxKind.ByteKeyword) ) ) .WithRankSpecifiers ( SingletonList <ArrayRankSpecifierSyntax> ( ArrayRankSpecifier ( SingletonSeparatedList <ExpressionSyntax> ( OmittedArraySizeExpression() ) ) .WithCloseBracketToken ( Token ( TriviaList(), SyntaxKind.CloseBracketToken, TriviaList ( Space ) ) ) ) ), Identifier ( TriviaList(), "Contents", TriviaList ( Space ) ) ) .WithAttributeLists ( List <AttributeListSyntax> ( new AttributeListSyntax[] { AttributeList ( SingletonSeparatedList <AttributeSyntax> ( Attribute ( IdentifierName(nameof(WireMemberAttribute).Replace("Attribute", "")) ) .WithArgumentList ( AttributeArgumentList ( SingletonSeparatedList <AttributeArgumentSyntax> ( AttributeArgument ( LiteralExpression ( SyntaxKind.NumericLiteralExpression, Literal(1) ) ) ) ) ) ) ) .WithOpenBracketToken ( Token ( TriviaList ( new[] { Whitespace(" "), Trivia ( DocumentationCommentTrivia ( SyntaxKind.SingleLineDocumentationCommentTrivia, List <XmlNodeSyntax> ( new XmlNodeSyntax[] { XmlText() .WithTextTokens ( TokenList ( XmlTextLiteral ( TriviaList ( DocumentationCommentExterior("///") ), " ", " ", TriviaList() ) ) ), XmlNullKeywordElement() .WithName ( XmlName ( Identifier("inheritdoc") ) ) .WithSlashGreaterThanToken ( Token ( TriviaList ( Space ), SyntaxKind.SlashGreaterThanToken, TriviaList() ) ), XmlText() .WithTextTokens ( TokenList ( XmlTextNewLine ( TriviaList(), Environment.NewLine, Environment.NewLine, TriviaList() ) ) ) } ) ) ), Whitespace(" ") } ), SyntaxKind.OpenBracketToken, TriviaList() ) ) .WithCloseBracketToken ( Token ( TriviaList(), SyntaxKind.CloseBracketToken, TriviaList ( CarriageReturnLineFeed ) ) ), AttributeList ( SingletonSeparatedList <AttributeSyntax> ( Attribute ( IdentifierName(nameof(ReadToEndAttribute).Replace("Attribute", "")) ) ) ) .WithOpenBracketToken ( Token ( TriviaList ( Whitespace(" ") ), SyntaxKind.OpenBracketToken, TriviaList() ) ) .WithCloseBracketToken ( Token ( TriviaList(), SyntaxKind.CloseBracketToken, TriviaList ( CarriageReturnLineFeed ) ) ) } ) ) .WithModifiers ( TokenList ( Token ( TriviaList ( Whitespace(" ") ), SyntaxKind.PublicKeyword, TriviaList ( Space ) ) ) ) .WithAccessorList ( AccessorList ( List <AccessorDeclarationSyntax> ( new AccessorDeclarationSyntax[] { AccessorDeclaration ( SyntaxKind.GetAccessorDeclaration ) .WithSemicolonToken ( Token ( TriviaList(), SyntaxKind.SemicolonToken, TriviaList ( Space ) ) ), AccessorDeclaration ( SyntaxKind.SetAccessorDeclaration ) .WithModifiers ( TokenList ( Token ( TriviaList(), SyntaxKind.InternalKeyword, TriviaList ( Space ) ) ) ) .WithSemicolonToken ( Token ( TriviaList(), SyntaxKind.SemicolonToken, TriviaList ( Space ) ) ) } ) ) .WithOpenBraceToken ( Token ( TriviaList(), SyntaxKind.OpenBraceToken, TriviaList ( Space ) ) ) .WithCloseBraceToken ( Token ( TriviaList(), SyntaxKind.CloseBraceToken, TriviaList ( CarriageReturnLineFeed ) ) ) ), ConstructorDeclaration ( Identifier(className) ) .WithModifiers ( TokenList ( Token ( TriviaList ( new[] { CarriageReturnLineFeed, Whitespace(" ") } ), SyntaxKind.PublicKeyword, TriviaList ( Space ) ) ) ) .WithParameterList ( ParameterList ( SingletonSeparatedList <ParameterSyntax> ( Parameter ( Identifier("contents") ) .WithType ( ArrayType ( PredefinedType ( Token(SyntaxKind.ByteKeyword) ) ) .WithRankSpecifiers ( SingletonList <ArrayRankSpecifierSyntax> ( ArrayRankSpecifier ( SingletonSeparatedList <ExpressionSyntax> ( OmittedArraySizeExpression() ) ) .WithCloseBracketToken ( Token ( TriviaList(), SyntaxKind.CloseBracketToken, TriviaList ( Space ) ) ) ) ) ) ) ) .WithCloseParenToken ( Token ( TriviaList(), SyntaxKind.CloseParenToken, TriviaList ( CarriageReturnLineFeed ) ) ) ) .WithInitializer ( ConstructorInitializer ( SyntaxKind.ThisConstructorInitializer, ArgumentList() .WithCloseParenToken ( Token ( TriviaList(), SyntaxKind.CloseParenToken, TriviaList ( CarriageReturnLineFeed ) ) ) ) .WithColonToken ( Token ( TriviaList ( Whitespace(" ") ), SyntaxKind.ColonToken, TriviaList ( Space ) ) ) ) .WithBody ( Block ( SingletonList <StatementSyntax> ( ExpressionStatement ( AssignmentExpression ( SyntaxKind.SimpleAssignmentExpression, IdentifierName ( Identifier ( TriviaList ( Whitespace(" ") ), "Contents", TriviaList ( Space ) ) ), BinaryExpression ( SyntaxKind.CoalesceExpression, IdentifierName ( Identifier ( TriviaList(), "contents", TriviaList ( Space ) ) ), ThrowExpression ( ObjectCreationExpression ( IdentifierName("ArgumentNullException") ) .WithNewKeyword ( Token ( TriviaList(), SyntaxKind.NewKeyword, TriviaList ( Space ) ) ) .WithArgumentList ( ArgumentList ( SingletonSeparatedList <ArgumentSyntax> ( Argument ( InvocationExpression ( IdentifierName ( Identifier ( TriviaList(), SyntaxKind.NameOfKeyword, "nameof", "nameof", TriviaList() ) ) ) .WithArgumentList ( ArgumentList ( SingletonSeparatedList <ArgumentSyntax> ( Argument ( IdentifierName("contents") ) ) ) ) ) ) ) ) ) .WithThrowKeyword ( Token ( TriviaList(), SyntaxKind.ThrowKeyword, TriviaList ( Space ) ) ) ) .WithOperatorToken ( Token ( TriviaList(), SyntaxKind.QuestionQuestionToken, TriviaList ( Space ) ) ) ) .WithOperatorToken ( Token ( TriviaList(), SyntaxKind.EqualsToken, TriviaList ( Space ) ) ) ) .WithSemicolonToken ( Token ( TriviaList(), SyntaxKind.SemicolonToken, TriviaList ( CarriageReturnLineFeed ) ) ) ) ) .WithOpenBraceToken ( Token ( TriviaList ( Whitespace(" ") ), SyntaxKind.OpenBraceToken, TriviaList ( CarriageReturnLineFeed ) ) ) .WithCloseBraceToken ( Token ( TriviaList ( Whitespace(" ") ), SyntaxKind.CloseBraceToken, TriviaList ( CarriageReturnLineFeed ) ) ) ), ConstructorDeclaration ( Identifier(className) ) .WithModifiers ( TokenList ( Token ( TriviaList ( new[] { CarriageReturnLineFeed, Whitespace(" ") } ), SyntaxKind.PublicKeyword, TriviaList ( Space ) ) ) ) .WithParameterList ( ParameterList() .WithCloseParenToken ( Token ( TriviaList(), SyntaxKind.CloseParenToken, TriviaList ( CarriageReturnLineFeed ) ) ) ) .WithInitializer ( ConstructorInitializer ( SyntaxKind.BaseConstructorInitializer, ArgumentList ( SingletonSeparatedList <ArgumentSyntax> ( Argument ( MemberAccessExpression ( SyntaxKind.SimpleMemberAccessExpression, IdentifierName(nameof(NetworkOperationCode)), IdentifierName(code.ToString()) ) ) ) ) .WithCloseParenToken ( Token ( TriviaList(), SyntaxKind.CloseParenToken, TriviaList ( CarriageReturnLineFeed ) ) ) ) .WithColonToken ( Token ( TriviaList ( Whitespace(" ") ), SyntaxKind.ColonToken, TriviaList ( Space ) ) ) ) .WithBody ( Block() .WithOpenBraceToken ( Token ( TriviaList ( Whitespace(" ") ), SyntaxKind.OpenBraceToken, TriviaList ( CarriageReturnLineFeed ) ) ) .WithCloseBraceToken ( Token ( TriviaList ( new[] { CarriageReturnLineFeed, Whitespace(" ") } ), SyntaxKind.CloseBraceToken, TriviaList ( CarriageReturnLineFeed ) ) ) ) } ) ) .WithCloseBraceToken ( Token ( TriviaList ( Tab ), SyntaxKind.CloseBraceToken, TriviaList ( CarriageReturnLineFeed ) ) ) ) ) ) )); }
public static CompilationUnitSyntax BuildPayloadClassSyntax(string className, NetworkOperationCode code) { return(SyntaxFactory.CompilationUnit() .WithUsings ( SyntaxFactory.List <UsingDirectiveSyntax> ( new UsingDirectiveSyntax[] { SyntaxFactory.UsingDirective ( SyntaxFactory.IdentifierName(nameof(FreecraftCore)) ), SyntaxFactory.UsingDirective ( SyntaxFactory.QualifiedName ( SyntaxFactory.IdentifierName(nameof(FreecraftCore)), SyntaxFactory.IdentifierName(nameof(Serializer)) ) ) } ) ) .WithMembers ( SyntaxFactory.SingletonList <MemberDeclarationSyntax> ( SyntaxFactory.ClassDeclaration(className) .WithAttributeLists ( SyntaxFactory.List <AttributeListSyntax> ( new AttributeListSyntax[] { SyntaxFactory.AttributeList ( SyntaxFactory.SingletonSeparatedList <AttributeSyntax> ( SyntaxFactory.Attribute ( SyntaxFactory.IdentifierName(nameof(GamePayloadOperationCodeAttribute)) ) .WithArgumentList ( SyntaxFactory.AttributeArgumentList ( SyntaxFactory.SingletonSeparatedList <AttributeArgumentSyntax> ( SyntaxFactory.AttributeArgument ( SyntaxFactory.MemberAccessExpression ( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(nameof(NetworkOperationCode)), SyntaxFactory.IdentifierName(code.ToString()) ) ) ) ) ) ) ), SyntaxFactory.AttributeList ( SyntaxFactory.SingletonSeparatedList <AttributeSyntax> ( SyntaxFactory.Attribute ( SyntaxFactory.IdentifierName(nameof(WireDataContractAttribute)) ) ) ) } ) ) .WithModifiers ( SyntaxFactory.TokenList ( new[] { SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.SealedKeyword) } ) ) .WithBaseList ( SyntaxFactory.BaseList ( SyntaxFactory.SeparatedList <BaseTypeSyntax> ( new SyntaxNodeOrToken[] { SyntaxFactory.SimpleBaseType ( SyntaxFactory.IdentifierName(nameof(GamePacketPayload)) ), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.SimpleBaseType ( SyntaxFactory.IdentifierName(nameof(IUnimplementedGamePacketPayload)) ) } ) ) ) .WithMembers ( SyntaxFactory.List <MemberDeclarationSyntax> ( new MemberDeclarationSyntax[] { SyntaxFactory.FieldDeclaration ( SyntaxFactory.VariableDeclaration ( SyntaxFactory.ArrayType ( SyntaxFactory.PredefinedType ( SyntaxFactory.Token(SyntaxKind.ByteKeyword) ) ) .WithRankSpecifiers ( SyntaxFactory.SingletonList <ArrayRankSpecifierSyntax> ( SyntaxFactory.ArrayRankSpecifier ( SyntaxFactory.SingletonSeparatedList <ExpressionSyntax> ( SyntaxFactory.OmittedArraySizeExpression() ) ) ) ) ) .WithVariables ( SyntaxFactory.SingletonSeparatedList <VariableDeclaratorSyntax> ( SyntaxFactory.VariableDeclarator ( SyntaxFactory.Identifier($"_{nameof(IUnimplementedGamePacketPayload.Data)}") ) ) ) ) .WithAttributeLists ( SyntaxFactory.List <AttributeListSyntax> ( new AttributeListSyntax[] { SyntaxFactory.AttributeList ( SyntaxFactory.SingletonSeparatedList <AttributeSyntax> ( SyntaxFactory.Attribute ( SyntaxFactory.IdentifierName(nameof(ReadToEndAttribute)) ) ) ), SyntaxFactory.AttributeList ( SyntaxFactory.SingletonSeparatedList <AttributeSyntax> ( SyntaxFactory.Attribute ( SyntaxFactory.IdentifierName(nameof(WireMemberAttribute)) ) .WithArgumentList ( SyntaxFactory.AttributeArgumentList ( SyntaxFactory.SingletonSeparatedList <AttributeArgumentSyntax> ( SyntaxFactory.AttributeArgument ( SyntaxFactory.LiteralExpression ( SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(1) ) ) ) ) ) ) ) } ) ) .WithModifiers ( SyntaxFactory.TokenList ( SyntaxFactory.Token(SyntaxKind.PrivateKeyword) ) ), SyntaxFactory.PropertyDeclaration ( SyntaxFactory.ArrayType ( SyntaxFactory.PredefinedType ( SyntaxFactory.Token(SyntaxKind.ByteKeyword) ) ) .WithRankSpecifiers ( SyntaxFactory.SingletonList <ArrayRankSpecifierSyntax> ( SyntaxFactory.ArrayRankSpecifier ( SyntaxFactory.SingletonSeparatedList <ExpressionSyntax> ( SyntaxFactory.OmittedArraySizeExpression() ) ) ) ), SyntaxFactory.Identifier(nameof(IUnimplementedGamePacketPayload.Data)) ) .WithModifiers ( SyntaxFactory.TokenList ( SyntaxFactory.Token(SyntaxKind.PublicKeyword) ) ) .WithAccessorList ( SyntaxFactory.AccessorList ( SyntaxFactory.List <AccessorDeclarationSyntax> ( new AccessorDeclarationSyntax[] { SyntaxFactory.AccessorDeclaration ( SyntaxKind.GetAccessorDeclaration ) .WithBody ( SyntaxFactory.Block ( SyntaxFactory.SingletonList <StatementSyntax> ( SyntaxFactory.ReturnStatement ( SyntaxFactory.IdentifierName($"_{nameof(IUnimplementedGamePacketPayload.Data)}") ) ) ) ), SyntaxFactory.AccessorDeclaration ( SyntaxKind.SetAccessorDeclaration ) .WithBody ( SyntaxFactory.Block ( SyntaxFactory.SingletonList <StatementSyntax> ( SyntaxFactory.ExpressionStatement ( SyntaxFactory.AssignmentExpression ( SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.IdentifierName($"_{nameof(IUnimplementedGamePacketPayload.Data)}"), SyntaxFactory.IdentifierName("value") ) ) ) ) ) } ) ) ), SyntaxFactory.ConstructorDeclaration ( SyntaxFactory.Identifier(className) ) .WithModifiers ( SyntaxFactory.TokenList ( SyntaxFactory.Token(SyntaxKind.PublicKeyword) ) ) .WithBody ( SyntaxFactory.Block() ) } ) ) ) ) .NormalizeWhitespace()); }