SerializableProperty.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Runtime.CompilerServices;
  6. using System.Text;
  7. namespace BansheeEngine
  8. {
  9. /// <summary>
  10. /// Allows you to transparently retrieve and set values of an entry, whether that entry is an object field or
  11. /// an array/list/dictionary entry.
  12. /// </summary>
  13. public sealed class SerializableProperty : ScriptObject
  14. {
  15. /// <summary>
  16. /// Object field types support by the serializable property.
  17. /// </summary>
  18. public enum FieldType
  19. {
  20. Int,
  21. Float,
  22. Bool,
  23. String,
  24. Color,
  25. Vector2,
  26. Vector3,
  27. Vector4,
  28. GameObjectRef,
  29. ResourceRef,
  30. Object,
  31. Array,
  32. List,
  33. Dictionary
  34. }
  35. public delegate object Getter();
  36. public delegate void Setter(object value);
  37. private FieldType type;
  38. private Type internalType;
  39. private Getter getter;
  40. private Setter setter;
  41. /// <summary>
  42. /// Constructor for internal use by the native code.
  43. /// </summary>
  44. private SerializableProperty()
  45. { }
  46. /// <summary>
  47. /// Creates a new serializable property.
  48. /// </summary>
  49. /// <param name="type">Type of data the property contains.</param>
  50. /// <param name="internalType">Type of data the property contains, as C# type.</param>
  51. /// <param name="getter">Method that allows you to retrieve contents of the property.</param>
  52. /// <param name="setter">Method that allows you to set contents of the property</param>
  53. public SerializableProperty(FieldType type, Type internalType, Getter getter, Setter setter)
  54. {
  55. this.type = type;
  56. this.internalType = internalType;
  57. this.getter = getter;
  58. this.setter = setter;
  59. Internal_CreateInstance(this, internalType);
  60. }
  61. /// <summary>
  62. /// Finalizes construction of the serializable property. Must be called after creation.
  63. /// </summary>
  64. /// <param name="type">Type of data the property contains.</param>
  65. /// <param name="internalType">Type of data the property contains, as C# type.</param>
  66. /// <param name="getter">Method that allows you to retrieve contents of the property.</param>
  67. /// <param name="setter">Method that allows you to set contents of the property</param>
  68. internal void Construct(FieldType type, Type internalType, Getter getter, Setter setter)
  69. {
  70. this.type = type;
  71. this.internalType = internalType;
  72. this.getter = getter;
  73. this.setter = setter;
  74. }
  75. /// <summary>
  76. /// Returns type of data the property contains.
  77. /// </summary>
  78. public FieldType Type
  79. {
  80. get { return type; }
  81. }
  82. /// <summary>
  83. /// Returns type of data the property contains, as C# type.
  84. /// </summary>
  85. public Type InternalType
  86. {
  87. get { return internalType; }
  88. }
  89. /// <summary>
  90. /// Is the containing type a value type (true), or a reference type (false).
  91. /// </summary>
  92. public bool IsValueType
  93. {
  94. get { return internalType.IsValueType; }
  95. }
  96. /// <summary>
  97. /// Retrieves the value contained in the property.
  98. /// </summary>
  99. /// <typeparam name="T">Type of the value to retrieve. Caller must ensure the type matches the property type.</typeparam>
  100. /// <returns>Value of the property.</returns>
  101. public T GetValue<T>()
  102. {
  103. if (!typeof(T).IsAssignableFrom(internalType))
  104. throw new Exception("Attempted to retrieve a serializable value using an invalid type. Provided type: " + typeof(T) + ". Needed type: " + internalType);
  105. return (T)getter();
  106. }
  107. /// <summary>
  108. /// Changes the value of the property.
  109. /// </summary>
  110. /// <typeparam name="T">Type of the value to set. Caller must ensure the type matches the property type.</typeparam>
  111. /// <param name="value">New value to assign to the property.</param>
  112. public void SetValue<T>(T value)
  113. {
  114. if (!typeof(T).IsAssignableFrom(internalType))
  115. throw new Exception("Attempted to set a serializable value using an invalid type. Provided type: " + typeof(T) + ". Needed type: " + internalType);
  116. setter(value);
  117. }
  118. /// <summary>
  119. /// Returns a serializable object wrapper around the value contained in the property.
  120. /// </summary>
  121. /// <returns>Serializable object wrapper around the value contained in the property.</returns>
  122. public SerializableObject GetObject()
  123. {
  124. if (type != FieldType.Object)
  125. throw new Exception("Attempting to retrieve object information from a field that doesn't contain an object.");
  126. return Internal_CreateObject(mCachedPtr);
  127. }
  128. /// <summary>
  129. /// Returns a serializable array around the value contained in the property. Caller must ensure the property
  130. /// contains an array.
  131. /// </summary>
  132. /// <returns>Serializable array around the value contained in the property.</returns>
  133. public SerializableArray GetArray()
  134. {
  135. if (type != FieldType.Array)
  136. throw new Exception("Attempting to retrieve array information from a field that doesn't contain an array.");
  137. return Internal_CreateArray(mCachedPtr);
  138. }
  139. /// <summary>
  140. /// Returns a serializable list around the value contained in the property. Caller must ensure the property
  141. /// contains a list.
  142. /// </summary>
  143. /// <returns>Serializable list around the value contained in the property.</returns>
  144. public SerializableList GetList()
  145. {
  146. if (type != FieldType.List)
  147. throw new Exception("Attempting to retrieve array information from a field that doesn't contain a list.");
  148. return Internal_CreateList(mCachedPtr);
  149. }
  150. /// <summary>
  151. /// Returns a serializable dictionary around the value contained in the property. Caller must ensure the property
  152. /// contains a dictionary.
  153. /// </summary>
  154. /// <returns>Serializable dictionary around the value contained in the property.</returns>
  155. public SerializableDictionary GetDictionary()
  156. {
  157. if (type != FieldType.Dictionary)
  158. throw new Exception("Attempting to retrieve array information from a field that doesn't contain a dictionary.");
  159. return Internal_CreateDictionary(mCachedPtr);
  160. }
  161. /// <summary>
  162. /// Creates a new instance of the type wrapped by this property.
  163. /// </summary>
  164. /// <typeparam name="T">Type of the object to create. Caller must ensure the type matches the property type and that
  165. /// the property wraps an object.</typeparam>
  166. /// <returns>A new instance of an object of type <typeparamref name="T"/>.</returns>
  167. public T CreateObjectInstance<T>()
  168. {
  169. if (type != FieldType.Object)
  170. throw new Exception("Attempting to retrieve object information from a field that doesn't contain an object.");
  171. return (T)Internal_CreateManagedObjectInstance(mCachedPtr);
  172. }
  173. /// <summary>
  174. /// Creates a new instance of the array wrapped by this property. Caller must ensure this property contains an array.
  175. /// </summary>
  176. /// <param name="lengths">Size of each dimension of the array. Number of dimensions must match the number
  177. /// of dimensions in the array wrapped by this property.</param>
  178. /// <returns>A new array containing the same element type as the array wrapped by this property, of
  179. /// <paramref name="lengths"/> sizes.</returns>
  180. public Array CreateArrayInstance(int[] lengths)
  181. {
  182. if (type != FieldType.Array)
  183. throw new Exception("Attempting to retrieve array information from a field that doesn't contain an array.");
  184. return Internal_CreateManagedArrayInstance(mCachedPtr, lengths);
  185. }
  186. /// <summary>
  187. /// Creates a new instance of the list wrapped by this property. Caller must ensure this property contains a list.
  188. /// </summary>
  189. /// <param name="length">Size of the list.</param>
  190. /// <returns>A new list containing the same element type as the list wrapped by this property, of
  191. /// <paramref name="length"/> size.</returns>
  192. public IList CreateListInstance(int length)
  193. {
  194. if (type != FieldType.List)
  195. throw new Exception("Attempting to retrieve array information from a field that doesn't contain a list.");
  196. return Internal_CreateManagedListInstance(mCachedPtr, length);
  197. }
  198. /// <summary>
  199. /// Creates a new instance of the dictionary wrapped by this property. Caller must ensure this property contains
  200. /// a dictionary.
  201. /// </summary>
  202. /// <returns>A new dictionary containing the same key/value types as the dictionary wrapped by this property.
  203. /// </returns>
  204. public IDictionary CreateDictionaryInstance()
  205. {
  206. if (type != FieldType.Dictionary)
  207. throw new Exception("Attempting to retrieve array information from a field that doesn't contain a dictionary.");
  208. return Internal_CreateManagedDictionaryInstance(mCachedPtr);
  209. }
  210. [MethodImpl(MethodImplOptions.InternalCall)]
  211. private static extern void Internal_CreateInstance(SerializableProperty instance, Type type);
  212. [MethodImpl(MethodImplOptions.InternalCall)]
  213. private static extern SerializableObject Internal_CreateObject(IntPtr nativeInstance);
  214. [MethodImpl(MethodImplOptions.InternalCall)]
  215. private static extern SerializableArray Internal_CreateArray(IntPtr nativeInstance);
  216. [MethodImpl(MethodImplOptions.InternalCall)]
  217. private static extern SerializableList Internal_CreateList(IntPtr nativeInstance);
  218. [MethodImpl(MethodImplOptions.InternalCall)]
  219. private static extern SerializableDictionary Internal_CreateDictionary(IntPtr nativeInstance);
  220. [MethodImpl(MethodImplOptions.InternalCall)]
  221. private static extern object Internal_CreateManagedObjectInstance(IntPtr nativeInstance);
  222. [MethodImpl(MethodImplOptions.InternalCall)]
  223. private static extern Array Internal_CreateManagedArrayInstance(IntPtr nativeInstance, int[] lengths);
  224. [MethodImpl(MethodImplOptions.InternalCall)]
  225. private static extern IList Internal_CreateManagedListInstance(IntPtr nativeInstance, int length);
  226. [MethodImpl(MethodImplOptions.InternalCall)]
  227. private static extern IDictionary Internal_CreateManagedDictionaryInstance(IntPtr nativeInstance);
  228. [MethodImpl(MethodImplOptions.InternalCall)]
  229. private static extern object Internal_CloneManagedInstance(IntPtr nativeInstance, object original);
  230. /// <summary>
  231. /// Converts a C# type into Banshee-specific serialization type.
  232. /// </summary>
  233. /// <param name="internalType">C# to convert.</param>
  234. /// <returns>Banshee-specific serialization type. Throws an exception if matching type cannot be found.</returns>
  235. public static FieldType DetermineFieldType(Type internalType)
  236. {
  237. if (!internalType.IsArray)
  238. {
  239. if (internalType == typeof (Byte))
  240. return FieldType.Int;
  241. else if (internalType == typeof (SByte))
  242. return FieldType.Int;
  243. else if (internalType == typeof (Int16))
  244. return FieldType.Int;
  245. else if (internalType == typeof (UInt16))
  246. return FieldType.Int;
  247. else if (internalType == typeof (Int32))
  248. return FieldType.Int;
  249. else if (internalType == typeof (UInt32))
  250. return FieldType.Int;
  251. else if (internalType == typeof (Int64))
  252. return FieldType.Int;
  253. else if (internalType == typeof (UInt64))
  254. return FieldType.Int;
  255. else if (internalType == typeof (bool))
  256. return FieldType.Bool;
  257. else if (internalType == typeof (float))
  258. return FieldType.Float;
  259. else if (internalType == typeof (double))
  260. return FieldType.Float;
  261. else if (internalType == typeof (string))
  262. return FieldType.String;
  263. else if (internalType == typeof (Vector2))
  264. return FieldType.Vector2;
  265. else if (internalType == typeof (Vector3))
  266. return FieldType.Vector3;
  267. else if (internalType == typeof (Vector4))
  268. return FieldType.Vector4;
  269. else if (internalType == typeof (Color))
  270. return FieldType.Color;
  271. else if (internalType.IsSubclassOf(typeof (GameObject)))
  272. return FieldType.GameObjectRef;
  273. else if (internalType.IsSubclassOf(typeof (Resource)))
  274. return FieldType.ResourceRef;
  275. else if (internalType.IsGenericType)
  276. {
  277. Type genericType = internalType.GetGenericTypeDefinition();
  278. if (genericType == typeof (List<>))
  279. {
  280. return FieldType.List;
  281. }
  282. else if (genericType == typeof (Dictionary<,>))
  283. {
  284. return FieldType.Dictionary;
  285. }
  286. // Shouldn't happen because native code should only supply us with supported types
  287. throw new Exception("Cannot determine field type. Found an unsupported generic type.");
  288. }
  289. // Otherwise the type must be an object, unless some error occurred
  290. return FieldType.Object;
  291. }
  292. return FieldType.Array;
  293. }
  294. }
  295. }