BsGameObjectManager.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsModule.h"
  4. #include "BsGameObject.h"
  5. namespace BansheeEngine
  6. {
  7. /**
  8. * @brief Possible modes to use when deserializing games objects.
  9. */
  10. enum GameObjectHandleDeserializationMode
  11. {
  12. /** All handles will point to old ID that were restored from the deserialized file. */
  13. GODM_UseOriginalIds = 0x01,
  14. /** All handles will point to new IDs that were given to the deserialized GameObjects. */
  15. GODM_UseNewIds = 0x02,
  16. /** Handles pointing to GameObjects outside of the currently deserialized set
  17. will attempt to be restored in case those objects are still active. */
  18. GODM_RestoreExternal = 0x04,
  19. /** Handles pointing to GameObjects outside of the currently deserialized set
  20. will be broken. */
  21. GODM_BreakExternal = 0x08,
  22. /** Handles pointing to GameObjects that cannot be found will not be set to null. */
  23. GODM_KeepMissing = 0x10
  24. };
  25. /**
  26. * @brief Tracks GameObject creation and destructions. Also resolves
  27. * GameObject references from GameObject handles.
  28. *
  29. * @note Sim thread only.
  30. */
  31. class BS_CORE_EXPORT GameObjectManager : public Module<GameObjectManager>
  32. {
  33. /**
  34. * @brief Contains data for an yet unresolved game object handle.
  35. */
  36. struct UnresolvedHandle
  37. {
  38. UINT64 originalInstanceId;
  39. GameObjectHandleBase handle;
  40. };
  41. public:
  42. GameObjectManager();
  43. ~GameObjectManager();
  44. /**
  45. * @brief Registers a new GameObject and returns the handle to the object.
  46. */
  47. GameObjectHandleBase registerObject(const std::shared_ptr<GameObject>& object);
  48. /**
  49. * @brief Unregisters a GameObject.
  50. */
  51. void unregisterObject(const GameObjectHandleBase& object);
  52. /**
  53. * @brief Attempts to find a GameObject handle based on the GameObject instance ID.
  54. * Returns empty handle if ID cannot be found.
  55. */
  56. GameObjectHandleBase getObject(UINT64 id) const;
  57. /**
  58. * @brief Attempts to find a GameObject handle based on the GameObject instance ID.
  59. * Returns true if object with the specified ID is found, false otherwise.
  60. */
  61. bool tryGetObject(UINT64 id, GameObjectHandleBase& object) const;
  62. /**
  63. * @brief Checks if the GameObject with the specified instance ID exists.
  64. */
  65. bool objectExists(UINT64 id) const;
  66. /**
  67. * @brief Changes the instance ID by which an object can be retrieved by.
  68. *
  69. * @note Caller is required to update the object itself with the new ID.
  70. */
  71. void remapId(UINT64 oldId, UINT64 newId);
  72. /**
  73. * @brief Queues the object to be destroyed at the end of a GameObject update cycle.
  74. */
  75. void queueForDestroy(const GameObjectHandleBase& object);
  76. /**
  77. * @brief Destroys any GameObjects that were queued for destruction.
  78. */
  79. void destroyQueuedObjects();
  80. /**
  81. * @brief Triggered when a game object is being destroyed.
  82. */
  83. Event<void(const HGameObject&)> onDestroyed;
  84. /************************************************************************/
  85. /* DESERIALIZATION */
  86. /************************************************************************/
  87. // Note: GameObjects need a bit of special handling when it comes to deserialization,
  88. // which is what this part of the code is used for. It performs two main actions:
  89. // - 1. Resolves all GameObjectHandles on deserialization
  90. // - We can't just resolve them as we go because during deserialization not all objects
  91. // have necessarily been created.
  92. // - 2. Maps serialized IDs to actual in-engine ids.
  93. /**
  94. * @brief Needs to be called whenever GameObject deserialization starts. Must be followed
  95. * by endDeserialization call.
  96. */
  97. void startDeserialization();
  98. /**
  99. * @brief Needs to be called whenever GameObject deserialization ends. Must be preceded
  100. * by startDeserialization call.
  101. */
  102. void endDeserialization();
  103. /**
  104. * @brief Returns true if GameObject deserialization is currently in progress.
  105. */
  106. bool isGameObjectDeserializationActive() const { return mIsDeserializationActive; }
  107. /**
  108. * @brief Registers an id that was deserialized, and has been remapped to
  109. * an actual in-engine ID. This will be used when resolving GameObjectHandles
  110. * (since they might point to the invalid deserialized id).
  111. */
  112. void registerDeserializedId(UINT64 deserializedId, UINT64 actualId);
  113. /**
  114. * @brief Queues the specified handle and resolves it when deserialization ends.
  115. */
  116. void registerUnresolvedHandle(UINT64 originalId, const GameObjectHandleBase& object);
  117. /**
  118. * @brief Registers a callback that will be triggered when GameObject serialization ends.
  119. */
  120. void registerOnDeserializationEndCallback(std::function<void()> callback);
  121. /**
  122. * @brief Changes the deserialization mode for any following GameObject handle.
  123. *
  124. * @param gameObjectDeserializationMode Mode that controls how are GameObjects handles resolved when being deserialized.
  125. */
  126. void setDeserializationMode(UINT32 gameObjectDeserializationMode);
  127. /**
  128. * @brief Attempts to update the ID of the provided handle by mapping its old ID to
  129. * the newly deserialized object and its new ID. Game object deserialization
  130. * must be active.
  131. */
  132. void resolveDeserializedHandle(UnresolvedHandle& data, UINT32 flags);
  133. /**
  134. * @brief Gets the currently active flags that control how are game object handles deserialized.
  135. */
  136. UINT32 getDeserializationFlags() const { return mGODeserializationMode; }
  137. private:
  138. UINT64 mNextAvailableID; // 0 is not a valid ID
  139. Map<UINT64, GameObjectHandleBase> mObjects;
  140. Map<UINT64, GameObjectHandleBase> mQueuedForDestroy;
  141. GameObject* mActiveDeserializedObject;
  142. bool mIsDeserializationActive;
  143. Map<UINT64, UINT64> mIdMapping;
  144. Vector<UnresolvedHandle> mUnresolvedHandles;
  145. Vector<std::function<void()>> mEndCallbacks;
  146. UINT32 mGODeserializationMode;
  147. };
  148. }