mission_script_lua_internal.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #pragma once
  2. #include <array>
  3. #include <cstddef>
  4. #include <span>
  5. #include "../../../state/activity_sdk/runtime.h"
  6. #include "mission_script_lua_args.h"
  7. #include "mission_script_lua_bounded_internal.h"
  8. #include "mission_script_lua_enum_internal.h"
  9. #include "mission_script_lua_key_internal.h"
  10. #include "mission_script_lua_names.h"
  11. #include "mission_script_lua_peer_internal.h"
  12. #include "mission_script_lua_resolve.h"
  13. #include "mission_script_lua_surface.h"
  14. #include "mission_script_lua_types.h"
  15. #include "mission_script_lua_value_internal.h"
  16. #include "mission_script_vm_internal.h"
  17. namespace sunrise::server::activity::mission::lua_vm::detail {
  18. int context_cohort(lua_State* state);
  19. void register_population_metatables(lua_State* state);
  20. // Every mission API entry point, grouped by the translation unit that defines it. The names,
  21. // handles, re-read helpers and surface matchers live in the headers included above.
  22. /** Decodes the generated SDK build identity into its durable digest. */
  23. [[nodiscard]] inline bool decode_sdk_build_sha256(std::string_view text,
  24. std::array<std::byte, 32>& output) noexcept {
  25. output = {};
  26. // The generated SDK build identity is spelled as this prefix and 64 hex digits.
  27. constexpr std::string_view kPrefix = "sha256:";
  28. if (!text.starts_with(kPrefix)) {
  29. return false;
  30. }
  31. text.remove_prefix(kPrefix.size());
  32. if (text.size() != output.size() * 2U) {
  33. return false;
  34. }
  35. const auto nibble = [](char value, std::uint8_t& result) noexcept {
  36. if (value >= '0' && value <= '9') {
  37. result = static_cast<std::uint8_t>(value - '0');
  38. return true;
  39. }
  40. if (value >= 'a' && value <= 'f') {
  41. result = static_cast<std::uint8_t>(value - 'a' + 10);
  42. return true;
  43. }
  44. if (value >= 'A' && value <= 'F') {
  45. result = static_cast<std::uint8_t>(value - 'A' + 10);
  46. return true;
  47. }
  48. return false;
  49. };
  50. for (std::size_t index = 0; index < output.size(); ++index) {
  51. std::uint8_t high = 0;
  52. std::uint8_t low = 0;
  53. if (!nibble(text[index * 2U], high) || !nibble(text[index * 2U + 1U], low)) {
  54. output = {};
  55. return false;
  56. }
  57. output[index] = static_cast<std::byte>((high << 4U) | low);
  58. }
  59. return true;
  60. }
  61. /**
  62. * Queues one built intent on the pending candidate and returns its RequestKey.
  63. * It owns the allocation failure, so no effect repeats one.
  64. * @return The Lua result count.
  65. */
  66. [[nodiscard]] inline int queue_intent(lua_State* state, CallFrame& frame, Intent& intent) {
  67. intent.attemptGeneration = impl_from_state(state)->attempt.generation;
  68. intent.requestKey = mint_intent_key(frame.candidate);
  69. if (intent.requestKey == state::activity::mission::kAbsentIntentKey) {
  70. return luaL_error(state, "mission request keys are exhausted");
  71. }
  72. const std::uint64_t requestKey = intent.requestKey;
  73. try {
  74. frame.candidate.intents.push_back(std::move(intent));
  75. } catch (const std::bad_alloc&) {
  76. frame.intentAllocationFailed = true;
  77. return luaL_error(state, "mission intent allocation failed");
  78. }
  79. push_request_key(state, requestKey);
  80. return 1;
  81. }
  82. // Metatable registration, one function per domain. setup_sandbox calls these and holds no
  83. // per-type list of its own.
  84. void register_context_metatables(lua_State* state);
  85. void register_definition_metatables(lua_State* state);
  86. void register_collection_metatables(lua_State* state);
  87. void register_state_metatables(lua_State* state);
  88. void register_lifetime_metatables(lua_State* state);
  89. void register_event_metatables(lua_State* state);
  90. void register_squad_metatables(lua_State* state);
  91. void register_scene_metatables(lua_State* state);
  92. void register_slot_metatables(lua_State* state);
  93. void register_actor_sequence_metatables(lua_State* state);
  94. [[nodiscard]] int slot_actor_sequences(lua_State* state);
  95. [[nodiscard]] int slot_play_actor_sequence(lua_State* state);
  96. [[nodiscard]] int slot_applied(lua_State* state);
  97. // SDK definition readers, defined in mission_script_lua_definition_api.cpp.
  98. [[nodiscard]] int message_index(lua_State* state);
  99. [[nodiscard]] int message_field_index(lua_State* state);
  100. [[nodiscard]] int activity_binding_locator_index(lua_State* state);
  101. [[nodiscard]] int activity_index(lua_State* state);
  102. // The sandbox and the mission callback frame, defined in mission_script_lua_sandbox.cpp.
  103. // initialize_sandbox, protected_callback, set_error and destroy_state are declared in
  104. // mission_script_vm_internal.h.
  105. // Row collections, defined in mission_script_lua_collection_api.cpp.
  106. [[nodiscard]] int activity_binding_tag_collection_index(lua_State* state);
  107. [[nodiscard]] int activity_binding_locator_collection_index(lua_State* state);
  108. [[nodiscard]] int squad_collection_index(lua_State* state);
  109. [[nodiscard]] int scene_collection_index(lua_State* state);
  110. [[nodiscard]] int slot_collection_index(lua_State* state);
  111. [[nodiscard]] int message_collection_index(lua_State* state);
  112. [[nodiscard]] int message_field_collection_index(lua_State* state);
  113. // The running callback's activity binding, defined in mission_script_lua_context_api.cpp.
  114. [[nodiscard]] int context_index(lua_State* state);
  115. // Typed effects, each defined beside the handle it needs.
  116. [[nodiscard]] int squad_index(lua_State* state);
  117. [[nodiscard]] int squad_place(lua_State* state);
  118. [[nodiscard]] int squad_actor_command(lua_State* state);
  119. [[nodiscard]] int scene_index(lua_State* state);
  120. [[nodiscard]] int scene_activate(lua_State* state);
  121. [[nodiscard]] int scene_send_event(lua_State* state);
  122. [[nodiscard]] int scene_stop(lua_State* state);
  123. [[nodiscard]] int lifetime_index(lua_State* state);
  124. [[nodiscard]] int lifetime_set(lua_State* state);
  125. // Mission context, variables and timers, defined in mission_script_lua_state_api.cpp.
  126. [[nodiscard]] int context_set_phase(lua_State* state);
  127. [[nodiscard]] int context_select_state(lua_State* state);
  128. [[nodiscard]] int context_set_variable(lua_State* state);
  129. [[nodiscard]] int context_clear_variable(lua_State* state);
  130. [[nodiscard]] int context_start_timer(lua_State* state);
  131. [[nodiscard]] int context_cancel_timer(lua_State* state);
  132. [[nodiscard]] int state_index(lua_State* state);
  133. // Message matching, defined in mission_script_lua_message_api.cpp.
  134. [[nodiscard]] const char* lua_client_message_status_name(host::ClientMessageStatus status) noexcept;
  135. [[nodiscard]] int message_matches(lua_State* state);
  136. /** Sets one verified device channel through the same guarded route. */
  137. [[nodiscard]] int slot_set_channel(lua_State* state);
  138. /** Applies one named device transition over the same guarded channel route. */
  139. [[nodiscard]] int slot_transition(lua_State* state);
  140. /** Sets the object filter and caller value one occupancy condition carries. */
  141. [[nodiscard]] int slot_set_occupancy_condition(lua_State* state);
  142. /** Shows one generated type-68 HUD directive. */
  143. [[nodiscard]] int slot_set_directive(lua_State* state);
  144. /** Hides the active type-68 HUD directive. */
  145. [[nodiscard]] int slot_clear_directives(lua_State* state);
  146. /** Reads one member of a Slot row. */
  147. [[nodiscard]] int slot_index(lua_State* state);
  148. /** @return True when one live Slot row is an exact type-2 combatant. */
  149. [[nodiscard]] bool exact_combatant_slot(const SlotDefinition& definition) noexcept;
  150. /** Stages one authored native Auth body on the guarded message-5 route. */
  151. [[nodiscard]] int queue_slot_auth(lua_State* state,
  152. const SlotDefinition& slot,
  153. std::uint32_t schema,
  154. std::size_t bitCount,
  155. std::span<const std::byte> body,
  156. IntentKind kind = IntentKind::applySlotAuth,
  157. bool active = false);
  158. void push_atom_kinds(lua_State* state);
  159. /** Reads one integer field from a generated declaration table. */
  160. [[nodiscard]] lua_Integer directive_integer(lua_State* state, int table, const char* field);
  161. [[nodiscard]] int slot_assign_combat_objective(lua_State* state);
  162. [[nodiscard]] int event_task_cost(lua_State* state);
  163. [[nodiscard]] int event_task_group(lua_State* state);
  164. [[nodiscard]] int slot_set_music_section(lua_State* state);
  165. [[nodiscard]] int slot_set_public_event_state(lua_State* state);
  166. [[nodiscard]] int slot_play_sequence(lua_State* state);
  167. [[nodiscard]] int slot_set_cinematic_active(lua_State* state);
  168. [[nodiscard]] int slot_reset_objectives(lua_State* state);
  169. [[nodiscard]] int slot_advance_task(lua_State* state);
  170. [[nodiscard]] int slot_play_performance(lua_State* state);
  171. [[nodiscard]] int slot_play_dialogue_cue(lua_State* state);
  172. [[nodiscard]] int slot_play_actor_path(lua_State* state);
  173. [[nodiscard]] int slot_play_actor_action(lua_State* state);
  174. [[nodiscard]] int slot_retire_actor(lua_State* state);
  175. [[nodiscard]] int slot_bind_combatant_to_squad(lua_State* state);
  176. [[nodiscard]] int slot_set_darkness_zone(lua_State* state);
  177. [[nodiscard]] int slot_set_object_filter(lua_State* state);
  178. [[nodiscard]] int slot_generate_map(lua_State* state);
  179. [[nodiscard]] int slot_watch_damage(lua_State* state);
  180. [[nodiscard]] int slot_set_interactable_object(lua_State* state);
  181. [[nodiscard]] int slot_set_ghost_link(lua_State* state);
  182. [[nodiscard]] int slot_ghost_link(lua_State* state);
  183. [[nodiscard]] int slot_set_object_active(lua_State* state);
  184. [[nodiscard]] int slot_fire_trigger(lua_State* state);
  185. [[nodiscard]] int slot_disarm_trigger(lua_State* state);
  186. /** @return True for a positive counter accepted by every typed Auth body. */
  187. [[nodiscard]] bool valid_counter(lua_Integer value) noexcept;
  188. /** Reads an optional typed slot reference from the named argument table. */
  189. [[nodiscard]] bool optional_slot_reference(
  190. lua_State* state,
  191. const char* name,
  192. std::uint32_t slotType,
  193. middleware::bap::activity_message::scriptable_auth::Type2LaneClientRef& output);
  194. // The client-atom program, defined in mission_script_lua_slot_atoms.cpp.
  195. /** Loads the 32-lane client-atom program one type-2 combatant runs on its bound actor. */
  196. [[nodiscard]] int slot_run_atoms(lua_State* state);
  197. } // namespace sunrise::server::activity::mission::lua_vm::detail