BsManagedSerializableObjectInfo.h 12 KB

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