ScriptCanvasTraceUtilities.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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/Component/Entity.h>
  10. #include <AzCore/Component/EntityId.h>
  11. #include <AzCore/Debug/TraceMessageBus.h>
  12. #include <AzCore/RTTI/BehaviorContext.h>
  13. #include <AzFramework/Entity/EntityContextBus.h>
  14. #include <AzFramework/Entity/SliceEntityOwnershipServiceBus.h>
  15. #include <ScriptCanvas/Asset/RuntimeAsset.h>
  16. #include <ScriptCanvas/Asset/RuntimeAssetHandler.h>
  17. #include <ScriptCanvas/Core/Core.h>
  18. #include <ScriptCanvas/Core/Graph.h>
  19. #include <ScriptCanvas/Core/ScriptCanvasBus.h>
  20. #include <ScriptCanvas/Data/Data.h>
  21. #include <ScriptCanvas/Variable/VariableBus.h>
  22. #include <ScriptCanvas/Assets/ScriptCanvasFileHandling.h>
  23. namespace AZ
  24. {
  25. class ScriptAsset;
  26. }
  27. namespace ScriptCanvas
  28. {
  29. class RuntimeAsset;
  30. class RuntimeComponent;
  31. }
  32. namespace ScriptCanvasEditor
  33. {
  34. struct LoadTestGraphResult
  35. {
  36. AZStd::unique_ptr<AZ::Entity> m_entity;
  37. ScriptCanvas::RuntimeComponent* m_runtimeComponent = nullptr;
  38. bool m_nativeFunctionFound = false;
  39. SourceHandle m_editorAsset;
  40. AZ::Data::Asset<ScriptCanvas::RuntimeAsset> m_runtimeAsset;
  41. AZ::Data::Asset<AZ::ScriptAsset> m_scriptAsset;
  42. };
  43. // how long a unit test should be allowed to execute
  44. enum class eDuration
  45. {
  46. InitialActivation = 0, // kill the test as soon as control returns from the graph
  47. Seconds, // wait for the specified amount of seconds, regardless of ticks
  48. Ticks, // wait for the the specified amount of ticks, regardless of seconds
  49. };
  50. struct DurationSpec
  51. {
  52. eDuration m_spec = eDuration::InitialActivation;
  53. size_t m_ticks = 0;
  54. float m_seconds = 0.0f;
  55. float m_timeStep = (1.0f / 60.0f);
  56. static DurationSpec Seconds(float seconds);
  57. static DurationSpec Ticks(size_t ticks);
  58. };
  59. class TraceSuppressionRequests
  60. : public AZ::EBusTraits
  61. {
  62. public:
  63. static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Single;
  64. virtual void SuppressPreAssert(bool suppress) = 0;
  65. virtual void SuppressAssert(bool suppress) = 0;
  66. virtual void SuppressException(bool suppress) = 0;
  67. virtual void SuppressPreError(bool suppress) = 0;
  68. virtual void SuppressError(bool suppress) = 0;
  69. virtual void SuppressPreWarning(bool suppress) = 0;
  70. virtual void SuppressWarning(bool suppress) = 0;
  71. virtual void SuppressPrintf(bool suppress) = 0;
  72. virtual void SuppressAllOutput(bool suppress) = 0;
  73. };
  74. using TraceSuppressionBus = AZ::EBus<TraceSuppressionRequests>;
  75. class TraceMessageComponent
  76. : public AZ::Component
  77. , protected AZ::Debug::TraceMessageBus::Handler
  78. , protected TraceSuppressionBus::Handler
  79. {
  80. public:
  81. AZ_COMPONENT(TraceMessageComponent, "{E12144CE-809D-4056-9735-4384D7DBCCDC}");
  82. TraceMessageComponent() = default;
  83. ~TraceMessageComponent() override = default;
  84. void Activate() override
  85. {
  86. AZ::Debug::TraceMessageBus::Handler::BusConnect();
  87. TraceSuppressionBus::Handler::BusConnect();
  88. }
  89. void Deactivate() override
  90. {
  91. AZ::Debug::TraceMessageBus::Handler::BusDisconnect();
  92. TraceSuppressionBus::Handler::BusDisconnect();
  93. }
  94. private:
  95. /// \ref ComponentDescriptor::Reflect
  96. static void Reflect(AZ::ReflectContext* context)
  97. {
  98. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  99. {
  100. serializeContext->Class<TraceMessageComponent, AZ::Component>()
  101. ->Version(0)
  102. ;
  103. }
  104. }
  105. bool OnPreAssert(const char*, int, const char*, const char*) override { return suppressPreAssert; }
  106. bool OnAssert(const char*) override { return suppressAssert; }
  107. bool OnException(const char*) override { return suppressException; }
  108. bool OnPreError(const char*, const char*, int, const char*, const char*) override { return suppressPreError; }
  109. bool OnError(const char*, const char*) override { return suppressError; }
  110. bool OnPreWarning(const char*, const char*, int, const char*, const char*) override { return suppressPreWarning; }
  111. bool OnWarning(const char*, const char*) override { return suppressWarning; }
  112. bool OnPrintf(const char*, const char*) override { return suppressPrintf; }
  113. bool OnOutput(const char*, const char*) override { return suppressAllOutput; }
  114. void SuppressPreAssert(bool suppress) override { suppressPreAssert = suppress; }
  115. void SuppressAssert(bool suppress)override { suppressAssert = suppress; }
  116. void SuppressException(bool suppress) override { suppressException = suppress; }
  117. void SuppressPreError(bool suppress) override { suppressPreError = suppress; }
  118. void SuppressError(bool suppress) override { suppressPreError = suppress; }
  119. void SuppressPreWarning(bool suppress) override { suppressPreWarning = suppress; }
  120. void SuppressWarning(bool suppress) override { suppressWarning = suppress; }
  121. void SuppressPrintf(bool suppress) override { suppressPrintf = suppress; }
  122. void SuppressAllOutput(bool suppress) override { suppressAllOutput = suppress; }
  123. bool suppressPreAssert = false;
  124. bool suppressAssert = false;
  125. bool suppressException = false;
  126. bool suppressPreError = false;
  127. bool suppressError = false;
  128. bool suppressPreWarning = false;
  129. bool suppressWarning = false;
  130. bool suppressPrintf = false;
  131. bool suppressAllOutput = false;
  132. };
  133. struct ScopedOutputSuppression
  134. {
  135. ScopedOutputSuppression([[maybe_unused]] bool suppressState = true)
  136. {
  137. AZ::Debug::TraceMessageBus::BroadcastResult(m_oldSuppression, &AZ::Debug::TraceMessageEvents::OnOutput, "", "");
  138. TraceSuppressionBus::Broadcast(&TraceSuppressionRequests::SuppressAllOutput, suppressState);
  139. }
  140. ~ScopedOutputSuppression()
  141. {
  142. TraceSuppressionBus::Broadcast(&TraceSuppressionRequests::SuppressAllOutput, m_oldSuppression);
  143. }
  144. private:
  145. bool m_oldSuppression = false;
  146. };
  147. } // ScriptCanvasEditor