BsResourceManifest.cpp 4.3 KB

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