Asset.cpp 7.2 KB

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