BsManagedSerializableObjectInfo.h 15 KB

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