Skip to content

Commit 4de1e34

Browse files
committed
Fix U-PR comments
1 parent 366fc35 commit 4de1e34

2 files changed

Lines changed: 38 additions & 11 deletions

File tree

‎com.unity.netcode.gameobjects/Editor/CodeGen/CodeGenHelpers.cs‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,33 +211,58 @@ public static bool HasInterface(this TypeReference typeReference, string interfa
211211
/// <summary>
212212
/// Collects every type that assembly scoped code cannot access when it uses <paramref name="type"/>.
213213
/// This is each private or protected nested type in its nesting chain, or in the chain of its generic arguments.
214+
/// A protected internal type is only hidden when it is declared in an assembly whose internals <paramref name="consumer"/> cannot see.
214215
/// </summary>
215216
/// <param name="type">The type the generated code uses.</param>
217+
/// <param name="consumer">The assembly the generated code is emitted into.</param>
216218
/// <param name="hidden">Receives each inaccessible type, without duplicates.</param>
217-
public static void CollectTypesHiddenFromAssembly(this TypeReference type, List<TypeDefinition> hidden)
219+
public static void CollectTypesHiddenFromAssembly(this TypeReference type, AssemblyDefinition consumer, List<TypeDefinition> hidden)
218220
{
219221
if (type is GenericInstanceType genericInstance)
220222
{
221223
foreach (var argument in genericInstance.GenericArguments)
222224
{
223-
argument.CollectTypesHiddenFromAssembly(hidden);
225+
argument.CollectTypesHiddenFromAssembly(consumer, hidden);
224226
}
225227
}
226228
else if (type is TypeSpecification specification)
227229
{
228-
specification.ElementType.CollectTypesHiddenFromAssembly(hidden);
230+
specification.ElementType.CollectTypesHiddenFromAssembly(consumer, hidden);
229231
return;
230232
}
231233

232234
for (var definition = type.Resolve(); definition != null && definition.IsNested; definition = definition.DeclaringType)
233235
{
234-
if ((definition.IsNestedPrivate || definition.IsNestedFamily || definition.IsNestedFamilyAndAssembly) && !hidden.Contains(definition))
236+
var isHidden = definition.IsNestedPrivate || definition.IsNestedFamily || definition.IsNestedFamilyAndAssembly ||
237+
(definition.IsNestedFamilyOrAssembly && !consumer.CanSeeInternalsOf(definition.Module.Assembly));
238+
if (isHidden && !hidden.Contains(definition))
235239
{
236240
hidden.Add(definition);
237241
}
238242
}
239243
}
240244

245+
/// <summary>
246+
/// Checks whether code in <paramref name="consumer"/> can access the internal members of <paramref name="owner"/>.
247+
/// </summary>
248+
/// <param name="consumer">The assembly that accesses the members.</param>
249+
/// <param name="owner">The assembly that declares the members.</param>
250+
/// <returns>True if both are the same assembly, or <paramref name="owner"/> grants <paramref name="consumer"/> InternalsVisibleTo.</returns>
251+
private static bool CanSeeInternalsOf(this AssemblyDefinition consumer, AssemblyDefinition owner)
252+
{
253+
if (owner.Name.Name == consumer.Name.Name)
254+
{
255+
return true;
256+
}
257+
258+
// InternalsVisibleTo takes "Name" or "Name, PublicKey=..."; only the simple name matters here.
259+
return owner.CustomAttributes.Any(attribute =>
260+
attribute.AttributeType.FullName == typeof(System.Runtime.CompilerServices.InternalsVisibleToAttribute).FullName &&
261+
attribute.ConstructorArguments.Count == 1 &&
262+
attribute.ConstructorArguments[0].Value is string friend &&
263+
friend.Split(',')[0].Trim() == consumer.Name.Name);
264+
}
265+
241266
/// <summary>
242267
/// Checks a generic method instantiation against the generic constraints of the method it instantiates.
243268
/// Mono does not enforce these on the calls codegen emits, but CoreCLR does, and it rejects the entire

‎com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -439,22 +439,24 @@ private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly,
439439

440440
// CoreCLR enforces member access on the generic calls emitted below; Mono does not.
441441
var hiddenTypes = new List<TypeDefinition>();
442-
type.CollectTypesHiddenFromAssembly(hiddenTypes);
442+
type.CollectTypesHiddenFromAssembly(assembly, hiddenTypes);
443443
if (hiddenTypes.Count > 0)
444444
{
445-
#if ENABLE_CORECLR
446-
foreach (var hiddenType in hiddenTypes)
445+
// Use the target assembly's defines: a Mono editor can build a CoreCLR player.
446+
if (assemblyDefines.Contains("ENABLE_CORECLR"))
447447
{
448-
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.");
448+
foreach (var hiddenType in hiddenTypes)
449+
{
450+
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.");
451+
}
452+
continue;
449453
}
450-
continue;
451-
#else
454+
452455
// Log a warning in Mono so we can warn users who haven't yet switched their projects over.
453456
foreach (var hiddenType in hiddenTypes)
454457
{
455458
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.");
456459
}
457-
#endif
458460
}
459461

460462
if (serializeMethod != null)

0 commit comments

Comments
 (0)