SerializableList.cs 4.8 KB

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