Asset.cpp 8.8 KB

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