BsManagedComponent.h 6.3 KB

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