PrefabImporter.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <Atomic/Resource/ResourceCache.h>
  2. #include <Atomic/Resource/XMLFile.h>
  3. #include <Atomic/Scene/Scene.h>
  4. #include <Atomic/Scene/PrefabEvents.h>
  5. #include <Atomic/Scene/PrefabComponent.h>
  6. #include <Atomic/IO/FileSystem.h>
  7. #include "Asset.h"
  8. #include "AssetDatabase.h"
  9. #include "PrefabImporter.h"
  10. namespace ToolCore
  11. {
  12. PrefabImporter::PrefabImporter(Context* context, Asset* asset) : AssetImporter(context, asset)
  13. {
  14. SubscribeToEvent(E_PREFABSAVE, HANDLER(PrefabImporter, HandlePrefabSave));
  15. }
  16. PrefabImporter::~PrefabImporter()
  17. {
  18. }
  19. void PrefabImporter::SetDefaults()
  20. {
  21. AssetImporter::SetDefaults();
  22. }
  23. bool PrefabImporter::Preload()
  24. {
  25. if (!asset_)
  26. return false;
  27. preloadResourceScene_ = new Scene(context_);
  28. SharedPtr<File> file(new File(context_, asset_->GetCachePath()));
  29. preloadResourceScene_->LoadAsyncXML(file, LOAD_RESOURCES_ONLY);
  30. return true;
  31. }
  32. void PrefabImporter::HandlePrefabSave(StringHash eventType, VariantMap& eventData)
  33. {
  34. using namespace PrefabSave;
  35. PrefabComponent* component = static_cast<PrefabComponent*>(eventData[P_PREFABCOMPONENT].GetPtr());
  36. if (component->GetPrefabGUID() != asset_->GetGUID())
  37. return;
  38. Node* node = component->GetPrefabNode();
  39. node->SetPosition(Vector3::ZERO);
  40. node->SetRotation(Quaternion::IDENTITY);
  41. node->SetScale(Vector3::ONE);
  42. SharedPtr<File> file(new File(context_, asset_->GetPath(), FILE_WRITE));
  43. node->SaveXML(*file);
  44. file->Close();
  45. FileSystem* fs = GetSubsystem<FileSystem>();
  46. fs->Copy(asset_->GetPath(), asset_->GetCachePath());
  47. // reload it immediately so it is ready for use
  48. // TODO: The resource cache is reloading after this reload due to catching the file cache
  49. ResourceCache* cache = GetSubsystem<ResourceCache>();
  50. XMLFile* xmlfile = cache->GetResource<XMLFile>(asset_->GetGUID());
  51. cache->ReloadResource(xmlfile);
  52. VariantMap changedData;
  53. changedData[PrefabChanged::P_GUID] = asset_->GetGUID();
  54. SendEvent(E_PREFABCHANGED, changedData);
  55. }
  56. bool PrefabImporter::Import(const String& guid)
  57. {
  58. FileSystem* fs = GetSubsystem<FileSystem>();
  59. fs->Copy(asset_->GetPath(), asset_->GetCachePath());
  60. return true;
  61. }
  62. bool PrefabImporter::LoadSettingsInternal()
  63. {
  64. if (!AssetImporter::LoadSettingsInternal())
  65. return false;
  66. JSONValue import = jsonRoot_.GetChild("PrefabImporter", JSON_OBJECT);
  67. return true;
  68. }
  69. bool PrefabImporter::SaveSettingsInternal()
  70. {
  71. if (!AssetImporter::SaveSettingsInternal())
  72. return false;
  73. JSONValue import = jsonRoot_.CreateChild("PrefabImporter");
  74. return true;
  75. }
  76. }