BsManagedSerializableObjectInfo.h 16 KB

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