Asset.cpp 8.2 KB

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