Skip to content

Commit 7dbbdf1

Browse files
authored
Merge pull request #286 from Yahzy/fix/embedded-netvar-changes
Map embedded struct changes to their props in local entity transfer
2 parents 40b1131 + 43a93ed commit 7dbbdf1

3 files changed

Lines changed: 59 additions & 14 deletions

File tree

‎Source.CodeAnalysis.NetworkVars/NetworkVarGenerator.FieldAccessors.cs‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ private static bool IsInvocationNamed(SyntaxNode node, HashSet<string> names) {
164164
List<string> conversions = new List<string>();
165165
AddConversions(compilation, path.Type, conversions);
166166

167-
return new FieldAccessorModel("S|" + ownerDisplay + "|" + expression + "|" + (name ?? "\0"), registration, body.ToString(), string.Join("\n", conversions));
167+
return new FieldAccessorModel("S|" + ownerDisplay + "|" + expression + "|" + (name ?? "\0"), registration, body.ToString(), string.Join("\n", conversions)) { FieldKey = FieldKey("S", path, owner, expression) };
168168
}
169169

170170
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<string> names) {
222222
AddConversions(compilation, path.Type, conversions);
223223
AddConversions(compilation, element, conversions);
224224

225-
return new FieldAccessorModel("A|" + ownerDisplay + "|" + expression, registration, body.ToString(), string.Join("\n", conversions));
225+
return new FieldAccessorModel("A|" + ownerDisplay + "|" + expression, registration, body.ToString(), string.Join("\n", conversions)) { FieldKey = FieldKey("A", path, owner, expression) };
226226
}
227227

228228
private static ResolvedPath? Resolve(Compilation compilation, INamedTypeSymbol owner, string expression, AccessFailure failure) {
@@ -698,11 +698,14 @@ public static FieldAccessorModel Inaccessible(Location location, string member,
698698
public string Registration { get; }
699699
public string Body { get; }
700700
public string Conversions { get; }
701+
public string? FieldKey { get; set; }
702+
public bool PredictionOnly { get; set; }
701703
public Location? ErrorLocation { get; private set; }
702704
public object[]? ErrorArgs { get; private set; }
703705

704706
public bool Equals(FieldAccessorModel? other)
705-
=> other != null && Key == other.Key && Registration == other.Registration && Body == other.Body && Conversions == other.Conversions && Equals(ErrorLocation, other.ErrorLocation);
707+
=> other != null && Key == other.Key && Registration == other.Registration && Body == other.Body && Conversions == other.Conversions && FieldKey == other.FieldKey
708+
&& PredictionOnly == other.PredictionOnly && Equals(ErrorLocation, other.ErrorLocation);
706709

707710
public override bool Equals(object? obj) => Equals(obj as FieldAccessorModel);
708711
public override int GetHashCode() => Key.GetHashCode();

‎Source.CodeAnalysis.NetworkVars/NetworkVarGenerator.Tables.cs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,10 @@ private static void EmitTables(SourceProductionContext spc, ImmutableArray<Table
424424
ITypeSymbol memberType = member is IFieldSymbol pf ? pf.Type : ((IPropertySymbol)member).Type;
425425
if (SettingValue(entry.Settings, "Embedded") is bool embedded && embedded) {
426426
FieldAccessorModel? embeddedAccessor = BuildScalar(compilation, member.ContainingType, member.Name, null, new AccessFailure());
427-
if (embeddedAccessor != null)
427+
if (embeddedAccessor != null) {
428+
embeddedAccessor.PredictionOnly = true;
428429
accessors.Add(embeddedAccessor);
430+
}
429431
predicted.Add(PredictionDescription("Embedded", member, "1", "global::Source.Common.FieldTypeDescFlags.Save", Display(memberType) + ".PredMap", "0"));
430432
entries.Add("E");
431433
embeddedMaps.Add(memberType.ToDisplayString() + ".PredMap");
@@ -443,8 +445,10 @@ private static void EmitTables(SourceProductionContext spc, ImmutableArray<Table
443445

444446
INamedTypeSymbol declaring = member.ContainingType;
445447
FieldAccessorModel? predAccessor = BuildScalar(compilation, declaring, member.Name, null, new AccessFailure());
446-
if (predAccessor != null)
448+
if (predAccessor != null) {
449+
predAccessor.PredictionOnly = true;
447450
accessors.Add(predAccessor);
451+
}
448452

449453
bool hasFlags = entry.Settings.Values.ContainsKey("Flags");
450454
object? tolerance = SettingValue(entry.Settings, "Tolerance");

‎Source.CodeAnalysis.NetworkVars/NetworkVarGenerator.cs‎

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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("\tNetworkStateChanged(NetworkVarFields.").Append(name).AppendLine(");");
276+
sb.Append(indent).Append('\t').AppendLine(stateChanged);
240277
sb.Append(indent).Append("\treturn 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\tNetworkStateChanged(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("\tNetworkStateChanged(NetworkVarFields.").Append(name).AppendLine(");");
306+
sb.Append(indent).Append('\t').AppendLine(stateChanged);
270307
sb.Append(indent).Append("\treturn 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

Comments
 (0)