SerializableObject.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. using System.Text;
  6. namespace BansheeEngine
  7. {
  8. /** @addtogroup Serialization
  9. * @{
  10. */
  11. #pragma warning disable 649
  12. /// <summary>
  13. /// Allows you to access meta-data about a managed object and its fields. Similar to Reflection but simpler and faster.
  14. /// </summary>
  15. public sealed class SerializableObject : ScriptObject
  16. {
  17. internal SerializableProperty parentProperty;
  18. internal object parentObject;
  19. private SerializableField[] _fields;
  20. private Type type;
  21. /// <summary>
  22. /// Type of the underlying object.
  23. /// </summary>
  24. public Type Type { get { return type; } }
  25. /// <summary>
  26. /// Underlying object instance, if any.
  27. /// </summary>
  28. public object Object { get { return parentObject; } }
  29. /// <summary>
  30. /// Creates a new serializable object for the specified object type.
  31. /// </summary>
  32. /// <param name="objectType">C# type of the object.</param>
  33. /// <param name="parentProperty">Property used for retrieving this entry.</param>
  34. public SerializableObject(Type objectType, SerializableProperty parentProperty)
  35. {
  36. Internal_CreateInstance(this, objectType);
  37. this.parentProperty = parentProperty;
  38. this.parentObject = null;
  39. this.type = objectType;
  40. }
  41. /// <summary>
  42. /// Creates a new serializable object for the specified object type.
  43. /// </summary>
  44. /// <param name="objectType">C# type of the object.</param>
  45. /// <param name="parentObject">Specific instance of the object of <paramref name="objectType"/>.</param>
  46. public SerializableObject(Type objectType, object parentObject)
  47. {
  48. Internal_CreateInstance(this, objectType);
  49. this.parentProperty = null;
  50. this.parentObject = parentObject;
  51. this.type = objectType;
  52. }
  53. /// <summary>
  54. /// Creates a new serializable object for the specified object. Object must not be null.
  55. /// </summary>
  56. /// <param name="parentObject">Specific instance of the object.</param>
  57. public SerializableObject(object parentObject)
  58. {
  59. Internal_CreateInstance(this, parentObject.GetType());
  60. this.parentProperty = null;
  61. this.parentObject = parentObject;
  62. this.type = parentObject.GetType();
  63. }
  64. /// <summary>
  65. /// Returns all fields in the object.
  66. /// </summary>
  67. public SerializableField[] Fields
  68. {
  69. get { return _fields; }
  70. }
  71. /// <summary>
  72. /// Returns the specific object instance this object is operating on.
  73. /// </summary>
  74. /// <returns>Object instance.</returns>
  75. public object GetReferencedObject()
  76. {
  77. if (parentProperty != null)
  78. return parentProperty.GetValue<object>();
  79. else
  80. return parentObject;
  81. }
  82. /// <summary>
  83. /// Searches the object, and all child objects for a field or entry with the specified name.
  84. /// </summary>
  85. /// <param name="path">Slash separated path with field name(s) to search for. If a field contains an array, list or
  86. /// a dictionary append its name with a "[x]" where x is the element index in the array/list, or
  87. /// a key name (surrounded by "") in case of a dictionary. Only primitive dictionary keys are
  88. /// supported.
  89. ///
  90. /// Example path: subObject/myDictionary["someElement"]/theArray[4]/fieldToGet
  91. /// </param>
  92. /// <returns>Property you can use for reading or modifying the property, or null if not found.</returns>
  93. public SerializableProperty FindProperty(string path)
  94. {
  95. if (path == null)
  96. return null;
  97. string trimmedPath = path.Trim('/');
  98. string[] pathEntries = trimmedPath.Split('/');
  99. PropertyPathElement[] pathElements = new PropertyPathElement[pathEntries.Length];
  100. for (int i = 0; i < pathEntries.Length; i++)
  101. {
  102. string entry = pathEntries[i];
  103. if (string.IsNullOrEmpty(entry))
  104. return null;
  105. pathElements[i] = new PropertyPathElement();
  106. if (entry[entry.Length - 1] == ']')
  107. {
  108. bool foundKey = false;
  109. int j = entry.Length - 2;
  110. for (; j >= 0; j--)
  111. {
  112. if (entry[j] == '[')
  113. {
  114. foundKey = true;
  115. break;
  116. }
  117. }
  118. if (foundKey)
  119. {
  120. pathElements[i].name = entry.Substring(0, j);
  121. pathElements[i].key = entry.Substring(j + 1, (entry.Length - 1) - (j + 1));
  122. }
  123. else
  124. pathElements[i].name = entry;
  125. }
  126. else
  127. {
  128. pathElements[i].name = entry;
  129. }
  130. }
  131. return FindProperty(pathElements, 0);
  132. }
  133. /// <summary>
  134. /// Searches the object hierarchy using the provided path elements. <see cref="FindProperty(string)"/>
  135. /// </summary>
  136. /// <param name="pathElements">Path elements representing field names and keys to look for.</param>
  137. /// <param name="elementIdx">Index in the <paramref name="pathElements"/> array to start the search at.</param>
  138. /// <returns>Property representing the final path element, or null if not found.</returns>
  139. internal SerializableProperty FindProperty(PropertyPathElement[] pathElements, int elementIdx)
  140. {
  141. SerializableField field = FindField(pathElements[0].name);
  142. if (field != null)
  143. {
  144. SerializableProperty property = field.GetProperty();
  145. if (elementIdx == (pathElements.Length - 1))
  146. return property;
  147. return property.FindProperty(pathElements, elementIdx + 1);
  148. }
  149. return null;
  150. }
  151. /// <summary>
  152. /// Searches the object for a field with the specified name.
  153. /// </summary>
  154. /// <param name="name">Name of the field.</param>
  155. /// <returns>Object representing the field, if found, null otherwise.</returns>
  156. public SerializableField FindField(string name)
  157. {
  158. foreach (var field in _fields)
  159. {
  160. if (field.Name == name)
  161. return field;
  162. }
  163. return null;
  164. }
  165. [MethodImpl(MethodImplOptions.InternalCall)]
  166. private static extern void Internal_CreateInstance(SerializableObject instance, Type objectType);
  167. }
  168. /// <summary>
  169. /// Contains a single element of a path to a field or array/list/dictionary entry, as used for
  170. /// <see cref="SerializableObject"/>.
  171. /// </summary>
  172. internal struct PropertyPathElement
  173. {
  174. public string name;
  175. public string key;
  176. }
  177. /** @} */
  178. }