Asset.cpp 6.0 KB

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