BsManagedComponent.h 5.9 KB

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