Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 30 additions & 0 deletions com.unity.netcode.gameobjects/Editor/CodeGen/CodeGenHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,36 @@ public static bool HasInterface(this TypeReference typeReference, string interfa
}
}

/// <summary>
/// Collects every type that assembly scoped code cannot access when it uses <paramref name="type"/>.
/// This is each private or protected nested type in its nesting chain, or in the chain of its generic arguments.
/// </summary>
/// <param name="type">The type the generated code uses.</param>
/// <param name="hidden">Receives each inaccessible type, without duplicates.</param>
public static void CollectTypesHiddenFromAssembly(this TypeReference type, List<TypeDefinition> hidden)
{
if (type is GenericInstanceType genericInstance)
{
foreach (var argument in genericInstance.GenericArguments)
{
argument.CollectTypesHiddenFromAssembly(hidden);
}
}
else if (type is TypeSpecification specification)
{
specification.ElementType.CollectTypesHiddenFromAssembly(hidden);
return;
}

for (var definition = type.Resolve(); definition != null && definition.IsNested; definition = definition.DeclaringType)
{
if ((definition.IsNestedPrivate || definition.IsNestedFamily || definition.IsNestedFamilyAndAssembly) && !hidden.Contains(definition))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1

InitializeSerialization still fails on CoreCLR when NetworkVariable<Payload> uses a referenced base's protected internal nested enum without friend access. A derived NetworkBehaviour can legally declare that field using protected access. GetAllFieldsAndResolveGenerics collects Payload, but this predicate does not check IsNestedFamilyOrAssembly. The generated NetworkVariableSerializationHelper cannot access Payload because the helper is outside the base's assembly and inheritance hierarchy. Pass the consuming assembly into CollectTypesHiddenFromAssembly. Reject IsNestedFamilyOrAssembly types when that assembly lacks internal or friend access.

🤖 Helpful? 👍/👎

{
hidden.Add(definition);
}
}
}

/// <summary>
/// Checks a generic method instantiation against the generic constraints of the method it instantiates.
/// Mono does not enforce these on the calls codegen emits, but CoreCLR does, and it rejects the entire
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,26 @@ private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly,
continue;
}

// CoreCLR enforces member access on the generic calls emitted below; Mono does not.
var hiddenTypes = new List<TypeDefinition>();
type.CollectTypesHiddenFromAssembly(hiddenTypes);
if (hiddenTypes.Count > 0)
{
#if ENABLE_CORECLR
Comment thread
EmandM marked this conversation as resolved.
Resolved
foreach (var hiddenType in hiddenTypes)
{
m_Diagnostics.AddError($"{type} cannot be used in a network variable - Cannot access the {hiddenType.Name} type to generate serialization as it is {(hiddenType.IsNestedPrivate ? "private" : "protected")}. To fix this, change the access modifier to internal or public.");
}
continue;
#else
// Log a warning in Mono so we can warn users who haven't yet switched their projects over.
foreach (var hiddenType in hiddenTypes)
{
m_Diagnostics.AddWarning($"NetworkVariable serialization for {type.Name} will fail in CoreCLR! With CoreCLR generated code cannot access the {hiddenType.Name} type to generate serialization as it is {(hiddenType.IsNestedPrivate ? "private" : "protected")}. To fix this, change the access modifier to internal or public.");
}
#endif
}

if (serializeMethod != null)
{
instructions.Add(processor.Create(OpCodes.Call, m_MainModule.ImportReference(serializeMethod)));
Expand Down
Loading