@@ -63,10 +63,42 @@ public void Initialize(IncrementalGeneratorInitializationContext context) {
6363 . Where ( static m => m != null ) ;
6464
6565 IncrementalValueProvider < ImmutableArray < PropertyModel ? > > collected = properties . Collect ( ) ;
66+ IncrementalValueProvider < ImmutableArray < TableModel ? > > tables = InitializeTables ( context ) ;
6667
67- context . RegisterSourceOutput ( collected , static ( spc , models ) => Emit ( spc , models ) ) ;
68+ context . RegisterSourceOutput ( collected . Combine ( tables ) , static ( spc , data ) => Emit ( spc , data . Left , data . Right ) ) ;
6869
69- InitializeFieldAccessors ( context , collected , InitializeTables ( context ) ) ;
70+ InitializeFieldAccessors ( context , collected , tables ) ;
71+ }
72+
73+ private static HashSet < string > WholeChangeRootKeys ( ImmutableArray < TableModel ? > tables ) {
74+ var sent = new HashSet < string > ( ) ;
75+ var roots = new HashSet < string > ( ) ;
76+ foreach ( TableModel ? table in tables ) {
77+ if ( table == null )
78+ continue ;
79+ foreach ( FieldAccessorModel accessor in table . Accessors ) {
80+ string ? key = accessor . FieldKey ;
81+ if ( key == null || accessor . PredictionOnly )
82+ continue ;
83+ sent . Add ( key ) ;
84+ int expressionStart = key . LastIndexOf ( '|' ) + 1 ;
85+ int rootEnd = key . IndexOfAny ( new [ ] { '.' , '[' } , expressionStart ) ;
86+ if ( key [ 0 ] == 'A' || rootEnd >= 0 )
87+ roots . Add ( "S" + ( rootEnd >= 0 ? key . Substring ( 1 , rootEnd - 1 ) : key . Substring ( 1 ) ) ) ;
88+ }
89+ }
90+ roots . ExceptWith ( sent ) ;
91+ return roots ;
92+ }
93+
94+ private static bool HasWholeStateChanged ( INamedTypeSymbol ? type ) {
95+ for ( INamedTypeSymbol ? t = type ; t != null ; t = t . BaseType ) {
96+ foreach ( ISymbol member in t . GetMembers ( "NetworkStateChanged" ) ) {
97+ if ( member is IMethodSymbol method && method . Parameters . Length == 0 )
98+ return true ;
99+ }
100+ }
101+ return false ;
70102 }
71103
72104 private static bool HasStateTracking ( INamedTypeSymbol ? type ) {
@@ -135,7 +167,8 @@ private static bool IsNetworkArray(ITypeSymbol type)
135167 . Select ( m => m . Text ) ) ,
136168 RefReadonly = prop . ReturnsByRefReadonly ,
137169 BaseHasFields = BaseHasNetworkVarFields ( containingType . BaseType ) ,
138- ForModifyHidesBase = BaseHasForModify ( containingType . BaseType , prop . Name )
170+ ForModifyHidesBase = BaseHasForModify ( containingType . BaseType , prop . Name ) ,
171+ HasWholeStateChanged = HasWholeStateChanged ( containingType )
139172 } ;
140173 }
141174
@@ -166,7 +199,8 @@ private static bool BaseHasNetworkVarFields(INamedTypeSymbol? type) {
166199 return false ;
167200 }
168201
169- private static void Emit ( SourceProductionContext spc , ImmutableArray < PropertyModel ? > models ) {
202+ private static void Emit ( SourceProductionContext spc , ImmutableArray < PropertyModel ? > models , ImmutableArray < TableModel ? > tables ) {
203+ HashSet < string > wholeChangeRoots = WholeChangeRootKeys ( tables ) ;
170204 foreach ( var group in models
171205 . Where ( m => m != null )
172206 . Select ( m => m ! )
@@ -198,13 +232,13 @@ private static void Emit(SourceProductionContext spc, ImmutableArray<PropertyMod
198232 if ( valid . Count == 0 )
199233 continue ;
200234
201- string source = BuildSource ( first , valid ) ;
235+ string source = BuildSource ( first , valid , wholeChangeRoots ) ;
202236 string hintName = ( first . Namespace == null ? "" : first . Namespace + "." ) + first . TypeName + ".NetworkVars.g.cs" ;
203237 spc . AddSource ( hintName , SourceText . From ( source , Encoding . UTF8 ) ) ;
204238 }
205239 }
206240
207- private static string BuildSource ( PropertyModel type , List < PropertyModel > props ) {
241+ private static string BuildSource ( PropertyModel type , List < PropertyModel > props , HashSet < string > wholeChangeRoots ) {
208242 var sb = new StringBuilder ( ) ;
209243 sb . AppendLine ( "// <auto-generated/>" ) ;
210244 sb . AppendLine ( "#nullable enable" ) ;
@@ -226,6 +260,9 @@ private static string BuildSource(PropertyModel type, List<PropertyModel> props)
226260 string t = p . TypeDisplay ;
227261 string name = p . PropertyName ;
228262 string backing = "__nv_" + name ;
263+ string stateChanged = p . HasWholeStateChanged && wholeChangeRoots . Contains ( "S|" + type . TypeFullyQualified + "|" + name )
264+ ? "NetworkStateChanged();"
265+ : "NetworkStateChanged(NetworkVarFields." + name + ");" ;
229266
230267 sb . Append ( indent ) . Append ( "private " ) . Append ( t ) . Append ( ' ' ) . Append ( backing ) . AppendLine ( ";" ) ;
231268 string accessibility = p . Accessibility . Length == 0 ? "" : p . Accessibility + " " ;
@@ -236,7 +273,7 @@ private static string BuildSource(PropertyModel type, List<PropertyModel> props)
236273 sb . Append ( indent ) . Append ( forModify ) . Append ( t ) . Append ( ' ' ) . Append ( name ) . Append ( "ForModify() => ref " ) . Append ( backing ) . AppendLine ( ";" ) ;
237274 else {
238275 sb . Append ( indent ) . Append ( forModify ) . Append ( t ) . Append ( ' ' ) . Append ( name ) . AppendLine ( "ForModify() {" ) ;
239- sb . Append ( indent ) . Append ( " \t NetworkStateChanged(NetworkVarFields." ) . Append ( name ) . AppendLine ( ");" ) ;
276+ sb . Append ( indent ) . Append ( ' \t ' ) . AppendLine ( stateChanged ) ;
240277 sb . Append ( indent ) . Append ( "\t return ref " ) . Append ( backing ) . AppendLine ( ";" ) ;
241278 sb . Append ( indent ) . AppendLine ( "}" ) ;
242279 }
@@ -261,12 +298,12 @@ private static string BuildSource(PropertyModel type, List<PropertyModel> props)
261298 . Append ( ", (global::System.ReadOnlySpan<" ) . Append ( e ) . Append ( ">)value, global::System.Collections.Generic.EqualityComparer<" ) . Append ( e ) . AppendLine ( ">.Default)) {" ) ;
262299 }
263300 sb . Append ( indent ) . Append ( "\t \t \t " ) . Append ( backing ) . AppendLine ( " = value;" ) ;
264- sb . Append ( indent ) . Append ( "\t \t \t NetworkStateChanged(NetworkVarFields. " ) . Append ( name ) . AppendLine ( ");" ) ;
301+ sb . Append ( indent ) . Append ( "\t \t \t " ) . AppendLine ( stateChanged ) ;
265302 sb . Append ( indent ) . AppendLine ( "\t \t }" ) ;
266303 sb . Append ( indent ) . AppendLine ( "\t }" ) ;
267304 sb . Append ( indent ) . AppendLine ( "}" ) ;
268305 sb . Append ( indent ) . Append ( forModify ) . Append ( t ) . Append ( ' ' ) . Append ( name ) . AppendLine ( "ForModify() {" ) ;
269- sb . Append ( indent ) . Append ( " \t NetworkStateChanged(NetworkVarFields." ) . Append ( name ) . AppendLine ( ");" ) ;
306+ sb . Append ( indent ) . Append ( ' \t ' ) . AppendLine ( stateChanged ) ;
270307 sb . Append ( indent ) . Append ( "\t return ref " ) . Append ( backing ) . AppendLine ( ";" ) ;
271308 sb . Append ( indent ) . AppendLine ( "}" ) ;
272309 sb . AppendLine ( ) ;
@@ -341,6 +378,7 @@ public static PropertyModel ServerFieldError(string fieldName, string typeName,
341378 public bool RefReadonly { get ; set ; }
342379 public bool BaseHasFields { get ; set ; }
343380 public bool ForModifyHidesBase { get ; set ; }
381+ public bool HasWholeStateChanged { get ; set ; }
344382 public Location Location { get ; }
345383 }
346384 }
0 commit comments