BsManagedSerializableObjectInfo.h 12 KB

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