BsManagedComponent.h 6.2 KB

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