BsResourceManifest.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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<ResourceManifest>(name);
  17. }
  18. ResourceManifestPtr ResourceManifest::createEmpty()
  19. {
  20. return bs_shared_ptr<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. mUUIDToFilePath[uuid] = filePath;
  37. mFilePathToUUID[filePath] = uuid;
  38. }
  39. }
  40. void ResourceManifest::unregisterResource(const String& uuid)
  41. {
  42. auto iterFind = mUUIDToFilePath.find(uuid);
  43. if(iterFind != mUUIDToFilePath.end())
  44. {
  45. mFilePathToUUID.erase(iterFind->second);
  46. mUUIDToFilePath.erase(uuid);
  47. }
  48. }
  49. bool ResourceManifest::uuidToFilePath(const String& uuid, Path& filePath) const
  50. {
  51. auto iterFind = mUUIDToFilePath.find(uuid);
  52. if(iterFind != mUUIDToFilePath.end())
  53. {
  54. filePath = iterFind->second;
  55. return true;
  56. }
  57. else
  58. {
  59. filePath = Path::BLANK;
  60. return false;
  61. }
  62. }
  63. bool ResourceManifest::filePathToUUID(const Path& filePath, String& outUUID) const
  64. {
  65. auto iterFind = mFilePathToUUID.find(filePath);
  66. if(iterFind != mFilePathToUUID.end())
  67. {
  68. outUUID = iterFind->second;
  69. return true;
  70. }
  71. else
  72. {
  73. outUUID = StringUtil::BLANK;
  74. return false;
  75. }
  76. }
  77. bool ResourceManifest::uuidExists(const String& uuid) const
  78. {
  79. auto iterFind = mUUIDToFilePath.find(uuid);
  80. return iterFind != mUUIDToFilePath.end();
  81. }
  82. bool ResourceManifest::filePathExists(const Path& filePath) const
  83. {
  84. auto iterFind = mFilePathToUUID.find(filePath);
  85. return iterFind != mFilePathToUUID.end();
  86. }
  87. void ResourceManifest::save(const ResourceManifestPtr& manifest, const Path& path, const Path& relativePath)
  88. {
  89. ResourceManifestPtr copy = create(manifest->mName);
  90. for(auto& elem : manifest->mFilePathToUUID)
  91. {
  92. if (!relativePath.includes(elem.first))
  93. {
  94. BS_EXCEPT(InvalidStateException, "Path in resource manifest cannot be made relative to: \"" +
  95. relativePath.toString() + "\". Path: \"" + elem.first.toString() + "\"");
  96. }
  97. Path elementRelativePath = elem.first.getRelative(relativePath);
  98. copy->mFilePathToUUID[elementRelativePath] = elem.second;
  99. }
  100. for(auto& elem : manifest->mUUIDToFilePath)
  101. {
  102. if(!relativePath.includes(elem.second))
  103. {
  104. BS_EXCEPT(InvalidStateException, "Path in resource manifest cannot be made relative to: \"" +
  105. relativePath.toString() + "\". Path: \"" + elem.second.toString() + "\"");
  106. }
  107. Path elementRelativePath = elem.second.getRelative(relativePath);
  108. copy->mUUIDToFilePath[elem.first] = elementRelativePath;
  109. }
  110. FileEncoder fs(path);
  111. fs.encode(copy.get());
  112. }
  113. ResourceManifestPtr ResourceManifest::load(const Path& path, const Path& relativePath)
  114. {
  115. FileDecoder fs(path);
  116. ResourceManifestPtr manifest = std::static_pointer_cast<ResourceManifest>(fs.decode());
  117. ResourceManifestPtr copy = create(manifest->mName);
  118. for(auto& elem : manifest->mFilePathToUUID)
  119. {
  120. Path absPath = elem.first.getAbsolute(relativePath);
  121. copy->mFilePathToUUID[absPath] = elem.second;
  122. }
  123. for(auto& elem : manifest->mUUIDToFilePath)
  124. {
  125. Path absPath = elem.second.getAbsolute(relativePath);
  126. copy->mUUIDToFilePath[elem.first] = absPath;
  127. }
  128. return copy;
  129. }
  130. RTTITypeBase* ResourceManifest::getRTTIStatic()
  131. {
  132. return ResourceManifestRTTI::instance();
  133. }
  134. RTTITypeBase* ResourceManifest::getRTTI() const
  135. {
  136. return ResourceManifest::getRTTIStatic();
  137. }
  138. }