AssetCommon.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #pragma once
  9. #include <AzCore/Asset/AssetManager.h>
  10. #include <AzCore/Asset/AssetTypeInfoBus.h>
  11. #include <AzCore/IO/FileIO.h>
  12. #include <AzFramework/Asset/AssetSystemBus.h>
  13. #include <EMotionFX/Source/Allocators.h>
  14. #include <Integration/System/SystemCommon.h>
  15. namespace EMotionFX
  16. {
  17. namespace Integration
  18. {
  19. /**
  20. *
  21. */
  22. class EMotionFXAsset : public AZ::Data::AssetData
  23. {
  24. public:
  25. AZ_RTTI(EMotionFXAsset, "{043F606A-A483-4910-8110-D8BC4B78922C}", AZ::Data::AssetData)
  26. AZ_CLASS_ALLOCATOR(EMotionFXAsset, EMotionFXAllocator)
  27. EMotionFXAsset(AZ::Data::AssetId id = AZ::Data::AssetId())
  28. : AZ::Data::AssetData(id)
  29. {}
  30. void ReleaseEMotionFXData()
  31. {
  32. m_emfxNativeData.clear();
  33. m_emfxNativeData.shrink_to_fit();
  34. }
  35. AZStd::vector<AZ::u8> m_emfxNativeData;
  36. };
  37. /**
  38. *
  39. */
  40. template<typename DataType>
  41. class EMotionFXAssetHandler
  42. : public AZ::Data::AssetHandler
  43. , protected AZ::AssetTypeInfoBus::Handler
  44. {
  45. public:
  46. AZ_CLASS_ALLOCATOR(EMotionFXAssetHandler<DataType>, EMotionFXAllocator)
  47. EMotionFXAssetHandler()
  48. {
  49. Register();
  50. }
  51. ~EMotionFXAssetHandler() override
  52. {
  53. Unregister();
  54. }
  55. AZ::Data::AssetPtr CreateAsset(const AZ::Data::AssetId& id, const AZ::Data::AssetType& type) override
  56. {
  57. (void)type;
  58. return aznew DataType(id);
  59. }
  60. AZ::Data::AssetId AssetMissingInCatalog(const AZ::Data::Asset<AZ::Data::AssetData>& asset) override
  61. {
  62. // missing assets should at least get escalated to the top of the list. Sub-handlers could override this and do
  63. // additional things like substitute some default asset Id.
  64. AzFramework::AssetSystemRequestBus::Broadcast(&AzFramework::AssetSystem::AssetSystemRequests::EscalateAssetByUuid, asset.GetId().m_guid);
  65. return AZ::Data::AssetId();
  66. }
  67. AZ::Data::AssetHandler::LoadResult LoadAssetData(
  68. const AZ::Data::Asset<AZ::Data::AssetData>& asset,
  69. AZStd::shared_ptr<AZ::Data::AssetDataStream> stream,
  70. const AZ::Data::AssetFilterCB& assetLoadFilterCB) override
  71. {
  72. (void)assetLoadFilterCB;
  73. DataType* assetData = asset.GetAs<DataType>();
  74. if (stream->GetLength() > 0)
  75. {
  76. assetData->m_emfxNativeData.resize(stream->GetLength());
  77. stream->Read(stream->GetLength(), assetData->m_emfxNativeData.data());
  78. return AZ::Data::AssetHandler::LoadResult::LoadComplete;
  79. }
  80. return AZ::Data::AssetHandler::LoadResult::Error;
  81. }
  82. bool SaveAssetData(const AZ::Data::Asset<AZ::Data::AssetData>& asset, AZ::IO::GenericStream* stream) override
  83. {
  84. (void)asset;
  85. (void)stream;
  86. AZ_Error("EMotionFX", false, "Asset handler does not support asset saving.");
  87. return false;
  88. }
  89. void DestroyAsset(AZ::Data::AssetPtr ptr) override
  90. {
  91. delete ptr;
  92. }
  93. void GetHandledAssetTypes(AZStd::vector<AZ::Data::AssetType>& assetTypes) override
  94. {
  95. assetTypes.push_back(azrtti_typeid<DataType>());
  96. }
  97. void Register()
  98. {
  99. AZ_Assert(AZ::Data::AssetManager::IsReady(), "Asset database isn't ready!");
  100. AZ::Data::AssetManager::Instance().RegisterHandler(this, azrtti_typeid<DataType>());
  101. AZ::AssetTypeInfoBus::Handler::BusConnect(azrtti_typeid<DataType>());
  102. }
  103. void Unregister()
  104. {
  105. AZ::AssetTypeInfoBus::Handler::BusDisconnect(azrtti_typeid<DataType>());
  106. if (AZ::Data::AssetManager::IsReady())
  107. {
  108. AZ::Data::AssetManager::Instance().UnregisterHandler(this);
  109. }
  110. }
  111. void InitAsset(const AZ::Data::Asset<AZ::Data::AssetData>& asset, bool loadStageSucceeded, bool isReload) override
  112. {
  113. if (!loadStageSucceeded || !OnInitAsset(asset))
  114. {
  115. AssetHandler::InitAsset(asset, false, isReload);
  116. return;
  117. }
  118. AssetHandler::InitAsset(asset, true, isReload);
  119. }
  120. virtual bool OnInitAsset(const AZ::Data::Asset<AZ::Data::AssetData>& asset)
  121. {
  122. (void)asset;
  123. return true;
  124. }
  125. const char* GetGroup() const override
  126. {
  127. return "Animation";
  128. }
  129. };
  130. } // namespace Integration
  131. } // namespace EMotionFX