SerializableProperty.cs 14 KB

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