2
0

SerializableField.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. public class SerializableField : ScriptObject
  9. {
  10. private SerializableObject parent;
  11. private SerializableProperty.FieldType type;
  12. private int flags;
  13. private Type internalType;
  14. private string name;
  15. // Only constructed from native code
  16. private SerializableField(SerializableObject parent, string name, int flags, Type internalType)
  17. {
  18. this.parent = parent;
  19. this.name = name;
  20. this.flags = flags;
  21. this.type = SerializableProperty.DetermineFieldType(internalType);
  22. this.internalType = internalType;
  23. }
  24. public SerializableProperty.FieldType Type
  25. {
  26. get { return type; }
  27. }
  28. public bool HasCustomInspector
  29. {
  30. get { return false; } // TODO - Add [UseCustomInspector(typeof(InspecableType))] attribute and parse it
  31. }
  32. public Type CustomInspectorType
  33. {
  34. get { return null; } // TODO - See above. Return type from UseCustomInspector attribute
  35. }
  36. public string Name
  37. {
  38. get { return name; }
  39. }
  40. public bool Inspectable
  41. {
  42. get { return (flags & 0x02) != 0; } // Flags as defined in native code in BsManagedSerializableObjectInfo.h
  43. }
  44. public bool Serializable
  45. {
  46. get { return (flags & 0x01) != 0; } // Flags as defined in native code in BsManagedSerializableObjectInfo.h
  47. }
  48. public SerializableProperty GetProperty()
  49. {
  50. SerializableProperty.Getter getter = () =>
  51. {
  52. object parentObject = parent.GetReferencedObject();
  53. if (parentObject != null)
  54. return Internal_GetValue(mCachedPtr, parentObject);
  55. else
  56. return null;
  57. };
  58. SerializableProperty.Setter setter = (object value) =>
  59. {
  60. object parentObject = parent.GetReferencedObject();
  61. if (parentObject != null)
  62. {
  63. Internal_SetValue(mCachedPtr, parentObject, value);
  64. // If value type we cannot just modify the parent object because it's just a copy
  65. if (parentObject.GetType().IsValueType && parent.parentProperty != null)
  66. parent.parentProperty.SetValue(parentObject);
  67. }
  68. };
  69. SerializableProperty newProperty = Internal_CreateProperty(mCachedPtr);
  70. newProperty.Construct(type, internalType, getter, setter);
  71. return newProperty;
  72. }
  73. [MethodImpl(MethodImplOptions.InternalCall)]
  74. private static extern SerializableProperty Internal_CreateProperty(IntPtr nativeInstance);
  75. [MethodImpl(MethodImplOptions.InternalCall)]
  76. private static extern object Internal_GetValue(IntPtr nativeInstance, object instance);
  77. [MethodImpl(MethodImplOptions.InternalCall)]
  78. private static extern void Internal_SetValue(IntPtr nativeInstance, object instance, object value);
  79. }
  80. }