BsManagedSerializableObjectInfo.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 "Reflection/BsIReflectable.h"
  6. namespace bs
  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. BuiltinResourceBase,
  33. BuiltinResource,
  34. ManagedResourceBase,
  35. ManagedResource,
  36. BuiltinComponentBase,
  37. BuiltinComponent,
  38. ManagedComponentBase,
  39. ManagedComponent,
  40. SceneObject,
  41. Count // Keep at end
  42. };
  43. /** Flags that are used to further define a field in a managed serializable object. */
  44. enum class ScriptFieldFlag
  45. {
  46. Serializable = 0x01,
  47. Inspectable = 0x02,
  48. Range = 0x04,
  49. Step = 0x08,
  50. Animable = 0x10
  51. };
  52. typedef Flags<ScriptFieldFlag> ScriptFieldFlags;
  53. BS_FLAGS_OPERATORS(ScriptFieldFlag);
  54. /** Contains information about a type of a managed serializable object. */
  55. class BS_SCR_BE_EXPORT ManagedSerializableTypeInfo : public IReflectable
  56. {
  57. public:
  58. virtual ~ManagedSerializableTypeInfo() {}
  59. /** Checks if the current type matches the provided type. */
  60. virtual bool matches(const SPtr<ManagedSerializableTypeInfo>& typeInfo) const = 0;
  61. /**
  62. * Checks does the managed type this object represents still exists.
  63. *
  64. * @note For example if assemblies get refreshed user could have renamed or removed some types.
  65. */
  66. virtual bool isTypeLoaded() const = 0;
  67. /**
  68. * Returns the internal managed class of the type this object represents. Returns null if the type doesn't exist.
  69. */
  70. virtual ::MonoClass* getMonoClass() const = 0;
  71. /************************************************************************/
  72. /* RTTI */
  73. /************************************************************************/
  74. public:
  75. friend class ManagedSerializableTypeInfoRTTI;
  76. static RTTITypeBase* getRTTIStatic();
  77. RTTITypeBase* getRTTI() const override;
  78. };
  79. /** Contains information about a type of a managed serializable primitive (for example int, float, etc.). */
  80. class BS_SCR_BE_EXPORT ManagedSerializableTypeInfoPrimitive : public ManagedSerializableTypeInfo
  81. {
  82. public:
  83. /** @copydoc ManagedSerializableTypeInfo::matches */
  84. bool matches(const SPtr<ManagedSerializableTypeInfo>& typeInfo) const override;
  85. /** @copydoc ManagedSerializableTypeInfo::isTypeLoaded */
  86. bool isTypeLoaded() const override;
  87. /** @copydoc ManagedSerializableTypeInfo::getMonoClass */
  88. ::MonoClass* getMonoClass() const override;
  89. ScriptPrimitiveType mType;
  90. /************************************************************************/
  91. /* RTTI */
  92. /************************************************************************/
  93. public:
  94. friend class ManagedSerializableTypeInfoPrimitiveRTTI;
  95. static RTTITypeBase* getRTTIStatic();
  96. RTTITypeBase* getRTTI() const override;
  97. };
  98. /** Contains information about a type of a managed serializable game object or resource reference. */
  99. class BS_SCR_BE_EXPORT ManagedSerializableTypeInfoRef : public ManagedSerializableTypeInfo
  100. {
  101. public:
  102. /** @copydoc ManagedSerializableTypeInfo::matches */
  103. bool matches(const SPtr<ManagedSerializableTypeInfo>& typeInfo) const override;
  104. /** @copydoc ManagedSerializableTypeInfo::isTypeLoaded */
  105. bool isTypeLoaded() const override;
  106. /** @copydoc ManagedSerializableTypeInfo::getMonoClass */
  107. ::MonoClass* getMonoClass() const override;
  108. ScriptReferenceType mType;
  109. UINT32 mRTIITypeId;
  110. String mTypeNamespace;
  111. String mTypeName;
  112. /************************************************************************/
  113. /* RTTI */
  114. /************************************************************************/
  115. public:
  116. friend class ManagedSerializableTypeInfoRefRTTI;
  117. static RTTITypeBase* getRTTIStatic();
  118. RTTITypeBase* getRTTI() const override;
  119. };
  120. /** Contains information about a type of a managed serializable complex object (for example struct or class). */
  121. class BS_SCR_BE_EXPORT ManagedSerializableTypeInfoObject : public ManagedSerializableTypeInfo
  122. {
  123. public:
  124. /** @copydoc ManagedSerializableTypeInfo::matches */
  125. bool matches(const SPtr<ManagedSerializableTypeInfo>& typeInfo) const override;
  126. /** @copydoc ManagedSerializableTypeInfo::isTypeLoaded */
  127. bool isTypeLoaded() const override;
  128. /** @copydoc ManagedSerializableTypeInfo::getMonoClass */
  129. ::MonoClass* getMonoClass() const override;
  130. String mTypeNamespace;
  131. String mTypeName;
  132. bool mValueType;
  133. UINT32 mTypeId;
  134. /************************************************************************/
  135. /* RTTI */
  136. /************************************************************************/
  137. public:
  138. friend class ManagedSerializableTypeInfoObjectRTTI;
  139. static RTTITypeBase* getRTTIStatic();
  140. RTTITypeBase* getRTTI() const override;
  141. };
  142. /** Contains information about a type of a managed serializable Array. */
  143. class BS_SCR_BE_EXPORT ManagedSerializableTypeInfoArray : public ManagedSerializableTypeInfo
  144. {
  145. public:
  146. /** @copydoc ManagedSerializableTypeInfo::matches */
  147. bool matches(const SPtr<ManagedSerializableTypeInfo>& typeInfo) const override;
  148. /** @copydoc ManagedSerializableTypeInfo::isTypeLoaded */
  149. bool isTypeLoaded() const override;
  150. /** @copydoc ManagedSerializableTypeInfo::getMonoClass */
  151. ::MonoClass* getMonoClass() const override;
  152. SPtr<ManagedSerializableTypeInfo> mElementType;
  153. UINT32 mRank;
  154. /************************************************************************/
  155. /* RTTI */
  156. /************************************************************************/
  157. public:
  158. friend class ManagedSerializableTypeInfoArrayRTTI;
  159. static RTTITypeBase* getRTTIStatic();
  160. RTTITypeBase* getRTTI() const override;
  161. };
  162. /** Contains information about a type of a managed serializable List. */
  163. class BS_SCR_BE_EXPORT ManagedSerializableTypeInfoList : public ManagedSerializableTypeInfo
  164. {
  165. public:
  166. /** @copydoc ManagedSerializableTypeInfo::matches */
  167. bool matches(const SPtr<ManagedSerializableTypeInfo>& typeInfo) const override;
  168. /** @copydoc ManagedSerializableTypeInfo::isTypeLoaded */
  169. bool isTypeLoaded() const override;
  170. /** @copydoc ManagedSerializableTypeInfo::getMonoClass */
  171. ::MonoClass* getMonoClass() const override;
  172. SPtr<ManagedSerializableTypeInfo> mElementType;
  173. /************************************************************************/
  174. /* RTTI */
  175. /************************************************************************/
  176. public:
  177. friend class ManagedSerializableTypeInfoListRTTI;
  178. static RTTITypeBase* getRTTIStatic();
  179. RTTITypeBase* getRTTI() const override;
  180. };
  181. /** Contains information about a type of a managed serializable Dictionary. */
  182. class BS_SCR_BE_EXPORT ManagedSerializableTypeInfoDictionary : public ManagedSerializableTypeInfo
  183. {
  184. public:
  185. /** @copydoc ManagedSerializableTypeInfo::matches */
  186. bool matches(const SPtr<ManagedSerializableTypeInfo>& typeInfo) const override;
  187. /** @copydoc ManagedSerializableTypeInfo::isTypeLoaded */
  188. bool isTypeLoaded() const override;
  189. /** @copydoc ManagedSerializableTypeInfo::getMonoClass */
  190. ::MonoClass* getMonoClass() const override;
  191. SPtr<ManagedSerializableTypeInfo> mKeyType;
  192. SPtr<ManagedSerializableTypeInfo> mValueType;
  193. /************************************************************************/
  194. /* RTTI */
  195. /************************************************************************/
  196. public:
  197. friend class ManagedSerializableTypeInfoDictionaryRTTI;
  198. static RTTITypeBase* getRTTIStatic();
  199. RTTITypeBase* getRTTI() const override;
  200. };
  201. /** Contains data about a single member in a managed complex object. */
  202. class BS_SCR_BE_EXPORT ManagedSerializableMemberInfo : public IReflectable
  203. {
  204. public:
  205. ManagedSerializableMemberInfo();
  206. virtual ~ManagedSerializableMemberInfo() {}
  207. /** Determines should the member be serialized when serializing the parent object. */
  208. bool isSerializable() const { return mFlags.isSet(ScriptFieldFlag::Serializable); }
  209. /** Returns the minimum value associated to a Range attribute. */
  210. virtual float getRangeMinimum() const = 0;
  211. /** Returns the maximum value associated to a Range attribute. */
  212. virtual float getRangeMaximum() const = 0;
  213. /** Checks whether the field should be rendered as a slider. */
  214. virtual bool renderAsSlider() const = 0;
  215. /** Returns the step value of the member. */
  216. virtual float getStep() const = 0;
  217. /**
  218. * Returns a boxed value contained in the member in the specified object instance.
  219. *
  220. * @param[in] instance Object instance to access the member on.
  221. * @return A boxed value of the member.
  222. */
  223. virtual MonoObject* getValue(MonoObject* instance) const = 0;
  224. /**
  225. * Sets a value of the member in the specified object instance.
  226. *
  227. * @param[in] instance Object instance to access the member on.
  228. * @param[in] value Value to set on the property. For value type it should be a pointer to the value and for
  229. * reference type it should be a pointer to MonoObject.
  230. */
  231. virtual void setValue(MonoObject* instance, void* value) const = 0;
  232. String mName;
  233. UINT32 mFieldId;
  234. UINT32 mParentTypeId;
  235. SPtr<ManagedSerializableTypeInfo> mTypeInfo;
  236. ScriptFieldFlags mFlags;
  237. /************************************************************************/
  238. /* RTTI */
  239. /************************************************************************/
  240. public:
  241. friend class ManagedSerializableMemberInfoRTTI;
  242. static RTTITypeBase* getRTTIStatic();
  243. RTTITypeBase* getRTTI() const override;
  244. };
  245. /** Contains data about a single field in a managed complex object. */
  246. class BS_SCR_BE_EXPORT ManagedSerializableFieldInfo : public ManagedSerializableMemberInfo
  247. {
  248. public:
  249. ManagedSerializableFieldInfo();
  250. /** @copydoc ManagedSerializableMemberInfo::getRangeMinimum */
  251. float getRangeMinimum() const override;
  252. /** @copydoc ManagedSerializableMemberInfo::getRangeMaximum */
  253. float getRangeMaximum() const override;
  254. /** @copydoc ManagedSerializableMemberInfo::renderAsSlider */
  255. bool renderAsSlider() const override;
  256. /** @copydoc ManagedSerializableMemberInfo::getStep */
  257. float getStep() const override;
  258. /** @copydoc ManagedSerializableMemberInfo::getValue */
  259. MonoObject* getValue(MonoObject* instance) const override;
  260. /** @copydoc ManagedSerializableMemberInfo::setValue */
  261. void setValue(MonoObject* instance, void* value) const override;
  262. MonoField* mMonoField;
  263. /************************************************************************/
  264. /* RTTI */
  265. /************************************************************************/
  266. public:
  267. friend class ManagedSerializableFieldInfoRTTI;
  268. static RTTITypeBase* getRTTIStatic();
  269. RTTITypeBase* getRTTI() const override;
  270. };
  271. /** Contains data about a single property in a managed complex object. */
  272. class BS_SCR_BE_EXPORT ManagedSerializablePropertyInfo : public ManagedSerializableMemberInfo
  273. {
  274. public:
  275. ManagedSerializablePropertyInfo();
  276. /** @copydoc ManagedSerializableMemberInfo::getRangeMinimum */
  277. float getRangeMinimum() const override;
  278. /** @copydoc ManagedSerializableMemberInfo::getRangeMaximum */
  279. float getRangeMaximum() const override;
  280. /** @copydoc ManagedSerializableMemberInfo::renderAsSlider */
  281. bool renderAsSlider() const override;
  282. /** @copydoc ManagedSerializableMemberInfo::getStep */
  283. float getStep() const override;
  284. /** @copydoc ManagedSerializableMemberInfo::getValue */
  285. MonoObject* getValue(MonoObject* instance) const override;
  286. /** @copydoc ManagedSerializableMemberInfo::setValue */
  287. void setValue(MonoObject* instance, void* value) const override;
  288. MonoProperty* mMonoProperty;
  289. /************************************************************************/
  290. /* RTTI */
  291. /************************************************************************/
  292. public:
  293. friend class ManagedSerializablePropertyInfoRTTI;
  294. static RTTITypeBase* getRTTIStatic();
  295. RTTITypeBase* getRTTI() const override;
  296. };
  297. /** Contains data about fields of a complex object, and the object's class hierarchy if it belongs to one. */
  298. class BS_SCR_BE_EXPORT ManagedSerializableObjectInfo : public IReflectable
  299. {
  300. public:
  301. ManagedSerializableObjectInfo();
  302. /** Returns the managed type name of the object's type, including the namespace in format "namespace.typename". */
  303. String getFullTypeName() const { return mTypeInfo->mTypeNamespace + "." + mTypeInfo->mTypeName; }
  304. /**
  305. * Attempts to find a field part of this object that matches the provided parameters.
  306. *
  307. * @param[in] fieldInfo Object describing the managed field. Normally this will be a field that was
  308. * deserialized and you need to ensure it still exists in its parent type, while
  309. * retrieving the new field info.
  310. * @param[in] fieldTypeInfo Type information about the type containing the object. Used for debug purposes to
  311. * ensure the current object's type matches.
  312. * @return Found field info within this object, or null if not found.
  313. */
  314. SPtr<ManagedSerializableMemberInfo> findMatchingField(const SPtr<ManagedSerializableMemberInfo>& fieldInfo,
  315. const SPtr<ManagedSerializableTypeInfo>& fieldTypeInfo) const;
  316. SPtr<ManagedSerializableTypeInfoObject> mTypeInfo;
  317. MonoClass* mMonoClass;
  318. UnorderedMap<String, UINT32> mFieldNameToId;
  319. UnorderedMap<UINT32, SPtr<ManagedSerializableMemberInfo>> mFields;
  320. SPtr<ManagedSerializableObjectInfo> mBaseClass;
  321. Vector<std::weak_ptr<ManagedSerializableObjectInfo>> mDerivedClasses;
  322. /************************************************************************/
  323. /* RTTI */
  324. /************************************************************************/
  325. public:
  326. friend class ManagedSerializableObjectInfoRTTI;
  327. static RTTITypeBase* getRTTIStatic();
  328. RTTITypeBase* getRTTI() const override;
  329. };
  330. /** Contains information about all managed serializable objects in a specific managed assembly. */
  331. class BS_SCR_BE_EXPORT ManagedSerializableAssemblyInfo : public IReflectable
  332. {
  333. public:
  334. String mName;
  335. UnorderedMap<String, UINT32> mTypeNameToId;
  336. UnorderedMap<UINT32, SPtr<ManagedSerializableObjectInfo>> mObjectInfos;
  337. /************************************************************************/
  338. /* RTTI */
  339. /************************************************************************/
  340. public:
  341. friend class ManagedSerializableAssemblyInfoRTTI;
  342. static RTTITypeBase* getRTTIStatic();
  343. RTTITypeBase* getRTTI() const override;
  344. };
  345. /** @} */
  346. }