Asset.cpp 7.7 KB

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