BsGameObjectRTTI.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 "BsRTTIType.h"
  6. #include "BsGameObject.h"
  7. #include "BsSceneObject.h"
  8. #include "BsGameObjectManager.h"
  9. namespace BansheeEngine
  10. {
  11. /** @cond RTTI */
  12. /** @addtogroup RTTI-Impl-Core
  13. * @{
  14. */
  15. /** Provides temporary storage for data used during GameObject deserialization. */
  16. struct GODeserializationData
  17. {
  18. GODeserializationData()
  19. :isDeserializationParent(false), originalId(0)
  20. { }
  21. SPtr<GameObject> ptr;
  22. bool isDeserializationParent;
  23. UINT64 originalId;
  24. Any moreData;
  25. };
  26. class BS_CORE_EXPORT GameObjectRTTI : public RTTIType<GameObject, IReflectable, GameObjectRTTI>
  27. {
  28. private:
  29. String& getName(GameObject* obj) { return obj->mName; }
  30. void setName(GameObject* obj, String& name) { obj->mName = name; }
  31. UINT64& getInstanceID(GameObject* obj) { return obj->mInstanceData->mInstanceId; }
  32. void setInstanceID(GameObject* obj, UINT64& instanceId)
  33. {
  34. // We record the ID for later use. Any child RTTI of GameObject must call GameObjectManager::registerObject
  35. // with this ID, so we know how to map deserialized GO handles to live objects, otherwise the handle
  36. // references will get broken.
  37. GameObject* go = static_cast<GameObject*>(obj);
  38. GODeserializationData& deserializationData = any_cast_ref<GODeserializationData>(go->mRTTIData);
  39. deserializationData.originalId = instanceId;
  40. }
  41. UINT32& getLinkId(GameObject* obj) { return obj->mLinkId; }
  42. void setLinkId(GameObject* obj, UINT32& linkId) { obj->mLinkId = linkId; }
  43. public:
  44. /** Helper method used for creating Component objects used during deserialization. */
  45. template <typename T>
  46. static SPtr<T> createGameObject()
  47. {
  48. SPtr<T> component = SceneObject::createEmptyComponent<T>();
  49. component->mRTTIData = component;
  50. return component;
  51. }
  52. public:
  53. GameObjectRTTI()
  54. {
  55. addPlainField("mInstanceID", 0, &GameObjectRTTI::getInstanceID, &GameObjectRTTI::setInstanceID);
  56. addPlainField("mName", 1, &GameObjectRTTI::getName, &GameObjectRTTI::setName);
  57. addPlainField("mLinkId", 2, &GameObjectRTTI::getLinkId, &GameObjectRTTI::setLinkId);
  58. }
  59. void onDeserializationStarted(IReflectable* obj, const UnorderedMap<String, UINT64>& params) override
  60. {
  61. GameObject* gameObject = static_cast<GameObject*>(obj);
  62. // It's possible we're just accessing the game object fields, in which case the process below is not needed
  63. // (it's only required for new game objects).
  64. if (gameObject->mRTTIData.empty())
  65. return;
  66. SPtr<GameObject> gameObjectPtr = any_cast<SPtr<GameObject>>(gameObject->mRTTIData);
  67. // Every GameObject must store GODeserializationData in its RTTI data field during deserialization
  68. gameObject->mRTTIData = GODeserializationData();
  69. GODeserializationData& deserializationData = any_cast_ref<GODeserializationData>(gameObject->mRTTIData);
  70. // Store shared pointer since the system only provides us with raw ones
  71. deserializationData.ptr = gameObjectPtr;
  72. }
  73. const String& getRTTIName() override
  74. {
  75. static String name = "GameObject";
  76. return name;
  77. }
  78. UINT32 getRTTIId() override
  79. {
  80. return TID_GameObject;
  81. }
  82. SPtr<IReflectable> newRTTIObject() override
  83. {
  84. BS_EXCEPT(InternalErrorException, "Cannot instantiate an abstract class.");
  85. return nullptr;
  86. }
  87. };
  88. /** @} */
  89. /** @endcond */
  90. }