Asset.cpp 5.7 KB

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