BsManagedSerializableObjectInfo.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsScriptEnginePrerequisites.h"
  5. #include "BsIReflectable.h"
  6. #include <mono/jit/jit.h>
  7. namespace BansheeEngine
  8. {
  9. /** @addtogroup SBansheeEngine
  10. * @{
  11. */
  12. /** Valid serializable script types. */
  13. enum class ScriptPrimitiveType
  14. {
  15. Bool,
  16. Char,
  17. I8,
  18. U8,
  19. I16,
  20. U16,
  21. I32,
  22. U32,
  23. I64,
  24. U64,
  25. Float,
  26. Double,
  27. String,
  28. Texture2DRef,
  29. Texture3DRef,
  30. TextureCubeRef,
  31. SpriteTextureRef,
  32. ManagedResourceRef,
  33. PlainTextRef,
  34. ScriptCodeRef,
  35. ShaderRef,
  36. ShaderIncludeRef,
  37. MaterialRef,
  38. MeshRef,
  39. PrefabRef,
  40. FontRef,
  41. StringTableRef,
  42. GUISkinRef,
  43. SceneObjectRef,
  44. ComponentRef,
  45. PhysicsMaterialRef,
  46. PhysicsMeshRef,
  47. Count // Keep at end
  48. };
  49. /** Flags that are used to further define a field in a managed serializable object. */
  50. enum class ScriptFieldFlags
  51. {
  52. Serializable = 0x01,
  53. Inspectable = 0x02
  54. };
  55. /** Contains information about a type of a managed serializable object. */
  56. class BS_SCR_BE_EXPORT ManagedSerializableTypeInfo : public IReflectable
  57. {
  58. public:
  59. virtual ~ManagedSerializableTypeInfo() {}
  60. /** Checks if the current type matches the provided type. */
  61. virtual bool matches(const ManagedSerializableTypeInfoPtr& typeInfo) const = 0;
  62. /**
  63. * Checks does the managed type this object represents still exists.
  64. *
  65. * @note For example if assemblies get refreshed user could have renamed or removed some types.
  66. */
  67. virtual bool isTypeLoaded() const = 0;
  68. /**
  69. * Returns the internal managed class of the type this object represents. Returns null if the type doesn't exist.
  70. */
  71. virtual ::MonoClass* getMonoClass() const = 0;
  72. /************************************************************************/
  73. /* RTTI */
  74. /************************************************************************/
  75. public:
  76. friend class ManagedSerializableTypeInfoRTTI;
  77. static RTTITypeBase* getRTTIStatic();
  78. virtual RTTITypeBase* getRTTI() const override;
  79. };
  80. /** Contains information about a type of a managed serializable primitive (for example int, float, etc.). */
  81. class BS_SCR_BE_EXPORT ManagedSerializableTypeInfoPrimitive : public ManagedSerializableTypeInfo
  82. {
  83. public:
  84. /** @copydoc ManagedSerializableTypeInfo::matches */
  85. bool matches(const ManagedSerializableTypeInfoPtr& typeInfo) const override;
  86. /** @copydoc ManagedSerializableTypeInfo::isTypeLoaded */
  87. bool isTypeLoaded() const override;
  88. /** @copydoc ManagedSerializableTypeInfo::getMonoClass */
  89. ::MonoClass* getMonoClass() const override;
  90. ScriptPrimitiveType mType;
  91. /************************************************************************/
  92. /* RTTI */
  93. /************************************************************************/
  94. public:
  95. friend class ManagedSerializableTypeInfoPrimitiveRTTI;
  96. static RTTITypeBase* getRTTIStatic();
  97. virtual RTTITypeBase* getRTTI() const override;
  98. };
  99. /** Contains information about a type of a managed serializable complex object (for example struct or class). */
  100. class BS_SCR_BE_EXPORT ManagedSerializableTypeInfoObject : public ManagedSerializableTypeInfo
  101. {
  102. public:
  103. /** @copydoc ManagedSerializableTypeInfo::matches */
  104. bool matches(const ManagedSerializableTypeInfoPtr& typeInfo) const override;
  105. /** @copydoc ManagedSerializableTypeInfo::isTypeLoaded */
  106. bool isTypeLoaded() const override;
  107. /** @copydoc ManagedSerializableTypeInfo::getMonoClass */
  108. ::MonoClass* getMonoClass() const override;
  109. String mTypeNamespace;
  110. String mTypeName;
  111. bool mValueType;
  112. UINT32 mTypeId;
  113. /************************************************************************/
  114. /* RTTI */
  115. /************************************************************************/
  116. public:
  117. friend class ManagedSerializableTypeInfoObjectRTTI;
  118. static RTTITypeBase* getRTTIStatic();
  119. virtual RTTITypeBase* getRTTI() const override;
  120. };
  121. /** Contains information about a type of a managed serializable Array. */
  122. class BS_SCR_BE_EXPORT ManagedSerializableTypeInfoArray : public ManagedSerializableTypeInfo
  123. {
  124. public:
  125. /** @copydoc ManagedSerializableTypeInfo::matches */
  126. bool matches(const ManagedSerializableTypeInfoPtr& typeInfo) const override;
  127. /** @copydoc ManagedSerializableTypeInfo::isTypeLoaded */
  128. bool isTypeLoaded() const override;
  129. /** @copydoc ManagedSerializableTypeInfo::getMonoClass */
  130. ::MonoClass* getMonoClass() const override;
  131. ManagedSerializableTypeInfoPtr mElementType;
  132. UINT32 mRank;
  133. /************************************************************************/
  134. /* RTTI */
  135. /************************************************************************/
  136. public:
  137. friend class ManagedSerializableTypeInfoArrayRTTI;
  138. static RTTITypeBase* getRTTIStatic();
  139. virtual RTTITypeBase* getRTTI() const override;
  140. };
  141. /** Contains information about a type of a managed serializable List. */
  142. class BS_SCR_BE_EXPORT ManagedSerializableTypeInfoList : public ManagedSerializableTypeInfo
  143. {
  144. public:
  145. /** @copydoc ManagedSerializableTypeInfo::matches */
  146. bool matches(const ManagedSerializableTypeInfoPtr& typeInfo) const override;
  147. /** @copydoc ManagedSerializableTypeInfo::isTypeLoaded */
  148. bool isTypeLoaded() const override;
  149. /** @copydoc ManagedSerializableTypeInfo::getMonoClass */
  150. ::MonoClass* getMonoClass() const override;
  151. ManagedSerializableTypeInfoPtr mElementType;
  152. /************************************************************************/
  153. /* RTTI */
  154. /************************************************************************/
  155. public:
  156. friend class ManagedSerializableTypeInfoListRTTI;
  157. static RTTITypeBase* getRTTIStatic();
  158. virtual RTTITypeBase* getRTTI() const override;
  159. };
  160. /** Contains information about a type of a managed serializable Dictionary. */
  161. class BS_SCR_BE_EXPORT ManagedSerializableTypeInfoDictionary : public ManagedSerializableTypeInfo
  162. {
  163. public:
  164. /** @copydoc ManagedSerializableTypeInfo::matches */
  165. bool matches(const ManagedSerializableTypeInfoPtr& typeInfo) const override;
  166. /** @copydoc ManagedSerializableTypeInfo::isTypeLoaded */
  167. bool isTypeLoaded() const override;
  168. /** @copydoc ManagedSerializableTypeInfo::getMonoClass */
  169. ::MonoClass* getMonoClass() const override;
  170. ManagedSerializableTypeInfoPtr mKeyType;
  171. ManagedSerializableTypeInfoPtr mValueType;
  172. /************************************************************************/
  173. /* RTTI */
  174. /************************************************************************/
  175. public:
  176. friend class ManagedSerializableTypeInfoDictionaryRTTI;
  177. static RTTITypeBase* getRTTIStatic();
  178. virtual RTTITypeBase* getRTTI() const override;
  179. };
  180. /** Contains data about a single field in a managed complex object. */
  181. class BS_SCR_BE_EXPORT ManagedSerializableFieldInfo : public IReflectable
  182. {
  183. public:
  184. ManagedSerializableFieldInfo();
  185. /** Determines should the field be serialized when serializing the parent object. */
  186. bool isSerializable() const { return ((UINT32)mFlags & (UINT32)ScriptFieldFlags::Serializable) != 0; }
  187. String mName;
  188. UINT32 mFieldId;
  189. UINT32 mParentTypeId;
  190. ManagedSerializableTypeInfoPtr mTypeInfo;
  191. ScriptFieldFlags mFlags;
  192. MonoField* mMonoField;
  193. /************************************************************************/
  194. /* RTTI */
  195. /************************************************************************/
  196. public:
  197. friend class ManagedSerializableFieldInfoRTTI;
  198. static RTTITypeBase* getRTTIStatic();
  199. virtual RTTITypeBase* getRTTI() const override;
  200. };
  201. /** Contains data about fields of a complex object, and the object's class hierarchy if it belongs to one. */
  202. class BS_SCR_BE_EXPORT ManagedSerializableObjectInfo : public IReflectable
  203. {
  204. public:
  205. ManagedSerializableObjectInfo();
  206. /** Returns the managed type name of the object's type, including the namespace in format "namespace.typename". */
  207. String getFullTypeName() const { return mTypeInfo->mTypeNamespace + "." + mTypeInfo->mTypeName; }
  208. /**
  209. * Attempts to find a field part of this object that matches the provided parameters.
  210. *
  211. * @param[in] fieldInfo Object describing the managed field. Normally this will be a field that was
  212. * deserialized and you need to ensure it still exists in its parent type, while
  213. * retrieving the new field info.
  214. * @param[in] fieldTypeInfo Type information about the type containing the object. Used for debug purposes to
  215. * ensure the current object's type matches.
  216. * @return Found field info within this object, or null if not found.
  217. */
  218. ManagedSerializableFieldInfoPtr findMatchingField(const ManagedSerializableFieldInfoPtr& fieldInfo,
  219. const ManagedSerializableTypeInfoPtr& fieldTypeInfo) const;
  220. ManagedSerializableTypeInfoObjectPtr mTypeInfo;
  221. MonoClass* mMonoClass;
  222. UnorderedMap<String, UINT32> mFieldNameToId;
  223. UnorderedMap<UINT32, std::shared_ptr<ManagedSerializableFieldInfo>> mFields;
  224. std::shared_ptr<ManagedSerializableObjectInfo> mBaseClass;
  225. Vector<std::weak_ptr<ManagedSerializableObjectInfo>> mDerivedClasses;
  226. /************************************************************************/
  227. /* RTTI */
  228. /************************************************************************/
  229. public:
  230. friend class ManagedSerializableObjectInfoRTTI;
  231. static RTTITypeBase* getRTTIStatic();
  232. virtual RTTITypeBase* getRTTI() const override;
  233. };
  234. /** Contains information about all managed serializable objects in a specific managed assembly. */
  235. class BS_SCR_BE_EXPORT ManagedSerializableAssemblyInfo : public IReflectable
  236. {
  237. public:
  238. String mName;
  239. UnorderedMap<String, UINT32> mTypeNameToId;
  240. UnorderedMap<UINT32, std::shared_ptr<ManagedSerializableObjectInfo>> mObjectInfos;
  241. /************************************************************************/
  242. /* RTTI */
  243. /************************************************************************/
  244. public:
  245. friend class ManagedSerializableAssemblyInfoRTTI;
  246. static RTTITypeBase* getRTTIStatic();
  247. virtual RTTITypeBase* getRTTI() const override;
  248. };
  249. /** @} */
  250. }