3
0

SoundAssetHandler.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 <AzCore/Asset/AssetSerializer.h>
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. #include <AzCore/Serialization/Utils.h>
  11. #include <Clients/MiniAudioPlaybackComponent.h>
  12. #include <Clients/SoundAssetHandler.h>
  13. #include <MiniAudio/SoundAsset.h>
  14. namespace MiniAudio
  15. {
  16. SoundAssetHandler::SoundAssetHandler()
  17. {
  18. Register();
  19. }
  20. SoundAssetHandler::~SoundAssetHandler()
  21. {
  22. Unregister();
  23. }
  24. void SoundAssetHandler::Register()
  25. {
  26. const bool assetManagerReady = AZ::Data::AssetManager::IsReady();
  27. AZ_Error("SoundAssetHandler", assetManagerReady, "Asset manager isn't ready.");
  28. if (assetManagerReady)
  29. {
  30. AZ::Data::AssetManager::Instance().RegisterHandler(this, AZ::AzTypeInfo<SoundAsset>::Uuid());
  31. }
  32. AZ::AssetTypeInfoBus::Handler::BusConnect(AZ::AzTypeInfo<SoundAsset>::Uuid());
  33. }
  34. void SoundAssetHandler::Unregister()
  35. {
  36. AZ::AssetTypeInfoBus::Handler::BusDisconnect();
  37. if (AZ::Data::AssetManager::IsReady())
  38. {
  39. AZ::Data::AssetManager::Instance().UnregisterHandler(this);
  40. }
  41. }
  42. // AZ::AssetTypeInfoBus
  43. AZ::Data::AssetType SoundAssetHandler::GetAssetType() const
  44. {
  45. return AZ::AzTypeInfo<SoundAsset>::Uuid();
  46. }
  47. void SoundAssetHandler::GetAssetTypeExtensions(AZStd::vector<AZStd::string>& extensions)
  48. {
  49. extensions.push_back(SoundAsset::FileExtension);
  50. }
  51. const char* SoundAssetHandler::GetAssetTypeDisplayName() const
  52. {
  53. return "Sound Asset (MiniAudio Gem)";
  54. }
  55. const char* SoundAssetHandler::GetBrowserIcon() const
  56. {
  57. return "Icons/Components/ColliderMesh.svg";
  58. }
  59. const char* SoundAssetHandler::GetGroup() const
  60. {
  61. return "Sound";
  62. }
  63. // Disable spawning of physics asset entities on drag and drop
  64. AZ::Uuid SoundAssetHandler::GetComponentTypeId() const
  65. {
  66. // NOTE: This doesn't do anything when CanCreateComponent returns false
  67. return AZ::Uuid(EditorMiniAudioPlaybackComponentTypeId);
  68. }
  69. bool SoundAssetHandler::CanCreateComponent([[maybe_unused]] const AZ::Data::AssetId& assetId) const
  70. {
  71. return false;
  72. }
  73. // AZ::Data::AssetHandler
  74. AZ::Data::AssetPtr SoundAssetHandler::CreateAsset([[maybe_unused]] const AZ::Data::AssetId& id, const AZ::Data::AssetType& type)
  75. {
  76. if (type == AZ::AzTypeInfo<SoundAsset>::Uuid())
  77. {
  78. return aznew SoundAsset();
  79. }
  80. AZ_Error("SoundAssetHandler", false, "This handler deals only with SoundAsset type.");
  81. return nullptr;
  82. }
  83. AZ::Data::AssetHandler::LoadResult SoundAssetHandler::LoadAssetData(
  84. const AZ::Data::Asset<AZ::Data::AssetData>& asset,
  85. AZStd::shared_ptr<AZ::Data::AssetDataStream> stream,
  86. [[maybe_unused]] const AZ::Data::AssetFilterCB& assetLoadFilterCB)
  87. {
  88. const bool result = AZ::Utils::LoadObjectFromStreamInPlace<SoundAsset>(*stream, *asset.GetAs<SoundAsset>());
  89. if (result == false)
  90. {
  91. AZ_Error(__FUNCTION__, false, "Failed to load asset");
  92. return AssetHandler::LoadResult::Error;
  93. }
  94. return AssetHandler::LoadResult::LoadComplete;
  95. }
  96. void SoundAssetHandler::DestroyAsset(AZ::Data::AssetPtr ptr)
  97. {
  98. delete ptr;
  99. }
  100. void SoundAssetHandler::GetHandledAssetTypes(AZStd::vector<AZ::Data::AssetType>& assetTypes)
  101. {
  102. assetTypes.push_back(AZ::AzTypeInfo<SoundAsset>::Uuid());
  103. }
  104. } // namespace MiniAudio