BsManagedComponent.h 4.6 KB

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