AssetImporter.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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::Rename(const String& newName)
  45. {
  46. String pathName, fileName, ext;
  47. SplitPath(asset_->path_, pathName, fileName, ext);
  48. String newPath = pathName + newName + ext;
  49. FileSystem* fs = GetSubsystem<FileSystem>();
  50. if (fs->FileExists(newPath) || fs->DirExists(newPath))
  51. return false;
  52. // rename asset first, ahead of the filesystem watcher
  53. String oldPath = asset_->path_;
  54. asset_->name_ = newName;
  55. asset_->path_ = newPath;
  56. // first rename the .asset file
  57. if (!fs->Rename(oldPath + ".asset", newPath + ".asset"))
  58. {
  59. LOGERRORF("Unable to rename asset: %s to %s", GetNativePath(oldPath + ".asset").CString(), GetNativePath(newPath + ".asset").CString());
  60. return false;
  61. }
  62. // now rename the asset file itself
  63. if (!fs->Rename(oldPath, newPath))
  64. {
  65. // restore .asset
  66. fs->Rename(newPath + ".asset", oldPath + ".asset");
  67. LOGERRORF("Unable to rename: %s to %s", GetNativePath(oldPath).CString(), GetNativePath(newPath).CString());
  68. return false;
  69. }
  70. return true;
  71. }
  72. }