BsScriptComponent.h 4.9 KB

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