Asset.cpp 8.9 KB

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