BsResourceManifest.cpp 4.3 KB

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