actor_command_runtime_codec.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #pragma once
  2. #include <cstddef>
  3. #include <cstdint>
  4. #include <span>
  5. #include <utility>
  6. #include "../../../state/activity_sdk/runtime.h"
  7. #include "../../bap/activity_message/wire_schema/activity_wire_codec.h"
  8. namespace sunrise::middleware::gameplay::external {
  9. namespace actor_wire = bap::activity_message::wire_schema;
  10. /** Immutable actor-message rows pinned by one SDK snapshot. */
  11. struct ActorCommandCatalog final {
  12. state::activity_sdk::Snapshot owner{};
  13. std::span<const state::activity_sdk::format::ActorMessageSchema> messages{};
  14. std::span<const state::activity_sdk::format::ActorCommandDefinition> commands{};
  15. std::span<const state::activity_sdk::format::SimulationEventDefinition> events{};
  16. std::span<const state::activity_sdk::format::RuntimeSchema> schemas{};
  17. std::span<const state::activity_sdk::format::RuntimeField> fields{};
  18. std::span<const state::activity_sdk::format::ActorBehaviorProfile> profiles{};
  19. };
  20. /** Structural type-36 values which do not belong to the selected payload schema. */
  21. struct ActorCommandHeader final {
  22. std::uint16_t target{};
  23. std::uint16_t auxiliary16{};
  24. std::uint8_t defaultValue{};
  25. std::uint8_t mode{};
  26. std::uint8_t targetReference{};
  27. std::uint8_t auxiliary8{};
  28. };
  29. /** Resolved identity retained with one encoded or decoded command body. */
  30. struct ActorCommandIdentity final {
  31. std::uint32_t messageIndex{state::activity_sdk::format::kAbsentIndex};
  32. std::uint32_t commandIndex{state::activity_sdk::format::kAbsentIndex};
  33. std::uint32_t definitionHandle{};
  34. std::uint32_t durableKey{};
  35. std::uint32_t ownerClass{};
  36. std::uint32_t handlerSlot{};
  37. std::uint32_t selector{};
  38. std::uint32_t payloadHandle{};
  39. };
  40. /**
  41. * Pins one snapshot's SDK actor-message rows for one complete operation.
  42. * @param snapshot Snapshot the rows are read from and which keeps them alive.
  43. * @param output Left cleared unless every row family is present.
  44. * @return True only when every row family the encoder needs is non-empty.
  45. */
  46. [[nodiscard]] inline bool
  47. published_actor_command_catalog(const state::activity_sdk::Snapshot& snapshot,
  48. ActorCommandCatalog& output) noexcept {
  49. output = {};
  50. if (snapshot == nullptr) {
  51. return false;
  52. }
  53. ActorCommandCatalog candidate{};
  54. candidate.owner = snapshot;
  55. candidate.messages = candidate.owner->actor_message_schemas();
  56. candidate.commands = candidate.owner->actor_command_definitions();
  57. candidate.events = candidate.owner->simulation_event_definitions();
  58. candidate.schemas = candidate.owner->runtime_schemas();
  59. candidate.fields = candidate.owner->runtime_fields();
  60. candidate.profiles = candidate.owner->actor_behavior_profiles();
  61. if (candidate.messages.empty() || candidate.commands.empty() || candidate.events.empty()
  62. || candidate.schemas.empty() || candidate.fields.empty() || candidate.profiles.empty()) {
  63. return false;
  64. }
  65. output = std::move(candidate);
  66. return true;
  67. }
  68. /** Pins the currently published SDK actor-message rows for one complete operation. */
  69. [[nodiscard]] inline bool published_actor_command_catalog(ActorCommandCatalog& output) noexcept {
  70. return published_actor_command_catalog(state::activity_sdk::snapshot(), output);
  71. }
  72. /** Builds a generic reflection resolver over the catalog's exact runtime rows. */
  73. [[nodiscard]] actor_wire::RuntimeSchemaResolver
  74. actor_runtime_schema_resolver(const ActorCommandCatalog& catalog) noexcept;
  75. /**
  76. * Encodes one SDK-selected actor command body.
  77. * Payload values must use the command's payload schema identity.
  78. */
  79. [[nodiscard]] bool
  80. encode_actor_command_body(const ActorCommandCatalog& catalog,
  81. std::uint32_t messageIndex,
  82. std::uint32_t commandIndex,
  83. const ActorCommandHeader& header,
  84. std::span<const actor_wire::RuntimeDraftValue> payloadValues,
  85. const actor_wire::RuntimeSchemaResolver& reflection,
  86. std::span<std::byte> output,
  87. std::size_t& written,
  88. std::size_t& writtenBits,
  89. actor_wire::CodecStatus& status,
  90. ActorCommandIdentity& identity) noexcept;
  91. /** Decodes one SDK-selected actor command body and resolves its selector row. */
  92. [[nodiscard]] bool decode_actor_command_body(const ActorCommandCatalog& catalog,
  93. std::uint32_t messageIndex,
  94. std::span<const std::byte> payload,
  95. std::size_t bitCount,
  96. const actor_wire::RuntimeSchemaResolver& reflection,
  97. std::span<actor_wire::RuntimeDecodedValue> values,
  98. actor_wire::RuntimeDecodeResult& result,
  99. ActorCommandIdentity& identity) noexcept;
  100. } // namespace sunrise::middleware::gameplay::external