oleg-st / ZstdSharp

Port of zstd compression library to c#
MIT License
200 stars 29 forks source link

Unity WebGL build gives an IL2CPP error #20

Closed Fleurman closed 2 months ago

Fleurman commented 1 year ago

Hi,

I'm on an Unity project that uses MongoDB. I have all the MongoDB assemblies and dependencies wich includes ZstdSharp.

When building for WebGL, I get this error:

Error: IL2CPP error for method 'System.Void ZstdSharp.Unsafe.Methods::.cctor()' in assembly 'C:\Users\Fleurman\Scriipts\Library\Bee\artifacts\WebGL\ManagedStripped\ZstdSharp.dll'

System.ArgumentOutOfRangeException: Cannot create a constant value for types of System.UIntPtr for System.UIntPtr ZstdSharp.Unsafe.Methods::ZSTD_blockHeaderSize (Parameter 'declaredParameterOrFieldType')

I asked for help on the Unity forum and got this explanation: "Unity uses the IL2CPP scripting backend to convert .NET assemblies to C++ to support AOT (Ahead Of Time) compilation which is a requirement on platforms like WebGL. The System.UIntPtr type in your error message is a pointer type and pointer arithmetic is not supported in IL2CPP which could be causing the error."

Also I'm told to "Check if there's a version of ZstdSharp that's compatible with WebGL."

I know it is more likely to be an issue with Unity and their IL2CPP, but do you think there is a way I could make it work ?

Thanks a lot !

TechPizzaDev commented 1 year ago

While IL2CPP does support the System.UIntPtr and pointer arithmetic, constant nint/nuint are a recent addition to C# and IL2CPP clearly does not handle them well. https://github.com/oleg-st/ZstdSharp/blob/314fbc4c7e87c4eec503090c23d93329d25e4b59/src/ZstdSharp/Unsafe/ZstdInternal.cs#L17 Potential fix would be changing this const to a static readonly, or reporting a IL2CPP bug and waiting.

oleg-st commented 1 year ago

Hi,

I am not familiar with the Unity. How I can reproduce this issue?

Is it possible to run IL2CPP itself with ZstSharp?

oleg-st commented 1 year ago

@Fleurman I think it's fixed in 0.7.2 Could you check it?

Fleurman commented 1 year ago

It is fixed ! You are formidable thank you very much !!

PS: to answer to your first question, I don't think you can run IL2CPP by istelf.

hoainam1593 commented 8 months ago

Hi, it's fixed in version 0.7.2, but reopen in version 0.7.3, please check

oleg-st commented 8 months ago

@hoainam1593 What version of the Unity are you using?

hoainam1593 commented 8 months ago

it happen on both unity 2021.3.22 and 2022.3.13

oleg-st commented 8 months ago

The compilation error for 0.7.3 can be fixed, but now I'm getting a runtime error for 0.7.2 with Unity 2022.3.13f1 for code that previously worked

oleg-st commented 8 months ago

@hoainam1593 Released 0.7.4 with compilation fix for IL2CPP, runtime error still needs to be looked into

hoainam1593 commented 8 months ago

it works now on v0.7.4, many thanks