BsManagedComponent.h 5.7 KB

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