SerializableObject.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. /// <summary>
  21. /// Creates a new serializable object for the specified object type.
  22. /// </summary>
  23. /// <param name="objectType">C# type of the object.</param>
  24. /// <param name="parentProperty">Property used for retrieving this entry.</param>
  25. public SerializableObject(Type objectType, SerializableProperty parentProperty)
  26. {
  27. Internal_CreateInstance(this, objectType);
  28. this.parentProperty = parentProperty;
  29. this.parentObject = null;
  30. }
  31. /// <summary>
  32. /// Creates a new serializable object for the specified object type.
  33. /// </summary>
  34. /// <param name="objectType">C# type of the object.</param>
  35. /// <param name="parentObject">Specific instance of the object of <paramref name="objectType"/>.</param>
  36. public SerializableObject(Type objectType, object parentObject)
  37. {
  38. Internal_CreateInstance(this, objectType);
  39. this.parentProperty = null;
  40. this.parentObject = parentObject;
  41. }
  42. /// <summary>
  43. /// Creates a new serializable object for the specified object. Object must not be null.
  44. /// </summary>
  45. /// <param name="parentObject">Specific instance of the object.</param>
  46. public SerializableObject(object parentObject)
  47. {
  48. Internal_CreateInstance(this, parentObject.GetType());
  49. this.parentProperty = null;
  50. this.parentObject = parentObject;
  51. }
  52. /// <summary>
  53. /// Returns all fields in the object.
  54. /// </summary>
  55. public SerializableField[] Fields
  56. {
  57. get { return _fields; }
  58. }
  59. /// <summary>
  60. /// Returns the specific object instance this object is operating on.
  61. /// </summary>
  62. /// <returns>Object instance.</returns>
  63. public object GetReferencedObject()
  64. {
  65. if (parentProperty != null)
  66. return parentProperty.GetValue<object>();
  67. else
  68. return parentObject;
  69. }
  70. /// <summary>
  71. /// Searches the object, and all child objects for a field or entry with the specified name.
  72. /// </summary>
  73. /// <param name="path">Slash separated path with field name(s) to search for. If a field contains an array, list or
  74. /// a dictionary append its name with a "[x]" where x is the element index in the array/list, or
  75. /// a key name (surrounded by "") in case of a dictionary. Only primitive dictionary keys are
  76. /// supported.
  77. ///
  78. /// Example path: subObject/myDictionary["someElement"]/theArray[4]/fieldToGet
  79. /// </param>
  80. /// <returns>Property you can use for reading or modifying the property, or null if not found.</returns>
  81. public SerializableProperty FindProperty(string path)
  82. {
  83. if (path == null)
  84. return null;
  85. string[] pathEntries = path.Split('/');
  86. PropertyPathElement[] pathElements = new PropertyPathElement[pathEntries.Length];
  87. for (int i = 0; i < pathEntries.Length; i++)
  88. {
  89. string entry = pathEntries[i];
  90. if (string.IsNullOrEmpty(entry))
  91. return null;
  92. pathElements[i] = new PropertyPathElement();
  93. if (entry[entry.Length - 1] == ']')
  94. {
  95. bool foundKey = false;
  96. int j = entry.Length - 2;
  97. for (; j >= 0; j--)
  98. {
  99. if (entry[j] == '[')
  100. {
  101. foundKey = true;
  102. break;
  103. }
  104. }
  105. if (foundKey)
  106. {
  107. pathElements[i].name = entry.Substring(0, j);
  108. pathElements[i].key = entry.Substring(j + 1, (entry.Length - 1) - (j + 1));
  109. }
  110. else
  111. pathElements[i].name = entry;
  112. }
  113. else
  114. {
  115. pathElements[i].name = entry;
  116. }
  117. }
  118. return FindProperty(pathElements, 0);
  119. }
  120. /// <summary>
  121. /// Searches the object hierarchy using the provided path elements. <see cref="FindProperty(string)"/>
  122. /// </summary>
  123. /// <param name="pathElements">Path elements representing field names and keys to look for.</param>
  124. /// <param name="elementIdx">Index in the <paramref name="pathElements"/> array to start the search at.</param>
  125. /// <returns>Property representing the final path element, or null if not found.</returns>
  126. internal SerializableProperty FindProperty(PropertyPathElement[] pathElements, int elementIdx)
  127. {
  128. SerializableField field = FindField(pathElements[0].name);
  129. if (field != null)
  130. {
  131. SerializableProperty property = field.GetProperty();
  132. if (elementIdx == (pathElements.Length - 1))
  133. return property;
  134. return property.FindProperty(pathElements, elementIdx + 1);
  135. }
  136. return null;
  137. }
  138. /// <summary>
  139. /// Searches the object for a field with the specified name.
  140. /// </summary>
  141. /// <param name="name">Name of the field.</param>
  142. /// <returns>Object representing the field, if found, null otherwise.</returns>
  143. public SerializableField FindField(string name)
  144. {
  145. foreach (var field in _fields)
  146. {
  147. if (field.Name == name)
  148. return field;
  149. }
  150. return null;
  151. }
  152. [MethodImpl(MethodImplOptions.InternalCall)]
  153. private static extern void Internal_CreateInstance(SerializableObject instance, Type objectType);
  154. }
  155. /// <summary>
  156. /// Contains a single element of a path to a field or array/list/dictionary entry, as used for
  157. /// <see cref="SerializableObject"/>.
  158. /// </summary>
  159. internal struct PropertyPathElement
  160. {
  161. public string name;
  162. public string key;
  163. }
  164. /** @} */
  165. }