BsManagedSerializableObjectInfo.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 ScriptFieldFlag
  59. {
  60. Serializable = 0x01,
  61. Inspectable = 0x02,
  62. Range = 0x04,
  63. Step = 0x08,
  64. Animable = 0x10
  65. };
  66. typedef Flags<ScriptFieldFlag> ScriptFieldFlags;
  67. BS_FLAGS_OPERATORS(ScriptFieldFlag);
  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() {}
  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 game object or resource reference. */
  113. class BS_SCR_BE_EXPORT ManagedSerializableTypeInfoRef : 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. ScriptReferenceType mType;
  123. String mTypeNamespace;
  124. String mTypeName;
  125. /************************************************************************/
  126. /* RTTI */
  127. /************************************************************************/
  128. public:
  129. friend class ManagedSerializableTypeInfoRefRTTI;
  130. static RTTITypeBase* getRTTIStatic();
  131. RTTITypeBase* getRTTI() const override;
  132. };
  133. /** Contains information about a type of a managed serializable complex object (for example struct or class). */
  134. class BS_SCR_BE_EXPORT ManagedSerializableTypeInfoObject : 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. String mTypeNamespace;
  144. String mTypeName;
  145. bool mValueType;
  146. UINT32 mTypeId;
  147. /************************************************************************/
  148. /* RTTI */
  149. /************************************************************************/
  150. public:
  151. friend class ManagedSerializableTypeInfoObjectRTTI;
  152. static RTTITypeBase* getRTTIStatic();
  153. RTTITypeBase* getRTTI() const override;
  154. };
  155. /** Contains information about a type of a managed serializable Array. */
  156. class BS_SCR_BE_EXPORT ManagedSerializableTypeInfoArray : 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> mElementType;
  166. UINT32 mRank;
  167. /************************************************************************/
  168. /* RTTI */
  169. /************************************************************************/
  170. public:
  171. friend class ManagedSerializableTypeInfoArrayRTTI;
  172. static RTTITypeBase* getRTTIStatic();
  173. RTTITypeBase* getRTTI() const override;
  174. };
  175. /** Contains information about a type of a managed serializable List. */
  176. class BS_SCR_BE_EXPORT ManagedSerializableTypeInfoList : public ManagedSerializableTypeInfo
  177. {
  178. public:
  179. /** @copydoc ManagedSerializableTypeInfo::matches */
  180. bool matches(const SPtr<ManagedSerializableTypeInfo>& typeInfo) const override;
  181. /** @copydoc ManagedSerializableTypeInfo::isTypeLoaded */
  182. bool isTypeLoaded() const override;
  183. /** @copydoc ManagedSerializableTypeInfo::getMonoClass */
  184. ::MonoClass* getMonoClass() const override;
  185. SPtr<ManagedSerializableTypeInfo> mElementType;
  186. /************************************************************************/
  187. /* RTTI */
  188. /************************************************************************/
  189. public:
  190. friend class ManagedSerializableTypeInfoListRTTI;
  191. static RTTITypeBase* getRTTIStatic();
  192. RTTITypeBase* getRTTI() const override;
  193. };
  194. /** Contains information about a type of a managed serializable Dictionary. */
  195. class BS_SCR_BE_EXPORT ManagedSerializableTypeInfoDictionary : public ManagedSerializableTypeInfo
  196. {
  197. public:
  198. /** @copydoc ManagedSerializableTypeInfo::matches */
  199. bool matches(const SPtr<ManagedSerializableTypeInfo>& typeInfo) const override;
  200. /** @copydoc ManagedSerializableTypeInfo::isTypeLoaded */
  201. bool isTypeLoaded() const override;
  202. /** @copydoc ManagedSerializableTypeInfo::getMonoClass */
  203. ::MonoClass* getMonoClass() const override;
  204. SPtr<ManagedSerializableTypeInfo> mKeyType;
  205. SPtr<ManagedSerializableTypeInfo> mValueType;
  206. /************************************************************************/
  207. /* RTTI */
  208. /************************************************************************/
  209. public:
  210. friend class ManagedSerializableTypeInfoDictionaryRTTI;
  211. static RTTITypeBase* getRTTIStatic();
  212. RTTITypeBase* getRTTI() const override;
  213. };
  214. /** Contains data about a single member in a managed complex object. */
  215. class BS_SCR_BE_EXPORT ManagedSerializableMemberInfo : public IReflectable
  216. {
  217. public:
  218. ManagedSerializableMemberInfo();
  219. virtual ~ManagedSerializableMemberInfo() {}
  220. /** Determines should the member be serialized when serializing the parent object. */
  221. bool isSerializable() const { return mFlags.isSet(ScriptFieldFlag::Serializable); }
  222. /** Returns the minimum value associated to a Range attribute. */
  223. virtual float getRangeMinimum() const = 0;
  224. /** Returns the maximum value associated to a Range attribute. */
  225. virtual float getRangeMaximum() const = 0;
  226. /** Checks whether the field should be rendered as a slider. */
  227. virtual bool renderAsSlider() const = 0;
  228. /** Returns the step value of the member. */
  229. virtual float getStep() const = 0;
  230. /**
  231. * Returns a boxed value contained in the member in the specified object instance.
  232. *
  233. * @param[in] instance Object instance to access the member on.
  234. * @return A boxed value of the member.
  235. */
  236. virtual MonoObject* getValue(MonoObject* instance) const = 0;
  237. /**
  238. * Sets a value of the member in the specified object instance.
  239. *
  240. * @param[in] instance Object instance to access the member on.
  241. * @param[in] value Value to set on the property. For value type it should be a pointer to the value and for
  242. * reference type it should be a pointer to MonoObject.
  243. */
  244. virtual void setValue(MonoObject* instance, void* value) const = 0;
  245. String mName;
  246. UINT32 mFieldId;
  247. UINT32 mParentTypeId;
  248. SPtr<ManagedSerializableTypeInfo> mTypeInfo;
  249. ScriptFieldFlags mFlags;
  250. /************************************************************************/
  251. /* RTTI */
  252. /************************************************************************/
  253. public:
  254. friend class ManagedSerializableMemberInfoRTTI;
  255. static RTTITypeBase* getRTTIStatic();
  256. RTTITypeBase* getRTTI() const override;
  257. };
  258. /** Contains data about a single field in a managed complex object. */
  259. class BS_SCR_BE_EXPORT ManagedSerializableFieldInfo : public ManagedSerializableMemberInfo
  260. {
  261. public:
  262. ManagedSerializableFieldInfo();
  263. /** @copydoc ManagedSerializableMemberInfo::getRangeMinimum */
  264. float getRangeMinimum() const override;
  265. /** @copydoc ManagedSerializableMemberInfo::getRangeMaximum */
  266. float getRangeMaximum() const override;
  267. /** @copydoc ManagedSerializableMemberInfo::renderAsSlider */
  268. bool renderAsSlider() const override;
  269. /** @copydoc ManagedSerializableMemberInfo::getStep */
  270. float getStep() const override;
  271. /** @copydoc ManagedSerializableMemberInfo::getValue */
  272. MonoObject* getValue(MonoObject* instance) const override;
  273. /** @copydoc ManagedSerializableMemberInfo::setValue */
  274. void setValue(MonoObject* instance, void* value) const override;
  275. MonoField* mMonoField;
  276. /************************************************************************/
  277. /* RTTI */
  278. /************************************************************************/
  279. public:
  280. friend class ManagedSerializableFieldInfoRTTI;
  281. static RTTITypeBase* getRTTIStatic();
  282. RTTITypeBase* getRTTI() const override;
  283. };
  284. /** Contains data about a single property in a managed complex object. */
  285. class BS_SCR_BE_EXPORT ManagedSerializablePropertyInfo : public ManagedSerializableMemberInfo
  286. {
  287. public:
  288. ManagedSerializablePropertyInfo();
  289. /** @copydoc ManagedSerializableMemberInfo::getRangeMinimum */
  290. float getRangeMinimum() const override;
  291. /** @copydoc ManagedSerializableMemberInfo::getRangeMaximum */
  292. float getRangeMaximum() const override;
  293. /** @copydoc ManagedSerializableMemberInfo::renderAsSlider */
  294. bool renderAsSlider() const override;
  295. /** @copydoc ManagedSerializableMemberInfo::getStep */
  296. float getStep() const override;
  297. /** @copydoc ManagedSerializableMemberInfo::getValue */
  298. MonoObject* getValue(MonoObject* instance) const override;
  299. /** @copydoc ManagedSerializableMemberInfo::setValue */
  300. void setValue(MonoObject* instance, void* value) const override;
  301. MonoProperty* mMonoProperty;
  302. /************************************************************************/
  303. /* RTTI */
  304. /************************************************************************/
  305. public:
  306. friend class ManagedSerializablePropertyInfoRTTI;
  307. static RTTITypeBase* getRTTIStatic();
  308. RTTITypeBase* getRTTI() const override;
  309. };
  310. /** Contains data about fields of a complex object, and the object's class hierarchy if it belongs to one. */
  311. class BS_SCR_BE_EXPORT ManagedSerializableObjectInfo : public IReflectable
  312. {
  313. public:
  314. ManagedSerializableObjectInfo();
  315. /** Returns the managed type name of the object's type, including the namespace in format "namespace.typename". */
  316. String getFullTypeName() const { return mTypeInfo->mTypeNamespace + "." + mTypeInfo->mTypeName; }
  317. /**
  318. * Attempts to find a field part of this object that matches the provided parameters.
  319. *
  320. * @param[in] fieldInfo Object describing the managed field. Normally this will be a field that was
  321. * deserialized and you need to ensure it still exists in its parent type, while
  322. * retrieving the new field info.
  323. * @param[in] fieldTypeInfo Type information about the type containing the object. Used for debug purposes to
  324. * ensure the current object's type matches.
  325. * @return Found field info within this object, or null if not found.
  326. */
  327. SPtr<ManagedSerializableMemberInfo> findMatchingField(const SPtr<ManagedSerializableMemberInfo>& fieldInfo,
  328. const SPtr<ManagedSerializableTypeInfo>& fieldTypeInfo) const;
  329. SPtr<ManagedSerializableTypeInfoObject> mTypeInfo;
  330. MonoClass* mMonoClass;
  331. UnorderedMap<String, UINT32> mFieldNameToId;
  332. UnorderedMap<UINT32, SPtr<ManagedSerializableMemberInfo>> mFields;
  333. SPtr<ManagedSerializableObjectInfo> mBaseClass;
  334. Vector<std::weak_ptr<ManagedSerializableObjectInfo>> mDerivedClasses;
  335. /************************************************************************/
  336. /* RTTI */
  337. /************************************************************************/
  338. public:
  339. friend class ManagedSerializableObjectInfoRTTI;
  340. static RTTITypeBase* getRTTIStatic();
  341. RTTITypeBase* getRTTI() const override;
  342. };
  343. /** Contains information about all managed serializable objects in a specific managed assembly. */
  344. class BS_SCR_BE_EXPORT ManagedSerializableAssemblyInfo : public IReflectable
  345. {
  346. public:
  347. String mName;
  348. UnorderedMap<String, UINT32> mTypeNameToId;
  349. UnorderedMap<UINT32, SPtr<ManagedSerializableObjectInfo>> mObjectInfos;
  350. /************************************************************************/
  351. /* RTTI */
  352. /************************************************************************/
  353. public:
  354. friend class ManagedSerializableAssemblyInfoRTTI;
  355. static RTTITypeBase* getRTTIStatic();
  356. RTTITypeBase* getRTTI() const override;
  357. };
  358. /** @} */
  359. }