CompiledControl.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System.Reflection;
  2. using System.Runtime.InteropServices;
  3. using System.Text;
  4. using PixiEditor.Extensions.CommonApi.FlyUI;
  5. using PixiEditor.Extensions.CommonApi.FlyUI.Properties;
  6. namespace PixiEditor.Extensions.Wasm.Api.FlyUI;
  7. public class CompiledControl
  8. {
  9. public string ControlTypeId { get; set; }
  10. public List<(object value, Type type)> Properties { get; set; } = new();
  11. public List<CompiledControl> Children { get; set; } = new();
  12. public int UniqueId { get; set; }
  13. internal List<string> QueuedEvents => _buildQueuedEvents;
  14. private List<string> _buildQueuedEvents = new List<string>();
  15. public CompiledControl(int uniqueId, string controlTypeId)
  16. {
  17. ControlTypeId = controlTypeId;
  18. UniqueId = uniqueId;
  19. }
  20. public void AddProperty<T>(T value)
  21. {
  22. InternalAddProperty(value);
  23. }
  24. private void InternalAddProperty(object value)
  25. {
  26. if (value is string s)
  27. {
  28. AddStringProperty(s);
  29. }
  30. else if (value is Enum enumProp)
  31. {
  32. var enumValue = Convert.ChangeType(value, enumProp.GetTypeCode());
  33. Properties.Add((enumValue, enumValue.GetType()));
  34. }
  35. else if (value is IStructProperty structProperty)
  36. {
  37. Properties.Add((value, typeof(byte[])));
  38. }
  39. else
  40. {
  41. Properties.Add((value, value.GetType()));
  42. }
  43. }
  44. private void AddStringProperty(string value)
  45. {
  46. Properties.Add((value, typeof(string)));
  47. }
  48. public void AddChild(CompiledControl child)
  49. {
  50. Children.Add(child);
  51. }
  52. public Span<byte> Serialize()
  53. {
  54. return Serialize(new List<byte>()).ToArray();
  55. }
  56. // DO NOT REMOVE, used by reflection-based layout compiler, using Serialize with Span<byte> throws error.
  57. public byte[] SerializeBytes()
  58. {
  59. return Serialize(new List<byte>()).ToArray();
  60. }
  61. private List<byte> Serialize(List<byte> bytes)
  62. {
  63. // TODO: Make it more efficient
  64. byte[] uniqueIdBytes = BitConverter.GetBytes(UniqueId);
  65. bytes.AddRange(uniqueIdBytes);
  66. byte[] idLengthBytes = BitConverter.GetBytes(ControlTypeId.Length);
  67. bytes.AddRange(idLengthBytes);
  68. byte[] idBytes = Encoding.UTF8.GetBytes(ControlTypeId);
  69. bytes.AddRange(idBytes);
  70. bytes.AddRange(BitConverter.GetBytes(Properties.Count));
  71. bytes.AddRange(SerializeProperties());
  72. bytes.AddRange(BitConverter.GetBytes(Children.Count));
  73. SerializeChildren(bytes);
  74. return bytes;
  75. }
  76. private void SerializeChildren(List<byte> bytes)
  77. {
  78. foreach (CompiledControl child in Children)
  79. {
  80. child.Serialize(bytes);
  81. }
  82. }
  83. private List<byte> SerializeProperties()
  84. {
  85. var result = new List<byte>();
  86. foreach (var property in Properties)
  87. {
  88. result.Add(ByteMap.GetTypeByteId(property.type));
  89. if (property.type == typeof(string))
  90. {
  91. result.AddRange(BitConverter.GetBytes(property.value is string s ? s.Length : 0));
  92. }
  93. result.AddRange(property.value switch
  94. {
  95. int i => BitConverter.GetBytes(i),
  96. float f => BitConverter.GetBytes(f),
  97. bool b => BitConverter.GetBytes(b),
  98. double d => BitConverter.GetBytes(d),
  99. long l => BitConverter.GetBytes(l),
  100. short s => BitConverter.GetBytes(s),
  101. byte b => new byte[] { b },
  102. char c => BitConverter.GetBytes(c),
  103. string s => Encoding.UTF8.GetBytes(s),
  104. IStructProperty structProperty => GetWellKnownStructBytes(structProperty),
  105. null => [],
  106. _ => throw new Exception($"Unknown unmanaged type: {property.value.GetType()}")
  107. });
  108. }
  109. return result;
  110. }
  111. private static List<byte> GetWellKnownStructBytes(IStructProperty structProperty)
  112. {
  113. List<byte> bytes = new List<byte>(BitConverter.GetBytes(structProperty.GetType().Name.Length));
  114. bytes.AddRange(Encoding.UTF8.GetBytes(structProperty.GetType().Name));
  115. byte[] structBytes = structProperty.Serialize();
  116. bytes.AddRange(BitConverter.GetBytes(structBytes.Length));
  117. bytes.AddRange(structBytes);
  118. return bytes;
  119. }
  120. internal void AddEvent(string eventName)
  121. {
  122. _buildQueuedEvents.Add(eventName);
  123. }
  124. }