SourceAssetsStorage.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 "SourceAssetsStorage.h"
  9. #include "RobotImporterUtils.h"
  10. namespace ROS2::Utils
  11. {
  12. /// Function computes CRC32 on first kilobyte of file.
  13. AZ::Crc32 GetFileCRC(const AZStd::string& filename)
  14. {
  15. auto fileSize = AZ::IO::SystemFile::Length(filename.c_str());
  16. fileSize = AZStd::min(fileSize, 1024ull); // limit crc computation to first kilobyte
  17. if (fileSize == 0)
  18. {
  19. return AZ::Crc32();
  20. }
  21. AZStd::vector<char> buffer(fileSize + 1);
  22. buffer[fileSize] = '\0';
  23. if (!AZ::IO::SystemFile::Read(filename.c_str(), buffer.data(), fileSize))
  24. {
  25. return AZ::Crc32();
  26. }
  27. AZ::Crc32 r;
  28. r.Add(buffer.data(), fileSize);
  29. return r;
  30. }
  31. AZStd::unordered_map<AZ::Crc32, AvailableAsset> GetInterestingSourceAssetsCRC()
  32. {
  33. const AZStd::unordered_set<AZStd::string> kInterestingExtensions{ ".dae", ".stl", ".obj" };
  34. AZStd::unordered_map<AZ::Crc32, AvailableAsset> availableAssets;
  35. // take all meshes in catalog
  36. AZ::Data::AssetCatalogRequests::AssetEnumerationCB collectAssetsCb =
  37. [&](const AZ::Data::AssetId id, const AZ::Data::AssetInfo& info)
  38. {
  39. if (AZ::Data::AssetManager::Instance().GetHandler(info.m_assetType))
  40. {
  41. if (!info.m_relativePath.ends_with(".azmodel"))
  42. {
  43. return;
  44. }
  45. using AssetSysReqBus = AzToolsFramework::AssetSystemRequestBus;
  46. bool pathFound{ false };
  47. AZStd::string fullSourcePathStr;
  48. AssetSysReqBus::BroadcastResult(
  49. pathFound, &AssetSysReqBus::Events::GetFullSourcePathFromRelativeProductPath, info.m_relativePath, fullSourcePathStr);
  50. if (!pathFound)
  51. {
  52. return;
  53. }
  54. const AZ::IO::Path fullSourcePath(fullSourcePathStr);
  55. const AZStd::string extension = fullSourcePath.Extension().Native();
  56. if (!kInterestingExtensions.contains(extension))
  57. {
  58. return;
  59. }
  60. AZ::Crc32 crc = Utils::GetFileCRC(fullSourcePathStr);
  61. AZ_Printf(
  62. "RobotImporterWidget",
  63. "m_relativePath %s %s : %s %llu \n",
  64. info.m_relativePath.c_str(),
  65. info.m_assetId.ToString<AZStd::string>().c_str(),
  66. fullSourcePath.c_str(),
  67. crc);
  68. AvailableAsset t;
  69. t.m_sourceAssetRelativePath = info.m_relativePath;
  70. t.m_assetId = info.m_assetId;
  71. t.m_sourceAssetGlobalPath = fullSourcePathStr;
  72. t.m_productAssetRelativePath = info.m_relativePath;
  73. availableAssets[crc] = t;
  74. }
  75. };
  76. AZ::Data::AssetCatalogRequestBus::Broadcast(
  77. &AZ::Data::AssetCatalogRequestBus::Events::EnumerateAssets, nullptr, collectAssetsCb, nullptr);
  78. return availableAssets;
  79. }
  80. UrdfAssetMap FindAssetsForUrdf(const AZStd::unordered_set<AZStd::string>& meshesFilenames, const AZStd::string& urdFilename)
  81. {
  82. UrdfAssetMap urdfToAsset;
  83. for (const auto& t : meshesFilenames)
  84. {
  85. Utils::UrdfAsset asset;
  86. asset.m_urdfPath = t;
  87. asset.m_resolvedUrdfPath = Utils::ResolveURDFPath(asset.m_urdfPath, urdFilename);
  88. asset.m_urdfFileCRC = Utils::GetFileCRC(asset.m_resolvedUrdfPath);
  89. urdfToAsset.emplace(t, AZStd::move(asset));
  90. }
  91. const AZStd::unordered_map<AZ::Crc32, AvailableAsset> availableAssets = Utils::GetInterestingSourceAssetsCRC();
  92. // Search for suitable mappings by comparing checksum
  93. for (auto it = urdfToAsset.begin(); it != urdfToAsset.end(); it++)
  94. {
  95. Utils::UrdfAsset& asset = it->second;
  96. auto found_source_asset = availableAssets.find(asset.m_urdfFileCRC);
  97. if (found_source_asset != availableAssets.end())
  98. {
  99. asset.m_availableAssetInfo = found_source_asset->second;
  100. }
  101. }
  102. return urdfToAsset;
  103. }
  104. } // namespace ROS2::Utils