PrefabImporter.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/Resource/ResourceCache.h>
  23. #include <Atomic/Resource/XMLFile.h>
  24. #include <Atomic/Scene/Scene.h>
  25. #include <Atomic/Scene/PrefabEvents.h>
  26. #include <Atomic/Scene/PrefabComponent.h>
  27. #include <Atomic/IO/FileSystem.h>
  28. #include "Asset.h"
  29. #include "AssetDatabase.h"
  30. #include "PrefabImporter.h"
  31. namespace ToolCore
  32. {
  33. PrefabImporter::PrefabImporter(Context* context, Asset* asset) : AssetImporter(context, asset),
  34. lastFileStamp_(0xFFFFFFFF)
  35. {
  36. SubscribeToEvent(E_PREFABSAVE, ATOMIC_HANDLER(PrefabImporter, HandlePrefabSave));
  37. }
  38. PrefabImporter::~PrefabImporter()
  39. {
  40. }
  41. void PrefabImporter::SetDefaults()
  42. {
  43. AssetImporter::SetDefaults();
  44. }
  45. bool PrefabImporter::Preload()
  46. {
  47. if (!asset_)
  48. return false;
  49. preloadResourceScene_ = new Scene(context_);
  50. SharedPtr<File> file(new File(context_, asset_->GetCachePath()));
  51. preloadResourceScene_->LoadAsyncXML(file, LOAD_RESOURCES_ONLY);
  52. return true;
  53. }
  54. void PrefabImporter::HandlePrefabSave(StringHash eventType, VariantMap& eventData)
  55. {
  56. using namespace PrefabSave;
  57. PrefabComponent* component = static_cast<PrefabComponent*>(eventData[P_PREFABCOMPONENT].GetPtr());
  58. if (component->GetPrefabGUID() != asset_->GetGUID())
  59. return;
  60. Node* node = component->GetNode();
  61. if (!node)
  62. return;
  63. // flip temporary root children and components to not be temporary for save
  64. const Vector<SharedPtr<Component>>& rootComponents = node->GetComponents();
  65. const Vector<SharedPtr<Node> >& children = node->GetChildren();
  66. PODVector<Component*> tempComponents;
  67. PODVector<Node*> tempChildren;
  68. PODVector<Node*> filterNodes;
  69. for (unsigned i = 0; i < rootComponents.Size(); i++)
  70. {
  71. if (rootComponents[i]->IsTemporary())
  72. {
  73. rootComponents[i]->SetTemporary(false);
  74. tempComponents.Push(rootComponents[i]);
  75. }
  76. }
  77. for (unsigned i = 0; i < children.Size(); i++)
  78. {
  79. if (filterNodes.Contains(children[i].Get()))
  80. continue;
  81. if (children[i]->IsTemporary())
  82. {
  83. children[i]->SetTemporary(false);
  84. tempChildren.Push(children[i]);
  85. }
  86. }
  87. // store original transform
  88. Vector3 pos = node->GetPosition();
  89. Quaternion rot = node->GetRotation();
  90. Vector3 scale = node->GetScale();
  91. node->SetPosition(Vector3::ZERO);
  92. node->SetRotation(Quaternion::IDENTITY);
  93. node->SetScale(Vector3::ONE);
  94. component->SetTemporary(true);
  95. SharedPtr<File> file(new File(context_, asset_->GetPath(), FILE_WRITE));
  96. node->SaveXML(*file);
  97. file->Close();
  98. component->SetTemporary(false);
  99. // restore
  100. node->SetPosition(pos);
  101. node->SetRotation(rot);
  102. node->SetScale(scale);
  103. for (unsigned i = 0; i < tempComponents.Size(); i++)
  104. {
  105. tempComponents[i]->SetTemporary(true);
  106. }
  107. for (unsigned i = 0; i < tempChildren.Size(); i++)
  108. {
  109. tempChildren[i]->SetTemporary(true);
  110. }
  111. FileSystem* fs = GetSubsystem<FileSystem>();
  112. fs->Copy(asset_->GetPath(), asset_->GetCachePath());
  113. asset_->UpdateFileTimestamp();
  114. lastFileStamp_ = asset_->GetFileTimestamp();
  115. OnPrefabFileChanged();
  116. }
  117. bool PrefabImporter::Import()
  118. {
  119. // The asset database will see the file changed, when the file watcher catches it
  120. // though we already loaded/imported in OnPrefabFileChanged
  121. if (lastFileStamp_ == asset_->GetFileTimestamp())
  122. return true;
  123. FileSystem* fs = GetSubsystem<FileSystem>();
  124. fs->Copy(asset_->GetPath(), asset_->GetCachePath());
  125. OnPrefabFileChanged();
  126. return true;
  127. }
  128. void PrefabImporter::OnPrefabFileChanged()
  129. {
  130. // reload it immediately so it is ready for use
  131. ResourceCache* cache = GetSubsystem<ResourceCache>();
  132. XMLFile* xmlfile = cache->GetResource<XMLFile>(asset_->GetGUID());
  133. cache->ReloadResource(xmlfile);
  134. VariantMap changedData;
  135. changedData[PrefabChanged::P_GUID] = asset_->GetGUID();
  136. SendEvent(E_PREFABCHANGED, changedData);
  137. }
  138. bool PrefabImporter::LoadSettingsInternal(JSONValue& jsonRoot)
  139. {
  140. if (!AssetImporter::LoadSettingsInternal(jsonRoot))
  141. return false;
  142. JSONValue import = jsonRoot.Get("PrefabImporter");
  143. return true;
  144. }
  145. bool PrefabImporter::SaveSettingsInternal(JSONValue& jsonRoot)
  146. {
  147. if (!AssetImporter::SaveSettingsInternal(jsonRoot))
  148. return false;
  149. JSONValue import(JSONValue::emptyObject);
  150. jsonRoot.Set("PrefabImporter", import);
  151. return true;
  152. }
  153. Node* PrefabImporter::InstantiateNode(Node* parent, const String& name)
  154. {
  155. Node* node = parent->CreateChild(asset_->GetName());
  156. PrefabComponent* pc = node->CreateComponent<PrefabComponent>();
  157. pc->SetPrefabGUID(asset_->GetGUID());
  158. return node;
  159. }
  160. }