royalapplications / beyondnet

A toolset that makes it possible to call .NET code from other programming languages, including C and Swift.
https://royalapps.com
MIT License
108 stars 4 forks source link

Fix locale (icudt.dat) related stuff when targeting iOS #53

Closed lemonmojo closed 1 year ago

lemonmojo commented 1 year ago

Some unit tests succeed when targeting macOS but fail when targeting the iOS simulator. They are marked with // TODO: Test fails on iOS.

lemonmojo commented 1 year ago

Just verified on an actual iPhone; fails as well.

lemonmojo commented 1 year ago

Here's a list of all the APIs I've found to SIGSEGV up until now:

System.Type.GetType
System.String.IndexOf
System.DateTime.Now
System.Globalization.CultureInfo ctor
System.Version ctor
System.Version.TryParse
System.Convert.ToInt32
System.Reflection.Assembly.GetName
System.Decimal.TryParse
lemonmojo commented 1 year ago

Setting InvariantGlobalization in the csproj to true works around the crashes but makes tests using a specific culture fail.

lemonmojo commented 1 year ago

Can also be fixed by having <RuntimeHostConfigurationOption Include="ICU_DAT_FILE_PATH" Value="icudt.dat" /> in the csproj and including a copy of icudt.dat in the bundle. The file can be found in the NuGet cache (ie. ~/.nuget/packages/microsoft.netcore.app.runtime.nativeaot.ios-arm64/8.0.0-preview.6.23329.7/runtimes/ios-arm64/native/icudt.dat)

lemonmojo commented 1 year ago

Unfortunately that doesn't work in a XCTest bundle because the runtime locates the file by using -[NSBundle mainBundle]. See https://github.com/dotnet/runtime/issues/88835 and https://github.com/xamarin/xamarin-macios/issues/18471#issuecomment-1634287195

lemonmojo commented 1 year ago

Might be able to work around this by calling AppContext.SetData("ICU_DAT_FILE_PATH", "<path>") early on in startup.

lemonmojo commented 1 year ago

Using System.AppContext to set the icudt.dat file path at runtime does indeed work. See https://github.com/royalapplications/beyondnet/commit/f2d4f2b6d51641ac43412eb70bae9c19d4b11ab8.