SerializableArray.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. namespace BansheeEngine
  6. {
  7. /** @addtogroup Serialization
  8. * @{
  9. */
  10. #pragma warning disable 649
  11. /// <summary>
  12. /// Allows you to access meta-data about a managed array and its children. Similar to Reflection but simpler and faster.
  13. /// </summary>
  14. public sealed class SerializableArray : ScriptObject
  15. {
  16. private SerializableProperty.FieldType elementPropertyType;
  17. private Type elementType;
  18. private SerializableProperty parentProperty;
  19. /// <summary>
  20. /// Type of serializable property used for the elements stored in the array.
  21. /// </summary>
  22. public SerializableProperty.FieldType ElementPropertyType
  23. {
  24. get { return elementPropertyType; }
  25. }
  26. /// <summary>
  27. /// Type used for the elements stored in the array.
  28. /// </summary>
  29. public Type ElementType
  30. {
  31. get { return elementType; }
  32. }
  33. /// <summary>
  34. /// Constructor for use by the runtime only.
  35. /// </summary>
  36. /// <param name="elementType">C# type of the elements in the array.</param>
  37. /// <param name="parentProperty">Property used for retrieving this entry.</param>
  38. private SerializableArray(Type elementType, SerializableProperty parentProperty)
  39. {
  40. this.parentProperty = parentProperty;
  41. this.elementType = elementType;
  42. elementPropertyType = SerializableProperty.DetermineFieldType(elementType);
  43. }
  44. /// <summary>
  45. /// Returns a serializable property for a specific array element.
  46. /// </summary>
  47. /// <param name="elementIdx">Index of the element in the array.</param>
  48. /// <returns>Serializable property that allows you to manipulate contents of the array entry.</returns>
  49. public SerializableProperty GetProperty(int elementIdx)
  50. {
  51. SerializableProperty.Getter getter = () =>
  52. {
  53. Array array = parentProperty.GetValue<Array>();
  54. if (array != null)
  55. return array.GetValue(elementIdx);
  56. else
  57. return null;
  58. };
  59. SerializableProperty.Setter setter = (object value) =>
  60. {
  61. Array array = parentProperty.GetValue<Array>();
  62. if(array != null)
  63. array.SetValue(value, elementIdx);
  64. };
  65. SerializableProperty property = Internal_CreateProperty(mCachedPtr);
  66. property.Construct(ElementPropertyType, elementType, getter, setter);
  67. return property;
  68. }
  69. /// <summary>
  70. /// Returns number of elements in the array.
  71. /// </summary>
  72. /// <returns>Number of elements in the array.</returns>
  73. public int GetLength()
  74. {
  75. Array array = parentProperty.GetValue<Array>();
  76. if (array != null)
  77. return array.GetLength(0); // TODO - Support multi-rank arrays
  78. else
  79. return 0;
  80. }
  81. /// <summary>
  82. /// Uses the provided path elements to find an array element at the specified index, and returns a property
  83. /// to the element, or to a child property of that element.
  84. /// </summary>
  85. /// <param name="pathElements">Path elements representing field names and keys to look for.</param>
  86. /// <param name="elementIdx">Index in the <paramref name="pathElements"/> array to start the search at.</param>
  87. /// <returns>Property representing the final path element, or null if not found (array index is out of range, or
  88. /// property with that path doesn't exist).</returns>
  89. internal SerializableProperty FindProperty(PropertyPathElement[] pathElements, int elementIdx)
  90. {
  91. int arrayIdx;
  92. if (string.IsNullOrEmpty(pathElements[elementIdx].key))
  93. arrayIdx = 0;
  94. else
  95. {
  96. if (!int.TryParse(pathElements[elementIdx].key, out arrayIdx))
  97. return null;
  98. else
  99. {
  100. if (arrayIdx < 0 || arrayIdx >= GetLength())
  101. return null;
  102. }
  103. }
  104. SerializableProperty property = GetProperty(arrayIdx);
  105. if (elementIdx == (pathElements.Length - 1))
  106. return property;
  107. return property.FindProperty(pathElements, elementIdx + 1);
  108. }
  109. [MethodImpl(MethodImplOptions.InternalCall)]
  110. private static extern SerializableProperty Internal_CreateProperty(IntPtr nativeInstance);
  111. }
  112. /** @} */
  113. }