com.unity.netcode.gameobjects icon indicating copy to clipboard operation
com.unity.netcode.gameobjects copied to clipboard

NetworkManagerEditor can cause reflection issues

Open Reag opened this issue 2 years ago • 3 comments

Description

During the ReloadTransports method of NetworkManagerEditor.cs, it calls AppDomain.CurrentDomain.GetAssemblies(); in order to dynamically load all assemblies, and then search through every type in order to find subclasses of NetworkTransport. While this likely wont be an issue for the vast majority of projects, the product I'm currently developing references several native plugins and dlls (Specifically GRPC and Protobuf) that cause reflection errors when this is called.

Reproduce Steps

  1. Add some DLL that can cause reflection issues to the project, such as GRPC.Core.IAsyncStreamWriter
  2. Click on the NetworkManager

Actual Outcome

A ReflectionTypeLoadException is thrown

Expected Outcome

Nothing interesting happens

Screenshots

image

Environment

  • OS: Windows 10
  • Unity Version: 2022.3.2f1
  • Netcode Version:1.4.0

Additional Context

Technically speaking, this is an issue with the GRPC dll and not the editor script. However it does feel a little odd to me that an editor script is iterating over every type in the project to find classes that are of type UnityTransport. I am definitely NOT an expect of domains, low level C#, or Editor Tools (Im a graphics engineer), but it does seem somewhat unusual to me, so Im raising the issue! I hope its helpful <3

Reag avatar Jul 17 '23 07:07 Reag

(Nevermind this comment, I commented on the wrong issue.)

ShadauxCat avatar Jul 20 '23 19:07 ShadauxCat

I'm unsure of how this would solve the problem? Could you elaborate a bit to help me out?

Reag avatar Jul 24 '23 07:07 Reag

@Reag Sorry, I commented on the wrong issue, ignore me!

ShadauxCat avatar Jul 24 '23 14:07 ShadauxCat

@Reag I haven't yet been able to replicate this issue. What I have done so far:

  • Went to the nuget GRPC package
  • Opened it with 7zip
    • extracted the .NET 2.0 Grpc.Core.Api.dll into a test project
  • created a script file
    • referenced the Grpc.Core namespace
    • put together a quick stream writer implementation using IAsyncStreamWriter
    • created class property from that implementation class type.
    • in the Start method instantiated and assigned the instance to the property
  • tried this two different ways:
    • As a MonoBehaviour component attached to the GameObject with a NetworkManager component.
    • As a NetworkBehaviour component attached to an in-scene placed NetworkObject.

With the above steps, I could not get the same exception to occur. There could be something else unique to what you are doing that is causing the issue.

Could you provide a reproduction project?

NoelStephensUnity avatar May 31 '24 17:05 NoelStephensUnity

@NoelStephensUnity

I believe this error stopped happening some time around 1.6 or so. I remember reading through the changes and noticing that the code that caused this reflection issue was fixed. I honestly thought that it was handled internally and this issue was closed!

Reag avatar Jun 02 '24 05:06 Reag