22using Microsoft . CodeAnalysis . CSharp . Syntax ;
33using Microsoft . CodeAnalysis . Text ;
44using System . Collections . Immutable ;
5+ using System . Diagnostics ;
56using System . Text ;
67using Terraria ;
78using TrProtocol . Attributes ;
@@ -21,7 +22,7 @@ namespace TrProtocol.SerializerGenerator
2122 public partial class SerializeGenerator : IIncrementalGenerator
2223 {
2324 private static bool FilterTypes ( SyntaxNode syntaxNode , CancellationToken token ) {
24- if ( syntaxNode is TypeDeclarationSyntax td /* && td.Keyword.ToString() is not "interface"*/ && td . Keyword . ToString ( ) is not "record" && td . BaseList is not null ) {
25+ if ( syntaxNode is TypeDeclarationSyntax td /* && td.Keyword.ToString() is not "interface" && td.Keyword.ToString() is not "record" && td.BaseList is not null*/ ) {
2526 return true ;
2627 }
2728 return false ;
@@ -65,15 +66,15 @@ private static ProtocolTypeInfo Transform(TypeDeclarationSyntax typeDeclaration)
6566 }
6667 #endregion
6768
68- static readonly string [ ] NeccessaryUsings = new string [ ] {
69+ static readonly string [ ] NeccessaryUsings = [
6970 "System.Runtime.CompilerServices" ,
7071 "System.Runtime.InteropServices" ,
7172 "System.Diagnostics.CodeAnalysis" ,
7273 "TrProtocol.Attributes" ,
7374 "TrProtocol.Interfaces" ,
7475 "TrProtocol.Exceptions" ,
7576 "TrProtocol.Models" ,
76- } ;
77+ ] ;
7778
7879 // TODO: Split the serialization code generation process into more smaller SyntaxTemplates
7980 private static void Execute ( SourceProductionContext context , ( Compilation compilation , ImmutableArray < ProtocolTypeInfo > infos ) data ) {
@@ -173,25 +174,31 @@ static void CheckMemberSymbol(INamedTypeSymbol typeSym, SerializationExpandConte
173174 var classNode = namespaceBlock . WriteTypeDefinition ( model ) ;
174175 classNode . WriteAutoDiscriminator ( model ) ;
175176
176- #region Manage type constructors and special behavior
177+ #region Add Global ID
178+ if ( model . GlobalID >= 0 ) {
179+ classNode . WriteLine ( $ "public static int GlobalID => { model . GlobalID } ;") ;
180+ }
181+ #endregion
182+
183+ #region Add type constructors and special behavior
177184
178185 List < ( string memberName , string memberType ) > externalMembers = model . DefSyntax . Members
179- . Where ( m => m . AttributeLists
180- . SelectMany ( a => a . Attributes )
181- . Any ( a => a . AttributeMatch < ExternalMemberAttribute > ( ) ) )
182- . Select ( ( m ) => {
183- if ( m is PropertyDeclarationSyntax prop ) {
184- return new List < ( string , string ) > ( ) { ( prop . Identifier . ToString ( ) , prop . Type . ToString ( ) ) } ;
185- }
186- else {
187- var field = ( FieldDeclarationSyntax ) m ;
188- var list = new List < ( string memberName , string memberType ) > ( ) ;
189- foreach ( var variable in field . Declaration . Variables ) {
190- list . Add ( ( variable . Identifier . ToString ( ) , field . Declaration . Type . ToString ( ) ) ) ;
186+ . Where ( m => m . AttributeLists
187+ . SelectMany ( a => a . Attributes )
188+ . Any ( a => a . AttributeMatch < ExternalMemberAttribute > ( ) ) )
189+ . Select ( ( m ) => {
190+ if ( m is PropertyDeclarationSyntax prop ) {
191+ return new List < ( string , string ) > ( ) { ( prop . Identifier . ToString ( ) , prop . Type . ToString ( ) ) } ;
191192 }
192- return list ;
193- }
194- } ) . SelectMany ( m => m ) . ToList ( ) ;
193+ else {
194+ var field = ( FieldDeclarationSyntax ) m ;
195+ var list = new List < ( string memberName , string memberType ) > ( ) ;
196+ foreach ( var variable in field . Declaration . Variables ) {
197+ list . Add ( ( variable . Identifier . ToString ( ) , field . Declaration . Type . ToString ( ) ) ) ;
198+ }
199+ return list ;
200+ }
201+ } ) . SelectMany ( m => m ) . ToList ( ) ;
195202
196203 string externalMemberParams ;
197204 if ( externalMembers . Count == 0 ) {
@@ -1489,7 +1496,7 @@ object GetArraySize(ExpressionSyntax indexExp, int i) {
14891496
14901497 writeNode . WriteLine ( "var ptr_current = ptr;" ) ;
14911498 writeNode . WriteLine ( ) ;
1492- if ( model . IsConcreteType ) {
1499+ if ( model . IsConcreteImpl ) {
14931500 INamedTypeSymbol [ ] ExtractAbstractModelInheritance ( INamedTypeSymbol type ) {
14941501 var abstractModelAncestors = type . GetFullInheritanceTree ( )
14951502 . Where ( t => t . HasAbstractModelAttribute ( ) )
@@ -1603,7 +1610,7 @@ param[0].Type is PointerTypeSyntax pointerType &&
16031610 }
16041611 return false ;
16051612 } ) ) {
1606- classNode . Write ( $ "public unsafe { ( ( model . IsConcreteType && ! model . IsValueType ) ? "override " : "" ) } void WriteContent(ref void* ptr) ") ;
1613+ classNode . Write ( $ "public unsafe { ( ( model . IsConcreteImpl && ! model . IsValueType ) ? "override " : "" ) } void WriteContent(ref void* ptr) ") ;
16071614 classNode . Sources . Add ( writeNode ) ;
16081615 }
16091616 #endregion
@@ -1630,7 +1637,7 @@ param[0].Type is PointerTypeSyntax pointerType &&
16301637 classNode . WriteLine ( "/// This operation is not supported and always throws a System.NotSupportedException." ) ;
16311638 classNode . WriteLine ( "/// </summary>" ) ;
16321639 classNode . WriteLine ( $ "[Obsolete]") ;
1633- if ( ( model . IsConcreteType && ! model . IsValueType ) ) {
1640+ if ( ( model . IsConcreteImpl && ! model . IsValueType ) ) {
16341641 classNode . WriteLine ( $ "public override void ReadContent(ref void* ptr) => throw new { nameof ( NotSupportedException ) } ();") ;
16351642 }
16361643 else {
@@ -1641,7 +1648,7 @@ param[0].Type is PointerTypeSyntax pointerType &&
16411648 }
16421649 else {
16431650 classNode . WriteLine ( $ "[MemberNotNull({ string . Join ( ", " , memberNullables . Select ( m => $ "nameof({ m } )") ) } )]") ;
1644- classNode . Write ( $ "public unsafe { ( ( model . IsConcreteType && ! model . IsValueType ) ? "override " : "" ) } void ReadContent(ref void* ptr) ") ;
1651+ classNode . Write ( $ "public unsafe { ( ( model . IsConcreteImpl && ! model . IsValueType ) ? "override " : "" ) } void ReadContent(ref void* ptr) ") ;
16451652 }
16461653 classNode . Sources . Add ( readNode ) ;
16471654 }
@@ -1683,16 +1690,15 @@ param[0].Type is PointerTypeSyntax pointerType &&
16831690 l . DefSyntax . GetNamespace ( out _ , out var ns , out _ ) ;
16841691 return ns ;
16851692 } ) ) . Distinct ( ) ) {
1686-
16871693 source . WriteLine ( $ "using { us } ;") ;
16881694 }
16891695
16901696
1691- List < ( string memberName , string memberType ) > externalMembers = polymorphicBase . DefSyntax . Members
1697+ List < ( string memberName , string memberType ) > externalMembers = [ .. polymorphicBase . DefSyntax . Members
16921698 . Where ( m => m . AttributeLists
16931699 . SelectMany ( a => a . Attributes )
16941700 . Any ( a => a . AttributeMatch < ExternalMemberAttribute > ( ) ) )
1695- . Select ( ( m ) => {
1701+ . SelectMany ( ( m ) => {
16961702 if ( m is PropertyDeclarationSyntax prop ) {
16971703 return new List < ( string , string ) > ( ) { ( prop . Identifier . ToString ( ) , prop . Type . ToString ( ) ) } ;
16981704 }
@@ -1704,7 +1710,7 @@ param[0].Type is PointerTypeSyntax pointerType &&
17041710 }
17051711 return list ;
17061712 }
1707- } ) . SelectMany ( m => m ) . ToList ( ) ;
1713+ } ) ] ;
17081714 string externalMemberParams ;
17091715 string externalMemberParamsCall ;
17101716 if ( externalMembers . Count == 0 ) {
@@ -1721,6 +1727,12 @@ param[0].Type is PointerTypeSyntax pointerType &&
17211727 var typeKind = polymorphicBase . IsInterface ? "interface" : polymorphicBase . IsValueType ? "struct" : "class" ;
17221728 source . Write ( $ "public unsafe partial { typeKind } { polymorphicBase . TypeName } ") ;
17231729 source . BlockWrite ( ( source ) => {
1730+
1731+ if ( polymorphicBase . IsGlobalIDRoot ) {
1732+ source . WriteLine ( $ "public const int GlobalIDCount = { polymorphicBase . AllocatedGlobalIDCount } ;") ;
1733+ source . WriteLine ( "public static abstract int GlobalID { get; }" ) ;
1734+ }
1735+
17241736 source . Write ( $ "public unsafe static { polymorphicBase . TypeName } Read{ polymorphicBase . TypeName } (ref void* ptr{ ( polymorphicBase . IsNetPacket ? ", void* ptr_end" : "" ) } { ( polymorphicBase . IsNetPacket ? ", bool isServerSide" : "" ) } { externalMemberParams } ) ") ;
17251737 source . BlockWrite ( ( source ) => {
17261738 source . WriteLine ( $ "{ enumType . Name } identity = ({ enumType . Name } )Unsafe.Read<{ enumType . EnumUnderlyingType } >(ptr);") ;
@@ -1753,13 +1765,6 @@ param[0].Type is PointerTypeSyntax pointerType &&
17531765 static CompilationContext Compilation = new CompilationContext ( ) ;
17541766
17551767 public void Initialize ( IncrementalGeneratorInitializationContext initContext ) {
1756-
1757- #if DEBUG
1758- //if (!Debugger.IsAttached) {
1759- // Debugger.Launch();
1760- //}
1761- #endif
1762-
17631768 initContext . RegisterSourceOutput ( initContext . CompilationProvider . WithComparer ( Compilation ) , Compilation . LoadCompilation ) ;
17641769
17651770
0 commit comments