Asset.cpp 6.2 KB

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