SerializableField.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. public enum FieldType
  11. {
  12. Int,
  13. Float,
  14. Bool,
  15. String,
  16. Color,
  17. Vector2,
  18. Vector3,
  19. Vector4,
  20. GameObjectRef,
  21. ResourceRef,
  22. Object,
  23. Array,
  24. List,
  25. Dictionary
  26. }
  27. private SerializableObject parent;
  28. private FieldType type;
  29. private int flags;
  30. private Type internalType;
  31. private string name;
  32. // Only constructed from native code
  33. private SerializableField(SerializableObject parent, string name, int flags, Type internalType)
  34. {
  35. this.parent = parent;
  36. this.name = name;
  37. this.flags = flags;
  38. this.type = DetermineFieldType(internalType);
  39. this.internalType = internalType;
  40. }
  41. public FieldType Type
  42. {
  43. get { return type; }
  44. }
  45. public string Name
  46. {
  47. get { return name; }
  48. }
  49. public bool Inspectable
  50. {
  51. get { return (flags & 0x02) != 0; } // Flags as defined in native code in BsManagedSerializableObjectInfo.h
  52. }
  53. public bool Serializable
  54. {
  55. get { return (flags & 0x01) != 0; } // Flags as defined in native code in BsManagedSerializableObjectInfo.h
  56. }
  57. public SerializableValue GetValue()
  58. {
  59. SerializableValue.Getter getValue = () => Internal_GetValue(mCachedPtr, parent.referencedObject);
  60. SerializableValue.Setter setValue = (object value) => Internal_SetValue(mCachedPtr, parent.referencedObject, value);
  61. return new SerializableValue(internalType, getValue, setValue);
  62. }
  63. private static FieldType DetermineFieldType(Type internalType)
  64. {
  65. if (!internalType.IsArray)
  66. {
  67. if (internalType == typeof (Byte))
  68. return FieldType.Int;
  69. else if (internalType == typeof (SByte))
  70. return FieldType.Int;
  71. else if (internalType == typeof (Int16))
  72. return FieldType.Int;
  73. else if (internalType == typeof (UInt16))
  74. return FieldType.Int;
  75. else if (internalType == typeof (Int32))
  76. return FieldType.Int;
  77. else if (internalType == typeof (UInt32))
  78. return FieldType.Int;
  79. else if (internalType == typeof (Int64))
  80. return FieldType.Int;
  81. else if (internalType == typeof (UInt64))
  82. return FieldType.Int;
  83. else if (internalType == typeof (bool))
  84. return FieldType.Bool;
  85. else if (internalType == typeof (float))
  86. return FieldType.Float;
  87. else if (internalType == typeof (double))
  88. return FieldType.Float;
  89. else if (internalType == typeof (string))
  90. return FieldType.String;
  91. else if (internalType == typeof (Vector2))
  92. return FieldType.Vector2;
  93. else if (internalType == typeof (Vector3))
  94. return FieldType.Vector3;
  95. else if (internalType == typeof (Vector4))
  96. return FieldType.Vector4;
  97. else if (internalType == typeof (Color))
  98. return FieldType.Color;
  99. else if (internalType.IsSubclassOf(typeof (GameObject)))
  100. return FieldType.GameObjectRef;
  101. else if (internalType.IsSubclassOf(typeof (Resource)))
  102. return FieldType.ResourceRef;
  103. else if (internalType.IsGenericType)
  104. {
  105. Type genericType = internalType.GetGenericTypeDefinition();
  106. if (genericType == typeof (List<>))
  107. {
  108. return FieldType.List;
  109. }
  110. else if (genericType == typeof (Dictionary<,>))
  111. {
  112. return FieldType.Dictionary;
  113. }
  114. // Shouldn't happen because native code should only supply us with supported types
  115. throw new Exception("Cannot determine field type. Found an unsupported generic type.");
  116. }
  117. // Otherwise the type must be an object, unless some error occurred
  118. return FieldType.Object;
  119. }
  120. return FieldType.Array;
  121. }
  122. [MethodImpl(MethodImplOptions.InternalCall)]
  123. private static extern object Internal_GetValue(IntPtr nativeInstance, object instance);
  124. [MethodImpl(MethodImplOptions.InternalCall)]
  125. private static extern void Internal_SetValue(IntPtr nativeInstance, object instance, object value);
  126. }
  127. }