Asset.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 "ModelImporter.h"
  8. #include "FolderImporter.h"
  9. #include "SceneImporter.h"
  10. #include "MaterialImporter.h"
  11. #include "TextureImporter.h"
  12. #include "PrefabImporter.h"
  13. #include "JavascriptImporter.h"
  14. #include "Asset.h"
  15. namespace ToolCore
  16. {
  17. Asset::Asset(Context* context) :
  18. Object(context),
  19. dirty_(false),
  20. isFolder_(false)
  21. {
  22. }
  23. Asset::~Asset()
  24. {
  25. }
  26. Asset* Asset::GetParent()
  27. {
  28. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  29. String pathName;
  30. String fileName;
  31. String ext;
  32. SplitPath(path_, pathName, fileName, ext);
  33. return db->GetAssetByPath(RemoveTrailingSlash(pathName));
  34. }
  35. String Asset::GetRelativePath()
  36. {
  37. Project* project =GetSubsystem<ToolSystem>()->GetProject();
  38. String path = path_;
  39. path.Replace(project->GetResourcePath(), "", false);
  40. return path;
  41. }
  42. bool Asset::CheckCacheFile()
  43. {
  44. if (importer_.Null() || !importer_->RequiresCacheFile())
  45. return true;
  46. FileSystem* fs = GetSubsystem<FileSystem>();
  47. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  48. String cachePath = db->GetCachePath();
  49. String cacheFile = cachePath + guid_;
  50. if (!fs->FileExists(cacheFile) || fs->GetLastModifiedTime(cacheFile) < fs->GetLastModifiedTime(path_))
  51. return false;
  52. return true;
  53. }
  54. bool Asset::Import()
  55. {
  56. if (importer_.Null())
  57. return true;
  58. return importer_->Import(guid_);
  59. }
  60. bool Asset::Preload()
  61. {
  62. if (importer_.Null())
  63. return true;
  64. return importer_->Preload();
  65. }
  66. // load .asset
  67. bool Asset::Load()
  68. {
  69. FileSystem* fs = GetSubsystem<FileSystem>();
  70. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  71. String assetFilename = GetDotAssetFilename();
  72. SharedPtr<File> file(new File(context_, assetFilename));
  73. json_ = new JSONFile(context_);
  74. json_->Load(*file);
  75. file->Close();
  76. JSONValue root = json_->GetRoot();
  77. assert(root.GetInt("version") == ASSET_VERSION);
  78. guid_ = root.GetString("guid");
  79. db->RegisterGUID(guid_);
  80. dirty_ = false;
  81. if (!CheckCacheFile())
  82. {
  83. LOGINFOF("CheckCacheFile:false - %s", path_.CString());
  84. dirty_ = true;
  85. }
  86. // handle import
  87. if (importer_.NotNull())
  88. importer_->LoadSettings(root);
  89. json_ = 0;
  90. return true;
  91. }
  92. // save .asset
  93. bool Asset::Save()
  94. {
  95. FileSystem* fs = GetSubsystem<FileSystem>();
  96. String assetFilename = GetDotAssetFilename();
  97. json_ = new JSONFile(context_);
  98. JSONValue root = json_->CreateRoot();
  99. root.SetInt("version", ASSET_VERSION);
  100. root.SetString("guid", guid_);
  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. Save();
  200. }
  201. // TODO: handle failed
  202. return true;
  203. }
  204. }