dump_lua_symbols.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. # The easiest way to run this script in O3DE
  9. # is to run it via the "Python Scripts" window in the Editor:
  10. # Tools -> Other -> Python Scripts.
  11. # Will produce the file <game_project>\lua_symbols.txt
  12. #
  13. # Alternatively from the Editor console:
  14. # pyRunFile C:\GIT\o3de\Gems\EditorPythonBindings\Editor\Scripts\dump_lua_symbols.py
  15. # Will produce the file <game_project>\lua_symbols.txt
  16. #
  17. # or if you need to customize the name of the output file
  18. # pyRunFile C:\GIT\o3de\Gems\EditorPythonBindings\Editor\Scripts\dump_lua_symbols.py -o my_lua_name.txt
  19. # Will produce the file <game_project>\my_lua_name.txt
  20. # This script shows basic usage of the LuaSymbolsReporterBus,
  21. # Which can be used to report all symbols available for
  22. # game scripting with Lua.
  23. import sys
  24. import os
  25. import argparse
  26. from typing import TextIO
  27. import azlmbr.bus as azbus
  28. import azlmbr.script as azscript
  29. import azlmbr.legacy.general as azgeneral
  30. def _dump_class_symbol(file_obj: TextIO, class_symbol: azlmbr.script.LuaClassSymbol):
  31. file_obj.write(f"** {class_symbol}\n")
  32. file_obj.write("Properties:\n")
  33. for property_symbol in class_symbol.properties:
  34. file_obj.write(f" - {property_symbol}\n")
  35. file_obj.write("Methods:\n")
  36. for method_symbol in class_symbol.methods:
  37. file_obj.write(f" - {method_symbol}\n")
  38. def _dump_lua_classes(file_obj: TextIO):
  39. class_symbols = azscript.LuaSymbolsReporterBus(azbus.Broadcast,
  40. "GetListOfClasses")
  41. file_obj.write("======== Classes ==========\n")
  42. sorted_classes_by_named = sorted(class_symbols, key=lambda class_symbol: class_symbol.name)
  43. for class_symbol in sorted_classes_by_named:
  44. _dump_class_symbol(file_obj, class_symbol)
  45. file_obj.write("\n\n")
  46. def _dump_lua_globals(file_obj: TextIO):
  47. global_properties = azscript.LuaSymbolsReporterBus(azbus.Broadcast,
  48. "GetListOfGlobalProperties")
  49. file_obj.write("======== Global Properties ==========\n")
  50. sorted_properties_by_name = sorted(global_properties, key=lambda symbol: symbol.name)
  51. for property_symbol in sorted_properties_by_name:
  52. file_obj.write(f"- {property_symbol}\n")
  53. file_obj.write("\n\n")
  54. global_functions = azscript.LuaSymbolsReporterBus(azbus.Broadcast,
  55. "GetListOfGlobalFunctions")
  56. file_obj.write("======== Global Functions ==========\n")
  57. sorted_functions_by_name = sorted(global_functions, key=lambda symbol: symbol.name)
  58. for function_symbol in sorted_functions_by_name:
  59. file_obj.write(f"- {function_symbol}\n")
  60. file_obj.write("\n\n")
  61. def _dump_lua_ebus(file_obj: TextIO, ebus_symbol: azlmbr.script.LuaEBusSymbol):
  62. file_obj.write(f">> {ebus_symbol}\n")
  63. sorted_senders = sorted(ebus_symbol.senders, key=lambda symbol: symbol.name)
  64. for sender in sorted_senders:
  65. file_obj.write(f" - {sender}\n")
  66. file_obj.write("\n\n")
  67. def _dump_lua_ebuses(file_obj: TextIO):
  68. ebuses = azscript.LuaSymbolsReporterBus(azbus.Broadcast,
  69. "GetListOfEBuses")
  70. file_obj.write("======== Ebus List ==========\n")
  71. sorted_ebuses_by_name = sorted(ebuses, key=lambda symbol: symbol.name)
  72. for ebus_symbol in sorted_ebuses_by_name:
  73. _dump_lua_ebus(file_obj, ebus_symbol)
  74. file_obj.write("\n\n")
  75. if __name__ == "__main__":
  76. parser = argparse.ArgumentParser(description='Dumps All ebuses, classes and global symbols available for Lua scripting.')
  77. parser.add_argument('--outfile', '--o', default='lua_symbols.txt',
  78. help='output file file where all the symbols will be dumped to. If relative, will be under the game project folder.')
  79. parser.add_argument('--all', '--a', default=True, action='store_true',
  80. help='If true dumps all symbols to the outfile. Equivalent to specifying --c --g --e')
  81. parser.add_argument('--classes', '--c', default=False, action='store_true',
  82. help='If true dumps Class symbols.')
  83. parser.add_argument('--globals', '--g', default=False, action='store_true',
  84. help='If true dumps Global symbols.')
  85. parser.add_argument('--ebuses', '--e', default=False, action='store_true',
  86. help='If true dumps Ebus symbols.')
  87. args = parser.parse_args()
  88. output_file_name = args.outfile
  89. if not os.path.isabs(output_file_name):
  90. game_root_path = os.path.normpath(azgeneral.get_game_folder())
  91. output_file_name = os.path.join(game_root_path, output_file_name)
  92. try:
  93. file_obj = open(output_file_name, 'wt')
  94. except Exception as e:
  95. print(f"Failed to open {output_file_name}: {e}")
  96. sys.exit(-1)
  97. if args.classes:
  98. _dump_lua_classes(file_obj)
  99. if args.globals:
  100. _dump_lua_globals(file_obj)
  101. if args.ebuses:
  102. _dump_lua_ebuses(file_obj)
  103. if (not args.classes) and (not args.globals) and (not args.ebuses):
  104. _dump_lua_classes(file_obj)
  105. _dump_lua_globals(file_obj)
  106. _dump_lua_ebuses(file_obj)
  107. file_obj.close()
  108. print(f" Lua Symbols Are available in: {output_file_name}")