1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#pragma once
4
5#include "BsCorePrerequisites.h"
6#include "Reflection/BsRTTIType.h"
7#include "Scene/BsComponent.h"
8#include "Private/RTTI/BsGameObjectRTTI.h"
9#include "Utility/BsUtility.h"
10
11namespace bs
12{
13 /** @cond RTTI */
14 /** @addtogroup RTTI-Impl-Core
15 * @{
16 */
17
18 class BS_CORE_EXPORT ComponentRTTI : public RTTIType<Component, GameObject, ComponentRTTI>
19 {
20 public:
21 void onDeserializationEnded(IReflectable* obj, SerializationContext* context) override
22 {
23 Component* comp = static_cast<Component*>(obj);
24
25 // It's possible we're just accessing the game object fields, in which case the process below is not needed
26 // (it's only required for new components).
27 if (comp->mRTTIData.empty())
28 return;
29
30 BS_ASSERT(context != nullptr && rtti_is_of_type<CoreSerializationContext>(context));
31 auto coreContext = static_cast<CoreSerializationContext*>(context);
32
33 GODeserializationData& deserializationData = any_cast_ref<GODeserializationData>(comp->mRTTIData);
34
35 // This shouldn't be null during normal deserialization but could be during some other operations, like applying
36 // a binary diff.
37 if (deserializationData.ptr != nullptr)
38 {
39 // Register the newly created SO with the GameObjectManager and provide it with the original ID so that
40 // deserialized handles pointing to this object can be resolved.
41 SPtr<Component> compPtr = std::static_pointer_cast<Component>(deserializationData.ptr);
42
43 GameObjectHandleBase handle = GameObjectManager::instance().registerObject(compPtr);
44 coreContext->goState->registerObject(deserializationData.originalId, handle);
45 }
46
47 if(comp->mUUID.empty() || ((coreContext->flags & GODM_UseNewUUID) != 0))
48 comp->mUUID = UUIDGenerator::generateRandom();
49
50 comp->mRTTIData = nullptr;
51 }
52
53 const String& getRTTIName() override
54 {
55 static String name = "Component";
56 return name;
57 }
58
59 UINT32 getRTTIId() override
60 {
61 return TID_Component;
62 }
63
64 SPtr<IReflectable> newRTTIObject() override
65 {
66 BS_EXCEPT(InternalErrorException, "Cannot instantiate an abstract class.");
67 return nullptr;
68 }
69 };
70
71 /** @} */
72 /** @endcond */
73}
74