Asset.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/IO/Log.h>
  23. #include <Atomic/IO/File.h>
  24. #include <Atomic/IO/FileSystem.h>
  25. #include "../ToolSystem.h"
  26. #include "../Project/Project.h"
  27. #include "AssetDatabase.h"
  28. #include "AudioImporter.h"
  29. #include "ModelImporter.h"
  30. #include "FolderImporter.h"
  31. #include "SceneImporter.h"
  32. #include "MaterialImporter.h"
  33. #include "TextureImporter.h"
  34. #include "PrefabImporter.h"
  35. #include "JavascriptImporter.h"
  36. #include "JSONImporter.h"
  37. #include "SpriterImporter.h"
  38. #include "TMXImporter.h"
  39. #include "PEXImporter.h"
  40. #include "TextImporter.h"
  41. #include "NETAssemblyImporter.h"
  42. #include "TypeScriptImporter.h"
  43. #include "ParticleEffectImporter.h"
  44. #include "AssetEvents.h"
  45. #include "Asset.h"
  46. namespace ToolCore
  47. {
  48. Asset::Asset(Context* context) :
  49. Object(context),
  50. dirty_(false),
  51. isFolder_(false),
  52. fileTimestamp_(0xffffffff)
  53. {
  54. }
  55. Asset::~Asset()
  56. {
  57. }
  58. void Asset::UpdateFileTimestamp()
  59. {
  60. FileSystem* fs = GetSubsystem<FileSystem>();
  61. if (fs->FileExists(path_))
  62. {
  63. fileTimestamp_ = fs->GetLastModifiedTime(path_);
  64. }
  65. }
  66. Asset* Asset::GetParent()
  67. {
  68. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  69. String pathName;
  70. String fileName;
  71. String ext;
  72. SplitPath(path_, pathName, fileName, ext);
  73. return db->GetAssetByPath(RemoveTrailingSlash(pathName));
  74. }
  75. String Asset::GetRelativePath()
  76. {
  77. Project* project =GetSubsystem<ToolSystem>()->GetProject();
  78. String path = path_;
  79. path.Replace(project->GetResourcePath(), "", false);
  80. return path;
  81. }
  82. bool Asset::CheckCacheFile()
  83. {
  84. if (importer_.Null())
  85. return true;
  86. FileSystem* fs = GetSubsystem<FileSystem>();
  87. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  88. String cachePath = db->GetCachePath();
  89. String cacheFile = cachePath + guid_;
  90. unsigned modifiedTime = fs->GetLastModifiedTime(path_);
  91. if (importer_->RequiresCacheFile()) {
  92. if (!fs->FileExists(cacheFile) || fs->GetLastModifiedTime(cacheFile) < modifiedTime)
  93. return false;
  94. }
  95. if (fs->GetLastModifiedTime(GetDotAssetFilename()) < modifiedTime)
  96. {
  97. return false;
  98. }
  99. return true;
  100. }
  101. bool Asset::Import()
  102. {
  103. if (importer_.Null())
  104. return true;
  105. return importer_->Import();
  106. }
  107. bool Asset::Preload()
  108. {
  109. if (importer_.Null())
  110. return true;
  111. // disabled preload for now, as this is on a background thread and causing init problems
  112. return true;
  113. //return importer_->Preload();
  114. }
  115. void Asset::PostImportError(const String& message)
  116. {
  117. VariantMap eventData;
  118. eventData[AssetImportError::P_PATH] = path_;
  119. eventData[AssetImportError::P_GUID] = guid_;
  120. eventData[AssetImportError::P_ERROR] = message;
  121. SendEvent(E_ASSETIMPORTERROR, eventData);
  122. }
  123. // load .asset
  124. bool Asset::Load()
  125. {
  126. FileSystem* fs = GetSubsystem<FileSystem>();
  127. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  128. String assetFilename = GetDotAssetFilename();
  129. SharedPtr<File> file(new File(context_, assetFilename));
  130. json_ = new JSONFile(context_);
  131. json_->Load(*file);
  132. file->Close();
  133. JSONValue root = json_->GetRoot();
  134. assert(root.Get("version").GetInt() == ASSET_VERSION);
  135. guid_ = root.Get("guid").GetString();
  136. db->RegisterGUID(guid_);
  137. dirty_ = false;
  138. if (!CheckCacheFile())
  139. {
  140. LOGINFOF("CheckCacheFile:false - %s", path_.CString());
  141. dirty_ = true;
  142. }
  143. // handle import
  144. if (importer_.NotNull())
  145. importer_->LoadSettings(root);
  146. json_ = 0;
  147. return true;
  148. }
  149. // save .asset
  150. bool Asset::Save()
  151. {
  152. FileSystem* fs = GetSubsystem<FileSystem>();
  153. String assetFilename = GetDotAssetFilename();
  154. json_ = new JSONFile(context_);
  155. JSONValue& root = json_->GetRoot();
  156. root.Set("version", JSONValue(ASSET_VERSION));
  157. root.Set("guid", JSONValue(guid_));
  158. // handle import
  159. if (importer_.NotNull())
  160. {
  161. importer_->SaveSettings(root);
  162. SharedPtr<File> file(new File(context_, assetFilename, FILE_WRITE));
  163. json_->Save(*file);
  164. file->Close();
  165. }
  166. json_ = 0;
  167. return true;
  168. }
  169. String Asset::GetDotAssetFilename()
  170. {
  171. assert(path_.Length());
  172. FileSystem* fs = GetSubsystem<FileSystem>();
  173. String assetFilename = path_ + ".asset";
  174. if (fs->DirExists(path_)) {
  175. assetFilename = RemoveTrailingSlash(path_) + ".asset";
  176. }
  177. return assetFilename;
  178. }
  179. bool Asset::CreateImporter()
  180. {
  181. assert(importer_.Null());
  182. FileSystem* fs = GetSubsystem<FileSystem>();
  183. if (fs->DirExists(path_))
  184. {
  185. name_ = GetFileName(RemoveTrailingSlash(path_));
  186. isFolder_ = true;
  187. importer_ = new FolderImporter(context_, this);
  188. }
  189. else
  190. {
  191. String ext = Atomic::GetExtension(path_);
  192. name_ = GetFileName(path_);
  193. Vector<String> textureFormats;
  194. textureFormats.Push(".jpg");
  195. textureFormats.Push(".png");
  196. textureFormats.Push(".tga");
  197. textureFormats.Push(".dds");
  198. // todo, externalize recognizers
  199. if (ext == ".fbx" || ext == ".blend" || ext == ".dae" || ext == ".mdl")
  200. {
  201. importer_ = new ModelImporter(context_, this);
  202. }
  203. if (ext == ".ogg" || ext == ".wav")
  204. {
  205. importer_ = new AudioImporter(context_, this);
  206. }
  207. else if (ext == ".prefab")
  208. {
  209. importer_ = new PrefabImporter(context_, this);
  210. }
  211. else if (ext == ".js")
  212. {
  213. importer_ = new JavascriptImporter(context_, this);
  214. }
  215. else if (ext == ".ts")
  216. {
  217. importer_ = new TypeScriptImporter(context_, this);
  218. }
  219. else if (ext == ".json")
  220. {
  221. importer_ = new JSONImporter(context_, this);
  222. }
  223. else if (ext == ".scene")
  224. {
  225. importer_ = new SceneImporter(context_, this);
  226. }
  227. else if (ext == ".material")
  228. {
  229. importer_ = new MaterialImporter(context_, this);
  230. }
  231. else if (ext == ".scml")
  232. {
  233. importer_ = new SpriterImporter(context_, this);
  234. }
  235. else if (ext == ".tmx")
  236. {
  237. importer_ = new TMXImporter(context_, this);
  238. }
  239. else if (ext == ".pex")
  240. {
  241. importer_ = new PEXImporter(context_, this);
  242. }
  243. else if (ext == ".peffect")
  244. {
  245. importer_ = new ParticleEffectImporter(context_, this);
  246. }
  247. else if (ext == ".txt" || ext == ".xml" || ext == ".hlsl" || ext == ".glsl")
  248. {
  249. importer_ = new TextImporter(context_, this);
  250. }
  251. else if (ext == ".dll")
  252. {
  253. // TODO: check for native dll
  254. #ifdef ATOMIC_DOTNET
  255. importer_ = new NETAssemblyImporter(context_, this);
  256. #endif
  257. }
  258. else if (textureFormats.Contains(ext))
  259. {
  260. importer_ = new TextureImporter(context_, this);
  261. }
  262. }
  263. if (importer_.Null())
  264. return false;
  265. return true;
  266. }
  267. String Asset::GetCachePath() const
  268. {
  269. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  270. String cachePath = db->GetCachePath();
  271. cachePath += guid_;
  272. return cachePath;
  273. }
  274. String Asset::GetExtension() const
  275. {
  276. return Atomic::GetExtension(path_);
  277. }
  278. bool Asset::SetPath(const String& path)
  279. {
  280. assert(!guid_.Length());
  281. assert(!path_.Length());
  282. // need to update path, not set, which should only be done on first import
  283. assert(importer_.Null());
  284. FileSystem* fs = GetSubsystem<FileSystem>();
  285. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  286. path_ = path;
  287. // create importer based on path
  288. if (!CreateImporter())
  289. return false;
  290. String assetFilename = GetDotAssetFilename();
  291. if (fs->FileExists(assetFilename))
  292. {
  293. // load the json, todo: handle fail
  294. Load();
  295. }
  296. else
  297. {
  298. dirty_ = true;
  299. guid_ = db->GenerateAssetGUID();
  300. Save();
  301. }
  302. // TODO: handle failed
  303. return true;
  304. }
  305. bool Asset::Move(const String& newPath)
  306. {
  307. if (importer_.Null())
  308. return false;
  309. String oldPath = path_;
  310. bool result = importer_->Move(newPath);
  311. if (result)
  312. {
  313. VariantMap eventData;
  314. eventData[AssetMoved::P_ASSET] = this;
  315. eventData[AssetMoved::P_OLDPATH] = oldPath;
  316. SendEvent(E_ASSETMOVED, eventData);
  317. }
  318. return result;
  319. }
  320. bool Asset::Rename(const String& newName)
  321. {
  322. if (importer_.Null())
  323. return false;
  324. bool result = importer_->Rename(newName);
  325. if (result)
  326. {
  327. VariantMap eventData;
  328. eventData[AssetRenamed::P_ASSET] = this;
  329. SendEvent(E_ASSETRENAMED, eventData);
  330. }
  331. return result;
  332. }
  333. Resource* Asset::GetResource(const String &typeName)
  334. {
  335. if (importer_)
  336. return importer_->GetResource(typeName);
  337. return 0;
  338. }
  339. Node* Asset::InstantiateNode(Node* parent, const String& name)
  340. {
  341. if (!parent)
  342. return 0;
  343. if (importer_)
  344. return importer_->InstantiateNode(parent, name);
  345. return 0;
  346. }
  347. }