Efferent-Health / fo-dicom.Codecs

Cross-platform Dicom native codecs for fo-dicom
Other
64 stars 22 forks source link

Unity engine compatibility lost #63

Closed NicolasAvatar closed 4 months ago

NicolasAvatar commented 5 months ago

Bug description Since the validation of processor architecture in addition to the operating system, we cannot use fo-dicom.Codecs in Unity because the test is not owrking in the Mono environment there. Possibly related to https://github.com/Efferent-Health/fo-dicom.Codecs/issues/62

To Reproduce Invoke FellowOakDicom.Imaging.NativeCodec.DicomJpegLsNativeCodec.Decode method with a Jpeg encoded DICOM in Unity, it will throw an Unsupported OS Platform exception.

Explanation In .NET everything works fine, but in Unity Platform.Current property returns Platform.Type.unsupported even if we're on Windows 64, so the Decode method throws immediately: image And the reason why Platform.Current is unsupported is because in Unity environment the ProcessorArchitecture value is ProcessorArchitecture.MSIL, instead of expected ProcessorArchitecture.Amd64, in the Platform.getCurrentType method feeding this property: image

Possible fix Find below the fix I'm applying to make this work in Unity and Windows environment:

private Type GetCurrentTypeFix()
{
#if UNITY_64 || UNITY_EDITOR
    // In Unity we are working and testing only on Windows.
    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
    {
        return Type.win_x64;
    }

    return Type.unsupported;
#endif
    var arch = typeof(string).Assembly.GetName().ProcessorArchitecture;
    ...

Environment:

jaime-olivares commented 4 months ago

Hi @NicolasAvatar,

Your suggestion points to generate a different output binary using compilation directives like UNITY_EDITOR or UNITY_64. That doesn't seem to be a universal approach but ad-hoc for your particular needs.

We can implement only a universal solution. That implies to detect Unity at runtime, by any means.

I haven't seen a straightforward approach, but maybe we can detect certain class at runtime, probably "UnityEngine.Application". Can you research on that?

jaime-olivares commented 4 months ago

Closing this one as we don't have more feedback. We cannot test Unity by our own neither it is an expected environment.

jaime-olivares commented 3 months ago

In case somebody wants to contribute, here is a Unity detection code: https://github.com/grpc/grpc/blob/2ebd3c3c02a986a377680209e2ad9c6f10c69032/src/csharp/Grpc.Core/Internal/PlatformApis.cs