BsManagedSerializableObjectInfo.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. /** @addtogroup SBansheeEngine
  10. * @{
  11. */
  12. /** Valid serializable script types. */
  13. enum class ScriptPrimitiveType
  14. {
  15. Bool,
  16. Char,
  17. I8,
  18. U8,
  19. I16,
  20. U16,
  21. I32,
  22. U32,
  23. I64,
  24. U64,
  25. Float,
  26. Double,
  27. String,
  28. Count // Keep at end
  29. };
  30. /** Valid reference script types. */
  31. enum class ScriptReferenceType
  32. {
  33. Texture2D,
  34. Texture3D,
  35. TextureCube,
  36. SpriteTexture,
  37. ManagedResource,
  38. PlainText,
  39. ScriptCode,
  40. Shader,
  41. ShaderInclude,
  42. Material,
  43. Mesh,
  44. Prefab,
  45. Font,
  46. StringTable,
  47. GUISkin,
  48. SceneObject,
  49. Component,
  50. PhysicsMaterial,
  51. PhysicsMesh,
  52. Count // Keep at end
  53. };
  54. /** Flags that are used to further define a field in a managed serializable object. */
  55. enum class ScriptFieldFlags
  56. {
  57. Serializable = 0x01,
  58. Inspectable = 0x02
  59. };
  60. /** Contains information about a type of a managed serializable object. */
  61. class BS_SCR_BE_EXPORT ManagedSerializableTypeInfo : public IReflectable
  62. {
  63. public:
  64. virtual ~ManagedSerializableTypeInfo() {}
  65. /** Checks if the current type matches the provided type. */
  66. virtual bool matches(const ManagedSerializableTypeInfoPtr& typeInfo) const = 0;
  67. /**
  68. * Checks does the managed type this object represents still exists.
  69. *
  70. * @note For example if assemblies get refreshed user could have renamed or removed some types.
  71. */
  72. virtual bool isTypeLoaded() const = 0;
  73. /**
  74. * Returns the internal managed class of the type this object represents. 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. /** Contains information about a type of a managed serializable primitive (for example int, float, etc.). */
  86. class BS_SCR_BE_EXPORT ManagedSerializableTypeInfoPrimitive : public ManagedSerializableTypeInfo
  87. {
  88. public:
  89. /** @copydoc ManagedSerializableTypeInfo::matches */
  90. bool matches(const ManagedSerializableTypeInfoPtr& typeInfo) const override;
  91. /** @copydoc ManagedSerializableTypeInfo::isTypeLoaded */
  92. bool isTypeLoaded() const override;
  93. /** @copydoc ManagedSerializableTypeInfo::getMonoClass */
  94. ::MonoClass* getMonoClass() const override;
  95. ScriptPrimitiveType mType;
  96. /************************************************************************/
  97. /* RTTI */
  98. /************************************************************************/
  99. public:
  100. friend class ManagedSerializableTypeInfoPrimitiveRTTI;
  101. static RTTITypeBase* getRTTIStatic();
  102. virtual RTTITypeBase* getRTTI() const override;
  103. };
  104. /** Contains information about a type of a managed serializable game object or resource reference. */
  105. class BS_SCR_BE_EXPORT ManagedSerializableTypeInfoRef : public ManagedSerializableTypeInfo
  106. {
  107. public:
  108. /** @copydoc ManagedSerializableTypeInfo::matches */
  109. bool matches(const ManagedSerializableTypeInfoPtr& typeInfo) const override;
  110. /** @copydoc ManagedSerializableTypeInfo::isTypeLoaded */
  111. bool isTypeLoaded() const override;
  112. /** @copydoc ManagedSerializableTypeInfo::getMonoClass */
  113. ::MonoClass* getMonoClass() const override;
  114. ScriptReferenceType mType;
  115. String mTypeNamespace;
  116. String mTypeName;
  117. /************************************************************************/
  118. /* RTTI */
  119. /************************************************************************/
  120. public:
  121. friend class ManagedSerializableTypeInfoRefRTTI;
  122. static RTTITypeBase* getRTTIStatic();
  123. virtual RTTITypeBase* getRTTI() const override;
  124. };
  125. /** Contains information about a type of a managed serializable complex object (for example struct or class). */
  126. class BS_SCR_BE_EXPORT ManagedSerializableTypeInfoObject : public ManagedSerializableTypeInfo
  127. {
  128. public:
  129. /** @copydoc ManagedSerializableTypeInfo::matches */
  130. bool matches(const ManagedSerializableTypeInfoPtr& typeInfo) const override;
  131. /** @copydoc ManagedSerializableTypeInfo::isTypeLoaded */
  132. bool isTypeLoaded() const override;
  133. /** @copydoc ManagedSerializableTypeInfo::getMonoClass */
  134. ::MonoClass* getMonoClass() const override;
  135. String mTypeNamespace;
  136. String mTypeName;
  137. bool mValueType;
  138. UINT32 mTypeId;
  139. /************************************************************************/
  140. /* RTTI */
  141. /************************************************************************/
  142. public:
  143. friend class ManagedSerializableTypeInfoObjectRTTI;
  144. static RTTITypeBase* getRTTIStatic();
  145. virtual RTTITypeBase* getRTTI() const override;
  146. };
  147. /** Contains information about a type of a managed serializable Array. */
  148. class BS_SCR_BE_EXPORT ManagedSerializableTypeInfoArray : public ManagedSerializableTypeInfo
  149. {
  150. public:
  151. /** @copydoc ManagedSerializableTypeInfo::matches */
  152. bool matches(const ManagedSerializableTypeInfoPtr& typeInfo) const override;
  153. /** @copydoc ManagedSerializableTypeInfo::isTypeLoaded */
  154. bool isTypeLoaded() const override;
  155. /** @copydoc ManagedSerializableTypeInfo::getMonoClass */
  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. /** Contains information about a type of a managed serializable List. */
  168. class BS_SCR_BE_EXPORT ManagedSerializableTypeInfoList : public ManagedSerializableTypeInfo
  169. {
  170. public:
  171. /** @copydoc ManagedSerializableTypeInfo::matches */
  172. bool matches(const ManagedSerializableTypeInfoPtr& typeInfo) const override;
  173. /** @copydoc ManagedSerializableTypeInfo::isTypeLoaded */
  174. bool isTypeLoaded() const override;
  175. /** @copydoc ManagedSerializableTypeInfo::getMonoClass */
  176. ::MonoClass* getMonoClass() const override;
  177. ManagedSerializableTypeInfoPtr mElementType;
  178. /************************************************************************/
  179. /* RTTI */
  180. /************************************************************************/
  181. public:
  182. friend class ManagedSerializableTypeInfoListRTTI;
  183. static RTTITypeBase* getRTTIStatic();
  184. virtual RTTITypeBase* getRTTI() const override;
  185. };
  186. /** Contains information about a type of a managed serializable Dictionary. */
  187. class BS_SCR_BE_EXPORT ManagedSerializableTypeInfoDictionary : public ManagedSerializableTypeInfo
  188. {
  189. public:
  190. /** @copydoc ManagedSerializableTypeInfo::matches */
  191. bool matches(const ManagedSerializableTypeInfoPtr& typeInfo) const override;
  192. /** @copydoc ManagedSerializableTypeInfo::isTypeLoaded */
  193. bool isTypeLoaded() const override;
  194. /** @copydoc ManagedSerializableTypeInfo::getMonoClass */
  195. ::MonoClass* getMonoClass() const override;
  196. ManagedSerializableTypeInfoPtr mKeyType;
  197. ManagedSerializableTypeInfoPtr mValueType;
  198. /************************************************************************/
  199. /* RTTI */
  200. /************************************************************************/
  201. public:
  202. friend class ManagedSerializableTypeInfoDictionaryRTTI;
  203. static RTTITypeBase* getRTTIStatic();
  204. virtual RTTITypeBase* getRTTI() const override;
  205. };
  206. /** Contains data about a single field in a managed complex object. */
  207. class BS_SCR_BE_EXPORT ManagedSerializableFieldInfo : public IReflectable
  208. {
  209. public:
  210. ManagedSerializableFieldInfo();
  211. /** Determines should the field be serialized when serializing the parent object. */
  212. bool isSerializable() const { return ((UINT32)mFlags & (UINT32)ScriptFieldFlags::Serializable) != 0; }
  213. String mName;
  214. UINT32 mFieldId;
  215. UINT32 mParentTypeId;
  216. ManagedSerializableTypeInfoPtr mTypeInfo;
  217. ScriptFieldFlags mFlags;
  218. MonoField* mMonoField;
  219. /************************************************************************/
  220. /* RTTI */
  221. /************************************************************************/
  222. public:
  223. friend class ManagedSerializableFieldInfoRTTI;
  224. static RTTITypeBase* getRTTIStatic();
  225. virtual RTTITypeBase* getRTTI() const override;
  226. };
  227. /** Contains data about fields of a complex object, and the object's class hierarchy if it belongs to one. */
  228. class BS_SCR_BE_EXPORT ManagedSerializableObjectInfo : public IReflectable
  229. {
  230. public:
  231. ManagedSerializableObjectInfo();
  232. /** Returns the managed type name of the object's type, including the namespace in format "namespace.typename". */
  233. String getFullTypeName() const { return mTypeInfo->mTypeNamespace + "." + mTypeInfo->mTypeName; }
  234. /**
  235. * Attempts to find a field part of this object that matches the provided parameters.
  236. *
  237. * @param[in] fieldInfo Object describing the managed field. Normally this will be a field that was
  238. * deserialized and you need to ensure it still exists in its parent type, while
  239. * retrieving the new field info.
  240. * @param[in] fieldTypeInfo Type information about the type containing the object. Used for debug purposes to
  241. * ensure the current object's type matches.
  242. * @return Found field info within this object, or null if not found.
  243. */
  244. ManagedSerializableFieldInfoPtr findMatchingField(const ManagedSerializableFieldInfoPtr& fieldInfo,
  245. const ManagedSerializableTypeInfoPtr& fieldTypeInfo) const;
  246. ManagedSerializableTypeInfoObjectPtr mTypeInfo;
  247. MonoClass* mMonoClass;
  248. UnorderedMap<String, UINT32> mFieldNameToId;
  249. UnorderedMap<UINT32, std::shared_ptr<ManagedSerializableFieldInfo>> mFields;
  250. std::shared_ptr<ManagedSerializableObjectInfo> mBaseClass;
  251. Vector<std::weak_ptr<ManagedSerializableObjectInfo>> mDerivedClasses;
  252. /************************************************************************/
  253. /* RTTI */
  254. /************************************************************************/
  255. public:
  256. friend class ManagedSerializableObjectInfoRTTI;
  257. static RTTITypeBase* getRTTIStatic();
  258. virtual RTTITypeBase* getRTTI() const override;
  259. };
  260. /** Contains information about all managed serializable objects in a specific managed assembly. */
  261. class BS_SCR_BE_EXPORT ManagedSerializableAssemblyInfo : public IReflectable
  262. {
  263. public:
  264. String mName;
  265. UnorderedMap<String, UINT32> mTypeNameToId;
  266. UnorderedMap<UINT32, std::shared_ptr<ManagedSerializableObjectInfo>> mObjectInfos;
  267. /************************************************************************/
  268. /* RTTI */
  269. /************************************************************************/
  270. public:
  271. friend class ManagedSerializableAssemblyInfoRTTI;
  272. static RTTITypeBase* getRTTIStatic();
  273. virtual RTTITypeBase* getRTTI() const override;
  274. };
  275. /** @} */
  276. }