Asset.cpp 5.1 KB

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