AssetImporter.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include <Atomic/IO/File.h>
  2. #include <Atomic/IO/FileSystem.h>
  3. #include "AssetDatabase.h"
  4. #include "AssetImporter.h"
  5. namespace ToolCore
  6. {
  7. AssetImporter::AssetImporter(Context* context) : Object(context),
  8. dirty_(true),
  9. timestamp_(0)
  10. {
  11. SetDefaults();
  12. }
  13. AssetImporter::~AssetImporter()
  14. {
  15. }
  16. void AssetImporter::SetDefaults()
  17. {
  18. }
  19. bool AssetImporter::Load(const String& guid)
  20. {
  21. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  22. Asset* asset = db->GetAssetByGUID(guid);
  23. assert(asset);
  24. const String& path = asset->GetPath();
  25. String assetPath = path + ".asset";
  26. SharedPtr<File> file(new File(context_, assetPath));
  27. json_ = new JSONFile(context_);
  28. json_->Load(*file);
  29. file->Close();
  30. LoadInternal();
  31. json_ = 0;
  32. return true;
  33. }
  34. bool AssetImporter::LoadInternal()
  35. {
  36. JSONValue root = json_->GetRoot();
  37. assert(root.GetInt("version") == 1);
  38. guid_ = root.GetString("guid");
  39. timestamp_ = (unsigned) root.GetDouble("timestamp");
  40. return true;
  41. }
  42. bool AssetImporter::Save(const String& guid)
  43. {
  44. FileSystem* fs = GetSubsystem<FileSystem>();
  45. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  46. Asset* asset = db->GetAssetByGUID(guid);
  47. assert(asset);
  48. const String& path = asset->GetPath();
  49. String assetPath = path + ".asset";
  50. timestamp_ = fs->GetLastModifiedTime(path);
  51. guid_ = guid;
  52. json_ = new JSONFile(context_);
  53. json_->CreateRoot();
  54. SaveInternal();
  55. SharedPtr<File> file(new File(context_, assetPath, FILE_WRITE));
  56. json_->Save(*file, String(" "));
  57. file->Close();
  58. json_ = 0;
  59. return true;
  60. }
  61. bool AssetImporter::SaveInternal()
  62. {
  63. JSONValue root = json_->GetRoot();
  64. root.SetInt("version", ASSET_VERSION);
  65. root.SetString("guid", guid_);
  66. root.SetDouble("timestamp", (double) timestamp_);
  67. return true;
  68. }
  69. }