2
0

SerializableArray.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #pragma warning disable 649
  8. /// <summary>
  9. /// Allows you to access meta-data about a managed array and its children. Similar to Reflection but simpler and faster.
  10. /// </summary>
  11. public sealed class SerializableArray : ScriptObject
  12. {
  13. private SerializableProperty.FieldType elementPropertyType;
  14. private Type elementType;
  15. private SerializableProperty parentProperty;
  16. /// <summary>
  17. /// Type of serializable property used for the elements stored in the array.
  18. /// </summary>
  19. public SerializableProperty.FieldType ElementPropertyType
  20. {
  21. get { return elementPropertyType; }
  22. }
  23. /// <summary>
  24. /// Type used for the elements stored in the array.
  25. /// </summary>
  26. public Type ElementType
  27. {
  28. get { return elementType; }
  29. }
  30. /// <summary>
  31. /// Constructor for use by the runtime only.
  32. /// </summary>
  33. /// <param name="elementType">C# type of the elements in the array.</param>
  34. /// <param name="parentProperty">Property used for retrieving this entry.</param>
  35. private SerializableArray(Type elementType, SerializableProperty parentProperty)
  36. {
  37. this.parentProperty = parentProperty;
  38. this.elementType = elementType;
  39. elementPropertyType = SerializableProperty.DetermineFieldType(elementType);
  40. }
  41. /// <summary>
  42. /// Returns a serializable property for a specific array element.
  43. /// </summary>
  44. /// <param name="elementIdx">Index of the element in the array.</param>
  45. /// <returns>Serializable property that allows you to manipulate contents of the array entry.</returns>
  46. public SerializableProperty GetProperty(int elementIdx)
  47. {
  48. SerializableProperty.Getter getter = () =>
  49. {
  50. Array array = parentProperty.GetValue<Array>();
  51. if (array != null)
  52. return array.GetValue(elementIdx);
  53. else
  54. return null;
  55. };
  56. SerializableProperty.Setter setter = (object value) =>
  57. {
  58. Array array = parentProperty.GetValue<Array>();
  59. if(array != null)
  60. array.SetValue(value, elementIdx);
  61. };
  62. SerializableProperty property = Internal_CreateProperty(mCachedPtr);
  63. property.Construct(ElementPropertyType, elementType, getter, setter);
  64. return property;
  65. }
  66. /// <summary>
  67. /// Returns number of elements in the array.
  68. /// </summary>
  69. /// <returns>Number of elements in the array.</returns>
  70. public int GetLength()
  71. {
  72. Array array = parentProperty.GetValue<Array>();
  73. if (array != null)
  74. return array.GetLength(0); // TODO - Support multi-rank arrays
  75. else
  76. return 0;
  77. }
  78. [MethodImpl(MethodImplOptions.InternalCall)]
  79. private static extern SerializableProperty Internal_CreateProperty(IntPtr nativeInstance);
  80. }
  81. }