BsManagedSerializableObjectInfo.h 17 KB

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