AnyAsset.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/System/AnyAsset.h>
  9. #include <AzCore/RTTI/ReflectContext.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. namespace AZ
  12. {
  13. namespace RPI
  14. {
  15. const char* AnyAsset::DisplayName = "AnyAsset";
  16. const char* AnyAsset::Group = "Common";
  17. const char* AnyAsset::Extension = "azasset";
  18. void AnyAsset::SetReady()
  19. {
  20. m_status = AssetStatus::Ready;
  21. }
  22. bool AnyAssetCreator::CreateAnyAsset(const AZStd::any& anyData, const Data::AssetId& assetId, Data::Asset<AnyAsset>& result)
  23. {
  24. result = Data::AssetManager::Instance().CreateAsset<AnyAsset>(assetId, AZ::Data::AssetLoadBehavior::PreLoad);
  25. result->m_data = anyData;
  26. return true;
  27. }
  28. void AnyAssetCreator::SetAnyAssetData(const AZStd::any& anyData, AnyAsset& result)
  29. {
  30. result.m_data = anyData;
  31. }
  32. Data::AssetHandler::LoadResult AnyAssetHandler::LoadAssetData(
  33. const Data::Asset<Data::AssetData>& asset,
  34. AZStd::shared_ptr<Data::AssetDataStream> stream,
  35. const Data::AssetFilterCB& assetLoadFilterCB)
  36. {
  37. AnyAsset* assetData = asset.GetAs<AnyAsset>();
  38. AZ_Assert(assetData, "Asset is of the wrong type.");
  39. SerializeContext* context = m_serializeContext;
  40. if (!context)
  41. {
  42. ComponentApplicationBus::BroadcastResult(context, &ComponentApplicationRequests::GetSerializeContext);
  43. }
  44. if (!context)
  45. {
  46. AZ_Assert(false, "No serialize context");
  47. return Data::AssetHandler::LoadResult::Error;
  48. }
  49. Uuid loadedClassId;
  50. void* loadedInstance = nullptr;
  51. bool success = ObjectStream::LoadBlocking(stream.get(), *context,
  52. [&loadedInstance, &loadedClassId](void* classPtr, const Uuid& classId, const SerializeContext*)
  53. {
  54. loadedClassId = classId;
  55. loadedInstance = classPtr;
  56. },
  57. AZ::ObjectStream::FilterDescriptor(assetLoadFilterCB),
  58. ObjectStream::InplaceLoadRootInfoCB()
  59. );
  60. if (!success)
  61. {
  62. return Data::AssetHandler::LoadResult::Error;
  63. }
  64. // Create temporary one with the type id then we can get its type info to construct a new AZStd::any with new data
  65. auto tempAny = context->CreateAny(loadedClassId);
  66. AZStd::any::type_info typeInfo = tempAny.get_type_info();
  67. AZStd::any newAny(loadedInstance, typeInfo);
  68. assetData->m_data = newAny;
  69. // Release loadedInstance
  70. auto classData = context->FindClassData(loadedClassId);
  71. if (classData && classData->m_factory)
  72. {
  73. classData->m_factory->Destroy(loadedInstance);
  74. }
  75. return Data::AssetHandler::LoadResult::LoadComplete;
  76. }
  77. bool AnyAssetHandler::SaveAssetData(const Data::Asset<Data::AssetData>& asset, IO::GenericStream* stream)
  78. {
  79. AnyAsset* assetData = asset.GetAs<AnyAsset>();
  80. AZ_Assert(assetData, "Asset is of the wrong type.");
  81. SerializeContext* context = m_serializeContext;
  82. if (!context)
  83. {
  84. ComponentApplicationBus::BroadcastResult(context, &ComponentApplicationRequests::GetSerializeContext);
  85. }
  86. if (!context)
  87. {
  88. AZ_Assert(false, "No serialize context");
  89. return false;
  90. }
  91. if (assetData)
  92. {
  93. return Utils::SaveObjectToStream(*stream, ObjectStream::ST_XML, assetData->GetDataAs<void>(),
  94. assetData->m_data.get_type_info().m_id, context);
  95. }
  96. return false;
  97. }
  98. } // namespace RPI
  99. } // namespace AZ