ByteMap.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. namespace PixiEditor.Extensions.CommonApi.LayoutBuilding;
  2. public static class ByteMap
  3. {
  4. public static IReadOnlyDictionary<string, int> ControlMap => controlMap;
  5. private static Dictionary<string, int> controlMap = new Dictionary<string, int>()
  6. {
  7. // If you do add a new control, make sure to add it to GlobalControlFactory inside PixiEditor project
  8. { "Layout", 0 },
  9. { "Center", 1 },
  10. { "Text", 2 },
  11. { "Button", 3 },
  12. { "StatefulContainer", 4 }
  13. };
  14. public static byte GetTypeByteId(Type type)
  15. {
  16. if (type == typeof(int))
  17. {
  18. return 0;
  19. }
  20. if (type == typeof(float))
  21. {
  22. return 1;
  23. }
  24. if (type == typeof(bool))
  25. {
  26. return 2;
  27. }
  28. if (type == typeof(double))
  29. {
  30. return 3;
  31. }
  32. if (type == typeof(long))
  33. {
  34. return 4;
  35. }
  36. if (type == typeof(short))
  37. {
  38. return 5;
  39. }
  40. if (type == typeof(byte))
  41. {
  42. return 6;
  43. }
  44. if (type == typeof(char))
  45. {
  46. return 7;
  47. }
  48. if (type == typeof(string))
  49. {
  50. return 8;
  51. }
  52. throw new Exception($"Unknown unmanaged type: {type}");
  53. }
  54. public static Type GetTypeFromByteId(byte id)
  55. {
  56. return id switch
  57. {
  58. 0 => typeof(int),
  59. 1 => typeof(float),
  60. 2 => typeof(bool),
  61. 3 => typeof(double),
  62. 4 => typeof(long),
  63. 5 => typeof(short),
  64. 6 => typeof(byte),
  65. 7 => typeof(char),
  66. 8 => typeof(string),
  67. _ => throw new Exception($"Unknown unmanaged type id: {id}")
  68. };
  69. }
  70. }