3
0

AssetUtils.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <Atom/RPI.Reflect/Asset/AssetUtils.h>
  9. #include <AzCore/Asset/AssetManagerBus.h>
  10. namespace AZ
  11. {
  12. namespace RPI
  13. {
  14. namespace AssetUtils
  15. {
  16. void AssetUtilsInternal::ReportIssue(TraceLevel traceLevel, [[maybe_unused]] const char* message)
  17. {
  18. switch (traceLevel)
  19. {
  20. case TraceLevel::None:
  21. break;
  22. case TraceLevel::Warning:
  23. AZ_Warning("AssetUtils", false, "%s", message);
  24. break;
  25. case TraceLevel::Error:
  26. AZ_Error("AssetUtils", false, "%s", message);
  27. break;
  28. case TraceLevel::Assert:
  29. AZ_Assert(false, "%s", message);
  30. break;
  31. }
  32. }
  33. bool TryToCompileAsset(const AZStd::string& assetFilePath, TraceLevel reporting)
  34. {
  35. AzFramework::AssetSystem::AssetStatus status = AzFramework::AssetSystem::AssetStatus_Unknown;
  36. AzFramework::AssetSystemRequestBus::BroadcastResult(
  37. status, &AzFramework::AssetSystemRequestBus::Events::CompileAssetSync, assetFilePath);
  38. if ((status != AzFramework::AssetSystem::AssetStatus_Compiled) && (status != AzFramework::AssetSystem::AssetStatus_Unknown))
  39. {
  40. AssetUtilsInternal::ReportIssue(
  41. reporting,
  42. AZStd::string::format(
  43. "Could not compile asset '%s', status = %u.", assetFilePath.c_str(), static_cast<uint32_t>(status))
  44. .c_str());
  45. return false;
  46. }
  47. return true;
  48. }
  49. Data::AssetId GetAssetIdForProductPath(const char* productPath, TraceLevel reporting, Data::AssetType assetType)
  50. {
  51. // Don't create a new entry in the asset catalog for this asset if it doesn't exist.
  52. // Since we only have a product path and not an asset id, any entry we create will have an incorrect id,
  53. // incorrect size and dependency information, and will point to a file that doesn't exist. Any attempt to use
  54. // that id will fail.
  55. constexpr bool AutoGenerateId = false;
  56. Data::AssetId assetId;
  57. Data::AssetCatalogRequestBus::BroadcastResult(
  58. assetId,
  59. &Data::AssetCatalogRequestBus::Events::GetAssetIdByPath,
  60. productPath,
  61. assetType,
  62. AutoGenerateId);
  63. if (!assetId.IsValid())
  64. {
  65. AZStd::string errorMessage = AZStd::string::format(
  66. "Unable to find product asset '%s'. Has the source asset finished building?", productPath);
  67. AssetUtilsInternal::ReportIssue(reporting, errorMessage.c_str());
  68. }
  69. return assetId;
  70. }
  71. // AsyncAssetLoader //
  72. AsyncAssetLoader::AsyncAssetLoader(AssetCallback callback)
  73. : m_callback(callback)
  74. {
  75. }
  76. AsyncAssetLoader::~AsyncAssetLoader()
  77. {
  78. Data::AssetBus::Handler::BusDisconnect();
  79. }
  80. void AsyncAssetLoader::OnAssetReady(Data::Asset<Data::AssetData> asset)
  81. {
  82. HandleCallback(asset);
  83. }
  84. void AsyncAssetLoader::OnAssetError(Data::Asset<Data::AssetData> asset)
  85. {
  86. HandleCallback(asset);
  87. }
  88. void AsyncAssetLoader::HandleCallback(Data::Asset<Data::AssetData> asset)
  89. {
  90. Data::AssetBus::Handler::BusDisconnect();
  91. if (m_callback)
  92. {
  93. m_callback(asset);
  94. }
  95. m_callback = {}; // Release the callback to avoid holding references to anything captured in the lambda.
  96. m_asset = {}; // Release the asset in case this AsyncAssetLoader hangs around longer than the asset needs to.
  97. }
  98. } // namespace AssetUtils
  99. } // namespace RPI
  100. } // namespace AZ