BsManagedComponent.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 OnReset callback.
  64. */
  65. void triggerOnReset();
  66. private:
  67. /**
  68. * @brief Finalizes construction of the object. Must be called before use or when
  69. * the managed component instance changes.
  70. *
  71. * @param object Managed component instance.
  72. */
  73. void initialize(MonoObject* object);
  74. typedef void(__stdcall *OnInitializedThunkDef) (MonoObject*, MonoException**);
  75. typedef void(__stdcall *UpdateThunkDef) (MonoObject*, MonoException**);
  76. typedef void(__stdcall *OnDestroyedThunkDef) (MonoObject*, MonoException**);
  77. typedef void(__stdcall *OnResetThunkDef) (MonoObject*, MonoException**);
  78. typedef void(__stdcall *OnEnabledThunkDef) (MonoObject*, MonoException**);
  79. typedef void(__stdcall *OnDisabledThunkDef) (MonoObject*, MonoException**);
  80. MonoObject* mManagedInstance;
  81. MonoReflectionType* mRuntimeType;
  82. uint32_t mManagedHandle;
  83. String mNamespace;
  84. String mTypeName;
  85. String mFullTypeName;
  86. bool mRequiresReset;
  87. bool mMissingType;
  88. ManagedSerializableObjectPtr mSerializedObjectData;
  89. ManagedSerializableObjectInfoPtr mObjInfo; // Transient
  90. OnInitializedThunkDef mOnInitializedThunk;
  91. UpdateThunkDef mUpdateThunk;
  92. OnResetThunkDef mOnResetThunk;
  93. OnDestroyedThunkDef mOnDestroyThunk;
  94. OnDestroyedThunkDef mOnDisabledThunk;
  95. OnDestroyedThunkDef mOnEnabledThunk;
  96. /************************************************************************/
  97. /* COMPONENT OVERRIDES */
  98. /************************************************************************/
  99. protected:
  100. friend class SceneObject;
  101. friend class ScriptComponent;
  102. ManagedComponent(const HSceneObject& parent, MonoReflectionType* runtimeType);
  103. /**
  104. * @copydoc Component::instantiate
  105. */
  106. void instantiate() override;
  107. /**
  108. * @copydoc Component::onInitialized
  109. */
  110. void onInitialized() override;
  111. /**
  112. * @copydoc Component::onDestroyed
  113. */
  114. void onDestroyed() override;
  115. /**
  116. * @copydoc Component::onEnabled
  117. */
  118. void onEnabled() override;
  119. /**
  120. * @copydoc Component::onDisabled
  121. */
  122. void onDisabled() override;
  123. public:
  124. /**
  125. * @copydoc Component::update
  126. */
  127. virtual void update() override;
  128. /**
  129. * @copydoc Component::typeEquals
  130. */
  131. virtual bool typeEquals(const Component& other) override;
  132. /************************************************************************/
  133. /* RTTI */
  134. /************************************************************************/
  135. public:
  136. friend class ManagedComponentRTTI;
  137. static RTTITypeBase* getRTTIStatic();
  138. virtual RTTITypeBase* getRTTI() const override;
  139. protected:
  140. ManagedComponent(); // Serialization only
  141. };
  142. /**
  143. * @brief Contains serialized component data buffer.
  144. */
  145. struct ComponentBackupData
  146. {
  147. UINT8* data;
  148. UINT32 size;
  149. };
  150. }