SourceAssetReference.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <native/AssetManager/SourceAssetReference.h>
  9. namespace AssetProcessor
  10. {
  11. SourceAssetReference::SourceAssetReference(AZ::IO::PathView absolutePath)
  12. {
  13. IPathConversion* pathConversion = AZ::Interface<IPathConversion>::Get();
  14. AZ_Assert(pathConversion, "IPathConversion interface is not available");
  15. if(absolutePath.empty())
  16. {
  17. return;
  18. }
  19. QString relativePath, scanFolderPath;
  20. if(!pathConversion->ConvertToRelativePath(absolutePath.FixedMaxPathStringAsPosix().c_str(), relativePath, scanFolderPath))
  21. {
  22. return;
  23. }
  24. auto* scanFolderInfo = pathConversion->GetScanFolderForFile(scanFolderPath);
  25. if(!scanFolderInfo)
  26. {
  27. return;
  28. }
  29. m_scanFolderPath = scanFolderPath.toUtf8().constData();
  30. m_relativePath = relativePath.toUtf8().constData();
  31. m_absolutePath = absolutePath;
  32. m_scanFolderId = scanFolderInfo->ScanFolderID();
  33. Normalize();
  34. }
  35. SourceAssetReference::SourceAssetReference(AZ::s64 scanFolderId, AZ::IO::PathView pathRelativeToScanFolder)
  36. {
  37. IPathConversion* pathConversion = AZ::Interface<IPathConversion>::Get();
  38. AZ_Assert(pathConversion, "IPathConversion interface is not available");
  39. auto* scanFolder = pathConversion->GetScanFolderById(scanFolderId);
  40. if(!scanFolder)
  41. {
  42. return;
  43. }
  44. AZ::IO::Path scanFolderPath = scanFolder->ScanPath().toUtf8().constData();
  45. if(scanFolderPath.empty() || pathRelativeToScanFolder.empty())
  46. {
  47. return;
  48. }
  49. auto* scanFolderInfo = pathConversion->GetScanFolderForFile(scanFolderPath.FixedMaxPathStringAsPosix().c_str());
  50. if(!scanFolderInfo)
  51. {
  52. return;
  53. }
  54. m_scanFolderPath = scanFolderPath;
  55. m_relativePath = pathRelativeToScanFolder;
  56. m_absolutePath = m_scanFolderPath / m_relativePath;
  57. m_scanFolderId = scanFolderInfo->ScanFolderID();
  58. Normalize();
  59. }
  60. SourceAssetReference::SourceAssetReference(AZ::IO::PathView scanFolderPath, AZ::IO::PathView pathRelativeToScanFolder)
  61. {
  62. IPathConversion* pathConversion = AZ::Interface<IPathConversion>::Get();
  63. AZ_Assert(pathConversion, "IPathConversion interface is not available");
  64. if(scanFolderPath.empty() || pathRelativeToScanFolder.empty())
  65. {
  66. return;
  67. }
  68. auto* scanFolderInfo = pathConversion->GetScanFolderForFile(scanFolderPath.FixedMaxPathStringAsPosix().c_str());
  69. if(!scanFolderInfo)
  70. {
  71. return;
  72. }
  73. m_scanFolderPath = scanFolderPath;
  74. m_relativePath = pathRelativeToScanFolder;
  75. m_absolutePath = m_scanFolderPath / m_relativePath;
  76. m_scanFolderId = scanFolderInfo->ScanFolderID();
  77. Normalize();
  78. }
  79. SourceAssetReference::SourceAssetReference(
  80. AZ::s64 scanFolderId, AZ::IO::PathView scanFolderPath, AZ::IO::PathView pathRelativeToScanFolder)
  81. {
  82. if (scanFolderPath.empty() || pathRelativeToScanFolder.empty())
  83. {
  84. return;
  85. }
  86. m_scanFolderPath = scanFolderPath;
  87. m_relativePath = pathRelativeToScanFolder;
  88. m_absolutePath = m_scanFolderPath / m_relativePath;
  89. m_scanFolderId = scanFolderId;
  90. Normalize();
  91. }
  92. bool SourceAssetReference::operator==(const SourceAssetReference& other) const
  93. {
  94. return m_absolutePath == other.m_absolutePath;
  95. }
  96. bool SourceAssetReference::operator!=(const SourceAssetReference& other) const
  97. {
  98. return !operator==(other);
  99. }
  100. bool SourceAssetReference::operator<(const SourceAssetReference& other) const
  101. {
  102. return m_absolutePath < other.m_absolutePath;
  103. }
  104. bool SourceAssetReference::operator>(const SourceAssetReference& other) const
  105. {
  106. return m_absolutePath > other.m_absolutePath;
  107. }
  108. SourceAssetReference::operator bool() const
  109. {
  110. return IsValid();
  111. }
  112. bool SourceAssetReference::IsValid() const
  113. {
  114. return !m_absolutePath.empty();
  115. }
  116. AZ::IO::FixedMaxPath SourceAssetReference::AbsolutePath() const
  117. {
  118. return AZ::IO::FixedMaxPath(m_absolutePath);
  119. }
  120. AZ::IO::FixedMaxPath SourceAssetReference::RelativePath() const
  121. {
  122. return AZ::IO::FixedMaxPath(m_relativePath);
  123. }
  124. AZ::IO::FixedMaxPath SourceAssetReference::ScanFolderPath() const
  125. {
  126. return AZ::IO::FixedMaxPath(m_scanFolderPath);
  127. }
  128. AZ::s64 SourceAssetReference::ScanFolderId() const
  129. {
  130. return m_scanFolderId;
  131. }
  132. void SourceAssetReference::Normalize()
  133. {
  134. m_scanFolderPath = m_scanFolderPath.LexicallyNormal().AsPosix();
  135. m_relativePath = m_relativePath.LexicallyNormal().AsPosix();
  136. m_absolutePath = m_absolutePath.LexicallyNormal().AsPosix();
  137. }
  138. }
  139. AZStd::hash<AssetProcessor::SourceAssetReference>::result_type AZStd::hash<AssetProcessor::SourceAssetReference>::operator()(const argument_type& obj) const
  140. {
  141. size_t h = 0;
  142. hash_combine(h, obj.AbsolutePath());
  143. return h;
  144. }