BsManagedComponent.h 6.2 KB

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