BsManagedSerializableObjectInfo.h 17 KB

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