BsScriptComponent.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 "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>
  32. class BS_SCR_BE_EXPORT TScriptComponent : public ScriptObject <ScriptClass, ScriptComponentBase>
  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, ScriptComponentBase>(instance), mComponent(component)
  45. {
  46. mManagedHandle = MonoUtil::newGCHandle(instance);
  47. BS_DEBUG_ONLY(mHandleValid = true);
  48. }
  49. virtual ~TScriptComponent() {}
  50. /**
  51. * Called after assembly reload starts to give the object a chance to restore the data backed up by the previous
  52. * beginRefresh() call.
  53. */
  54. virtual void endRefresh(const ScriptObjectBackup& backupData) override
  55. {
  56. BS_ASSERT(!mHandleValid);
  57. mManagedHandle = MonoUtil::newGCHandle(this->mManagedInstance);
  58. ScriptObject<ScriptClass, ScriptComponentBase>::endRefresh(backupData);
  59. }
  60. /**
  61. * Triggered by the script game object manager when the native component handle this object point to has been
  62. * destroyed.
  63. */
  64. void _notifyDestroyed() override
  65. {
  66. MonoUtil::freeGCHandle(mManagedHandle);
  67. BS_DEBUG_ONLY(mHandleValid = false);
  68. }
  69. /** Called when the managed instance gets finalized by the CLR. */
  70. void _onManagedInstanceDeleted() override
  71. {
  72. MonoUtil::freeGCHandle(mManagedHandle);
  73. BS_DEBUG_ONLY(mHandleValid = false);
  74. this->destroy();
  75. }
  76. GameObjectHandle<CompType> mComponent;
  77. uint32_t mManagedHandle;
  78. BS_DEBUG_ONLY(bool mHandleValid);
  79. };
  80. /** Interop class between C++ & CLR for Component. */
  81. class BS_SCR_BE_EXPORT ScriptComponent : public ScriptObject<ScriptComponent, ScriptComponentBase>
  82. {
  83. public:
  84. SCRIPT_OBJ(ENGINE_ASSEMBLY, "BansheeEngine", "Component")
  85. private:
  86. friend class ScriptGameObjectManager;
  87. ScriptComponent(MonoObject* instance);
  88. /************************************************************************/
  89. /* CLR HOOKS */
  90. /************************************************************************/
  91. static MonoObject* internal_addComponent(MonoObject* parentSceneObject, MonoReflectionType* type);
  92. static MonoObject* internal_getComponent(MonoObject* parentSceneObject, MonoReflectionType* type);
  93. static MonoArray* internal_getComponents(MonoObject* parentSceneObject);
  94. static MonoArray* internal_getComponentsPerType(MonoObject* parentSceneObject, MonoReflectionType* type);
  95. static void internal_removeComponent(MonoObject* parentSceneObject, MonoReflectionType* type);
  96. static MonoObject* internal_getSceneObject(ScriptComponentBase* nativeInstance);
  97. static TransformChangedFlags internal_getNotifyFlags(ScriptComponentBase* nativeInstance);
  98. static void internal_setNotifyFlags(ScriptComponentBase* nativeInstance, TransformChangedFlags flags);
  99. static void internal_destroy(ScriptComponentBase* nativeInstance, bool immediate);
  100. };
  101. /** @} */
  102. }