Asset.cpp 4.9 KB

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