BsManagedSerializableObjectInfo.h 10 KB

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