BsResourceManifest.cpp 3.9 KB

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