BsManagedSerializableObjectInfo.h 11 KB

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