MixedRealityToolkit-Unity icon indicating copy to clipboard operation
MixedRealityToolkit-Unity copied to clipboard

glTF Support Using System.IO.File APIs Causes File Read/Write Problems?

Open mtaulty opened this issue 7 years ago • 5 comments

Describe the bug

The new glTF support in the toolkit is very welcome but it looks to rely on using System.IO.File APIs in order to read/write files. For example, there are calls to File.ReadAllText and File.ReadAllBytes in the code (GltfUtility.cs, ConstructGltf.cs).

When I try and use this code on HoloLens 1 running RS5 I find that the calls do not work - files which have been selected by the file picker and which the UWP app has access to are not opened and read correctly by these APIs.

As far as I understand it, this became a problem in the underlying System.IO UWP implementation as it moved from RS2 to RS3 and the File.* APIs were re-implemented on top of Win32 APIs rather than on the WinRT APIs they previously were implemented with.

This is described in this Stack Overflow thread with Peter from the MS team providing the information about the underlying changes and the problems that they can cause.

To reproduce

Make a toolkit app, give it permissions to access the 3D Objects folder on the device. Place a multi-file glTF object into a sub-folder of that folder and then use GltfUtility.ImportGltfObjectFromPathAsync() to load up that GLTF file at runtime.

I find that I get exceptions from the File.ReadAllText and File.ReadAllBytes methods that the code uses whereas if I replace them with StorageFile based calls then I can read the files fine.

Expected behavior

As above - I think there are some problems with using File.* APIs in a UWP app above RS3.

Screenshots

N/A

Your Setup (please complete the following information)

  • Unity Version 2018.3.14f1
  • MRTK Version V2.0 RC1
  • UWP Version 18362

Target Platform (please complete the following information)

  • HoloLens V1

Additional context

I don't know if this is a problem which might be improved on Windows 19H1 as I do not think that is yet available for HoloLens 1 at the time of writing.

mtaulty avatar May 31 '19 17:05 mtaulty

This is resolved on Unity's side if update to 2019.1.8f1/2018.4.3f1 or later and using IL2CPP scripting backend. We did the work to make System.IO APIs use StorageFile APIs if regular win32 file access fails.

TautvydasZilys avatar Jun 17 '19 05:06 TautvydasZilys

Agree - I moved the code in question to 2018.4.3f1 and took out my modifications of the toolkit (I made it use Windows.Storage APIs) and the System.IO.File.* APIs used in the GLTF support now work for me. That's when I build with IL2CPP, I'm unsure whether the Unity fix will also work for .NET but I don't build this code on .NET for other reasons (needing .NET Standard 2.0 supprt).

mtaulty avatar Jul 03 '19 13:07 mtaulty

The fix doesn't apply to .NET scripting backend. That's the beauty of IL2CPP: we can fix internals ourselves. We cannot do the same with .NET scripting backend since the .NET part lies on Microsoft's side. It was one of the biggest pain points we had with .NET.

TautvydasZilys avatar Jul 08 '19 21:07 TautvydasZilys

Yes, understood that you're fixing this in the internals of IL2CPP here and I can see the positive sides of this but I can also see a downside in the possible divergence between implementations. For example, if I now have some code using System.IO.File APIs that I share between say a UWP app on the desktop and a UWP app built with Unity and IL2CPP then I will perhaps see different behaviour.

Naturally, I'm not saying that this is a problem for Unity to address and it's not really a problem with MRTK for developers who just want their File APIs to work with Unity/IL2CPP - it's really more a problem that I think Microsoft created when changing this implementation between the pre-16299 SDKs and the post-16299 SDKs.

mtaulty avatar Jul 08 '19 22:07 mtaulty

Closing as resolution is to select IL2CPP and move to 2018.4.3+ and not really a MRTK issue

Troy-Ferrell avatar Jul 16 '19 17:07 Troy-Ferrell