NeighTools / UnityDoorstop

Doorstop -- run C# before Unity does!
GNU Lesser General Public License v2.1
419 stars 62 forks source link

Use string_chars instead of string_to_utf8 in UNICODE mode #30

Closed xelrach closed 1 month ago

xelrach commented 1 year ago

The original code was taking a MonoString and then calling string_to_utf8 on it. That result was passed to LOG, which called narrow on the result, which was mangling our string.

Before: Error invoking code! Error message: 祓瑳浥吮灹䱥慯䕤捸灥楴湯›潃汵⁤潮⁴潬摡琠灹⁥景映敩摬✠協獅楰湯条⹥獅楰湯条䍥湯楦㩧楨敤汐祡牥 ...

After: Error invoking code! Error message: System.TypeLoadException: Could not load type of field 'TSEspionage.EspionageConfig:hidePlayers' (1) due to: Could not load file or assembly 'System.Collections, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. assembly:System.Collections, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a type: member:(null) signature: at Doorstop.Entrypoint.Start () [0x00001] in ...

ghorsington commented 1 year ago

Greetings!

Thank you for your PR and sorry for the wait! I see this being an issue on Windows where strings might be internally Unicode depending on mono version. However, I am unsure how well that applies to all mono versions on different OSs.

How about instead of using mono_string_chars and having a separate preprocessor check one would simply do

char *exc_str_n = mono.string_to_utf8(str);
char_t *exc_str = widen(exc_str_n);
LOG("Error message: %s", exc_str);

widen is a helper function in Doorstop to convert char/wchar_t to the general char_t which LOG supports:

https://github.com/NeighTools/UnityDoorstop/blob/master/src/util/util.h#L36-L44

widen already has necessary preprocessor checks for different OSs (it's either a conversion or no-op depending on OS).

ManlyMarco commented 1 month ago

This issue was resolved by merging #58