Interpreter.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/Preprocessor/Enum.h>
  10. #include <AzCore/std/parallel/mutex.h>
  11. #include <Builder/ScriptCanvasBuilderDataSystemBus.h>
  12. #include <Editor/Framework/Configuration.h>
  13. #include <ScriptCanvas/Asset/RuntimeAsset.h>
  14. #include <ScriptCanvas/Execution/Executor.h>
  15. #include <ScriptCanvas/Execution/ExecutionStateDeclarations.h>
  16. namespace AZ
  17. {
  18. class ReflectContext;
  19. }
  20. namespace ScriptCanvasEditor
  21. {
  22. using SourceHandle = ScriptCanvas::SourceHandle;
  23. AZ_ENUM_CLASS_WITH_UNDERLYING_TYPE(InterpreterStatus, AZ::u8,
  24. Waiting, // no configuration
  25. Misconfigured, // configuration error
  26. Incompatible, // source is incompatible with with interpreter settings
  27. Configured, // configuration is good
  28. Pending, // waiting for asset readiness
  29. Ready, // asset ready
  30. Running, // running
  31. Stopped // (manually) stopped
  32. );
  33. /// This class defines provides source and property configuration for ScriptCanvas graphs, and executes them
  34. /// as safely as possible. This can be used while the graph is being actively edited, whether in the O3DE provided editor or in
  35. /// another editor. When the graph properties are updated, the interpreter will always present and (attempt to) run the latest version.
  36. class Interpreter final
  37. : public ScriptCanvasBuilder::DataSystemAssetNotificationsBus::Handler
  38. {
  39. using Mutex = AZStd::recursive_mutex;
  40. using MutexLock = AZStd::lock_guard<Mutex>;
  41. public:
  42. AZ_TYPE_INFO(Interpreter, "{B77E5BC8-766A-4657-A30F-67797D04D10E}");
  43. AZ_CLASS_ALLOCATOR(Interpreter, AZ::SystemAllocator);
  44. static void Reflect(AZ::ReflectContext* context);
  45. Interpreter();
  46. ~Interpreter();
  47. AZ::EventHandler<const Interpreter&> ConnectOnStatusChanged(AZStd::function<void(const Interpreter&)>&& function) const;
  48. /// <summary
  49. /// Executes the selected Script if possible, and returns true if it did so.
  50. /// </summary>
  51. /// <returns>true iff the script was Executable</returns>
  52. bool Execute();
  53. const Configuration& GetConfiguration() const;
  54. InterpreterStatus GetStatus() const;
  55. AZStd::string_view GetStatusString() const;
  56. bool IsExecutable() const;
  57. Configuration& ModConfiguration();
  58. /// <summary>
  59. /// Allows a manual refresh of the configuration to update Editor properties.
  60. /// </summary>
  61. void RefreshConfiguration();
  62. /// <summary>
  63. /// Sets the default user data in the Executable to a pointer to this Interpreter object.
  64. /// \note It will not be used until the next execution.
  65. /// </summary>
  66. void ResetUserData();
  67. void SetScript(SourceHandle source);
  68. /// <summary>
  69. /// Stops the execution of the script if it is executable and stoppable. If the script does not require being stopped, does nothing.
  70. /// </summary>
  71. void Stop();
  72. /// <summary>
  73. /// Sets the user data in the Executable to the input runtimeUserData.
  74. /// \note It will not be used until the next execution.
  75. /// </summary>
  76. void TakeUserData(ScriptCanvas::ExecutionUserData&& runtimeUserData);
  77. private:
  78. Mutex m_mutex;
  79. AZ::EventHandler<const Configuration&> m_handlerPropertiesChanged;
  80. AZ::EventHandler<const Configuration&> m_handlerSourceCompiled;
  81. AZ::EventHandler<const Configuration&> m_handlerSourceFailed;
  82. // #scriptcanvas_component_extension...
  83. AZ::EventHandler<const Configuration&> m_handlerUnacceptedComponentScript;
  84. mutable AZ::Event<const Interpreter&> m_onStatusChanged;
  85. bool m_runtimePropertiesDirty = true;
  86. InterpreterStatus m_status = InterpreterStatus::Waiting;
  87. Configuration m_configuration;
  88. ScriptCanvas::Executor m_executor;
  89. bool InitializeExecution(ScriptCanvas::RuntimeAssetPtr asset);
  90. void OnAssetNotReady() override;
  91. void OnPropertiesChanged();
  92. void OnReady(ScriptCanvas::RuntimeAssetPtr asset) override;
  93. void OnSourceCompiled();
  94. void OnSourceFailed();
  95. void OnSourceIncompatible();
  96. void SetSatus(InterpreterStatus status);
  97. };
  98. }