BsManagedComponent.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "Scene/BsComponent.h"
  6. namespace bs
  7. {
  8. /** @addtogroup SBansheeEngine
  9. * @{
  10. */
  11. struct ComponentBackupData;
  12. /**
  13. * Component that internally wraps a Mono component object that can be of user-defined type. Acts as interop
  14. * layer between native Component and a Mono user defined component type since managed types cannot simply derive
  15. * from Component to implement its functionality.
  16. */
  17. class BS_SCR_BE_EXPORT ManagedComponent : public Component
  18. {
  19. public:
  20. ~ManagedComponent();
  21. /** Returns managed component object instance. */
  22. MonoObject* getManagedInstance() const { return mManagedInstance; }
  23. /** Returns managed class of the component. */
  24. MonoClass* getClass() const { return mManagedClass; }
  25. /** Returns managed type of the component. */
  26. MonoReflectionType* getRuntimeType() const { return mRuntimeType; }
  27. /** Returns namespace of the managed component. */
  28. const String& getManagedNamespace() const { return mNamespace; }
  29. /** Returns type name of the managed component. */
  30. const String& getManagedTypeName() const { return mTypeName; }
  31. /** Returns namespace and type name of the component in format "namespace.typename". */
  32. const String& getManagedFullTypeName() const { return mFullTypeName; }
  33. /**
  34. * Serializes the internal managed component.
  35. *
  36. * @param[in] clearExisting Should the managed component handle be released. (Will trigger a finalizer if this
  37. * is the last reference to it)
  38. * @return An object containing the serialized component. You can provide this to restore()
  39. * method to re-create the original component.
  40. */
  41. ComponentBackupData backup(bool clearExisting = true);
  42. /**
  43. * Restores a component from previously serialized data.
  44. *
  45. * @param[in] instance New instance of the managed component. Must be of the valid component type or of
  46. * BansheeEngine.MissingComponent type if the original type is missing.
  47. * @param[in] data Serialized managed component data that will be used for initializing the new managed
  48. * instance.
  49. * @param[in] missingType Is the component's type missing (can happen after assembly reload). If true then the
  50. * serialized data will be stored internally until later date when user perhaps restores
  51. * the type with another refresh. @p instance must be null if this is true.
  52. */
  53. void restore(MonoObject* instance, const ComponentBackupData& data, bool missingType);
  54. /** Triggers the managed OnReset callback. */
  55. void triggerOnReset();
  56. private:
  57. /**
  58. * Finalizes construction of the object. Must be called before use or when the managed component instance changes.
  59. *
  60. * @param[in] object Managed component instance.
  61. */
  62. void initialize(MonoObject* object);
  63. typedef void(BS_THUNKCALL *OnCreatedThunkDef) (MonoObject*, MonoException**);
  64. typedef void(BS_THUNKCALL *OnInitializedThunkDef) (MonoObject*, MonoException**);
  65. typedef void(BS_THUNKCALL *OnUpdateThunkDef) (MonoObject*, MonoException**);
  66. typedef void(BS_THUNKCALL *OnDestroyedThunkDef) (MonoObject*, MonoException**);
  67. typedef void(BS_THUNKCALL *OnResetThunkDef) (MonoObject*, MonoException**);
  68. typedef void(BS_THUNKCALL *OnEnabledThunkDef) (MonoObject*, MonoException**);
  69. typedef void(BS_THUNKCALL *OnDisabledThunkDef) (MonoObject*, MonoException**);
  70. typedef void(BS_THUNKCALL *OnTransformChangedThunkDef) (MonoObject*, TransformChangedFlags, MonoException**);
  71. MonoObject* mManagedInstance;
  72. MonoClass* mManagedClass;
  73. MonoReflectionType* mRuntimeType;
  74. uint32_t mManagedHandle;
  75. String mNamespace;
  76. String mTypeName;
  77. String mFullTypeName;
  78. bool mRequiresReset;
  79. bool mMissingType;
  80. SPtr<ManagedSerializableObject> mSerializedObjectData;
  81. SPtr<ManagedSerializableObjectInfo> mObjInfo; // Transient
  82. OnCreatedThunkDef mOnCreatedThunk;
  83. OnInitializedThunkDef mOnInitializedThunk;
  84. OnUpdateThunkDef mOnUpdateThunk;
  85. OnResetThunkDef mOnResetThunk;
  86. OnDestroyedThunkDef mOnDestroyThunk;
  87. OnDestroyedThunkDef mOnDisabledThunk;
  88. OnDestroyedThunkDef mOnEnabledThunk;
  89. OnTransformChangedThunkDef mOnTransformChangedThunk;
  90. MonoMethod* mCalculateBoundsMethod;
  91. /************************************************************************/
  92. /* COMPONENT OVERRIDES */
  93. /************************************************************************/
  94. protected:
  95. friend class SceneObject;
  96. friend class ScriptComponent;
  97. ManagedComponent(const HSceneObject& parent, MonoReflectionType* runtimeType);
  98. /** @copydoc Component::_instantiate */
  99. void _instantiate() override;
  100. /** @copydoc Component::onCreated */
  101. void onCreated() override;
  102. /** @copydoc Component::onInitialized */
  103. void onInitialized() override;
  104. /** @copydoc Component::onDestroyed */
  105. void onDestroyed() override;
  106. /** @copydoc Component::onEnabled */
  107. void onEnabled() override;
  108. /** @copydoc Component::onDisabled */
  109. void onDisabled() override;
  110. /** @copydoc Component::onTransformChanged */
  111. void onTransformChanged(TransformChangedFlags flags) override;
  112. public:
  113. /** @copydoc Component::update */
  114. void update() override;
  115. /** @copydoc Component::typeEquals */
  116. bool typeEquals(const Component& other) override;
  117. /** @copydoc Component::calculateBounds */
  118. bool calculateBounds(Bounds& bounds) override;
  119. /************************************************************************/
  120. /* RTTI */
  121. /************************************************************************/
  122. public:
  123. friend class ManagedComponentRTTI;
  124. static RTTITypeBase* getRTTIStatic();
  125. RTTITypeBase* getRTTI() const override;
  126. protected:
  127. ManagedComponent(); // Serialization only
  128. };
  129. /** Contains serialized component data buffer. */
  130. struct ComponentBackupData
  131. {
  132. UINT8* data;
  133. UINT32 size;
  134. };
  135. /** @} */
  136. }