CmResourceManifest.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #include "CmResourceManifest.h"
  2. #include "CmResourceManifestRTTI.h"
  3. #include "CmPath.h"
  4. #include "CmFileSerializer.h"
  5. #include "CmException.h"
  6. namespace CamelotFramework
  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 cm_shared_ptr<ResourceManifest>(name);
  18. }
  19. ResourceManifestPtr ResourceManifest::createEmpty()
  20. {
  21. return cm_shared_ptr<ResourceManifest>(ConstructPrivately());
  22. }
  23. void ResourceManifest::registerResource(const String& uuid, const WString& filePath)
  24. {
  25. auto iterFind = mUUIDToFilePath.find(uuid);
  26. WString standardizedFilePath = Path::standardizePath(filePath);
  27. StringUtil::toLowerCase(standardizedFilePath);
  28. if(iterFind != mUUIDToFilePath.end())
  29. {
  30. if(iterFind->second != standardizedFilePath)
  31. {
  32. mFilePathToUUID.erase(iterFind->second);
  33. mUUIDToFilePath[uuid] = standardizedFilePath;
  34. mFilePathToUUID[standardizedFilePath] = uuid;
  35. }
  36. }
  37. else
  38. {
  39. mUUIDToFilePath[uuid] = standardizedFilePath;
  40. mFilePathToUUID[standardizedFilePath] = 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, WString& 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 = StringUtil::WBLANK;
  63. return false;
  64. }
  65. }
  66. bool ResourceManifest::filePathToUUID(const WString& filePath, String& outUUID) const
  67. {
  68. WString standardizedFilePath = Path::standardizePath(filePath);
  69. StringUtil::toLowerCase(standardizedFilePath);
  70. auto iterFind = mFilePathToUUID.find(standardizedFilePath);
  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 WString& filePath) const
  88. {
  89. WString standardizedFilePath = Path::standardizePath(filePath);
  90. StringUtil::toLowerCase(standardizedFilePath);
  91. auto iterFind = mFilePathToUUID.find(standardizedFilePath);
  92. return iterFind != mFilePathToUUID.end();
  93. }
  94. void ResourceManifest::save(const ResourceManifestPtr& manifest, const WString& path, const WString& relativePath)
  95. {
  96. WString standRelativePath = Path::standardizePath(relativePath);
  97. StringUtil::toLowerCase(standRelativePath);
  98. ResourceManifestPtr copy = create(manifest->mName);
  99. for(auto& elem : manifest->mFilePathToUUID)
  100. {
  101. if(!Path::includes(elem.first, standRelativePath))
  102. {
  103. CM_EXCEPT(InvalidStateException, "Path in resource manifest cannot be made relative to: \"" +
  104. toString(relativePath) + "\". Path: \"" + toString(elem.first) + "\"");
  105. }
  106. WString relativePath = Path::relative(standRelativePath, elem.first);
  107. copy->mFilePathToUUID[relativePath] = elem.second;
  108. }
  109. for(auto& elem : manifest->mUUIDToFilePath)
  110. {
  111. if(!Path::includes(elem.second, standRelativePath))
  112. {
  113. CM_EXCEPT(InvalidStateException, "Path in resource manifest cannot be made relative to: \"" +
  114. toString(relativePath) + "\". Path: \"" + toString(elem.second) + "\"");
  115. }
  116. WString relativePath = Path::relative(standRelativePath, elem.second);
  117. copy->mUUIDToFilePath[elem.first] = relativePath;
  118. }
  119. FileSerializer fs;
  120. fs.encode(copy.get(), path);
  121. }
  122. ResourceManifestPtr ResourceManifest::load(const WString& path, const WString& relativePath)
  123. {
  124. WString standRelativePath = Path::standardizePath(relativePath);
  125. StringUtil::toLowerCase(standRelativePath);
  126. FileSerializer fs;
  127. ResourceManifestPtr manifest = std::static_pointer_cast<ResourceManifest>(fs.decode(path));
  128. ResourceManifestPtr copy = create(manifest->mName);
  129. for(auto& elem : manifest->mFilePathToUUID)
  130. {
  131. WString absPath = Path::combine(standRelativePath, elem.first);
  132. copy->mFilePathToUUID[absPath] = elem.second;
  133. }
  134. for(auto& elem : manifest->mUUIDToFilePath)
  135. {
  136. WString absPath = Path::combine(standRelativePath, elem.second);
  137. copy->mUUIDToFilePath[elem.first] = absPath;
  138. }
  139. return copy;
  140. }
  141. RTTITypeBase* ResourceManifest::getRTTIStatic()
  142. {
  143. return ResourceManifestRTTI::instance();
  144. }
  145. RTTITypeBase* ResourceManifest::getRTTI() const
  146. {
  147. return ResourceManifest::getRTTIStatic();
  148. }
  149. }