PythonLogSymbolsComponent.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 <Source/PythonCommon.h>
  13. #include <Source/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. //! Exports Python symbols to the log folder for Python script developers to include into their local projects
  26. class PythonLogSymbolsComponent
  27. : public AZ::Component
  28. , private EditorPythonBindingsNotificationBus::Handler
  29. , private PythonSymbolEventBus::Handler
  30. , private AzToolsFramework::EditorPythonConsoleInterface
  31. {
  32. public:
  33. AZ_COMPONENT(PythonLogSymbolsComponent, "{F1873D04-C472-41A2-8AA4-48B0CE4A5979}", AZ::Component);
  34. static void Reflect(AZ::ReflectContext* context);
  35. protected:
  36. ////////////////////////////////////////////////////////////////////////
  37. // AZ::Component interface implementation
  38. void Activate() override;
  39. void Deactivate() override;
  40. ////////////////////////////////////////////////////////////////////////
  41. // EditorPythonBindingsNotificationBus::Handler
  42. void OnPostInitialize() override;
  43. ////////////////////////////////////////////////////////////////////////
  44. // PythonSymbolEventBus::Handler
  45. void LogClass(const AZStd::string moduleName, const AZ::BehaviorClass* behaviorClass) override;
  46. void LogClassWithName(const AZStd::string moduleName, const AZ::BehaviorClass* behaviorClass, const AZStd::string className) override;
  47. void LogClassMethod(
  48. const AZStd::string moduleName,
  49. const AZStd::string globalMethodName,
  50. const AZ::BehaviorClass* behaviorClass,
  51. const AZ::BehaviorMethod* behaviorMethod) override;
  52. void LogBus(const AZStd::string moduleName, const AZStd::string busName, const AZ::BehaviorEBus* behaviorEBus) override;
  53. void LogGlobalMethod(const AZStd::string moduleName, const AZStd::string methodName, const AZ::BehaviorMethod* behaviorMethod) override;
  54. void LogGlobalProperty(
  55. const AZStd::string moduleName,
  56. const AZStd::string propertyName,
  57. const AZ::BehaviorProperty* behaviorProperty) override;
  58. void Finalize() override;
  59. AZStd::string FetchPythonTypeName(const AZ::BehaviorParameter& param) override;
  60. ////////////////////////////////////////////////////////////////////////
  61. // EditorPythonConsoleInterface
  62. void GetModuleList(AZStd::vector<AZStd::string_view>& moduleList) const override;
  63. void GetGlobalFunctionList(GlobalFunctionCollection& globalFunctionCollection) const override;
  64. ////////////////////////////////////////////////////////////////////////
  65. // Python type deduction
  66. AZStd::string_view FetchPythonTypeAndTraits(const AZ::TypeId& typeId, AZ::u32 traits);
  67. private:
  68. using ModuleSet = AZStd::unordered_set<AZStd::string>;
  69. using GlobalFunctionEntry = AZStd::pair<const AZ::BehaviorMethod*, AZStd::string>;
  70. using GlobalFunctionList = AZStd::vector<GlobalFunctionEntry>;
  71. using GlobalFunctionMap = AZStd::unordered_map<AZStd::string_view, GlobalFunctionList>;
  72. using TypeMap = AZStd::unordered_map<AZ::TypeId, AZStd::string>;
  73. AZStd::string m_basePath;
  74. ModuleSet m_moduleSet;
  75. GlobalFunctionMap m_globalFunctionMap;
  76. TypeMap m_typeCache;
  77. AZStd::string FetchListType(const AZ::TypeId& typeId);
  78. AZStd::string FetchMapType(const AZ::TypeId& typeId);
  79. AZStd::string FetchOutcomeType(const AZ::TypeId& typeId);
  80. AZStd::string TypeNameFallback(const AZ::TypeId& typeId);
  81. AZ::IO::HandleType OpenInitFileAt(AZStd::string_view moduleName);
  82. AZ::IO::HandleType 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. }