BsManagedSerializableObjectInfo.h 10 KB

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