BsScriptGameObjectManager.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #pragma once
  2. #include "BsScriptEnginePrerequisites.h"
  3. #include "BsModule.h"
  4. #include <mono/jit/jit.h>
  5. namespace BansheeEngine
  6. {
  7. /**
  8. * @brief Manages all active GameObject interop objects. GameObjects can be created from native
  9. * code and used in managed code therefore we need to keep a dictionary or all the native
  10. * objects we have mapped to managed objects.
  11. *
  12. * @note Thread safe.
  13. */
  14. class BS_SCR_BE_EXPORT ScriptGameObjectManager : public Module<ScriptGameObjectManager>
  15. {
  16. /**
  17. * @brief Contains information about a single interop object containing a game object.
  18. */
  19. struct ScriptGameObjectEntry
  20. {
  21. ScriptGameObjectEntry();
  22. ScriptGameObjectEntry(ScriptGameObjectBase* instance, bool isComponent);
  23. ScriptGameObjectBase* instance;
  24. bool isComponent;
  25. };
  26. public:
  27. ScriptGameObjectManager();
  28. ~ScriptGameObjectManager();
  29. /**
  30. * @brief Attempts to find the interop object for the specified SceneObject. If one cannot be found
  31. * new one is created and returned.
  32. */
  33. ScriptSceneObject* getOrCreateScriptSceneObject(const HSceneObject& sceneObject);
  34. /**
  35. * @brief Creates a new interop object for the specified SceneObject. Throws an exception if one
  36. * already exists.
  37. */
  38. ScriptSceneObject* createScriptSceneObject(const HSceneObject& sceneObject);
  39. /**
  40. * @brief Connects an existing managed SceneObject instance with the native SceneObject by creating
  41. * the interop object. Throws an exception if the interop object already exists.
  42. */
  43. ScriptSceneObject* createScriptSceneObject(MonoObject* existingInstance, const HSceneObject& sceneObject);
  44. /**
  45. * @brief Connects an existing managed Component instance with the native Component by creating
  46. * the interop object. Throws an exception if the interop object already exists.
  47. */
  48. ScriptComponent* createScriptComponent(MonoObject* existingInstance, const GameObjectHandle<ManagedComponent>& component);
  49. /**
  50. * @brief Attempts to find the interop object for the specified managed component.
  51. * If one cannot be found null is returned.
  52. */
  53. ScriptComponent* getScriptComponent(const GameObjectHandle<ManagedComponent>& component) const;
  54. /**
  55. * @brief Attempts to find the interop object for a managed component with the specified instance ID.
  56. * If one cannot be found null is returned.
  57. */
  58. ScriptComponent* getScriptComponent(UINT64 instanceId) const;
  59. /**
  60. * @brief Attempts to find the interop object for the specified SceneObject. If one cannot be found
  61. * null is returned.
  62. */
  63. ScriptSceneObject* getScriptSceneObject(const HSceneObject& sceneObject) const;
  64. /**
  65. * @brief Attempts to find the interop object for a GameObject with the specified instance ID.
  66. * If one cannot be found null is returned.
  67. */
  68. ScriptGameObjectBase* getScriptGameObject(UINT64 instanceId) const;
  69. /**
  70. * @brief Destroys and unregisters the specified GameObject interop object.
  71. */
  72. void destroyScriptGameObject(ScriptGameObjectBase* gameObject);
  73. private:
  74. /**
  75. * @brief Triggers OnReset methods on all registered managed components.
  76. *
  77. * @note Usually this happens after an assembly reload.
  78. */
  79. void sendComponentResetEvents();
  80. UnorderedMap<UINT64, ScriptGameObjectEntry> mScriptGameObjects;
  81. HEvent mOnAssemblyReloadDoneConn;
  82. // Mutex needed as we need to be able unregister game objects as soon as they're finalized, and that happens on
  83. // a separate finalizer thread.
  84. mutable Mutex mMutex;
  85. };
  86. }