Asset.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #include <Atomic/IO/File.h>
  2. #include <Atomic/IO/FileSystem.h>
  3. #include "AssetDatabase.h"
  4. #include "ModelImporter.h"
  5. #include "FolderImporter.h"
  6. #include "SceneImporter.h"
  7. #include "MaterialImporter.h"
  8. #include "Asset.h"
  9. namespace ToolCore
  10. {
  11. Asset::Asset(Context* context) :
  12. Object(context),
  13. dirty_(false),
  14. isFolder_(false)
  15. {
  16. }
  17. Asset::~Asset()
  18. {
  19. }
  20. bool Asset::CheckCacheFile()
  21. {
  22. FileSystem* fs = GetSubsystem<FileSystem>();
  23. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  24. String cachePath = db->GetCachePath();
  25. String cacheFile = cachePath + guid_;
  26. if (!fs->FileExists(cacheFile))
  27. return false;
  28. return true;
  29. }
  30. bool Asset::Import()
  31. {
  32. if (importer_.Null())
  33. return true;
  34. return importer_->Import(guid_);
  35. }
  36. // load .asset
  37. bool Asset::Load()
  38. {
  39. FileSystem* fs = GetSubsystem<FileSystem>();
  40. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  41. String assetFilename = GetDotAssetFilename();
  42. SharedPtr<File> file(new File(context_, assetFilename));
  43. json_ = new JSONFile(context_);
  44. json_->Load(*file);
  45. file->Close();
  46. JSONValue root = json_->GetRoot();
  47. assert(root.GetInt("version") == ASSET_VERSION);
  48. guid_ = root.GetString("guid");
  49. db->RegisterGUID(guid_);
  50. timestamp_ = (unsigned) root.GetDouble("timestamp");
  51. dirty_ = false;
  52. if (!CheckCacheFile() || timestamp_ < fs->GetLastModifiedTime(path_))
  53. dirty_ = true;
  54. // handle import
  55. if (importer_.NotNull())
  56. importer_->LoadSettings(root);
  57. json_ = 0;
  58. return true;
  59. }
  60. // save .asset
  61. bool Asset::Save()
  62. {
  63. String assetFilename = GetDotAssetFilename();
  64. json_ = new JSONFile(context_);
  65. JSONValue root = json_->CreateRoot();
  66. root.SetInt("version", ASSET_VERSION);
  67. root.SetString("guid", guid_);
  68. root.SetDouble("timestamp", (double) timestamp_);
  69. // handle import
  70. if (importer_.NotNull())
  71. {
  72. importer_->SaveSettings(root);
  73. SharedPtr<File> file(new File(context_, assetFilename, FILE_WRITE));
  74. json_->Save(*file);
  75. file->Close();
  76. }
  77. json_ = 0;
  78. return true;
  79. }
  80. String Asset::GetDotAssetFilename()
  81. {
  82. assert(path_.Length());
  83. FileSystem* fs = GetSubsystem<FileSystem>();
  84. String assetFilename = path_ + ".asset";
  85. if (fs->DirExists(path_)) {
  86. assetFilename = RemoveTrailingSlash(path_) + ".asset";
  87. }
  88. return assetFilename;
  89. }
  90. bool Asset::CreateImporter()
  91. {
  92. assert(importer_.Null());
  93. FileSystem* fs = GetSubsystem<FileSystem>();
  94. if (fs->DirExists(path_))
  95. {
  96. name_ = GetFileName(RemoveTrailingSlash(path_));
  97. isFolder_ = true;
  98. importer_ = new FolderImporter(context_);
  99. }
  100. else
  101. {
  102. String ext = GetExtension(path_);
  103. name_ = GetFileName(path_);
  104. // todo, externalize recognizers
  105. if (ext == ".fbx")
  106. {
  107. importer_ = new ModelImporter(context_);
  108. }
  109. else if (ext == ".scene")
  110. {
  111. importer_ = new SceneImporter(context_);
  112. }
  113. else if (ext == ".material")
  114. {
  115. importer_ = new MaterialImporter(context_);
  116. }
  117. }
  118. if (importer_.Null())
  119. return false;
  120. return true;
  121. }
  122. bool Asset::SetPath(const String& path)
  123. {
  124. assert(!guid_.Length());
  125. assert(!path_.Length());
  126. // need to update path, not set, which should only be done on first import
  127. assert(importer_.Null());
  128. FileSystem* fs = GetSubsystem<FileSystem>();
  129. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  130. path_ = path;
  131. // create importer based on path
  132. if (!CreateImporter())
  133. return false;
  134. String assetFilename = GetDotAssetFilename();
  135. if (fs->FileExists(assetFilename))
  136. {
  137. // load the json, todo: handle fail
  138. Load();
  139. }
  140. else
  141. {
  142. dirty_ = true;
  143. guid_ = db->GenerateAssetGUID();
  144. timestamp_ = fs->GetLastModifiedTime(path);
  145. Save();
  146. }
  147. // TODO: handle failed
  148. return true;
  149. }
  150. }