Skip to content

Commit

Permalink
Fix not running hooks when copying non-byref data definition fields w…
Browse files Browse the repository at this point in the history
…ithout a custom serializer (space-wizards#4324)
  • Loading branch information
DrSmugleaf authored Aug 26, 2023
1 parent b6c8060 commit aeeaaae
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 32 deletions.
16 changes: 1 addition & 15 deletions Robust.Serialization.Generator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -446,18 +446,6 @@ private static StringBuilder CopyDataFields(DataDefinition definition)
""");
}

var instantiator = string.Empty;
if (IsDataDefinition(type))
{
instantiator = $"{tempVarName} = {name}.Instantiate();";
}
else if (!type.IsAbstract &&
HasEmptyPublicConstructor(type) &&
(type.IsReferenceType || IsNullableType(type)))
{
instantiator = $"{tempVarName} = new();";
}

var hasHooks = ImplementsInterface(type, SerializationHooksNamespace) || !type.IsSealed;
builder.AppendLine($$"""
if (!serialization.TryCustomCopy(this.{{name}}, ref {{tempVarName}}, hookCtx, {{hasHooks.ToString().ToLower()}}, context))
Expand All @@ -471,7 +459,6 @@ private static StringBuilder CopyDataFields(DataDefinition definition)
else if (IsDataDefinition(type) && !type.IsAbstract &&
type is not INamedTypeSymbol { TypeKind: TypeKind.Interface })
{
var nullability = type.IsValueType ? string.Empty : "?";
var nullable = !type.IsValueType || IsNullableType(type);

if (nullable)
Expand All @@ -487,8 +474,7 @@ private static StringBuilder CopyDataFields(DataDefinition definition)
}

builder.AppendLine($$"""
{{instantiator}}
{{name}}{{nullability}}.Copy(ref {{tempVarName}}, serialization, hookCtx, context);
serialization.CopyTo({{name}}, ref {{tempVarName}}, hookCtx, context{{nullableOverride}});
""");

if (nullable)
Expand Down
17 changes: 0 additions & 17 deletions Robust.Serialization.Generator/Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,23 +286,6 @@ internal static bool NeedsEmptyConstructor(ITypeSymbol type)
return true;
}

internal static bool HasEmptyPublicConstructor(ITypeSymbol type)
{
if (type is not INamedTypeSymbol named)
return false;

foreach (var constructor in named.InstanceConstructors)
{
if (constructor.DeclaredAccessibility == Accessibility.Public &&
constructor.Parameters.Length == 0)
{
return true;
}
}

return false;
}

internal static bool IsVirtualClass(ITypeSymbol type)
{
return type.IsReferenceType && !type.IsSealed && type.TypeKind != TypeKind.Interface;
Expand Down

0 comments on commit aeeaaae

Please sign in to comment.