SerializableDictionary.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.Collections.Generic;
  6. using System.Runtime.CompilerServices;
  7. namespace BansheeEngine
  8. {
  9. #pragma warning disable 649
  10. /// <summary>
  11. /// Allows you to access meta-data about a managed dictionary and its children. Similar to Reflection but simpler and
  12. /// faster.
  13. /// </summary>
  14. public sealed class SerializableDictionary : ScriptObject
  15. {
  16. private SerializableProperty.FieldType keyPropertyType;
  17. private SerializableProperty.FieldType valuePropertyType;
  18. private Type keyType;
  19. private Type valueType;
  20. private SerializableProperty parentProperty;
  21. /// <summary>
  22. /// Type of serializable property used for the keys stored in the dictionary.
  23. /// </summary>
  24. public SerializableProperty.FieldType KeyPropertyType
  25. {
  26. get { return keyPropertyType; }
  27. }
  28. /// <summary>
  29. /// Type of serializable property used for the values stored in the dictionary.
  30. /// </summary>
  31. public SerializableProperty.FieldType ValuePropertyType
  32. {
  33. get { return valuePropertyType; }
  34. }
  35. /// <summary>
  36. /// Type used for of keys stored in the dictionary.
  37. /// </summary>
  38. public Type KeyType
  39. {
  40. get { return keyType; }
  41. }
  42. /// <summary>
  43. /// Type used for values stored in the dictionary.
  44. /// </summary>
  45. public Type ValueType
  46. {
  47. get { return valueType; }
  48. }
  49. /// <summary>
  50. /// Constructor for use by the runtime only.
  51. /// </summary>
  52. /// <param name="keyType">C# type of the keys in the dictionary.</param>
  53. /// <param name="valueType">C# type of the values in the dictionary.</param>
  54. /// <param name="parentProperty">Property used for retrieving this entry.</param>
  55. private SerializableDictionary(Type keyType, Type valueType, SerializableProperty parentProperty)
  56. {
  57. this.parentProperty = parentProperty;
  58. this.keyType = keyType;
  59. this.valueType = valueType;
  60. keyPropertyType = SerializableProperty.DetermineFieldType(keyType);
  61. valuePropertyType = SerializableProperty.DetermineFieldType(valueType);
  62. }
  63. /// <summary>
  64. /// Returns a serializable property for the specified entry.
  65. /// </summary>
  66. /// <param name="key">Dictionary key for the value to retrieve.</param>
  67. /// <returns>Serializable property that allows you to manipulate contents of the dictionary entry.</returns>
  68. public KeyValuePair<SerializableProperty, SerializableProperty> GetProperty(object key)
  69. {
  70. IDictionary dictionary = parentProperty.GetValue<IDictionary>();
  71. if (dictionary == null || !dictionary.Contains(key))
  72. return new KeyValuePair<SerializableProperty, SerializableProperty>(null, null);
  73. SerializableProperty keyProperty;
  74. {
  75. SerializableProperty.Getter getter = () => key;
  76. SerializableProperty.Setter setter = (object value) => {};
  77. keyProperty = Internal_CreateKeyProperty(mCachedPtr);
  78. keyProperty.Construct(KeyPropertyType, keyType, getter, setter);
  79. }
  80. SerializableProperty valueProperty;
  81. {
  82. SerializableProperty.Getter getter = () =>
  83. {
  84. IDictionary dict = parentProperty.GetValue<IDictionary>();
  85. if (dict != null)
  86. return dict[key];
  87. else
  88. return null;
  89. };
  90. SerializableProperty.Setter setter = (object value) =>
  91. {
  92. IDictionary dict = parentProperty.GetValue<IDictionary>();
  93. if (dict != null)
  94. dict[key] = value;
  95. };
  96. valueProperty = Internal_CreateValueProperty(mCachedPtr);
  97. valueProperty.Construct(ValuePropertyType, valueType, getter, setter);
  98. }
  99. return new KeyValuePair<SerializableProperty, SerializableProperty>(keyProperty, valueProperty);
  100. }
  101. /// <summary>
  102. /// Returns the total number of elements in the dictionary.
  103. /// </summary>
  104. /// <returns>Total number of elements in the dictionary.</returns>
  105. public int GetLength()
  106. {
  107. IDictionary dictionary = parentProperty.GetValue<IDictionary>();
  108. if (dictionary != null)
  109. return dictionary.Count;
  110. else
  111. return 0;
  112. }
  113. [MethodImpl(MethodImplOptions.InternalCall)]
  114. private static extern SerializableProperty Internal_CreateKeyProperty(IntPtr nativeInstance);
  115. [MethodImpl(MethodImplOptions.InternalCall)]
  116. private static extern SerializableProperty Internal_CreateValueProperty(IntPtr nativeInstance);
  117. }
  118. }