BsManagedComponent.h 5.9 KB

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