Asset.cpp 9.7 KB

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