Asset.cpp 7.1 KB

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