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. //Import(importResults[i]);
  108. const String& path = importResults[i];
  109. String md5 = GeneratePathGUID(path);
  110. // get the current time stamp
  111. unsigned ctimestamp = fs->GetLastModifiedTime(path);
  112. SharedPtr<Asset> asset(new Asset(context_, md5, ctimestamp));
  113. assets_.Push(asset);
  114. asset->SetPath(path);
  115. }
  116. PODVector<Asset*> dirty;
  117. GetDirtyAssets(dirty);
  118. for (unsigned i = 0; i < dirty.Size(); i++)
  119. {
  120. dirty[i]->Import();
  121. }
  122. }
  123. void AssetDatabase::GetFolderAssets(String folder, PODVector<Asset*>& assets) const
  124. {
  125. if (project_.Null())
  126. return;
  127. List<SharedPtr<Asset>>::ConstIterator itr = assets_.Begin();
  128. if (!folder.Length())
  129. {
  130. folder = project_->GetResourcePath();
  131. }
  132. folder = AddTrailingSlash(folder);
  133. while (itr != assets_.End())
  134. {
  135. String path = GetPath((*itr)->GetPath());
  136. if (path == folder)
  137. assets.Push(*itr);
  138. itr++;
  139. }
  140. }
  141. void AssetDatabase::GetDirtyAssets(PODVector<Asset*>& assets)
  142. {
  143. assets.Clear();
  144. List<SharedPtr<Asset>>::ConstIterator itr = assets_.Begin();
  145. while (itr != assets_.End())
  146. {
  147. if ((*itr)->IsDirty())
  148. assets.Push(*itr);
  149. itr++;
  150. }
  151. }
  152. void AssetDatabase::HandleProjectLoaded(StringHash eventType, VariantMap& eventData)
  153. {
  154. project_ = GetSubsystem<ToolSystem>()->GetProject();
  155. ResourceCache* cache = GetSubsystem<ResourceCache>();
  156. cache->AddResourceDir(GetCachePath());
  157. Scan();
  158. }
  159. }