Asset.cpp 6.5 KB

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