@@ -18,6 +18,7 @@ private struct XnbTypeInfo
1818 public string TypeName ;
1919 public string TypeFullName ;
2020 public string QualifierString ;
21+ public List < string > GenericParameters ;
2122 public List < XnbPropertyInfo > Properties ;
2223 }
2324
@@ -50,11 +51,6 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
5051 return null ;
5152 }
5253
53- if ( typeSymbol . IsGenericType )
54- {
55- return null ;
56- }
57-
5854 var xnbReaderTypeAttribute = typeSymbol . GetAttributes ( )
5955 . FirstOrDefault ( a => a . AttributeClass ? . ToDisplayString ( ) == XnbReaderTypeAttributeName ) ;
6056
@@ -63,19 +59,19 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
6359 return null ;
6460 }
6561
66- bool useBaseClass = xnbReaderTypeAttribute . NamedArguments
67- . FirstOrDefault ( x => x . Key == "UseBaseClass " ) . Value . Value is true ;
62+ bool isPrivate = xnbReaderTypeAttribute . NamedArguments
63+ . FirstOrDefault ( x => x . Key == "IsPrivate " ) . Value . Value is true ;
6864
69- var logicalTypeSymbol = typeSymbol ;
70- if ( useBaseClass && typeSymbol . BaseType is not null )
65+ if ( isPrivate )
7166 {
72- logicalTypeSymbol = typeSymbol . BaseType ;
67+ return null ;
7368 }
7469
7570 var qualifierString = xnbReaderTypeAttribute . ConstructorArguments [ 0 ] . Value as string ?? string . Empty ;
71+ var genericParameters = typeSymbol . TypeParameters . Select ( tp => tp . Name ) . ToList ( ) ;
7672 var properties = new List < XnbPropertyInfo > ( ) ;
7773
78- foreach ( var member in logicalTypeSymbol . GetMembers ( ) . OfType < IPropertySymbol > ( ) )
74+ foreach ( var member in typeSymbol . GetMembers ( ) . OfType < IPropertySymbol > ( ) )
7975 {
8076 ct . ThrowIfCancellationRequested ( ) ;
8177
@@ -123,9 +119,10 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
123119 return new XnbTypeInfo
124120 {
125121 TypeName = typeSymbol . Name ,
126- TypeFullName = logicalTypeSymbol . ToDisplayString ( SymbolDisplayFormat . FullyQualifiedFormat ) ,
122+ TypeFullName = typeSymbol . ToDisplayString ( SymbolDisplayFormat . FullyQualifiedFormat ) ,
127123 QualifierString = qualifierString ,
128124 Properties = properties ,
125+ GenericParameters = genericParameters
129126 } ;
130127 }
131128
@@ -148,18 +145,53 @@ private static void EmitSerializer(CodeStringBuilder cb, XnbTypeInfo xnbTypeInfo
148145 cb . AppendLine ( ) ;
149146 cb . AppendLine ( "namespace FEZRepacker.Core.XNB.ContentSerialization;" ) ;
150147 cb . AppendLine ( ) ;
151- cb . Append ( $ "internal sealed class { xnbTypeInfo . TypeName } ContentSerializer ") ;
148+ cb . Append ( $ "internal sealed class { ConstructSerializerName ( xnbTypeInfo ) } ") ;
152149 cb . AppendLine ( $ " : XnbContentSerializer<{ xnbTypeInfo . TypeFullName } >") ;
153150 cb . BeginCodeBlock ( ) ;
154151 {
155- cb . AppendLine ( $ "public override XnbAssemblyQualifier Name => \" { xnbTypeInfo . QualifierString } \" ;" ) ;
152+ EmitConstructor ( cb , xnbTypeInfo ) ;
156153 cb . AppendLine ( ) ;
157154 EmitDeserialize ( cb , xnbTypeInfo ) ;
158155 cb . AppendLine ( ) ;
159156 EmitSerialize ( cb , xnbTypeInfo ) ;
160157 }
161158 cb . EndCodeBlock ( ) ;
162159 }
160+
161+ private static string ConstructSerializerName ( XnbTypeInfo xnbTypeInfo )
162+ {
163+ var name = $ "{ xnbTypeInfo . TypeName } ContentSerializer";
164+ if ( xnbTypeInfo . GenericParameters . Count > 0 )
165+ {
166+ var genericParametersList = string . Join ( ", " , xnbTypeInfo . GenericParameters ) ;
167+ name += $ "<{ genericParametersList } >";
168+ }
169+ return name ;
170+ }
171+
172+ private static void EmitConstructor ( CodeStringBuilder cb , XnbTypeInfo xnbTypeInfo )
173+ {
174+ if ( xnbTypeInfo . GenericParameters . Count == 0 )
175+ {
176+ cb . AppendLine ( $ "public override XnbAssemblyQualifier Name => \" { xnbTypeInfo . QualifierString } \" ;") ;
177+ return ;
178+ }
179+
180+ cb . AppendLine ( "private readonly XnbAssemblyQualifier _name;" ) ;
181+ cb . AppendLine ( ) ;
182+ cb . AppendLine ( "public override XnbAssemblyQualifier Name => _name;" ) ;
183+ cb . Append ( "public override Type[] UnderlyingContentTypes => [" ) ;
184+ cb . Append ( string . Join ( ", " , xnbTypeInfo . GenericParameters . Select ( type => $ "typeof({ type } )") ) ) ;
185+ cb . AppendLine ( "];" ) ;
186+ cb . AppendLine ( ) ;
187+ cb . AppendLine ( $ "public { xnbTypeInfo . TypeName } ContentSerializer() : base ()") ;
188+ cb . BeginCodeBlock ( ) ;
189+ {
190+ cb . AppendLine ( $ "var name = XnbAssemblyQualifier.TryGetFromXnbReaderType(typeof({ xnbTypeInfo . TypeFullName } ));") ;
191+ cb . AppendLine ( "if (name.HasValue) _name = name.Value;" ) ;
192+ }
193+ cb . EndCodeBlock ( ) ;
194+ }
163195
164196 private static void EmitDeserialize ( CodeStringBuilder cb , XnbTypeInfo model )
165197 {
0 commit comments