SerializableArray.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. [MethodImpl(MethodImplOptions.InternalCall)]
  82. private static extern SerializableProperty Internal_CreateProperty(IntPtr nativeInstance);
  83. }
  84. /** @} */
  85. }