SerializableField.cs 4.4 KB

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