AssetDatabase.cpp 4.1 KB

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