BsManagedSerializableObjectInfo.h 12 KB

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