BsManagedSerializableObjectInfo.h 11 KB

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