Fixed serialization of null-strings

This commit is contained in:
Simon Lübeß
2023-12-25 18:58:06 +01:00
parent 20814a61b6
commit 7bd69c162f
2 changed files with 21 additions and 9 deletions
@@ -105,17 +105,22 @@ class SerializedObject
if (primitiveType == .String)
{
MonoString* string = (.)value;
char8* rawStringValue = Mono.mono_string_to_utf8(string);
// If the string is null, we store a nullptr and 0-length
StringView valueView = StringView(null, 0);
String stringValue = new String(rawStringValue);
if (value != null)
{
MonoString* string = (.)value;
char8* rawStringValue = Mono.mono_string_to_utf8(string);
_ownedString.Add(stringValue);
String stringValue = new String(rawStringValue);
Mono.mono_free(rawStringValue);
StringView valueView = stringValue;
_ownedString.Add(stringValue);
Mono.mono_free(rawStringValue);
valueView = stringValue;
}
Internal.MemCpy(&data, &valueView, sizeof(StringView));
}
+8 -1
View File
@@ -128,7 +128,7 @@ public static class EntitySerializer
}
else if (fieldType == typeof(string))
{
AddField(fieldName, SerializationType.String, fieldValue.ToString());
AddField(fieldName, SerializationType.String, fieldValue);
}
else if (fieldType.IsEnum)
{
@@ -317,8 +317,15 @@ public static class EntitySerializer
{
// rawData contains a Pointer and a string length!
byte* utf8Ptr = *(byte**)rawData;
if (utf8Ptr == null)
return null;
ulong length = *(ulong*)(rawData + 8);
if (length == 0)
return string.Empty;
return Encoding.UTF8.GetString(utf8Ptr, (int)length);
}