activity_sdk_lua_scene_events.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include <array>
  2. #include <cstdint>
  3. #include <cstdio>
  4. #include <span>
  5. #include <string>
  6. #include <string_view>
  7. #include <unordered_set>
  8. #include "../../../state/activity_sdk/scene_triggers.h"
  9. #include "activity_sdk_lua_missions_internal.h"
  10. namespace sunrise::client::content::activity::sdk_generation::lua_artifacts::internal {
  11. namespace {
  12. /**
  13. * Appends the gates one predicate selects as constants, named where the trigger name is known.
  14. * @return The number of gates appended.
  15. */
  16. std::size_t append_gates(std::span<const format::AuthoredSceneEventKey> gates,
  17. bool (*selected)(const format::AuthoredSceneEventKey&) noexcept,
  18. std::string& output) {
  19. std::unordered_set<std::string> names;
  20. std::size_t appended = 0;
  21. for (const format::AuthoredSceneEventKey& gate : gates) {
  22. if (!selected(gate)) {
  23. continue;
  24. }
  25. ++appended;
  26. const std::string_view known = state::activity_sdk::scene_trigger_name(gate.key);
  27. std::string name = known.empty() ? std::string{} : identifier(known, true);
  28. // An unknown name, or one that folds onto an earlier name, takes the key itself.
  29. if (name.empty() || !names.insert(name).second) {
  30. std::array<char, 16> symbol{};
  31. const int length = std::snprintf(symbol.data(), symbol.size(), "KEY_%08X", gate.key);
  32. name.assign(symbol.data(), static_cast<std::size_t>(length));
  33. }
  34. output.append(" ");
  35. output.append(name);
  36. output.append(" = ");
  37. append_hex(output, gate.key);
  38. output.append(",\n");
  39. }
  40. return appended;
  41. }
  42. /** Appends one scene's block to a table when the predicate selects any of its gates. */
  43. void append_scene_table(std::string_view scene,
  44. std::span<const format::AuthoredSceneEventKey> gates,
  45. bool (*selected)(const format::AuthoredSceneEventKey&) noexcept,
  46. std::string& output) {
  47. std::string block;
  48. if (append_gates(gates, selected, block) == 0) {
  49. return;
  50. }
  51. output.append(" ").append(scene).append(" = {\n").append(block).append(" },\n");
  52. }
  53. } // namespace
  54. /** Appends one scene's inputs and outputs to their constant tables. */
  55. void append_scene_ports(std::string_view scene,
  56. std::span<const format::AuthoredSceneEventKey> gates,
  57. std::string& inputs,
  58. std::string& outputs) {
  59. append_scene_table(scene, gates, &state::activity_sdk::scene_trigger_is_input, inputs);
  60. append_scene_table(scene, gates, &state::activity_sdk::scene_trigger_is_output, outputs);
  61. }
  62. } // namespace sunrise::client::content::activity::sdk_generation::lua_artifacts::internal