Browse Source

Fixed wrong string byte serialization

Krzysztof Krysiński 3 tháng trước cách đây
mục cha
commit
838140567f

+ 5 - 1
src/PixiEditor.Extensions.Sdk/Api/FlyUI/ControlDefinition.cs

@@ -122,7 +122,11 @@ public class ControlDefinition
             result.Add(ByteMap.GetTypeByteId(property.type));
             if (property.type == typeof(string))
             {
-                result.AddRange(BitConverter.GetBytes(property.value is string s ? s.Length : 0));
+                if (property.value is string str)
+                {
+                    int stringLengthBytes = Encoding.UTF8.GetByteCount(str);
+                    result.AddRange(BitConverter.GetBytes(stringLengthBytes));
+                }
             }
 
             result.AddRange(property.value switch

+ 3 - 3
src/PixiEditor.Extensions/FlyUI/Elements/LayoutBuilder.cs

@@ -61,10 +61,10 @@ public class LayoutBuilder
             Type type = ByteMap.GetTypeFromByteId((byte)propertyType);
             if (type == typeof(string))
             {
-                int stringLength = BitConverter.ToInt32(layoutSpan[offset..(offset + int32Size)]);
+                int stringBytesLength = BitConverter.ToInt32(layoutSpan[offset..(offset + int32Size)]);
                 offset += int32Size;
-                string value = Encoding.UTF8.GetString(layoutSpan[offset..(offset + stringLength)]);
-                offset += stringLength;
+                string value = Encoding.UTF8.GetString(layoutSpan[offset..(offset + stringBytesLength)]);
+                offset += stringBytesLength;
                 properties.Add(value);
             }
             else if (type == typeof(byte[]))

+ 4 - 6
src/PixiEditor/Models/Serialization/Factories/ByteBuilder.cs

@@ -1,4 +1,5 @@
-using Drawie.Backend.Core.ColorsImpl;
+using System.Text;
+using Drawie.Backend.Core.ColorsImpl;
 using Drawie.Backend.Core.Numerics;
 using Drawie.Numerics;
 
@@ -69,11 +70,8 @@ public class ByteBuilder
 
     public void AddString(string str)
     {
-        AddInt(str.Length);
-        foreach (var c in str)
-        {
-            AddInt(c);
-        }
+        AddInt(Encoding.UTF8.GetByteCount(str));
+        _data.AddRange(Encoding.UTF8.GetBytes(str));
     }
 
     public void AddFloat(float value)