3
0

AssetCollectionAsyncLoaderTestComponent.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 <AzToolsFramework/ToolsComponents/EditorComponentBase.h>
  10. #include <Atom/Utils/AssetCollectionAsyncLoader.h>
  11. namespace AZ
  12. {
  13. namespace AtomBridge
  14. {
  15. /**
  16. * Interface for AZ::AtomBridge::AssetCollectionAsyncLoaderTestBus, which is an EBus that receives requests
  17. * to test AssetCollectionAsyncLoader API
  18. */
  19. class AssetCollectionAsyncLoaderTestInterface
  20. : public ComponentBus
  21. {
  22. public:
  23. AZ_RTTI(AssetCollectionAsyncLoaderTestInterface, "{2C000A68-3B9A-4462-B8CF-E2995FA2C208}");
  24. //////////////////////////////////////////////////////////////////////////
  25. // EBusTraits overrides
  26. static const EBusHandlerPolicy HandlerPolicy = EBusHandlerPolicy::Single;
  27. //////////////////////////////////////////////////////////////////////////
  28. /**
  29. * Destroys the instance of the class.
  30. */
  31. virtual ~AssetCollectionAsyncLoaderTestInterface() {}
  32. //////////////////////////////////////////////////////////////////////////
  33. // The API
  34. //! @pathToAssetListJson Path to a json file with a plain list of file paths.
  35. //! Each path is the path of an asset product.
  36. //! The AssetType will be deduced from the file extension.
  37. //! Returns true if the asset loading job starts successfully.
  38. virtual bool StartLoadingAssetsFromJsonFile(const AZStd::string& pathToAssetListJson) = 0;
  39. //! @assetList Similar as above but the list of assets is given directly.
  40. //! Returns true if the asset loading job starts successfully.
  41. virtual bool StartLoadingAssetsFromAssetList(const AZStd::vector<AZStd::string>& assetList) = 0;
  42. //! Cancels any pending job to that has been queued by this component.
  43. virtual void CancelLoadingAssets() = 0;
  44. //! Returns a list of the assets that have not been loaded yet from the Asset Processor Cache.
  45. virtual AZStd::vector<AZStd::string> GetPendingAssetsList() const = 0;
  46. //! Shortcut to GetPendingAssetsList().size()
  47. virtual uint32_t GetCountOfPendingAssets() const = 0;
  48. //! Returns true if the asset was loaded successfully.
  49. virtual bool ValidateAssetWasLoaded(const AZStd::string& assetPath) const = 0;
  50. };
  51. /**
  52. * The EBus for events defined in AssetCollectionAsyncLoaderTestInterface class .
  53. */
  54. typedef AZ::EBus<AssetCollectionAsyncLoaderTestInterface> AssetCollectionAsyncLoaderTestBus;
  55. /*
  56. * This class is designed to be used under automation, but the user can add it to an entity
  57. * and manually specify a json file with a list of asset paths to load asynchronously. From an user point of view
  58. * it has no value, but for debugging it can be useful to try the AssetCollectionAsyncLoader API without having to write
  59. * a test suite for it.
  60. */
  61. class AssetCollectionAsyncLoaderTestComponent
  62. : public AzToolsFramework::Components::EditorComponentBase
  63. , public AssetCollectionAsyncLoaderTestBus::Handler
  64. {
  65. public:
  66. AZ_EDITOR_COMPONENT(AssetCollectionAsyncLoaderTestComponent, "{D0A558AD-F8CD-4DB8-80A4-40B4E1F947FA}"
  67. , AzToolsFramework::Components::EditorComponentBase
  68. , AssetCollectionAsyncLoaderTestInterface)
  69. //////////////////////////////////////////////////////////////////////////
  70. // AZ::Component interface implementation
  71. void Activate() override;
  72. void Deactivate() override;
  73. //////////////////////////////////////////////////////////////////////////
  74. //////////////////////////////////////////////////////////////////////////
  75. // AssetCollectionAsyncLoaderTestBus overrides
  76. bool StartLoadingAssetsFromJsonFile(const AZStd::string& pathToAssetListJson) override;
  77. bool StartLoadingAssetsFromAssetList(const AZStd::vector<AZStd::string>& assetList) override;
  78. void CancelLoadingAssets() override;
  79. AZStd::vector<AZStd::string> GetPendingAssetsList() const override;
  80. uint32_t GetCountOfPendingAssets() const override;
  81. bool ValidateAssetWasLoaded(const AZStd::string& assetPath) const override;
  82. //////////////////////////////////////////////////////////////////////////
  83. protected:
  84. enum class State
  85. {
  86. Idle,
  87. LoadingAssets,
  88. FatalError,
  89. };
  90. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  91. {
  92. services.push_back(AZ_CRC("AssetCollectionAsyncLoaderTest", 0x66d04369));
  93. }
  94. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  95. {
  96. services.push_back(AZ_CRC("AssetCollectionAsyncLoaderTest", 0x66d04369));
  97. }
  98. static void Reflect(AZ::ReflectContext* context);
  99. AZ::Crc32 OnStartCancelButtonClicked();
  100. AZStd::string GetStartCancelButtonText() const;
  101. //! Serialized member variables.
  102. //! A user editable path to a json file that contains the list of assets to load.
  103. AZStd::string m_pathToAssetListJson;
  104. //! Non-serialized member variables.
  105. State m_state = State::Idle;
  106. AZStd::unordered_set<AZStd::string> m_pendingAssets; // List of assets that have not been loaded yet.
  107. // This is the Object Under Test
  108. AZStd::unique_ptr<AZ::AssetCollectionAsyncLoader> m_assetCollectionAsyncLoader;
  109. };
  110. } // namespace AtomBridge
  111. } // namespace AZ