SerializableProperty.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.CompilerServices;
  5. using System.Text;
  6. namespace BansheeEngine
  7. {
  8. public sealed class SerializableProperty : ScriptObject
  9. {
  10. public enum FieldType
  11. {
  12. Int,
  13. Float,
  14. Bool,
  15. String,
  16. Color,
  17. Vector2,
  18. Vector3,
  19. Vector4,
  20. GameObjectRef,
  21. ResourceRef,
  22. Object,
  23. Array,
  24. List,
  25. Dictionary
  26. }
  27. internal delegate object Getter();
  28. internal delegate void Setter(object value);
  29. private FieldType type;
  30. private Type internalType;
  31. private Getter getter;
  32. private Setter setter;
  33. // Constructed from native code
  34. private SerializableProperty()
  35. { }
  36. internal void Construct(FieldType type, Type internalType, Getter getter, Setter setter)
  37. {
  38. this.type = type;
  39. this.internalType = internalType;
  40. this.getter = getter;
  41. this.setter = setter;
  42. }
  43. public FieldType Type
  44. {
  45. get { return type; }
  46. }
  47. public T GetValue<T>()
  48. {
  49. if (!typeof(T).IsAssignableFrom(internalType))
  50. throw new Exception("Attempted to retrieve a serializable value using an invalid type. Provided type: " + typeof(T) + ". Needed type: " + internalType);
  51. return (T)getter();
  52. }
  53. public void SetValue<T>(T value)
  54. {
  55. if (!typeof(T).IsAssignableFrom(internalType))
  56. throw new Exception("Attempted to set a serializable value using an invalid type. Provided type: " + typeof(T) + ". Needed type: " + internalType);
  57. setter(value);
  58. }
  59. public SerializableObject GetObject()
  60. {
  61. if (type != FieldType.Object)
  62. throw new Exception("Attempting to retrieve object information from a field that doesn't contain an object.");
  63. return Internal_CreateObject(mCachedPtr, GetValue<object>());
  64. }
  65. public SerializableArray GetArray()
  66. {
  67. if (type != FieldType.Array)
  68. throw new Exception("Attempting to retrieve array information from a field that doesn't contain an array.");
  69. return Internal_CreateArray(mCachedPtr, GetValue<Array>());
  70. }
  71. [MethodImpl(MethodImplOptions.InternalCall)]
  72. private static extern SerializableObject Internal_CreateObject(IntPtr nativeInstance, object instance);
  73. [MethodImpl(MethodImplOptions.InternalCall)]
  74. private static extern SerializableArray Internal_CreateArray(IntPtr nativeInstance, Array instance);
  75. public static FieldType DetermineFieldType(Type internalType)
  76. {
  77. if (!internalType.IsArray)
  78. {
  79. if (internalType == typeof (Byte))
  80. return FieldType.Int;
  81. else if (internalType == typeof (SByte))
  82. return FieldType.Int;
  83. else if (internalType == typeof (Int16))
  84. return FieldType.Int;
  85. else if (internalType == typeof (UInt16))
  86. return FieldType.Int;
  87. else if (internalType == typeof (Int32))
  88. return FieldType.Int;
  89. else if (internalType == typeof (UInt32))
  90. return FieldType.Int;
  91. else if (internalType == typeof (Int64))
  92. return FieldType.Int;
  93. else if (internalType == typeof (UInt64))
  94. return FieldType.Int;
  95. else if (internalType == typeof (bool))
  96. return FieldType.Bool;
  97. else if (internalType == typeof (float))
  98. return FieldType.Float;
  99. else if (internalType == typeof (double))
  100. return FieldType.Float;
  101. else if (internalType == typeof (string))
  102. return FieldType.String;
  103. else if (internalType == typeof (Vector2))
  104. return FieldType.Vector2;
  105. else if (internalType == typeof (Vector3))
  106. return FieldType.Vector3;
  107. else if (internalType == typeof (Vector4))
  108. return FieldType.Vector4;
  109. else if (internalType == typeof (Color))
  110. return FieldType.Color;
  111. else if (internalType.IsSubclassOf(typeof (GameObject)))
  112. return FieldType.GameObjectRef;
  113. else if (internalType.IsSubclassOf(typeof (Resource)))
  114. return FieldType.ResourceRef;
  115. else if (internalType.IsGenericType)
  116. {
  117. Type genericType = internalType.GetGenericTypeDefinition();
  118. if (genericType == typeof (List<>))
  119. {
  120. return FieldType.List;
  121. }
  122. else if (genericType == typeof (Dictionary<,>))
  123. {
  124. return FieldType.Dictionary;
  125. }
  126. // Shouldn't happen because native code should only supply us with supported types
  127. throw new Exception("Cannot determine field type. Found an unsupported generic type.");
  128. }
  129. // Otherwise the type must be an object, unless some error occurred
  130. return FieldType.Object;
  131. }
  132. return FieldType.Array;
  133. }
  134. }
  135. }