Asset.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. timestamp_ = root.GetUInt("timestamp");
  81. dirty_ = false;
  82. if (!CheckCacheFile())
  83. {
  84. LOGINFOF("CheckCacheFile:false - %s", path_.CString());
  85. dirty_ = true;
  86. }
  87. else if (timestamp_ < fs->GetLastModifiedTime(path_))
  88. {
  89. LOGINFOF("Timestamp:false - %u vs modified %u - %s", timestamp_, fs->GetLastModifiedTime(path_), path_.CString());
  90. dirty_ = true;
  91. }
  92. // handle import
  93. if (importer_.NotNull())
  94. importer_->LoadSettings(root);
  95. json_ = 0;
  96. return true;
  97. }
  98. // save .asset
  99. bool Asset::Save()
  100. {
  101. FileSystem* fs = GetSubsystem<FileSystem>();
  102. String assetFilename = GetDotAssetFilename();
  103. json_ = new JSONFile(context_);
  104. JSONValue root = json_->CreateRoot();
  105. root.SetInt("version", ASSET_VERSION);
  106. root.SetString("guid", guid_);
  107. // update this where?
  108. timestamp_ = fs->GetLastModifiedTime(path_);
  109. root.SetUInt("timestamp", timestamp_);
  110. // handle import
  111. if (importer_.NotNull())
  112. {
  113. importer_->SaveSettings(root);
  114. SharedPtr<File> file(new File(context_, assetFilename, FILE_WRITE));
  115. json_->Save(*file);
  116. file->Close();
  117. }
  118. json_ = 0;
  119. return true;
  120. }
  121. String Asset::GetDotAssetFilename()
  122. {
  123. assert(path_.Length());
  124. FileSystem* fs = GetSubsystem<FileSystem>();
  125. String assetFilename = path_ + ".asset";
  126. if (fs->DirExists(path_)) {
  127. assetFilename = RemoveTrailingSlash(path_) + ".asset";
  128. }
  129. return assetFilename;
  130. }
  131. bool Asset::CreateImporter()
  132. {
  133. assert(importer_.Null());
  134. FileSystem* fs = GetSubsystem<FileSystem>();
  135. if (fs->DirExists(path_))
  136. {
  137. name_ = GetFileName(RemoveTrailingSlash(path_));
  138. isFolder_ = true;
  139. importer_ = new FolderImporter(context_, this);
  140. }
  141. else
  142. {
  143. String ext = GetExtension(path_);
  144. name_ = GetFileName(path_);
  145. Vector<String> textureFormats;
  146. textureFormats.Push(".jpg");
  147. textureFormats.Push(".png");
  148. textureFormats.Push(".tga");
  149. // todo, externalize recognizers
  150. if (ext == ".fbx" || ext == ".blend" || ext == ".dae")
  151. {
  152. importer_ = new ModelImporter(context_, this);
  153. }
  154. else if (ext == ".prefab")
  155. {
  156. importer_ = new PrefabImporter(context_, this);
  157. }
  158. else if (ext == ".js")
  159. {
  160. importer_ = new JavascriptImporter(context_, this);
  161. }
  162. else if (ext == ".scene")
  163. {
  164. importer_ = new SceneImporter(context_, this);
  165. }
  166. else if (ext == ".material")
  167. {
  168. importer_ = new MaterialImporter(context_, this);
  169. }
  170. else if (textureFormats.Contains(ext))
  171. {
  172. importer_ = new TextureImporter(context_, this);
  173. }
  174. }
  175. if (importer_.Null())
  176. return false;
  177. return true;
  178. }
  179. String Asset::GetCachePath() const
  180. {
  181. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  182. String cachePath = db->GetCachePath();
  183. cachePath += guid_;
  184. return cachePath;
  185. }
  186. bool Asset::SetPath(const String& path)
  187. {
  188. assert(!guid_.Length());
  189. assert(!path_.Length());
  190. // need to update path, not set, which should only be done on first import
  191. assert(importer_.Null());
  192. FileSystem* fs = GetSubsystem<FileSystem>();
  193. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  194. path_ = path;
  195. // create importer based on path
  196. if (!CreateImporter())
  197. return false;
  198. String assetFilename = GetDotAssetFilename();
  199. if (fs->FileExists(assetFilename))
  200. {
  201. // load the json, todo: handle fail
  202. Load();
  203. }
  204. else
  205. {
  206. dirty_ = true;
  207. guid_ = db->GenerateAssetGUID();
  208. timestamp_ = fs->GetLastModifiedTime(path);
  209. Save();
  210. }
  211. // TODO: handle failed
  212. return true;
  213. }
  214. }