BsManagedComponent.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #include "BsScriptObject.h"
  7. namespace bs
  8. {
  9. /** @addtogroup SBansheeEngine
  10. * @{
  11. */
  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;
  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. RawBackupData backup(bool clearExisting = true);
  42. /**
  43. * Restores a component from previously serialized data.
  44. *
  45. * @param[in] data Serialized managed component data that will be used for initializing the new managed
  46. * instance.
  47. * @param[in] missingType Is the component's type missing (can happen after assembly reload). If true then the
  48. * serialized data will be stored internally until later date when user perhaps restores
  49. * the type with another refresh. @p instance must be null if this is true.
  50. */
  51. void restore(const RawBackupData& data, bool missingType);
  52. /** Triggers the managed OnReset callback. */
  53. void triggerOnReset();
  54. private:
  55. friend class ScriptManagedComponent;
  56. /**
  57. * Finalizes construction of the object. Must be called before use or when the managed component instance changes.
  58. *
  59. * @param[in] owner Script class that handles interop between the native and managed code for this
  60. * component.
  61. */
  62. void initialize(ScriptManagedComponent* owner);
  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. MonoClass* mManagedClass = nullptr;
  72. MonoReflectionType* mRuntimeType = nullptr;
  73. ScriptManagedComponent* mOwner = nullptr;
  74. String mNamespace;
  75. String mTypeName;
  76. String mFullTypeName;
  77. bool mRequiresReset = true;
  78. bool mMissingType = false;
  79. SPtr<ManagedSerializableObject> mSerializedObjectData;
  80. SPtr<ManagedSerializableObjectInfo> mObjInfo; // Transient
  81. OnCreatedThunkDef mOnCreatedThunk = nullptr;
  82. OnInitializedThunkDef mOnInitializedThunk = nullptr;
  83. OnUpdateThunkDef mOnUpdateThunk = nullptr;
  84. OnResetThunkDef mOnResetThunk = nullptr;
  85. OnDestroyedThunkDef mOnDestroyThunk = nullptr;
  86. OnDestroyedThunkDef mOnDisabledThunk = nullptr;
  87. OnDestroyedThunkDef mOnEnabledThunk = nullptr;
  88. OnTransformChangedThunkDef mOnTransformChangedThunk = nullptr;
  89. MonoMethod* mCalculateBoundsMethod = nullptr;
  90. /************************************************************************/
  91. /* COMPONENT OVERRIDES */
  92. /************************************************************************/
  93. protected:
  94. friend class SceneObject;
  95. friend class ScriptComponent;
  96. ManagedComponent(const HSceneObject& parent, MonoReflectionType* runtimeType);
  97. /** @copydoc Component::_instantiate */
  98. void _instantiate() override;
  99. /** @copydoc Component::onCreated */
  100. void onCreated() override;
  101. /** @copydoc Component::onInitialized */
  102. void onInitialized() override;
  103. /** @copydoc Component::onDestroyed */
  104. void onDestroyed() override;
  105. /** @copydoc Component::onEnabled */
  106. void onEnabled() override;
  107. /** @copydoc Component::onDisabled */
  108. void onDisabled() override;
  109. /** @copydoc Component::onTransformChanged */
  110. void onTransformChanged(TransformChangedFlags flags) override;
  111. public:
  112. /** @copydoc Component::update */
  113. void update() override;
  114. /** @copydoc Component::typeEquals */
  115. bool typeEquals(const Component& other) override;
  116. /** @copydoc Component::calculateBounds */
  117. bool calculateBounds(Bounds& bounds) override;
  118. /************************************************************************/
  119. /* RTTI */
  120. /************************************************************************/
  121. public:
  122. friend class ManagedComponentRTTI;
  123. static RTTITypeBase* getRTTIStatic();
  124. RTTITypeBase* getRTTI() const override;
  125. protected:
  126. ManagedComponent(); // Serialization only
  127. };
  128. /** @} */
  129. }