Asset.cpp 5.8 KB

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