BsResourceManifest.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include "BsResourceManifest.h"
  2. #include "BsResourceManifestRTTI.h"
  3. #include "BsFileSerializer.h"
  4. #include "BsException.h"
  5. namespace BansheeEngine
  6. {
  7. ResourceManifest::ResourceManifest(const ConstructPrivately& dummy)
  8. {
  9. }
  10. ResourceManifest::ResourceManifest(const String& name)
  11. :mName(name)
  12. {
  13. }
  14. ResourceManifestPtr ResourceManifest::create(const String& name)
  15. {
  16. return bs_shared_ptr_new<ResourceManifest>(name);
  17. }
  18. ResourceManifestPtr ResourceManifest::createEmpty()
  19. {
  20. return bs_shared_ptr_new<ResourceManifest>(ConstructPrivately());
  21. }
  22. void ResourceManifest::registerResource(const String& uuid, const Path& filePath)
  23. {
  24. auto iterFind = mUUIDToFilePath.find(uuid);
  25. if(iterFind != mUUIDToFilePath.end())
  26. {
  27. if (iterFind->second != filePath)
  28. {
  29. mFilePathToUUID.erase(iterFind->second);
  30. mUUIDToFilePath[uuid] = filePath;
  31. mFilePathToUUID[filePath] = uuid;
  32. }
  33. }
  34. else
  35. {
  36. auto iterFind2 = mFilePathToUUID.find(filePath);
  37. if (iterFind2 != mFilePathToUUID.end())
  38. mUUIDToFilePath.erase(iterFind2->second);
  39. mUUIDToFilePath[uuid] = filePath;
  40. mFilePathToUUID[filePath] = uuid;
  41. }
  42. }
  43. void ResourceManifest::unregisterResource(const String& uuid)
  44. {
  45. auto iterFind = mUUIDToFilePath.find(uuid);
  46. if(iterFind != mUUIDToFilePath.end())
  47. {
  48. mFilePathToUUID.erase(iterFind->second);
  49. mUUIDToFilePath.erase(uuid);
  50. }
  51. }
  52. bool ResourceManifest::uuidToFilePath(const String& uuid, Path& filePath) const
  53. {
  54. auto iterFind = mUUIDToFilePath.find(uuid);
  55. if(iterFind != mUUIDToFilePath.end())
  56. {
  57. filePath = iterFind->second;
  58. return true;
  59. }
  60. else
  61. {
  62. filePath = Path::BLANK;
  63. return false;
  64. }
  65. }
  66. bool ResourceManifest::filePathToUUID(const Path& filePath, String& outUUID) const
  67. {
  68. auto iterFind = mFilePathToUUID.find(filePath);
  69. if(iterFind != mFilePathToUUID.end())
  70. {
  71. outUUID = iterFind->second;
  72. return true;
  73. }
  74. else
  75. {
  76. outUUID = StringUtil::BLANK;
  77. return false;
  78. }
  79. }
  80. bool ResourceManifest::uuidExists(const String& uuid) const
  81. {
  82. auto iterFind = mUUIDToFilePath.find(uuid);
  83. return iterFind != mUUIDToFilePath.end();
  84. }
  85. bool ResourceManifest::filePathExists(const Path& filePath) const
  86. {
  87. auto iterFind = mFilePathToUUID.find(filePath);
  88. return iterFind != mFilePathToUUID.end();
  89. }
  90. void ResourceManifest::save(const ResourceManifestPtr& manifest, const Path& path, const Path& relativePath)
  91. {
  92. ResourceManifestPtr copy = create(manifest->mName);
  93. for(auto& elem : manifest->mFilePathToUUID)
  94. {
  95. if (!relativePath.includes(elem.first))
  96. {
  97. BS_EXCEPT(InvalidStateException, "Path in resource manifest cannot be made relative to: \"" +
  98. relativePath.toString() + "\". Path: \"" + elem.first.toString() + "\"");
  99. }
  100. Path elementRelativePath = elem.first.getRelative(relativePath);
  101. copy->mFilePathToUUID[elementRelativePath] = elem.second;
  102. }
  103. for(auto& elem : manifest->mUUIDToFilePath)
  104. {
  105. if(!relativePath.includes(elem.second))
  106. {
  107. BS_EXCEPT(InvalidStateException, "Path in resource manifest cannot be made relative to: \"" +
  108. relativePath.toString() + "\". Path: \"" + elem.second.toString() + "\"");
  109. }
  110. Path elementRelativePath = elem.second.getRelative(relativePath);
  111. copy->mUUIDToFilePath[elem.first] = elementRelativePath;
  112. }
  113. FileEncoder fs(path);
  114. fs.encode(copy.get());
  115. }
  116. ResourceManifestPtr ResourceManifest::load(const Path& path, const Path& relativePath)
  117. {
  118. FileDecoder fs(path);
  119. ResourceManifestPtr manifest = std::static_pointer_cast<ResourceManifest>(fs.decode());
  120. ResourceManifestPtr copy = create(manifest->mName);
  121. for(auto& elem : manifest->mFilePathToUUID)
  122. {
  123. Path absPath = elem.first.getAbsolute(relativePath);
  124. copy->mFilePathToUUID[absPath] = elem.second;
  125. }
  126. for(auto& elem : manifest->mUUIDToFilePath)
  127. {
  128. Path absPath = elem.second.getAbsolute(relativePath);
  129. copy->mUUIDToFilePath[elem.first] = absPath;
  130. }
  131. return copy;
  132. }
  133. RTTITypeBase* ResourceManifest::getRTTIStatic()
  134. {
  135. return ResourceManifestRTTI::instance();
  136. }
  137. RTTITypeBase* ResourceManifest::getRTTI() const
  138. {
  139. return ResourceManifest::getRTTIStatic();
  140. }
  141. }