BsManagedSerializableObjectInfo.h 10 KB

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