Asset.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 "CSharpImporter.h"
  44. #include "NETAssemblyImporter.h"
  45. #include "AssetEvents.h"
  46. #include "Asset.h"
  47. namespace ToolCore
  48. {
  49. Asset::Asset(Context* context) :
  50. Object(context),
  51. dirty_(false),
  52. isFolder_(false),
  53. fileTimestamp_(0xffffffff)
  54. {
  55. }
  56. Asset::~Asset()
  57. {
  58. }
  59. void Asset::UpdateFileTimestamp()
  60. {
  61. FileSystem* fs = GetSubsystem<FileSystem>();
  62. if (fs->FileExists(path_))
  63. {
  64. fileTimestamp_ = fs->GetLastModifiedTime(path_);
  65. }
  66. }
  67. Asset* Asset::GetParent()
  68. {
  69. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  70. String pathName;
  71. String fileName;
  72. String ext;
  73. SplitPath(path_, pathName, fileName, ext);
  74. return db->GetAssetByPath(RemoveTrailingSlash(pathName));
  75. }
  76. String Asset::GetRelativePath()
  77. {
  78. Project* project =GetSubsystem<ToolSystem>()->GetProject();
  79. String path = path_;
  80. path.Replace(project->GetResourcePath(), "", false);
  81. return path;
  82. }
  83. bool Asset::CheckCacheFile()
  84. {
  85. if (importer_.Null())
  86. return true;
  87. FileSystem* fs = GetSubsystem<FileSystem>();
  88. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  89. String cachePath = db->GetCachePath();
  90. String cacheFile = cachePath + guid_;
  91. unsigned modifiedTime = fs->GetLastModifiedTime(path_);
  92. if (importer_->RequiresCacheFile()) {
  93. if (!fs->FileExists(cacheFile) || fs->GetLastModifiedTime(cacheFile) < modifiedTime)
  94. return false;
  95. }
  96. if (fs->GetLastModifiedTime(GetDotAssetFilename()) < modifiedTime)
  97. {
  98. return false;
  99. }
  100. return true;
  101. }
  102. bool Asset::Import()
  103. {
  104. if (importer_.Null())
  105. return true;
  106. return importer_->Import();
  107. }
  108. bool Asset::Preload()
  109. {
  110. if (importer_.Null())
  111. return true;
  112. // disabled preload for now, as this is on a background thread and causing init problems
  113. return true;
  114. //return importer_->Preload();
  115. }
  116. void Asset::PostImportError(const String& message)
  117. {
  118. VariantMap eventData;
  119. eventData[AssetImportError::P_PATH] = path_;
  120. eventData[AssetImportError::P_GUID] = guid_;
  121. eventData[AssetImportError::P_ERROR] = message;
  122. SendEvent(E_ASSETIMPORTERROR, eventData);
  123. }
  124. // load .asset
  125. bool Asset::Load()
  126. {
  127. FileSystem* fs = GetSubsystem<FileSystem>();
  128. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  129. String assetFilename = GetDotAssetFilename();
  130. SharedPtr<File> file(new File(context_, assetFilename));
  131. json_ = new JSONFile(context_);
  132. json_->Load(*file);
  133. file->Close();
  134. JSONValue& root = json_->GetRoot();
  135. assert(root.Get("version").GetInt() == ASSET_VERSION);
  136. guid_ = root.Get("guid").GetString();
  137. db->RegisterGUID(guid_);
  138. dirty_ = false;
  139. if (!CheckCacheFile())
  140. {
  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. else 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. importer_ = new NETAssemblyImporter(context_, this);
  254. }
  255. else if (ext == ".cs")
  256. {
  257. importer_ = new CSharpImporter(context_, this);
  258. }
  259. else if (textureFormats.Contains(ext))
  260. {
  261. importer_ = new TextureImporter(context_, this);
  262. }
  263. else
  264. {
  265. importer_ = new AssetImporter(context_, this);
  266. }
  267. }
  268. if (importer_.Null())
  269. return false;
  270. return true;
  271. }
  272. String Asset::GetCachePath() const
  273. {
  274. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  275. String cachePath = db->GetCachePath();
  276. cachePath += guid_;
  277. return cachePath;
  278. }
  279. String Asset::GetExtension() const
  280. {
  281. return Atomic::GetExtension(path_);
  282. }
  283. bool Asset::SetPath(const String& path)
  284. {
  285. assert(!guid_.Length());
  286. assert(!path_.Length());
  287. // need to update path, not set, which should only be done on first import
  288. assert(importer_.Null());
  289. FileSystem* fs = GetSubsystem<FileSystem>();
  290. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  291. path_ = path;
  292. // create importer based on path
  293. if (!CreateImporter())
  294. return false;
  295. String assetFilename = GetDotAssetFilename();
  296. if (fs->FileExists(assetFilename))
  297. {
  298. // load the json, todo: handle fail
  299. Load();
  300. }
  301. else
  302. {
  303. dirty_ = true;
  304. guid_ = db->GenerateAssetGUID();
  305. Save();
  306. }
  307. // TODO: handle failed
  308. return true;
  309. }
  310. bool Asset::Move(const String& newPath)
  311. {
  312. if (importer_.Null())
  313. return false;
  314. String oldPath = path_;
  315. bool result = importer_->Move(newPath);
  316. if (result)
  317. {
  318. VariantMap eventData;
  319. eventData[AssetMoved::P_ASSET] = this;
  320. eventData[AssetMoved::P_OLDPATH] = oldPath;
  321. SendEvent(E_ASSETMOVED, eventData);
  322. }
  323. return result;
  324. }
  325. bool Asset::Rename(const String& newName)
  326. {
  327. if (importer_.Null())
  328. return false;
  329. bool result = importer_->Rename(newName);
  330. if (result)
  331. {
  332. VariantMap eventData;
  333. eventData[AssetRenamed::P_ASSET] = this;
  334. SendEvent(E_ASSETRENAMED, eventData);
  335. }
  336. return result;
  337. }
  338. Resource* Asset::GetResource(const String &typeName)
  339. {
  340. if (importer_)
  341. return importer_->GetResource(typeName);
  342. return 0;
  343. }
  344. Node* Asset::InstantiateNode(Node* parent, const String& name)
  345. {
  346. if (!parent)
  347. return 0;
  348. if (importer_)
  349. return importer_->InstantiateNode(parent, name);
  350. return 0;
  351. }
  352. }