Asset.cpp 4.4 KB

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