2
0

SerializableField.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.CompilerServices;
  5. using System.Text;
  6. namespace BansheeEngine
  7. {
  8. /// <summary>
  9. /// Allows you to access meta-data about field in an object. Similar to Reflection but simpler and faster.
  10. /// </summary>
  11. public class SerializableField : ScriptObject
  12. {
  13. private SerializableObject parent;
  14. private SerializableProperty.FieldType type;
  15. private int flags;
  16. private Type internalType;
  17. private string name;
  18. /// <summary>
  19. /// Constructor for internal use by the runtime.
  20. /// </summary>
  21. /// <param name="parent">Object that conains the field.</param>
  22. /// <param name="name">Name of the field.</param>
  23. /// <param name="flags">Flags that control whether the field is inspectable or serializable.</param>
  24. /// <param name="internalType">Internal C# type of the field.</param>
  25. private SerializableField(SerializableObject parent, string name, int flags, Type internalType)
  26. {
  27. this.parent = parent;
  28. this.name = name;
  29. this.flags = flags;
  30. this.type = SerializableProperty.DetermineFieldType(internalType);
  31. this.internalType = internalType;
  32. }
  33. /// <summary>
  34. /// Returns the type of data contained in the field.
  35. /// </summary>
  36. public SerializableProperty.FieldType Type
  37. {
  38. get { return type; }
  39. }
  40. /// <summary>
  41. /// Returns the name of the field.
  42. /// </summary>
  43. public string Name
  44. {
  45. get { return name; }
  46. }
  47. /// <summary>
  48. /// Returns true if the field will be visible in the default inspector.
  49. /// </summary>
  50. public bool Inspectable
  51. {
  52. get { return (flags & 0x02) != 0; } // Flags as defined in native code in BsManagedSerializableObjectInfo.h
  53. }
  54. /// <summary>
  55. /// Returns true if the field will be automatically serialized.
  56. /// </summary>
  57. public bool Serializable
  58. {
  59. get { return (flags & 0x01) != 0; } // Flags as defined in native code in BsManagedSerializableObjectInfo.h
  60. }
  61. /// <summary>
  62. /// Returns a serializable property for the field.
  63. /// </summary>
  64. /// <returns>Serializable property that allows you to manipulate contents of the field.</returns>
  65. public SerializableProperty GetProperty()
  66. {
  67. SerializableProperty.Getter getter = () =>
  68. {
  69. object parentObject = parent.GetReferencedObject();
  70. if (parentObject != null)
  71. return Internal_GetValue(mCachedPtr, parentObject);
  72. else
  73. return null;
  74. };
  75. SerializableProperty.Setter setter = (object value) =>
  76. {
  77. object parentObject = parent.GetReferencedObject();
  78. if (parentObject != null)
  79. {
  80. Internal_SetValue(mCachedPtr, parentObject, value);
  81. // If value type we cannot just modify the parent object because it's just a copy
  82. if (parentObject.GetType().IsValueType && parent.parentProperty != null)
  83. parent.parentProperty.SetValue(parentObject);
  84. }
  85. };
  86. SerializableProperty newProperty = Internal_CreateProperty(mCachedPtr);
  87. newProperty.Construct(type, internalType, getter, setter);
  88. return newProperty;
  89. }
  90. [MethodImpl(MethodImplOptions.InternalCall)]
  91. private static extern SerializableProperty Internal_CreateProperty(IntPtr nativeInstance);
  92. [MethodImpl(MethodImplOptions.InternalCall)]
  93. private static extern object Internal_GetValue(IntPtr nativeInstance, object instance);
  94. [MethodImpl(MethodImplOptions.InternalCall)]
  95. private static extern void Internal_SetValue(IntPtr nativeInstance, object instance, object value);
  96. }
  97. }