BsManagedSerializableObjectInfo.h 15 KB

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