AssetDatabase.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. void AssetDatabase::Scan()
  71. {
  72. FileSystem* fs = GetSubsystem<FileSystem>();
  73. const String& resourcePath = project_->GetResourcePath();
  74. Vector<String> allResults;
  75. fs->ScanDir(allResults, resourcePath, "", SCAN_FILES | SCAN_DIRS, true);
  76. Vector<String> filteredResults;
  77. for (unsigned i = 0; i < allResults.Size(); i++)
  78. {
  79. allResults[i] = resourcePath + allResults[i];
  80. const String& path = allResults[i];
  81. if (path.StartsWith(".") || path.EndsWith("."))
  82. continue;
  83. String ext = GetExtension(path);
  84. if (ext == ".asset")
  85. continue;
  86. filteredResults.Push(path);
  87. }
  88. Vector<String> importResults;
  89. for (unsigned i = 0; i < filteredResults.Size(); i++)
  90. {
  91. const String& path = filteredResults[i];
  92. importResults.Push(path);
  93. }
  94. for (unsigned i = 0; i < importResults.Size(); i++)
  95. {
  96. //Import(importResults[i]);
  97. const String& path = importResults[i];
  98. String md5 = GeneratePathGUID(path);
  99. // get the current time stamp
  100. unsigned ctimestamp = fs->GetLastModifiedTime(path);
  101. SharedPtr<Asset> asset(new Asset(context_, md5, ctimestamp));
  102. assets_.Push(asset);
  103. asset->SetPath(path);
  104. }
  105. PODVector<Asset*> dirty;
  106. GetDirtyAssets(dirty);
  107. for (unsigned i = 0; i < dirty.Size(); i++)
  108. {
  109. dirty[i]->Import();
  110. }
  111. }
  112. void AssetDatabase::GetFolderAssets(String folder, PODVector<Asset*>& assets) const
  113. {
  114. if (project_.Null())
  115. return;
  116. List<SharedPtr<Asset>>::ConstIterator itr = assets_.Begin();
  117. if (!folder.Length())
  118. {
  119. folder = project_->GetResourcePath();
  120. }
  121. folder = AddTrailingSlash(folder);
  122. while (itr != assets_.End())
  123. {
  124. String path = GetPath((*itr)->GetPath());
  125. if (path == folder)
  126. assets.Push(*itr);
  127. itr++;
  128. }
  129. }
  130. void AssetDatabase::GetDirtyAssets(PODVector<Asset*>& assets)
  131. {
  132. assets.Clear();
  133. List<SharedPtr<Asset>>::ConstIterator itr = assets_.Begin();
  134. while (itr != assets_.End())
  135. {
  136. if ((*itr)->IsDirty())
  137. assets.Push(*itr);
  138. itr++;
  139. }
  140. }
  141. void AssetDatabase::HandleProjectLoaded(StringHash eventType, VariantMap& eventData)
  142. {
  143. project_ = GetSubsystem<ToolSystem>()->GetProject();
  144. ResourceCache* cache = GetSubsystem<ResourceCache>();
  145. cache->AddResourceDir(GetCachePath());
  146. Scan();
  147. }
  148. }