SerializableProperty.cs 15 KB

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