diff --git a/Source.CodeAnalysis.NetworkVars/NetworkVarGenerator.FieldAccessors.cs b/Source.CodeAnalysis.NetworkVars/NetworkVarGenerator.FieldAccessors.cs index d9833ccb..2d3f2b26 100644 --- a/Source.CodeAnalysis.NetworkVars/NetworkVarGenerator.FieldAccessors.cs +++ b/Source.CodeAnalysis.NetworkVars/NetworkVarGenerator.FieldAccessors.cs @@ -164,7 +164,7 @@ private static bool IsInvocationNamed(SyntaxNode node, HashSet names) { List conversions = new List(); AddConversions(compilation, path.Type, conversions); - return new FieldAccessorModel("S|" + ownerDisplay + "|" + expression + "|" + (name ?? "\0"), registration, body.ToString(), string.Join("\n", conversions)); + return new FieldAccessorModel("S|" + ownerDisplay + "|" + expression + "|" + (name ?? "\0"), registration, body.ToString(), string.Join("\n", conversions)) { FieldKey = FieldKey("S", path, owner, expression) }; } private static FieldAccessorModel? BuildArray(Compilation compilation, INamedTypeSymbol owner, string expression, bool isList, AccessFailure failure) { @@ -222,7 +222,7 @@ private static bool IsInvocationNamed(SyntaxNode node, HashSet names) { AddConversions(compilation, path.Type, conversions); AddConversions(compilation, element, conversions); - return new FieldAccessorModel("A|" + ownerDisplay + "|" + expression, registration, body.ToString(), string.Join("\n", conversions)); + return new FieldAccessorModel("A|" + ownerDisplay + "|" + expression, registration, body.ToString(), string.Join("\n", conversions)) { FieldKey = FieldKey("A", path, owner, expression) }; } private static ResolvedPath? Resolve(Compilation compilation, INamedTypeSymbol owner, string expression, AccessFailure failure) { @@ -698,11 +698,14 @@ public static FieldAccessorModel Inaccessible(Location location, string member, public string Registration { get; } public string Body { get; } public string Conversions { get; } + public string? FieldKey { get; set; } + public bool PredictionOnly { get; set; } public Location? ErrorLocation { get; private set; } public object[]? ErrorArgs { get; private set; } public bool Equals(FieldAccessorModel? other) - => other != null && Key == other.Key && Registration == other.Registration && Body == other.Body && Conversions == other.Conversions && Equals(ErrorLocation, other.ErrorLocation); + => other != null && Key == other.Key && Registration == other.Registration && Body == other.Body && Conversions == other.Conversions && FieldKey == other.FieldKey + && PredictionOnly == other.PredictionOnly && Equals(ErrorLocation, other.ErrorLocation); public override bool Equals(object? obj) => Equals(obj as FieldAccessorModel); public override int GetHashCode() => Key.GetHashCode(); diff --git a/Source.CodeAnalysis.NetworkVars/NetworkVarGenerator.Tables.cs b/Source.CodeAnalysis.NetworkVars/NetworkVarGenerator.Tables.cs index 861feef8..66108e0c 100644 --- a/Source.CodeAnalysis.NetworkVars/NetworkVarGenerator.Tables.cs +++ b/Source.CodeAnalysis.NetworkVars/NetworkVarGenerator.Tables.cs @@ -424,8 +424,10 @@ private static void EmitTables(SourceProductionContext spc, ImmutableArray m != null); IncrementalValueProvider> collected = properties.Collect(); + IncrementalValueProvider> tables = InitializeTables(context); - context.RegisterSourceOutput(collected, static (spc, models) => Emit(spc, models)); + context.RegisterSourceOutput(collected.Combine(tables), static (spc, data) => Emit(spc, data.Left, data.Right)); - InitializeFieldAccessors(context, collected, InitializeTables(context)); + InitializeFieldAccessors(context, collected, tables); + } + + private static HashSet WholeChangeRootKeys(ImmutableArray tables) { + var sent = new HashSet(); + var roots = new HashSet(); + foreach (TableModel? table in tables) { + if (table == null) + continue; + foreach (FieldAccessorModel accessor in table.Accessors) { + string? key = accessor.FieldKey; + if (key == null || accessor.PredictionOnly) + continue; + sent.Add(key); + int expressionStart = key.LastIndexOf('|') + 1; + int rootEnd = key.IndexOfAny(new[] { '.', '[' }, expressionStart); + if (key[0] == 'A' || rootEnd >= 0) + roots.Add("S" + (rootEnd >= 0 ? key.Substring(1, rootEnd - 1) : key.Substring(1))); + } + } + roots.ExceptWith(sent); + return roots; + } + + private static bool HasWholeStateChanged(INamedTypeSymbol? type) { + for (INamedTypeSymbol? t = type; t != null; t = t.BaseType) { + foreach (ISymbol member in t.GetMembers("NetworkStateChanged")) { + if (member is IMethodSymbol method && method.Parameters.Length == 0) + return true; + } + } + return false; } private static bool HasStateTracking(INamedTypeSymbol? type) { @@ -135,7 +167,8 @@ private static bool IsNetworkArray(ITypeSymbol type) .Select(m => m.Text)), RefReadonly = prop.ReturnsByRefReadonly, BaseHasFields = BaseHasNetworkVarFields(containingType.BaseType), - ForModifyHidesBase = BaseHasForModify(containingType.BaseType, prop.Name) + ForModifyHidesBase = BaseHasForModify(containingType.BaseType, prop.Name), + HasWholeStateChanged = HasWholeStateChanged(containingType) }; } @@ -166,7 +199,8 @@ private static bool BaseHasNetworkVarFields(INamedTypeSymbol? type) { return false; } - private static void Emit(SourceProductionContext spc, ImmutableArray models) { + private static void Emit(SourceProductionContext spc, ImmutableArray models, ImmutableArray tables) { + HashSet wholeChangeRoots = WholeChangeRootKeys(tables); foreach (var group in models .Where(m => m != null) .Select(m => m!) @@ -198,13 +232,13 @@ private static void Emit(SourceProductionContext spc, ImmutableArray props) { + private static string BuildSource(PropertyModel type, List props, HashSet wholeChangeRoots) { var sb = new StringBuilder(); sb.AppendLine("// "); sb.AppendLine("#nullable enable"); @@ -226,6 +260,9 @@ private static string BuildSource(PropertyModel type, List props) string t = p.TypeDisplay; string name = p.PropertyName; string backing = "__nv_" + name; + string stateChanged = p.HasWholeStateChanged && wholeChangeRoots.Contains("S|" + type.TypeFullyQualified + "|" + name) + ? "NetworkStateChanged();" + : "NetworkStateChanged(NetworkVarFields." + name + ");"; sb.Append(indent).Append("private ").Append(t).Append(' ').Append(backing).AppendLine(";"); string accessibility = p.Accessibility.Length == 0 ? "" : p.Accessibility + " "; @@ -236,7 +273,7 @@ private static string BuildSource(PropertyModel type, List props) sb.Append(indent).Append(forModify).Append(t).Append(' ').Append(name).Append("ForModify() => ref ").Append(backing).AppendLine(";"); else { sb.Append(indent).Append(forModify).Append(t).Append(' ').Append(name).AppendLine("ForModify() {"); - sb.Append(indent).Append("\tNetworkStateChanged(NetworkVarFields.").Append(name).AppendLine(");"); + sb.Append(indent).Append('\t').AppendLine(stateChanged); sb.Append(indent).Append("\treturn ref ").Append(backing).AppendLine(";"); sb.Append(indent).AppendLine("}"); } @@ -261,12 +298,12 @@ private static string BuildSource(PropertyModel type, List props) .Append(", (global::System.ReadOnlySpan<").Append(e).Append(">)value, global::System.Collections.Generic.EqualityComparer<").Append(e).AppendLine(">.Default)) {"); } sb.Append(indent).Append("\t\t\t").Append(backing).AppendLine(" = value;"); - sb.Append(indent).Append("\t\t\tNetworkStateChanged(NetworkVarFields.").Append(name).AppendLine(");"); + sb.Append(indent).Append("\t\t\t").AppendLine(stateChanged); sb.Append(indent).AppendLine("\t\t}"); sb.Append(indent).AppendLine("\t}"); sb.Append(indent).AppendLine("}"); sb.Append(indent).Append(forModify).Append(t).Append(' ').Append(name).AppendLine("ForModify() {"); - sb.Append(indent).Append("\tNetworkStateChanged(NetworkVarFields.").Append(name).AppendLine(");"); + sb.Append(indent).Append('\t').AppendLine(stateChanged); sb.Append(indent).Append("\treturn ref ").Append(backing).AppendLine(";"); sb.Append(indent).AppendLine("}"); sb.AppendLine(); @@ -341,6 +378,7 @@ public static PropertyModel ServerFieldError(string fieldName, string typeName, public bool RefReadonly { get; set; } public bool BaseHasFields { get; set; } public bool ForModifyHidesBase { get; set; } + public bool HasWholeStateChanged { get; set; } public Location Location { get; } } }