AssetDatabase.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #include <Poco/MD5Engine.h>
  2. #include <Atomic/IO/Log.h>
  3. #include <Atomic/IO/File.h>
  4. #include <Atomic/IO/FileSystem.h>
  5. #include <Atomic/Resource/ResourceCache.h>
  6. #include "../ToolEvents.h"
  7. #include "../ToolSystem.h"
  8. #include "../Project/Project.h"
  9. #include "AssetDatabase.h"
  10. namespace ToolCore
  11. {
  12. AssetDatabase::AssetDatabase(Context* context) : Object(context)
  13. {
  14. SubscribeToEvent(E_PROJECTLOADED, HANDLER(AssetDatabase, HandleProjectLoaded));
  15. }
  16. AssetDatabase::~AssetDatabase()
  17. {
  18. }
  19. String AssetDatabase::GetCachePath()
  20. {
  21. if (project_.Null())
  22. return String::EMPTY;
  23. return project_->GetProjectPath() + "Cache/";
  24. }
  25. String AssetDatabase::GeneratePathGUID(const String &path)
  26. {
  27. FileSystem* fs = GetSubsystem<FileSystem>();
  28. Poco::MD5Engine md5;
  29. if (fs->DirExists(path))
  30. {
  31. md5.update(path.CString(), path.Length());
  32. }
  33. else
  34. {
  35. SharedPtr<File> file(new File(context_, path));
  36. PODVector<unsigned char> data;
  37. if (!data.Size())
  38. {
  39. // zero length file uses path name instead of data
  40. data.Resize(path.Length());
  41. memcpy(&data[0], path.CString(), path.Length());
  42. }
  43. else
  44. {
  45. data.Resize(file->GetSize());
  46. file->Read(&data[0], data.Size());
  47. }
  48. md5.update(&data[0], data.Size());
  49. }
  50. return Poco::MD5Engine::digestToHex(md5.digest()).c_str();
  51. }
  52. void AssetDatabase::Import(const String& path)
  53. {
  54. FileSystem* fs = GetSubsystem<FileSystem>();
  55. // nothing for now
  56. if (fs->DirExists(path))
  57. return;
  58. }
  59. Asset* AssetDatabase::GetAssetByGUID(const String& guid)
  60. {
  61. List<SharedPtr<Asset>>::ConstIterator itr = assets_.Begin();
  62. while (itr != assets_.End())
  63. {
  64. if (guid == (*itr)->GetGUID())
  65. return *itr;
  66. itr++;
  67. }
  68. return 0;
  69. }
  70. Asset* AssetDatabase::GetAssetByPath(const String& path)
  71. {
  72. List<SharedPtr<Asset>>::ConstIterator itr = assets_.Begin();
  73. while (itr != assets_.End())
  74. {
  75. if (path == (*itr)->GetPath())
  76. return *itr;
  77. itr++;
  78. }
  79. return 0;
  80. }
  81. void AssetDatabase::Scan()
  82. {
  83. FileSystem* fs = GetSubsystem<FileSystem>();
  84. const String& resourcePath = project_->GetResourcePath();
  85. Vector<String> allResults;
  86. fs->ScanDir(allResults, resourcePath, "", SCAN_FILES | SCAN_DIRS, true);
  87. Vector<String> filteredResults;
  88. for (unsigned i = 0; i < allResults.Size(); i++)
  89. {
  90. allResults[i] = resourcePath + allResults[i];
  91. const String& path = allResults[i];
  92. if (path.StartsWith(".") || path.EndsWith("."))
  93. continue;
  94. String ext = GetExtension(path);
  95. if (ext == ".asset")
  96. continue;
  97. filteredResults.Push(path);
  98. }
  99. Vector<String> importResults;
  100. for (unsigned i = 0; i < filteredResults.Size(); i++)
  101. {
  102. const String& path = filteredResults[i];
  103. importResults.Push(path);
  104. }
  105. for (unsigned i = 0; i < importResults.Size(); i++)
  106. {
  107. const String& path = importResults[i];
  108. String md5 = GeneratePathGUID(path);
  109. // get the current time stamp
  110. unsigned ctimestamp = fs->GetLastModifiedTime(path);
  111. if (!GetAssetByPath(path))
  112. {
  113. SharedPtr<Asset> asset(new Asset(context_, md5, ctimestamp));
  114. assets_.Push(asset);
  115. asset->SetPath(path);
  116. }
  117. }
  118. PODVector<Asset*> dirty;
  119. GetDirtyAssets(dirty);
  120. for (unsigned i = 0; i < dirty.Size(); i++)
  121. {
  122. dirty[i]->Import();
  123. }
  124. }
  125. void AssetDatabase::GetFolderAssets(String folder, PODVector<Asset*>& assets) const
  126. {
  127. if (project_.Null())
  128. return;
  129. List<SharedPtr<Asset>>::ConstIterator itr = assets_.Begin();
  130. if (!folder.Length())
  131. {
  132. folder = project_->GetResourcePath();
  133. }
  134. folder = AddTrailingSlash(folder);
  135. while (itr != assets_.End())
  136. {
  137. String path = GetPath((*itr)->GetPath());
  138. if (path == folder)
  139. assets.Push(*itr);
  140. itr++;
  141. }
  142. }
  143. void AssetDatabase::GetDirtyAssets(PODVector<Asset*>& assets)
  144. {
  145. assets.Clear();
  146. List<SharedPtr<Asset>>::ConstIterator itr = assets_.Begin();
  147. while (itr != assets_.End())
  148. {
  149. if ((*itr)->IsDirty())
  150. assets.Push(*itr);
  151. itr++;
  152. }
  153. }
  154. void AssetDatabase::HandleProjectLoaded(StringHash eventType, VariantMap& eventData)
  155. {
  156. project_ = GetSubsystem<ToolSystem>()->GetProject();
  157. ResourceCache* cache = GetSubsystem<ResourceCache>();
  158. cache->AddResourceDir(GetCachePath());
  159. Scan();
  160. }
  161. }