TranslationGeneration.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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/std/string/string.h>
  10. #include <AzCore/std/containers/vector.h>
  11. #include <AzCore/Math/Crc.h>
  12. #include <AzCore/RTTI/ReflectContext.h>
  13. #include <AzCore/Serialization/EditContext.h>
  14. #include <AzCore/Script/ScriptContextAttributes.h>
  15. namespace AZ
  16. {
  17. class BehaviorClass;
  18. class BehaviorContext;
  19. class BehaviorEBus;
  20. class BehaviorMethod;
  21. struct BehaviorParameter;
  22. class BehaviorProperty;
  23. class Entity;
  24. class SerializeContext;
  25. }
  26. namespace ScriptCanvasEditorTools
  27. {
  28. //! Utility structures for generating the JSON files used for names of elements in Script Canvas
  29. struct EntryDetails
  30. {
  31. AZStd::string m_name;
  32. AZStd::string m_tooltip;
  33. AZStd::string m_category;
  34. AZStd::string m_subtitle;
  35. };
  36. using EntryDetailsList = AZStd::vector<EntryDetails>;
  37. //! Utility structure that represents a method's argument
  38. struct Argument
  39. {
  40. AZStd::string m_typeId;
  41. EntryDetails m_details;
  42. };
  43. //! Utility structure that represents a method
  44. struct Method
  45. {
  46. AZStd::string m_key;
  47. AZStd::string m_context;
  48. EntryDetails m_details;
  49. EntryDetails m_entry;
  50. EntryDetails m_exit;
  51. AZStd::vector<Argument> m_arguments;
  52. AZStd::vector<Argument> m_results;
  53. };
  54. //! Utility structure that represents a Script Canvas slot
  55. struct Slot
  56. {
  57. AZStd::string m_key;
  58. EntryDetails m_details;
  59. Argument m_data;
  60. };
  61. //! Utility structure that represents an reflected element
  62. struct Entry
  63. {
  64. AZStd::string m_key;
  65. AZStd::string m_context;
  66. AZStd::string m_variant;
  67. EntryDetails m_details;
  68. AZStd::vector<Method> m_methods;
  69. AZStd::vector<Slot> m_slots;
  70. };
  71. // The root level JSON object
  72. struct TranslationFormat
  73. {
  74. AZStd::vector<Entry> m_entries;
  75. };
  76. //! Class the wraps all the generation of translation data for all scripting types.
  77. class TranslationGeneration
  78. {
  79. public:
  80. TranslationGeneration();
  81. //! Generate the translation data for a given BehaviorClass
  82. bool TranslateBehaviorClass(const AZ::BehaviorClass* behaviorClass);
  83. //! Generate the translation data for all Behavior Context classes
  84. void TranslateBehaviorClasses();
  85. //! Generate the translation data for Behavior Ebus, handles both Handlers and Senders
  86. void TranslateEBus(const AZ::BehaviorEBus* behaviorEBus);
  87. //! Generate the translation data for a specific AZ::Event
  88. bool TranslateSingleAZEvent(const AZ::BehaviorMethod* method);
  89. //! Generate the translation data for AZ::Events
  90. void TranslateAZEvents();
  91. //! Generate the translation data for all ScriptCanvas::Node types
  92. void TranslateNodes();
  93. //! Generate the translation data for the specified TypeId (must inherit from ScriptCanvas::Node)
  94. void TranslateNode(const AZ::TypeId& nodeTypeId);
  95. //! Generate the translation data for on-demand reflected types
  96. void TranslateOnDemandReflectedTypes(TranslationFormat& translationRoot);
  97. //! Generates the translation data for all global properties and methods in the BehaviorContext
  98. void TranslateBehaviorGlobals();
  99. //! Generates the translation data for the specified global method in the BehaviorContext (global, by name)
  100. void TranslateBehaviorGlobalMethod(const AZStd::string& methodName);
  101. //! Generates the translation data for the specified property in the BehaviorContext (global, by name)
  102. void TranslateBehaviorProperty(const AZStd::string& propertyName);
  103. //! Generates the translation data for the specified property in the BehaviorContext
  104. void TranslateBehaviorProperty(const AZ::BehaviorProperty* behaviorProperty, const AZStd::string& className, const AZStd::string& context, Entry* entry = nullptr);
  105. //! Generates a type map from reflected types that are suitable for BehaviorContext objects used by ScriptCanvas
  106. void TranslateDataTypes();
  107. private:
  108. //! Returns the entity for a valid AZ::Event
  109. AZ::Entity* GetAZEventNode(const AZ::BehaviorMethod& method);
  110. //! Utility to populate a BehaviorMethod's translation data
  111. void TranslateMethod(AZ::BehaviorMethod* behaviorMethod, Method& methodEntry);
  112. //! Utility function to populate BehaviorMethod arguments translation data
  113. void TranslateMethodArguments(const AZ::BehaviorMethod* behaviorMethod, Method& methodEntry);
  114. //! Utility function to populate BehaviorMethod results translation data
  115. void TranslateMethodResults(const AZ::BehaviorParameter* resultParameter, Method& methodEntry);
  116. //! Generates the translation data for a BehaviorEBus that has an BehaviorEBusHandler
  117. bool TranslateEBusHandler(const AZ::BehaviorEBus* behaviorEbus, TranslationFormat& translationRoot);
  118. //! Utility function that saves a TranslationFormat object in the desired JSON format
  119. void SaveJSONData(const AZStd::string& filename, TranslationFormat& translationRoot);
  120. //! Utility function that splits camel-case syntax string into separate words
  121. void SplitCamelCase(AZStd::string&);
  122. //! Evaluates if the specified object has exclusion flags and should be skipped from generation
  123. template <typename T>
  124. bool ShouldSkip(const T* object) const
  125. {
  126. using namespace AZ::Script::Attributes;
  127. // Check for "ignore" attribute for ScriptCanvas
  128. const auto& excludeClassAttributeData = azdynamic_cast<AZ::Edit::AttributeData<ExcludeFlags>*>(AZ::FindAttribute(ExcludeFrom, object->m_attributes));
  129. const bool excludeClass = excludeClassAttributeData && (static_cast<AZ::u64>(excludeClassAttributeData->Get(nullptr)) & static_cast<AZ::u64>(ExcludeFlags::List | ExcludeFlags::Documentation));
  130. if (excludeClass)
  131. {
  132. return true; // skip this class
  133. }
  134. return false;
  135. }
  136. AZ::SerializeContext* m_serializeContext;
  137. AZ::BehaviorContext* m_behaviorContext;
  138. };
  139. namespace Helpers
  140. {
  141. //! Generic function that fetches from a valid type that has attributes a string attribute
  142. template <typename T>
  143. AZStd::string GetStringAttribute(const T* source, const AZ::Crc32& attribute)
  144. {
  145. AZStd::string attributeValue = "";
  146. if (auto attributeItem = azrtti_cast<AZ::AttributeData<AZStd::string>*>(AZ::FindAttribute(attribute, source->m_attributes)))
  147. {
  148. attributeValue = attributeItem->Get(nullptr);
  149. }
  150. return attributeValue;
  151. }
  152. //! Utility function that fetches from an AttributeArray a string attribute whether it's an AZStd::string or a const char*
  153. AZStd::string ReadStringAttribute(const AZ::AttributeArray& attributes, const AZ::Crc32& attribute);
  154. //! Utility function to verify if a BehaviorMethod has the specified attribute
  155. bool MethodHasAttribute(const AZ::BehaviorMethod* method, AZ::Crc32 attribute);
  156. //! Utility function to find a valid name from the ClassData/EditContext
  157. void GetTypeNameAndDescription(AZ::TypeId typeId, AZStd::string& outName, AZStd::string& outDescription);
  158. //! Get the category attribute for a given ClassData
  159. AZStd::string GetCategory(const AZ::SerializeContext::ClassData* classData);
  160. AZStd::vector<AZ::TypeId> GetUnpackedTypes(const AZ::TypeId& typeID);
  161. }
  162. }