BsScriptComponent.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 "Wrappers/BsScriptGameObject.h"
  6. #include "BsScriptObject.h"
  7. #include "BsMonoUtil.h"
  8. namespace bs
  9. {
  10. /** @addtogroup ScriptInteropEngine
  11. * @{
  12. */
  13. /** Base class for all Component interop classes. */
  14. class BS_SCR_BE_EXPORT ScriptComponentBase : public ScriptGameObjectBase
  15. {
  16. public:
  17. ScriptComponentBase(MonoObject* instance);
  18. virtual ~ScriptComponentBase() { }
  19. /** Returns the component wrapped by this object. */
  20. HComponent getComponent() const { return static_object_cast<Component>(getNativeHandle()); }
  21. protected:
  22. friend class ScriptGameObjectManager;
  23. /** Destroys the interop object, unless refresh is in progress in which case it is just prepared for re-creation. */
  24. void destroy();
  25. /** Triggered by the script game object manager when the handle this object is referencing is destroyed. */
  26. virtual void _notifyDestroyed() { }
  27. /** Checks if the provided game object is destroyed and logs a warning if it is. */
  28. static bool checkIfDestroyed(const GameObjectHandleBase& handle);
  29. };
  30. /** Base class for a specific builtin component's interop object. */
  31. template<class ScriptClass, class CompType, class BaseType = ScriptComponentBase>
  32. class BS_SCR_BE_EXPORT TScriptComponent : public ScriptObject <ScriptClass, BaseType>
  33. {
  34. public:
  35. /** Returns a generic handle to the internal wrapped component. */
  36. HGameObject getNativeHandle() const override { return mComponent; }
  37. /** Sets the internal component this object wraps. */
  38. void setNativeHandle(const HGameObject& gameObject) override { mComponent = static_object_cast<CompType>(gameObject); }
  39. /** Returns a handle to the internal wrapped component. */
  40. const GameObjectHandle<CompType>& getHandle() const { return mComponent; }
  41. protected:
  42. friend class ScriptGameObjectManager;
  43. TScriptComponent(MonoObject* instance, const GameObjectHandle<CompType>& component)
  44. :ScriptObject<ScriptClass, BaseType>(instance), mComponent(component)
  45. {
  46. mManagedHandle = MonoUtil::newGCHandle(instance);
  47. this->mManagedInstance = MonoUtil::getObjectFromGCHandle(mManagedHandle);
  48. BS_DEBUG_ONLY(mHandleValid = true);
  49. }
  50. virtual ~TScriptComponent() {}
  51. /**
  52. * Called after assembly reload starts to give the object a chance to restore the data backed up by the previous
  53. * beginRefresh() call.
  54. */
  55. virtual void endRefresh(const ScriptObjectBackup& backupData) override
  56. {
  57. BS_ASSERT(!mHandleValid);
  58. mManagedHandle = MonoUtil::newGCHandle(this->mManagedInstance);
  59. this->mManagedInstance = MonoUtil::getObjectFromGCHandle(mManagedHandle);
  60. ScriptObject<ScriptClass, BaseType>::endRefresh(backupData);
  61. }
  62. /**
  63. * Triggered by the script game object manager when the native component handle this object point to has been
  64. * destroyed.
  65. */
  66. void _notifyDestroyed() override
  67. {
  68. MonoUtil::freeGCHandle(mManagedHandle);
  69. BS_DEBUG_ONLY(mHandleValid = false);
  70. }
  71. /** Called when the managed instance gets finalized by the CLR. */
  72. void _onManagedInstanceDeleted() override
  73. {
  74. MonoUtil::freeGCHandle(mManagedHandle);
  75. BS_DEBUG_ONLY(mHandleValid = false);
  76. this->destroy();
  77. }
  78. GameObjectHandle<CompType> mComponent;
  79. uint32_t mManagedHandle;
  80. BS_DEBUG_ONLY(bool mHandleValid);
  81. };
  82. /** Interop class between C++ & CLR for Component. */
  83. class BS_SCR_BE_EXPORT ScriptComponent : public ScriptObject<ScriptComponent, ScriptComponentBase>
  84. {
  85. public:
  86. SCRIPT_OBJ(ENGINE_ASSEMBLY, "BansheeEngine", "Component")
  87. private:
  88. friend class ScriptGameObjectManager;
  89. ScriptComponent(MonoObject* instance);
  90. /************************************************************************/
  91. /* CLR HOOKS */
  92. /************************************************************************/
  93. static MonoObject* internal_addComponent(MonoObject* parentSceneObject, MonoReflectionType* type);
  94. static MonoObject* internal_getComponent(MonoObject* parentSceneObject, MonoReflectionType* type);
  95. static MonoArray* internal_getComponents(MonoObject* parentSceneObject);
  96. static MonoArray* internal_getComponentsPerType(MonoObject* parentSceneObject, MonoReflectionType* type);
  97. static void internal_removeComponent(MonoObject* parentSceneObject, MonoReflectionType* type);
  98. static MonoObject* internal_getSceneObject(ScriptComponentBase* nativeInstance);
  99. static TransformChangedFlags internal_getNotifyFlags(ScriptComponentBase* nativeInstance);
  100. static void internal_setNotifyFlags(ScriptComponentBase* nativeInstance, TransformChangedFlags flags);
  101. static void internal_destroy(ScriptComponentBase* nativeInstance, bool immediate);
  102. };
  103. /** @} */
  104. }