Asset.cpp 5.4 KB

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