2
0

BsGameObjectManager.h 6.1 KB

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