AssetImporter.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/IO/Log.h>
  23. #include <Atomic/IO/File.h>
  24. #include <Atomic/IO/FileSystem.h>
  25. #include "AssetDatabase.h"
  26. #include "AssetImporter.h"
  27. namespace ToolCore
  28. {
  29. AssetImporter::AssetImporter(Context* context, Asset *asset) : Object(context),
  30. asset_(asset),
  31. requiresCacheFile_(false)
  32. {
  33. SetDefaults();
  34. }
  35. AssetImporter::~AssetImporter()
  36. {
  37. }
  38. void AssetImporter::SetDefaults()
  39. {
  40. }
  41. bool AssetImporter::LoadSettings(JSONValue& root)
  42. {
  43. LoadSettingsInternal(root);
  44. return true;
  45. }
  46. bool AssetImporter::LoadSettingsInternal(JSONValue& jsonRoot)
  47. {
  48. return true;
  49. }
  50. bool AssetImporter::SaveSettings(JSONValue& root)
  51. {
  52. SaveSettingsInternal(root);
  53. return true;
  54. }
  55. bool AssetImporter::SaveSettingsInternal(JSONValue& jsonRoot)
  56. {
  57. return true;
  58. }
  59. bool AssetImporter::Move(const String& newPath)
  60. {
  61. FileSystem* fs = GetSubsystem<FileSystem>();
  62. if (newPath == asset_->path_)
  63. return false;
  64. String oldPath = asset_->path_;
  65. String oldName = asset_->name_;
  66. String pathName, newName, ext;
  67. SplitPath(newPath, pathName, newName, ext);
  68. // rename asset first, ahead of the filesystem watcher, so the assetdatabase doesn't see a new asset
  69. asset_->name_ = newName;
  70. asset_->path_ = newPath;
  71. // first rename the .asset file
  72. if (!fs->Rename(oldPath + ".asset", newPath + ".asset"))
  73. {
  74. asset_->name_ = oldName;
  75. asset_->path_ = oldPath;
  76. ATOMIC_LOGERRORF("Unable to rename asset: %s to %s", GetNativePath(oldPath + ".asset").CString(), GetNativePath(newPath + ".asset").CString());
  77. return false;
  78. }
  79. // now rename the asset file itself
  80. if (!fs->Rename(oldPath, newPath))
  81. {
  82. asset_->name_ = oldName;
  83. asset_->path_ = oldPath;
  84. // restore .asset
  85. fs->Rename(newPath + ".asset", oldPath + ".asset");
  86. ATOMIC_LOGERRORF("Unable to rename: %s to %s", GetNativePath(oldPath).CString(), GetNativePath(newPath).CString());
  87. return false;
  88. }
  89. return true;
  90. }
  91. bool AssetImporter::Rename(const String& newName)
  92. {
  93. String pathName, fileName, ext;
  94. SplitPath(asset_->path_, pathName, fileName, ext);
  95. String newPath = pathName + newName + ext;
  96. FileSystem* fs = GetSubsystem<FileSystem>();
  97. if (fs->FileExists(newPath) || fs->DirExists(newPath))
  98. return false;
  99. return Move(newPath);
  100. }
  101. }