ZstdSharp icon indicating copy to clipboard operation
ZstdSharp copied to clipboard

Unity WebGL build gives an IL2CPP error

Open Fleurman opened this issue 1 year ago • 10 comments

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 !

Fleurman avatar May 17 '23 20:05 Fleurman

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.

TechPizzaDev avatar May 17 '23 21:05 TechPizzaDev

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 avatar May 18 '23 17:05 oleg-st

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

oleg-st avatar May 18 '23 19:05 oleg-st

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.

Fleurman avatar May 18 '23 20:05 Fleurman

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

hoainam1593 avatar Nov 22 '23 03:11 hoainam1593

@hoainam1593 What version of the Unity are you using?

oleg-st avatar Nov 22 '23 08:11 oleg-st

it happen on both unity 2021.3.22 and 2022.3.13

hoainam1593 avatar Nov 22 '23 11:11 hoainam1593

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 avatar Nov 22 '23 16:11 oleg-st

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

oleg-st avatar Nov 23 '23 07:11 oleg-st

it works now on v0.7.4, many thanks

hoainam1593 avatar Nov 23 '23 08:11 hoainam1593