sharpdx / SharpDX

SharpDX GitHub Repository
http://sharpdx.org
MIT License
1.68k stars 641 forks source link

SharpDX.MediaFoundation UWP in Unity causes TypeLoadException on WinRT Bridge #1082

Open belveder79 opened 5 years ago

belveder79 commented 5 years ago

I'm fighting a super nasty bug trying to integrate a library I wrote to interact with Direct3D11 and SharpDX into Unity 2017.4.11f1.

What I did is use the SharpDX, SharpDX.DXGI and SharpDX.Direct3D11 4.2.0 Nuget packages and the uap10.0 dlls in my Unity app and I build a Visual Studio Project of the App. This works out perfectly, no warnings or anything. I can build and run the app - all perfect.

Now I decided to do a little more with MediaFoundation, so I imported that SharpDX.MediaFoundation one. As a result, the build of the VS project is ok, but on every start of the app I get a TypeLoadException exactly when the WinRT bridge is initialized (Line 32 in App.cs). Tried everything - reimport/delete cache/all folders/Process/Don'tProcess the DLLs - no help. Even if I don't do anything with MediaFoundation, it's pure existence causes the app to crash at startup.

 $exception {System.TypeInitializationException: The type initializer for 
 'WinRTBridge.WinRTBridge' threw an exception. ---> System.TypeInitializationException: The type initializer for 'WinRTBridge.TypeInformation' threw an exception. ---> 
 System.TypeLoadException: Method with non-zero RVA in an Import.
 at UnityEngineInternal.BootstrapHelpers.FillTypeMaps0(Dictionary`2 , List`1 )
 at UnityEngineInternal.BootstrapHelpers.FillTypeMaps(Dictionary`2& typeToTypeIdMap, List`1& typeIdToTypeMap)
 at WinRTBridge.TypeInformation..cctor()
 --- End of inner exception stack trace ---
 at WinRTBridge.TypeInformation..ctor()
 at WinRTBridge.WinRTBridge..cctor()
 --- End of inner exception stack trace ---
 at WinRTBridge.WinRTBridge.get_ExceptionHandling()
 at UnityPlayer.AppCallbacks.SetBridge(IBridge bridge)
 at UNITY_TestScreenCapture.App.Initialize(CoreApplicationView applicationView)}    System.TypeInitializationException

I have absolutely no idea how to fix that one. There are no references to such a problem except that an Assembly cannot be loaded, however, wtf should that happen exactly for that assembly? I've created a simple example project to recreate the issue. Just build an UWP app and try compile/build it for x86 (update nuget Microsoft.NetCore.UniversalWindowsPlatform to 6.1.7 first!)

Github minimalistic example

Any help highly appreciated.

One more note. I was first working with SharpDX, SharpDX.DXGI and SharpDX.Direct3D11 only, exported from Unity to Visual Studio and then started to develop with MediaFoundation (install nuget package) on the VS project only (without going back to Unity and exporting it). The weird thing is, it worked perfectly until I moved back to Unity->VS export, then it was broken and I can't get it to work again.