AssetImporter.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. #include <Atomic/IO/Log.h>
  8. #include <Atomic/IO/File.h>
  9. #include <Atomic/IO/FileSystem.h>
  10. #include "AssetDatabase.h"
  11. #include "AssetImporter.h"
  12. namespace ToolCore
  13. {
  14. AssetImporter::AssetImporter(Context* context, Asset *asset) : Object(context),
  15. asset_(asset),
  16. requiresCacheFile_(true)
  17. {
  18. SetDefaults();
  19. }
  20. AssetImporter::~AssetImporter()
  21. {
  22. }
  23. void AssetImporter::SetDefaults()
  24. {
  25. }
  26. bool AssetImporter::LoadSettings(JSONValue& root)
  27. {
  28. LoadSettingsInternal(root);
  29. return true;
  30. }
  31. bool AssetImporter::LoadSettingsInternal(JSONValue& jsonRoot)
  32. {
  33. return true;
  34. }
  35. bool AssetImporter::SaveSettings(JSONValue& root)
  36. {
  37. SaveSettingsInternal(root);
  38. return true;
  39. }
  40. bool AssetImporter::SaveSettingsInternal(JSONValue& jsonRoot)
  41. {
  42. return true;
  43. }
  44. bool AssetImporter::Move(const String& newPath)
  45. {
  46. FileSystem* fs = GetSubsystem<FileSystem>();
  47. if (newPath == asset_->path_)
  48. return false;
  49. String oldPath = asset_->path_;
  50. String oldName = asset_->name_;
  51. String pathName, newName, ext;
  52. SplitPath(newPath, pathName, newName, ext);
  53. // rename asset first, ahead of the filesystem watcher, so the assetdatabase doesn't see a new asset
  54. asset_->name_ = newName;
  55. asset_->path_ = newPath;
  56. // first rename the .asset file
  57. if (!fs->Rename(oldPath + ".asset", newPath + ".asset"))
  58. {
  59. asset_->name_ = oldName;
  60. asset_->path_ = oldPath;
  61. LOGERRORF("Unable to rename asset: %s to %s", GetNativePath(oldPath + ".asset").CString(), GetNativePath(newPath + ".asset").CString());
  62. return false;
  63. }
  64. // now rename the asset file itself
  65. if (!fs->Rename(oldPath, newPath))
  66. {
  67. asset_->name_ = oldName;
  68. asset_->path_ = oldPath;
  69. // restore .asset
  70. fs->Rename(newPath + ".asset", oldPath + ".asset");
  71. LOGERRORF("Unable to rename: %s to %s", GetNativePath(oldPath).CString(), GetNativePath(newPath).CString());
  72. return false;
  73. }
  74. return true;
  75. }
  76. bool AssetImporter::Rename(const String& newName)
  77. {
  78. String pathName, fileName, ext;
  79. SplitPath(asset_->path_, pathName, fileName, ext);
  80. String newPath = pathName + newName + ext;
  81. FileSystem* fs = GetSubsystem<FileSystem>();
  82. if (fs->FileExists(newPath) || fs->DirExists(newPath))
  83. return false;
  84. return Move(newPath);
  85. }
  86. }