SerializableObject.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 trimmedPath = path.Trim('/');
  86. string[] pathEntries = trimmedPath.Split('/');
  87. PropertyPathElement[] pathElements = new PropertyPathElement[pathEntries.Length];
  88. for (int i = 0; i < pathEntries.Length; i++)
  89. {
  90. string entry = pathEntries[i];
  91. if (string.IsNullOrEmpty(entry))
  92. return null;
  93. pathElements[i] = new PropertyPathElement();
  94. if (entry[entry.Length - 1] == ']')
  95. {
  96. bool foundKey = false;
  97. int j = entry.Length - 2;
  98. for (; j >= 0; j--)
  99. {
  100. if (entry[j] == '[')
  101. {
  102. foundKey = true;
  103. break;
  104. }
  105. }
  106. if (foundKey)
  107. {
  108. pathElements[i].name = entry.Substring(0, j);
  109. pathElements[i].key = entry.Substring(j + 1, (entry.Length - 1) - (j + 1));
  110. }
  111. else
  112. pathElements[i].name = entry;
  113. }
  114. else
  115. {
  116. pathElements[i].name = entry;
  117. }
  118. }
  119. return FindProperty(pathElements, 0);
  120. }
  121. /// <summary>
  122. /// Searches the object hierarchy using the provided path elements. <see cref="FindProperty(string)"/>
  123. /// </summary>
  124. /// <param name="pathElements">Path elements representing field names and keys to look for.</param>
  125. /// <param name="elementIdx">Index in the <paramref name="pathElements"/> array to start the search at.</param>
  126. /// <returns>Property representing the final path element, or null if not found.</returns>
  127. internal SerializableProperty FindProperty(PropertyPathElement[] pathElements, int elementIdx)
  128. {
  129. SerializableField field = FindField(pathElements[0].name);
  130. if (field != null)
  131. {
  132. SerializableProperty property = field.GetProperty();
  133. if (elementIdx == (pathElements.Length - 1))
  134. return property;
  135. return property.FindProperty(pathElements, elementIdx + 1);
  136. }
  137. return null;
  138. }
  139. /// <summary>
  140. /// Searches the object for a field with the specified name.
  141. /// </summary>
  142. /// <param name="name">Name of the field.</param>
  143. /// <returns>Object representing the field, if found, null otherwise.</returns>
  144. public SerializableField FindField(string name)
  145. {
  146. foreach (var field in _fields)
  147. {
  148. if (field.Name == name)
  149. return field;
  150. }
  151. return null;
  152. }
  153. [MethodImpl(MethodImplOptions.InternalCall)]
  154. private static extern void Internal_CreateInstance(SerializableObject instance, Type objectType);
  155. }
  156. /// <summary>
  157. /// Contains a single element of a path to a field or array/list/dictionary entry, as used for
  158. /// <see cref="SerializableObject"/>.
  159. /// </summary>
  160. internal struct PropertyPathElement
  161. {
  162. public string name;
  163. public string key;
  164. }
  165. /** @} */
  166. }