SerializableDictionary.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. public sealed class SerializableDictionary : ScriptObject
  9. {
  10. private SerializableProperty.FieldType keyType;
  11. private SerializableProperty.FieldType valueType;
  12. private Type internalKeyType;
  13. private Type internalValueType;
  14. private SerializableProperty parentProperty;
  15. public SerializableProperty.FieldType KeyType
  16. {
  17. get { return keyType; }
  18. }
  19. public SerializableProperty.FieldType ValueType
  20. {
  21. get { return valueType; }
  22. }
  23. // Constructed from native code
  24. private SerializableDictionary(Type internalKeyType, Type internalValueType, SerializableProperty parentProperty)
  25. {
  26. this.parentProperty = parentProperty;
  27. this.internalKeyType = internalKeyType;
  28. this.internalValueType = internalValueType;
  29. keyType = SerializableProperty.DetermineFieldType(internalKeyType);
  30. valueType = SerializableProperty.DetermineFieldType(internalValueType);
  31. }
  32. public KeyValuePair<SerializableProperty, SerializableProperty> GetProperty(object key)
  33. {
  34. IDictionary dictionary = parentProperty.GetValue<IDictionary>();
  35. if (dictionary == null || !dictionary.Contains(key))
  36. return new KeyValuePair<SerializableProperty, SerializableProperty>(null, null);
  37. SerializableProperty keyProperty;
  38. {
  39. SerializableProperty.Getter getter = () => key;
  40. SerializableProperty.Setter setter = (object value) => {};
  41. keyProperty = Internal_CreateKeyProperty(mCachedPtr);
  42. keyProperty.Construct(KeyType, internalKeyType, getter, setter);
  43. }
  44. SerializableProperty valueProperty;
  45. {
  46. SerializableProperty.Getter getter = () =>
  47. {
  48. IDictionary dict = parentProperty.GetValue<IDictionary>();
  49. if (dict != null)
  50. return dict[key];
  51. else
  52. return null;
  53. };
  54. SerializableProperty.Setter setter = (object value) =>
  55. {
  56. IDictionary dict = parentProperty.GetValue<IDictionary>();
  57. if (dict != null)
  58. dict[key] = value;
  59. };
  60. valueProperty = Internal_CreateValueProperty(mCachedPtr);
  61. valueProperty.Construct(ValueType, internalValueType, getter, setter);
  62. }
  63. return new KeyValuePair<SerializableProperty, SerializableProperty>(keyProperty, valueProperty);
  64. }
  65. public int GetLength()
  66. {
  67. IDictionary dictionary = parentProperty.GetValue<IDictionary>();
  68. if (dictionary != null)
  69. return dictionary.Count;
  70. else
  71. return 0;
  72. }
  73. [MethodImpl(MethodImplOptions.InternalCall)]
  74. private static extern SerializableProperty Internal_CreateKeyProperty(IntPtr nativeInstance);
  75. [MethodImpl(MethodImplOptions.InternalCall)]
  76. private static extern SerializableProperty Internal_CreateValueProperty(IntPtr nativeInstance);
  77. }
  78. }