BsManagedSerializableObjectInfo.h 12 KB

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