BsManagedSerializableObjectInfo.h 12 KB

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