BsManagedSerializableObjectInfo.h 11 KB

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