SerializableDictionary.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. /** @addtogroup Serialization
  10. * @{
  11. */
  12. #pragma warning disable 649
  13. /// <summary>
  14. /// Allows you to access meta-data about a managed dictionary and its children. Similar to Reflection but simpler and
  15. /// faster.
  16. /// </summary>
  17. public sealed class SerializableDictionary : ScriptObject
  18. {
  19. private SerializableProperty.FieldType keyPropertyType;
  20. private SerializableProperty.FieldType valuePropertyType;
  21. private Type keyType;
  22. private Type valueType;
  23. private SerializableProperty parentProperty;
  24. /// <summary>
  25. /// Type of serializable property used for the keys stored in the dictionary.
  26. /// </summary>
  27. public SerializableProperty.FieldType KeyPropertyType
  28. {
  29. get { return keyPropertyType; }
  30. }
  31. /// <summary>
  32. /// Type of serializable property used for the values stored in the dictionary.
  33. /// </summary>
  34. public SerializableProperty.FieldType ValuePropertyType
  35. {
  36. get { return valuePropertyType; }
  37. }
  38. /// <summary>
  39. /// Type used for of keys stored in the dictionary.
  40. /// </summary>
  41. public Type KeyType
  42. {
  43. get { return keyType; }
  44. }
  45. /// <summary>
  46. /// Type used for values stored in the dictionary.
  47. /// </summary>
  48. public Type ValueType
  49. {
  50. get { return valueType; }
  51. }
  52. /// <summary>
  53. /// Constructor for use by the runtime only.
  54. /// </summary>
  55. /// <param name="keyType">C# type of the keys in the dictionary.</param>
  56. /// <param name="valueType">C# type of the values in the dictionary.</param>
  57. /// <param name="parentProperty">Property used for retrieving this entry.</param>
  58. private SerializableDictionary(Type keyType, Type valueType, SerializableProperty parentProperty)
  59. {
  60. this.parentProperty = parentProperty;
  61. this.keyType = keyType;
  62. this.valueType = valueType;
  63. keyPropertyType = SerializableProperty.DetermineFieldType(keyType);
  64. valuePropertyType = SerializableProperty.DetermineFieldType(valueType);
  65. }
  66. /// <summary>
  67. /// Returns a serializable property for the specified entry.
  68. /// </summary>
  69. /// <param name="key">Dictionary key for the value to retrieve.</param>
  70. /// <returns>Serializable property that allows you to manipulate contents of the dictionary entry.</returns>
  71. public KeyValuePair<SerializableProperty, SerializableProperty> GetProperty(object key)
  72. {
  73. IDictionary dictionary = parentProperty.GetValue<IDictionary>();
  74. if (dictionary == null || !dictionary.Contains(key))
  75. return new KeyValuePair<SerializableProperty, SerializableProperty>(null, null);
  76. SerializableProperty keyProperty;
  77. {
  78. SerializableProperty.Getter getter = () => key;
  79. SerializableProperty.Setter setter = (object value) => {};
  80. keyProperty = Internal_CreateKeyProperty(mCachedPtr);
  81. keyProperty.Construct(KeyPropertyType, keyType, getter, setter);
  82. }
  83. SerializableProperty valueProperty;
  84. {
  85. SerializableProperty.Getter getter = () =>
  86. {
  87. IDictionary dict = parentProperty.GetValue<IDictionary>();
  88. if (dict != null)
  89. return dict[key];
  90. else
  91. return null;
  92. };
  93. SerializableProperty.Setter setter = (object value) =>
  94. {
  95. IDictionary dict = parentProperty.GetValue<IDictionary>();
  96. if (dict != null)
  97. dict[key] = value;
  98. };
  99. valueProperty = Internal_CreateValueProperty(mCachedPtr);
  100. valueProperty.Construct(ValuePropertyType, valueType, getter, setter);
  101. }
  102. return new KeyValuePair<SerializableProperty, SerializableProperty>(keyProperty, valueProperty);
  103. }
  104. /// <summary>
  105. /// Returns the total number of elements in the dictionary.
  106. /// </summary>
  107. /// <returns>Total number of elements in the dictionary.</returns>
  108. public int GetLength()
  109. {
  110. IDictionary dictionary = parentProperty.GetValue<IDictionary>();
  111. if (dictionary != null)
  112. return dictionary.Count;
  113. else
  114. return 0;
  115. }
  116. /// <summary>
  117. /// Uses the provided path elements to find an dictionary element with the specified key, and returns a property
  118. /// to the element, or to a child property of that element.
  119. /// </summary>
  120. /// <param name="pathElements">Path elements representing field names and keys to look for.</param>
  121. /// <param name="elementIdx">Index in the <paramref name="pathElements"/> array to start the search at.</param>
  122. /// <returns>Property representing the final path element, or null if not found (key or property with that path
  123. /// doesn't exist).</returns>
  124. internal SerializableProperty FindProperty(PropertyPathElement[] pathElements, int elementIdx)
  125. {
  126. if (pathElements[elementIdx].key == null)
  127. return null;
  128. object key = null;
  129. switch (KeyPropertyType)
  130. {
  131. case SerializableProperty.FieldType.String:
  132. key = pathElements[elementIdx].key;
  133. break;
  134. case SerializableProperty.FieldType.Bool:
  135. bool boolKey;
  136. if (bool.TryParse(pathElements[elementIdx].key, out boolKey))
  137. key = boolKey;
  138. break;
  139. case SerializableProperty.FieldType.Int:
  140. int intKey;
  141. if (int.TryParse(pathElements[elementIdx].key, out intKey))
  142. key = intKey;
  143. break;
  144. case SerializableProperty.FieldType.Float:
  145. float floatKey;
  146. if (float.TryParse(pathElements[elementIdx].key, out floatKey))
  147. key = floatKey;
  148. break;
  149. }
  150. if (key == null)
  151. return null;
  152. SerializableProperty property = GetProperty(key).Value;
  153. if (elementIdx == (pathElements.Length - 1))
  154. return property;
  155. if(property != null)
  156. return property.FindProperty(pathElements, elementIdx + 1);
  157. return null;
  158. }
  159. [MethodImpl(MethodImplOptions.InternalCall)]
  160. private static extern SerializableProperty Internal_CreateKeyProperty(IntPtr nativeInstance);
  161. [MethodImpl(MethodImplOptions.InternalCall)]
  162. private static extern SerializableProperty Internal_CreateValueProperty(IntPtr nativeInstance);
  163. }
  164. /** @} */
  165. }