BsPrefabDiff.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsIReflectable.h"
  4. #include "BsGameObject.h"
  5. namespace BansheeEngine
  6. {
  7. /**
  8. * @brief Contains differences between two components of the same type.
  9. *
  10. * @see PrefabDiff
  11. */
  12. struct BS_CORE_EXPORT PrefabComponentDiff : public IReflectable
  13. {
  14. INT32 id;
  15. SPtr<SerializedObject> data;
  16. /************************************************************************/
  17. /* RTTI */
  18. /************************************************************************/
  19. public:
  20. friend class PrefabComponentDiffRTTI;
  21. static RTTITypeBase* getRTTIStatic();
  22. virtual RTTITypeBase* getRTTI() const override;
  23. };
  24. /**
  25. * @brief Contains a set of prefab differences for a single scene object.
  26. *
  27. * @see PrefabDiff
  28. */
  29. struct BS_CORE_EXPORT PrefabObjectDiff : public IReflectable
  30. {
  31. INT32 id;
  32. String name;
  33. Vector<SPtr<PrefabComponentDiff>> componentDiffs;
  34. Vector<INT32> removedComponents;
  35. Vector<SPtr<SerializedObject>> addedComponents;
  36. Vector<SPtr<PrefabObjectDiff>> childDiffs;
  37. Vector<INT32> removedChildren;
  38. Vector<SPtr<SerializedObject>> addedChildren;
  39. /************************************************************************/
  40. /* RTTI */
  41. /************************************************************************/
  42. public:
  43. friend class PrefabObjectDiffRTTI;
  44. static RTTITypeBase* getRTTIStatic();
  45. virtual RTTITypeBase* getRTTI() const override;
  46. };
  47. /**
  48. * @brief Contains modifications between an prefab and its instance. The modifications are a set of
  49. * added/removed children or components and per-field "diffs" of their components.
  50. */
  51. class BS_CORE_EXPORT PrefabDiff : public IReflectable
  52. {
  53. public:
  54. /**
  55. * @brief Creates a new prefab diff by comparing the provided instanced scene object hierarchy
  56. * with the prefab scene object hierarchy.
  57. */
  58. static SPtr<PrefabDiff> create(const HSceneObject& prefab, const HSceneObject& instance);
  59. /**
  60. * @brief Applies the internal prefab diff to the provided object. The object should have
  61. * similar hierarchy as the prefab the diff was created for, otherwise the results are
  62. * undefined.
  63. */
  64. void apply(const HSceneObject& object);
  65. private:
  66. /**
  67. * @brief A reference to a renamed game object instance data, and its original ID
  68. * so it may be restored later.
  69. */
  70. struct RenamedGameObject
  71. {
  72. GameObjectInstanceDataPtr instanceData;
  73. UINT64 originalId;
  74. };
  75. /**
  76. * @brief Recurses over every scene object in the prefab a generates differences between itself
  77. * and the instanced version.
  78. *
  79. * @see create
  80. */
  81. static SPtr<PrefabObjectDiff> generateDiff(const HSceneObject& prefab, const HSceneObject& instance);
  82. /**
  83. * @brief Recursively applies a per-object set of prefab differences to a specific object.
  84. *
  85. * @see apply
  86. */
  87. static void applyDiff(const SPtr<PrefabObjectDiff>& diff, const HSceneObject& object);
  88. /**
  89. * @brief Renames all game objects in the provided instance so that IDs of the objects will match
  90. * the IDs of their counterparts in the prefab.
  91. *
  92. * @note This is a temporary action and should be undone by calling "restoreInstanceIds" and providing
  93. * it with the output of this method.
  94. * @par By doing this before calling "diff" we ensure that any game object handles pointing to objects
  95. * within the prefab instance hierarchy aren't recorded by the diff system, since we want those to
  96. * remain as they are after applying the diff.
  97. */
  98. static void renameInstanceIds(const HSceneObject& prefab, const HSceneObject& instance, Vector<RenamedGameObject>& output);
  99. /**
  100. * @brief Restores any instance IDs that were modified by the "renameInstanceIds" method.
  101. *
  102. * @see renameInstanceIds;
  103. */
  104. static void restoreInstanceIds(const Vector<RenamedGameObject>& renamedObjects);
  105. SPtr<PrefabObjectDiff> mRoot;
  106. /************************************************************************/
  107. /* RTTI */
  108. /************************************************************************/
  109. public:
  110. friend class PrefabDiffRTTI;
  111. static RTTITypeBase* getRTTIStatic();
  112. virtual RTTITypeBase* getRTTI() const override;
  113. };
  114. }