SerializableProperty.cs 15 KB

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