PythonLogSymbolsComponent.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/Component.h>
  10. #include <AzCore/IO/SystemFile.h>
  11. #include <AzCore/IO/FileIO.h>
  12. #include <EditorPythonBindings/PythonCommon.h>
  13. #include <EditorPythonBindings/PythonUtility.h>
  14. #include <Source/PythonSymbolsBus.h>
  15. #include <EditorPythonBindings/EditorPythonBindingsBus.h>
  16. #include <AzToolsFramework/API/EditorPythonConsoleBus.h>
  17. namespace AZ
  18. {
  19. class BehaviorClass;
  20. class BehaviorMethod;
  21. class BehaviorProperty;
  22. }
  23. namespace EditorPythonBindings
  24. {
  25. namespace Internal
  26. {
  27. struct FileHandle;
  28. }
  29. //! Exports Python symbols to the log folder for Python script developers to include into their local projects
  30. class PythonLogSymbolsComponent
  31. : public AZ::Component
  32. , private EditorPythonBindingsNotificationBus::Handler
  33. , private PythonSymbolEventBus::Handler
  34. , private AzToolsFramework::EditorPythonConsoleInterface
  35. {
  36. public:
  37. AZ_COMPONENT(PythonLogSymbolsComponent, "{F1873D04-C472-41A2-8AA4-48B0CE4A5979}", AZ::Component);
  38. static void Reflect(AZ::ReflectContext* context);
  39. protected:
  40. ////////////////////////////////////////////////////////////////////////
  41. // AZ::Component interface implementation
  42. void Activate() override;
  43. void Deactivate() override;
  44. ////////////////////////////////////////////////////////////////////////
  45. // EditorPythonBindingsNotificationBus::Handler
  46. void OnPostInitialize() override;
  47. ////////////////////////////////////////////////////////////////////////
  48. // PythonSymbolEventBus::Handler
  49. void LogClass(const AZStd::string moduleName, const AZ::BehaviorClass* behaviorClass) override;
  50. void LogClassWithName(const AZStd::string moduleName, const AZ::BehaviorClass* behaviorClass, const AZStd::string className) override;
  51. void LogClassMethod(
  52. const AZStd::string moduleName,
  53. const AZStd::string globalMethodName,
  54. const AZ::BehaviorClass* behaviorClass,
  55. const AZ::BehaviorMethod* behaviorMethod) override;
  56. void LogBus(const AZStd::string moduleName, const AZStd::string busName, const AZ::BehaviorEBus* behaviorEBus) override;
  57. void LogGlobalMethod(const AZStd::string moduleName, const AZStd::string methodName, const AZ::BehaviorMethod* behaviorMethod) override;
  58. void LogGlobalProperty(
  59. const AZStd::string moduleName,
  60. const AZStd::string propertyName,
  61. const AZ::BehaviorProperty* behaviorProperty) override;
  62. void Finalize() override;
  63. AZStd::string FetchPythonTypeName(const AZ::BehaviorParameter& param) override;
  64. ////////////////////////////////////////////////////////////////////////
  65. // EditorPythonConsoleInterface
  66. void GetModuleList(AZStd::vector<AZStd::string_view>& moduleList) const override;
  67. void GetGlobalFunctionList(GlobalFunctionCollection& globalFunctionCollection) const override;
  68. ////////////////////////////////////////////////////////////////////////
  69. // Python type deduction
  70. AZStd::string_view FetchPythonTypeAndTraits(const AZ::TypeId& typeId, AZ::u32 traits);
  71. private:
  72. using ModuleSet = AZStd::unordered_set<AZStd::string>;
  73. using GlobalFunctionEntry = AZStd::pair<const AZ::BehaviorMethod*, AZStd::string>;
  74. using GlobalFunctionList = AZStd::vector<GlobalFunctionEntry>;
  75. using GlobalFunctionMap = AZStd::unordered_map<AZStd::string_view, GlobalFunctionList>;
  76. using FileHandlePtr = AZStd::shared_ptr<Internal::FileHandle>;
  77. AZStd::string m_basePath;
  78. ModuleSet m_moduleSet;
  79. GlobalFunctionMap m_globalFunctionMap;
  80. Text::PythonBehaviorDescription m_pythonBehaviorDescription;
  81. FileHandlePtr OpenInitFileAt(AZStd::string_view moduleName);
  82. FileHandlePtr OpenModuleAt(AZStd::string_view moduleName);
  83. void WriteMethod(AZ::IO::HandleType handle, AZStd::string_view methodName, const AZ::BehaviorMethod& behaviorMethod, const AZ::BehaviorClass* behaviorClass);
  84. void WriteProperty(AZ::IO::HandleType handle, int level, AZStd::string_view propertyName, const AZ::BehaviorProperty& property, const AZ::BehaviorClass* behaviorClass);
  85. };
  86. }