Asset.cpp 7.0 KB

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