ScriptCanvasBuilderDataSystem.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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/AssetCommon.h>
  10. #include <AzFramework/Asset/AssetCatalogBus.h>
  11. #include <AzFramework/Asset/AssetSystemBus.h>
  12. #include <AzToolsFramework/API/EditorAssetSystemAPI.h>
  13. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  14. #include <AzToolsFramework/AssetBrowser/AssetBrowserBus.h>
  15. #include <Builder/ScriptCanvasBuilder.h>
  16. #include <Builder/ScriptCanvasBuilderDataSystemBus.h>
  17. #include <ScriptCanvas/Asset/RuntimeAsset.h>
  18. #include <ScriptCanvas/Core/Core.h>
  19. namespace ScriptCanvasEditor
  20. {
  21. class SourceTree;
  22. }
  23. namespace ScriptCanvasBuilder
  24. {
  25. /// this MACRO enables highly verbose status updates from the builder data system which will later be routed through an imgui window.
  26. // #define SCRIPT_CANVAS_BUILDER_DATA_SYSTEM_DIAGNOSTICS_ENABLED
  27. #if defined(SCRIPT_CANVAS_BUILDER_DATA_SYSTEM_DIAGNOSTICS_ENABLED)
  28. #define DATA_SYSTEM_STATUS(window, ...) AZ_TracePrintf(window, __VA_ARGS__);
  29. #else
  30. #define DATA_SYSTEM_STATUS(window, ...)
  31. #endif//defined(SCRIPT_CANVAS_BUILDER_DATA_SYSTEM_DIAGNOSTICS_ENABLED)
  32. /// <summary>
  33. /// Provides simplified access to status and compiled data for ScriptCanvas source files.
  34. /// </summary>
  35. ///
  36. /// This class handles both DataSystemAssetRequestsBus and DataSystemSourceRequestsBus. It listens to AP notifications and
  37. /// the tools framework notifications for ScriptCanvas source file changes. It stores the results of processing a source file for both
  38. /// editor-configurable properties and for runtime ready assets for faster retrieval when many are being simultaneously processed. For
  39. /// example, this occurs during prefab compilation time, when multiple ScriptCanvasEditorComponents require builder data for their
  40. /// configuration loaded from latest source file on disk. This system reduces file I/O and compilation work by maintaining and providing
  41. /// access to the very latest results.
  42. class DataSystem final
  43. : public AZ::Data::AssetBus::MultiHandler
  44. , public AzFramework::AssetCatalogEventBus::Handler
  45. , public AzFramework::AssetSystemInfoBus::Handler
  46. , public AzToolsFramework::AssetSystemBus::Handler
  47. , public DataSystemSourceRequestsBus::Handler
  48. , public DataSystemAssetRequestsBus::Handler
  49. {
  50. public:
  51. AZ_TYPE_INFO(DataSystem, "{27B74209-319D-4A8C-B37D-F85EFA6D2FFA}");
  52. AZ_CLASS_ALLOCATOR(DataSystem, AZ::SystemAllocator);
  53. DataSystem();
  54. virtual ~DataSystem();
  55. /// <summary>
  56. /// Returns the latest built editor properties for the source file
  57. /// </summary>
  58. /// <param name="sourceHandle"></param>
  59. /// <returns>BuilderSourceResult editor properties status and data </returns>
  60. BuilderSourceResult CompileBuilderData(SourceHandle sourceHandle) override;
  61. /// <summary>
  62. /// Returns the latest built runtime data for the source file
  63. /// </summary>
  64. /// <param name="sourceHandle"></param>
  65. /// <returns>BuilderAssetResult runtime status and data </returns>
  66. BuilderAssetResult LoadAsset(SourceHandle sourceHandle) override;
  67. private:
  68. struct BuilderSourceStorage
  69. {
  70. BuilderSourceStatus status = BuilderSourceStatus::Failed;
  71. BuildVariableOverrides data;
  72. };
  73. using MutexLock = AZStd::lock_guard<AZStd::recursive_mutex>;
  74. AZStd::recursive_mutex m_mutex;
  75. AZStd::unordered_map<AZ::Uuid, BuilderSourceStorage> m_buildResultsByHandle;
  76. AZStd::unordered_map<AZ::Uuid, BuilderAssetResult> m_assets;
  77. void AddResult(const SourceHandle& handle, BuilderSourceStorage&& result);
  78. void AddResult(AZ::Uuid&& id, BuilderSourceStorage&& result);
  79. void CompileBuilderDataInternal(SourceHandle sourceHandle);
  80. void MarkAssetInError(AZ::Uuid sourceId);
  81. BuilderAssetResult& MonitorAsset(AZ::Uuid fileAssetId);
  82. void OnAssetError(AZ::Data::Asset<AZ::Data::AssetData> asset) override;
  83. void OnAssetReady(AZ::Data::Asset<AZ::Data::AssetData> asset) override;
  84. void OnAssetReloaded(AZ::Data::Asset<AZ::Data::AssetData> asset) override;
  85. void OnAssetUnloaded(const AZ::Data::AssetId assetId, const AZ::Data::AssetType assetType);
  86. void OnCatalogAssetAdded(const AZ::Data::AssetId& assetId) override;
  87. void OnCatalogAssetChanged(const AZ::Data::AssetId& assetId) override;
  88. void OnCatalogAssetRemoved(const AZ::Data::AssetId& assetId, const AZ::Data::AssetInfo& assetInfo) override;
  89. void SourceFileChanged(AZStd::string relativePath, AZStd::string scanFolder, AZ::Uuid fileAssetId) override;
  90. void SourceFileRemoved(AZStd::string relativePath, AZStd::string scanFolder, AZ::Uuid fileAssetId) override;
  91. void SourceFileFailed(AZStd::string relativePath, AZStd::string scanFolder, AZ::Uuid fileAssetId) override;
  92. void ReportReady(AZ::Data::Asset<AZ::Data::AssetData> asset);
  93. // temporary work-around for the discrepancies between loaded Lua modules and loaded AZ::ScriptAssets.
  94. void ReportReadyFilter(AZ::Data::Asset<AZ::Data::AssetData> asset);
  95. };
  96. }