SerializableObject.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. int startIdx = 0;
  107. int nameEndIdx = 0;
  108. int endIdx = entry.Length - 1;
  109. if (entry[entry.Length - 1] == ']')
  110. {
  111. bool foundKey = false;
  112. for (int j = 0; j < entry.Length - 1; j++)
  113. {
  114. if (entry[j] == '[')
  115. {
  116. startIdx = j;
  117. nameEndIdx = j;
  118. foundKey = true;
  119. }
  120. }
  121. // Trim string quotes, if they exist
  122. if (startIdx < (endIdx - 1) && entry[startIdx + 1] == '"')
  123. startIdx++;
  124. if (endIdx > (startIdx + 1) && entry[endIdx - 1] == '"')
  125. endIdx--;
  126. if (foundKey)
  127. {
  128. pathElements[i].name = entry.Substring(0, nameEndIdx);
  129. pathElements[i].key = entry.Substring(startIdx + 1, endIdx - (startIdx + 1));
  130. }
  131. else
  132. pathElements[i].name = entry;
  133. }
  134. else
  135. {
  136. pathElements[i].name = entry;
  137. }
  138. }
  139. return FindProperty(pathElements, 0);
  140. }
  141. /// <summary>
  142. /// Searches the object hierarchy using the provided path elements. <see cref="FindProperty(string)"/>
  143. /// </summary>
  144. /// <param name="pathElements">Path elements representing field names and keys to look for.</param>
  145. /// <param name="elementIdx">Index in the <paramref name="pathElements"/> array to start the search at.</param>
  146. /// <returns>Property representing the final path element, or null if not found.</returns>
  147. internal SerializableProperty FindProperty(PropertyPathElement[] pathElements, int elementIdx)
  148. {
  149. SerializableField field = FindField(pathElements[0].name);
  150. if (field != null)
  151. {
  152. SerializableProperty property = field.GetProperty();
  153. if (elementIdx == (pathElements.Length - 1))
  154. return property;
  155. return property.FindProperty(pathElements, elementIdx + 1);
  156. }
  157. return null;
  158. }
  159. /// <summary>
  160. /// Searches the object for a field with the specified name.
  161. /// </summary>
  162. /// <param name="name">Name of the field.</param>
  163. /// <returns>Object representing the field, if found, null otherwise.</returns>
  164. public SerializableField FindField(string name)
  165. {
  166. foreach (var field in _fields)
  167. {
  168. if (field.Name == name)
  169. return field;
  170. }
  171. return null;
  172. }
  173. [MethodImpl(MethodImplOptions.InternalCall)]
  174. private static extern void Internal_CreateInstance(SerializableObject instance, Type objectType);
  175. }
  176. /// <summary>
  177. /// Contains a single element of a path to a field or array/list/dictionary entry, as used for
  178. /// <see cref="SerializableObject"/>.
  179. /// </summary>
  180. internal struct PropertyPathElement
  181. {
  182. public string name;
  183. public string key;
  184. }
  185. /** @} */
  186. }