PrefabImporter.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. requiresCacheFile_ = true;
  37. SubscribeToEvent(E_PREFABSAVE, ATOMIC_HANDLER(PrefabImporter, HandlePrefabSave));
  38. }
  39. PrefabImporter::~PrefabImporter()
  40. {
  41. }
  42. void PrefabImporter::SetDefaults()
  43. {
  44. AssetImporter::SetDefaults();
  45. }
  46. bool PrefabImporter::Preload()
  47. {
  48. if (!asset_)
  49. return false;
  50. preloadResourceScene_ = new Scene(context_);
  51. SharedPtr<File> file(new File(context_, asset_->GetCachePath()));
  52. preloadResourceScene_->LoadAsyncXML(file, LOAD_RESOURCES_ONLY);
  53. return true;
  54. }
  55. void PrefabImporter::HandlePrefabSave(StringHash eventType, VariantMap& eventData)
  56. {
  57. using namespace PrefabSave;
  58. PrefabComponent* component = static_cast<PrefabComponent*>(eventData[P_PREFABCOMPONENT].GetPtr());
  59. if (component->GetPrefabGUID() != asset_->GetGUID())
  60. return;
  61. Node* node = component->GetNode();
  62. if (!node)
  63. return;
  64. // flip temporary root children and components to not be temporary for save
  65. const Vector<SharedPtr<Component>>& rootComponents = node->GetComponents();
  66. const Vector<SharedPtr<Node> >& children = node->GetChildren();
  67. PODVector<Component*> tempComponents;
  68. PODVector<Node*> tempChildren;
  69. PODVector<Node*> filterNodes;
  70. for (unsigned i = 0; i < rootComponents.Size(); i++)
  71. {
  72. if (rootComponents[i]->IsTemporary())
  73. {
  74. rootComponents[i]->SetTemporary(false);
  75. tempComponents.Push(rootComponents[i]);
  76. }
  77. }
  78. for (unsigned i = 0; i < children.Size(); i++)
  79. {
  80. if (filterNodes.Contains(children[i].Get()))
  81. continue;
  82. if (children[i]->IsTemporary())
  83. {
  84. children[i]->SetTemporary(false);
  85. tempChildren.Push(children[i]);
  86. }
  87. }
  88. // store original transform
  89. Vector3 pos = node->GetPosition();
  90. Quaternion rot = node->GetRotation();
  91. Vector3 scale = node->GetScale();
  92. node->SetPosition(Vector3::ZERO);
  93. node->SetRotation(Quaternion::IDENTITY);
  94. node->SetScale(Vector3::ONE);
  95. component->SetTemporary(true);
  96. SharedPtr<File> file(new File(context_, asset_->GetPath(), FILE_WRITE));
  97. node->SaveXML(*file);
  98. file->Close();
  99. component->SetTemporary(false);
  100. // restore
  101. node->SetPosition(pos);
  102. node->SetRotation(rot);
  103. node->SetScale(scale);
  104. for (unsigned i = 0; i < tempComponents.Size(); i++)
  105. {
  106. tempComponents[i]->SetTemporary(true);
  107. }
  108. for (unsigned i = 0; i < tempChildren.Size(); i++)
  109. {
  110. tempChildren[i]->SetTemporary(true);
  111. }
  112. FileSystem* fs = GetSubsystem<FileSystem>();
  113. fs->Copy(asset_->GetPath(), asset_->GetCachePath());
  114. asset_->UpdateFileTimestamp();
  115. lastFileStamp_ = asset_->GetFileTimestamp();
  116. OnPrefabFileChanged();
  117. }
  118. bool PrefabImporter::Import()
  119. {
  120. // The asset database will see the file changed, when the file watcher catches it
  121. // though we already loaded/imported in OnPrefabFileChanged
  122. if (lastFileStamp_ == asset_->GetFileTimestamp())
  123. return true;
  124. FileSystem* fs = GetSubsystem<FileSystem>();
  125. fs->Copy(asset_->GetPath(), asset_->GetCachePath());
  126. OnPrefabFileChanged();
  127. return true;
  128. }
  129. void PrefabImporter::OnPrefabFileChanged()
  130. {
  131. // reload it immediately so it is ready for use
  132. ResourceCache* cache = GetSubsystem<ResourceCache>();
  133. XMLFile* xmlfile = cache->GetResource<XMLFile>(asset_->GetGUID());
  134. cache->ReloadResource(xmlfile);
  135. VariantMap changedData;
  136. changedData[PrefabChanged::P_GUID] = asset_->GetGUID();
  137. SendEvent(E_PREFABCHANGED, changedData);
  138. }
  139. bool PrefabImporter::LoadSettingsInternal(JSONValue& jsonRoot)
  140. {
  141. if (!AssetImporter::LoadSettingsInternal(jsonRoot))
  142. return false;
  143. JSONValue import = jsonRoot.Get("PrefabImporter");
  144. return true;
  145. }
  146. bool PrefabImporter::SaveSettingsInternal(JSONValue& jsonRoot)
  147. {
  148. if (!AssetImporter::SaveSettingsInternal(jsonRoot))
  149. return false;
  150. JSONValue import(JSONValue::emptyObject);
  151. jsonRoot.Set("PrefabImporter", import);
  152. return true;
  153. }
  154. Node* PrefabImporter::InstantiateNode(Node* parent, const String& name)
  155. {
  156. Node* node = parent->CreateChild(asset_->GetName());
  157. PrefabComponent* pc = node->CreateComponent<PrefabComponent>();
  158. pc->SetPrefabGUID(asset_->GetGUID());
  159. return node;
  160. }
  161. }