Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private static bool IsInvocationNamed(SyntaxNode node, HashSet<string> names) {
List<string> conversions = new List<string>();
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) {
Expand Down Expand Up @@ -222,7 +222,7 @@ private static bool IsInvocationNamed(SyntaxNode node, HashSet<string> 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) {
Expand Down Expand Up @@ -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();
Expand Down
8 changes: 6 additions & 2 deletions Source.CodeAnalysis.NetworkVars/NetworkVarGenerator.Tables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,10 @@ private static void EmitTables(SourceProductionContext spc, ImmutableArray<Table
ITypeSymbol memberType = member is IFieldSymbol pf ? pf.Type : ((IPropertySymbol)member).Type;
if (SettingValue(entry.Settings, "Embedded") is bool embedded && embedded) {
FieldAccessorModel? embeddedAccessor = BuildScalar(compilation, member.ContainingType, member.Name, null, new AccessFailure());
if (embeddedAccessor != null)
if (embeddedAccessor != null) {
embeddedAccessor.PredictionOnly = true;
accessors.Add(embeddedAccessor);
}
predicted.Add(PredictionDescription("Embedded", member, "1", "global::Source.Common.FieldTypeDescFlags.Save", Display(memberType) + ".PredMap", "0"));
entries.Add("E");
embeddedMaps.Add(memberType.ToDisplayString() + ".PredMap");
Expand All @@ -443,8 +445,10 @@ private static void EmitTables(SourceProductionContext spc, ImmutableArray<Table

INamedTypeSymbol declaring = member.ContainingType;
FieldAccessorModel? predAccessor = BuildScalar(compilation, declaring, member.Name, null, new AccessFailure());
if (predAccessor != null)
if (predAccessor != null) {
predAccessor.PredictionOnly = true;
accessors.Add(predAccessor);
}

bool hasFlags = entry.Settings.Values.ContainsKey("Flags");
object? tolerance = SettingValue(entry.Settings, "Tolerance");
Expand Down
56 changes: 47 additions & 9 deletions Source.CodeAnalysis.NetworkVars/NetworkVarGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,42 @@ public void Initialize(IncrementalGeneratorInitializationContext context) {
.Where(static m => m != null);

IncrementalValueProvider<ImmutableArray<PropertyModel?>> collected = properties.Collect();
IncrementalValueProvider<ImmutableArray<TableModel?>> 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<string> WholeChangeRootKeys(ImmutableArray<TableModel?> tables) {
var sent = new HashSet<string>();
var roots = new HashSet<string>();
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) {
Expand Down Expand Up @@ -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)
};
}

Expand Down Expand Up @@ -166,7 +199,8 @@ private static bool BaseHasNetworkVarFields(INamedTypeSymbol? type) {
return false;
}

private static void Emit(SourceProductionContext spc, ImmutableArray<PropertyModel?> models) {
private static void Emit(SourceProductionContext spc, ImmutableArray<PropertyModel?> models, ImmutableArray<TableModel?> tables) {
HashSet<string> wholeChangeRoots = WholeChangeRootKeys(tables);
foreach (var group in models
.Where(m => m != null)
.Select(m => m!)
Expand Down Expand Up @@ -198,13 +232,13 @@ private static void Emit(SourceProductionContext spc, ImmutableArray<PropertyMod
if (valid.Count == 0)
continue;

string source = BuildSource(first, valid);
string source = BuildSource(first, valid, wholeChangeRoots);
string hintName = (first.Namespace == null ? "" : first.Namespace + ".") + first.TypeName + ".NetworkVars.g.cs";
spc.AddSource(hintName, SourceText.From(source, Encoding.UTF8));
}
}

private static string BuildSource(PropertyModel type, List<PropertyModel> props) {
private static string BuildSource(PropertyModel type, List<PropertyModel> props, HashSet<string> wholeChangeRoots) {
var sb = new StringBuilder();
sb.AppendLine("// <auto-generated/>");
sb.AppendLine("#nullable enable");
Expand All @@ -226,6 +260,9 @@ private static string BuildSource(PropertyModel type, List<PropertyModel> 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 + " ";
Expand All @@ -236,7 +273,7 @@ private static string BuildSource(PropertyModel type, List<PropertyModel> 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("}");
}
Expand All @@ -261,12 +298,12 @@ private static string BuildSource(PropertyModel type, List<PropertyModel> 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();
Expand Down Expand Up @@ -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; }
}
}
Expand Down
Loading