SerializableDictionary.cs 4.5 KB

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