SerializableProperty.cs 14 KB

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