3
0

BuiltInAssetHandler.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/BuiltInAssetHandler.h>
  9. namespace AZ
  10. {
  11. namespace RPI
  12. {
  13. BuiltInAssetHandler::AssetHandlerFunctions::AssetHandlerFunctions(CreateFunction createFunction, DeleteFunction deleteFunction)
  14. : m_create(createFunction)
  15. , m_destroy(deleteFunction)
  16. {
  17. }
  18. BuiltInAssetHandler::BuiltInAssetHandler(const Data::AssetType& assetType, const AssetHandlerFunctions& handlerFunctions)
  19. : m_assetType(assetType)
  20. , m_handlerFunctions(handlerFunctions)
  21. {
  22. }
  23. BuiltInAssetHandler::BuiltInAssetHandler(const Data::AssetType& assetType, CreateFunction createFunction)
  24. : BuiltInAssetHandler(assetType, AssetHandlerFunctions{ createFunction })
  25. {
  26. }
  27. BuiltInAssetHandler::~BuiltInAssetHandler()
  28. {
  29. Unregister();
  30. }
  31. void BuiltInAssetHandler::Register()
  32. {
  33. AZ_Assert(AZ::Data::AssetManager::IsReady(), "AssetManager isn't ready!");
  34. AZ::Data::AssetManager::Instance().RegisterHandler(this, m_assetType);
  35. m_registered = true;
  36. }
  37. void BuiltInAssetHandler::Unregister()
  38. {
  39. if (AZ::Data::AssetManager::IsReady())
  40. {
  41. AZ::Data::AssetManager::Instance().UnregisterHandler(this);
  42. }
  43. m_registered = false;
  44. }
  45. void BuiltInAssetHandler::GetHandledAssetTypes(AZStd::vector<AZ::Data::AssetType>& assetTypes)
  46. {
  47. assetTypes.push_back(m_assetType);
  48. }
  49. Data::AssetPtr BuiltInAssetHandler::CreateAsset(const Data::AssetId&, [[maybe_unused]] const Data::AssetType& type)
  50. {
  51. AZ_Assert(type == m_assetType, "Handler called with wrong asset type");
  52. Data::AssetPtr asset = m_handlerFunctions.m_create();
  53. AZ_Assert(asset, "CreateFunction failed to create an asset of type %s", type.ToString<AZStd::string>().data());
  54. // The asset has to be initialized in the "Ready" state; if it were in the default "NotLoaded"
  55. // state then the asset system will automatically try to load it, which isn't valid because
  56. // BuiltInAssetHandler is for hard-coded assets that don't have an presence on disk.
  57. AZ_Assert(asset->IsReady(), "Asset must be the in the Ready state after CreateFunction is called.");
  58. return asset;
  59. }
  60. void BuiltInAssetHandler::DestroyAsset(AZ::Data::AssetPtr ptr)
  61. {
  62. AZ_Assert(ptr->GetType() == m_assetType, "Handler called with wrong asset type");
  63. m_handlerFunctions.m_destroy(ptr);
  64. }
  65. void BuiltInAssetHandler::StandardDestroyFunction(AZ::Data::AssetPtr ptr)
  66. {
  67. delete ptr;
  68. }
  69. Data::AssetHandler::LoadResult BuiltInAssetHandler::LoadAssetData(
  70. [[maybe_unused]] const Data::Asset<Data::AssetData>& asset,
  71. [[maybe_unused]] AZStd::shared_ptr<Data::AssetDataStream> stream,
  72. [[maybe_unused]] const Data::AssetFilterCB& assetLoadFilterCB)
  73. {
  74. // LoadAssetData should never be called on a BuiltIn asset type.
  75. return Data::AssetHandler::LoadResult::Error;
  76. }
  77. } // namespace RPI
  78. } // namespace AZ