family0_source_seed.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #include "family0_source_seed.h"
  2. #include <Windows.h>
  3. #include <array>
  4. #include <atomic>
  5. #include <cstddef>
  6. #include <cstdint>
  7. #include <cstdio>
  8. #include <cstring>
  9. #include "../../../../core/logging/log.h"
  10. namespace sunrise::client::hooks::queuez::family0 {
  11. namespace {
  12. /** Fields of the queuez manager this hook reads. */
  13. struct ManagerLayout {
  14. /** The account key the sweep itself passes to every family subscribe. */
  15. static constexpr std::size_t accountKey = 16;
  16. };
  17. /** Fields of the family-zero source list. The sweep diffs the whole fixed-size record. */
  18. struct SourceListLayout {
  19. /**
  20. * Members the producer counted with flags bit 0 or bit 4. Bit 4 is set by every upsert, so
  21. * this moves even when nothing is emitted.
  22. */
  23. static constexpr std::size_t countA = 0;
  24. /**
  25. * Entries the producer wrote with a non-zero soid. It moves once per emitted entry, so it is
  26. * the exact length.
  27. */
  28. static constexpr std::size_t countB = 8;
  29. /** First entry, {u64 key, u16 mask}. */
  30. static constexpr std::size_t firstKey = 16;
  31. static constexpr std::size_t firstMask = 24;
  32. /** Second entry. Two entries naming one soid is what gives the family two records. */
  33. static constexpr std::size_t secondKey = 32;
  34. static constexpr std::size_t secondMask = 40;
  35. };
  36. /**
  37. * The smallest entry mask that passes the sweep's own filter without claiming the family-zero
  38. * subscription is already synced. That synced state is what the subscribe must reach.
  39. */
  40. constexpr std::uint16_t kEntryMask = 1;
  41. /** One seeded entry: one record per source key, and one is all the account needs. */
  42. constexpr std::uint64_t kSeededCount = 1;
  43. /** Bytes of the source list the producer owns, rebuilt whole so no stale entry survives. */
  44. constexpr std::size_t kSourceListBytes = 0x210;
  45. /** Shortest gap between two seed reports. The sweep runs per frame, so this bounds the volume. */
  46. constexpr std::uint64_t kReportIntervalMs = 1'000;
  47. /** One line carries the replaced key and the rewrite count. */
  48. constexpr std::size_t kReportLimit = 128;
  49. /** One line carries both counters and the first two entries. */
  50. constexpr std::size_t kListReportLimit = 192;
  51. /** The producer's own output, as the seed found it before writing anything. */
  52. struct ListHead {
  53. std::uint64_t countA{};
  54. std::uint64_t countB{};
  55. std::uint64_t firstKey{};
  56. std::uint64_t secondKey{};
  57. std::uint16_t firstMask{};
  58. std::uint16_t secondMask{};
  59. };
  60. /** @return True when both heads hold the same values. */
  61. [[nodiscard]] bool equal(const ListHead& first, const ListHead& second) noexcept {
  62. return first.countA == second.countA && first.countB == second.countB
  63. && first.firstKey == second.firstKey && first.secondKey == second.secondKey
  64. && first.firstMask == second.firstMask && first.secondMask == second.secondMask;
  65. }
  66. std::atomic<SourceList> g_sourceList{nullptr};
  67. /** The account key, read from the manager rather than written by us, so it cannot disagree. */
  68. std::atomic<std::uint64_t> g_accountKey{0};
  69. /** Total rewrites. A rewrite means something else put its own list back. */
  70. std::atomic<std::uint64_t> g_rewrites{0};
  71. std::atomic<std::uint64_t> g_reportDueTick{0};
  72. /** The last head reported, so the producer's output is logged on change rather than per frame. */
  73. ListHead g_lastHead{};
  74. bool g_hasLastHead{false};
  75. /**
  76. * Reports the producer's own output whenever it changes.
  77. * @param head Source-list head as the seed found it.
  78. */
  79. void report_list(const ListHead& head) noexcept {
  80. if (g_hasLastHead && equal(head, g_lastHead)) {
  81. return;
  82. }
  83. g_lastHead = head;
  84. g_hasLastHead = true;
  85. std::array<char, kListReportLimit> line{};
  86. const int written = std::snprintf(line.data(),
  87. line.size(),
  88. "ev=queuez stage=family0_list countA=%llu countB=%llu "
  89. "first=0x%016llX/%u second=0x%016llX/%u",
  90. static_cast<unsigned long long>(head.countA),
  91. static_cast<unsigned long long>(head.countB),
  92. static_cast<unsigned long long>(head.firstKey),
  93. static_cast<unsigned>(head.firstMask),
  94. static_cast<unsigned long long>(head.secondKey),
  95. static_cast<unsigned>(head.secondMask));
  96. if (written > 0) {
  97. core::log::write(core::log::Channel::client,
  98. core::log::Level::info,
  99. {line.data(), static_cast<std::size_t>(written)});
  100. }
  101. }
  102. /**
  103. * Copies the source-list head the producer left behind.
  104. * @param list Borrowed source list.
  105. * @return Its counters and first two entries.
  106. */
  107. [[nodiscard]] ListHead read_head(const std::byte* list) noexcept {
  108. ListHead head{};
  109. std::memcpy(&head.countA, list + SourceListLayout::countA, sizeof head.countA);
  110. std::memcpy(&head.countB, list + SourceListLayout::countB, sizeof head.countB);
  111. std::memcpy(&head.firstKey, list + SourceListLayout::firstKey, sizeof head.firstKey);
  112. std::memcpy(&head.firstMask, list + SourceListLayout::firstMask, sizeof head.firstMask);
  113. std::memcpy(&head.secondKey, list + SourceListLayout::secondKey, sizeof head.secondKey);
  114. std::memcpy(&head.secondMask, list + SourceListLayout::secondMask, sizeof head.secondMask);
  115. return head;
  116. }
  117. /**
  118. * Reports how often the seed is having to put the account key back.
  119. * A steady rewrite rate means the game's own producer is rebuilding the list underneath.
  120. * @param replaced Key that was in the list before this rewrite.
  121. */
  122. void report_seed(std::uint64_t replaced) noexcept {
  123. const std::uint64_t count = g_rewrites.fetch_add(1, std::memory_order_relaxed) + 1;
  124. const std::uint64_t now = GetTickCount64();
  125. if (now < g_reportDueTick.load(std::memory_order_relaxed)) {
  126. return;
  127. }
  128. g_reportDueTick.store(now + kReportIntervalMs, std::memory_order_relaxed);
  129. std::array<char, kReportLimit> line{};
  130. const int written = std::snprintf(line.data(),
  131. line.size(),
  132. "ev=queuez stage=family0 result=seeded replaced=0x%016llX "
  133. "rewrites=%llu",
  134. static_cast<unsigned long long>(replaced),
  135. static_cast<unsigned long long>(count));
  136. if (written > 0) {
  137. core::log::write(core::log::Channel::client,
  138. core::log::Level::info,
  139. {line.data(), static_cast<std::size_t>(written)});
  140. }
  141. }
  142. } // namespace
  143. /** @param getter The source-list getter we found, or null to withdraw it. */
  144. void publish_source_list(SourceList getter) noexcept {
  145. g_sourceList.store(getter, std::memory_order_release);
  146. }
  147. /** Captures the account key the source-list seed needs. */
  148. void learn_account_key(std::byte* manager) noexcept {
  149. if (manager == nullptr || g_accountKey.load(std::memory_order_acquire) != 0) {
  150. return;
  151. }
  152. std::uint64_t key = 0;
  153. std::memcpy(&key, manager + ManagerLayout::accountKey, sizeof key);
  154. if (key != 0) {
  155. g_accountKey.store(key, std::memory_order_release);
  156. }
  157. }
  158. /** Seeds the source list with the account key. Runs every tick, writes only when it differs. */
  159. void seed_source_list() noexcept {
  160. const SourceList source = g_sourceList.load(std::memory_order_acquire);
  161. const std::uint64_t key = g_accountKey.load(std::memory_order_acquire);
  162. if (source == nullptr || key == 0) {
  163. return;
  164. }
  165. std::byte* const list = source();
  166. if (list == nullptr) {
  167. return;
  168. }
  169. // Read before the early return, so the producer's own output is visible once it starts working.
  170. const ListHead head = read_head(list);
  171. report_list(head);
  172. // Both counters have to agree, not just the first key. A counter mismatch arms a boot
  173. // deadline in the sweep, and rewriting the whole buffer clears it.
  174. if (head.firstKey == key && head.countA == kSeededCount && head.countB == kSeededCount
  175. && head.secondKey == 0) {
  176. return; // Already exactly ours. Rewriting the same bytes would only churn the subscribe.
  177. }
  178. const std::uint64_t existing = head.firstKey;
  179. // The whole buffer is rebuilt, so an entry the producer left past the first cannot survive
  180. // as a key the sweep would go on to subscribe.
  181. std::array<std::byte, kSourceListBytes> seeded{};
  182. std::memcpy(seeded.data() + SourceListLayout::countA, &kSeededCount, sizeof kSeededCount);
  183. std::memcpy(seeded.data() + SourceListLayout::countB, &kSeededCount, sizeof kSeededCount);
  184. std::memcpy(seeded.data() + SourceListLayout::firstKey, &key, sizeof key);
  185. std::memcpy(seeded.data() + SourceListLayout::firstMask, &kEntryMask, sizeof kEntryMask);
  186. std::memcpy(list, seeded.data(), seeded.size());
  187. report_seed(existing);
  188. }
  189. /** Clears the captured key, the published getter, and the report state. */
  190. void reset() noexcept {
  191. g_sourceList.store(nullptr, std::memory_order_release);
  192. g_accountKey.store(0, std::memory_order_release);
  193. g_rewrites.store(0, std::memory_order_release);
  194. g_reportDueTick.store(0, std::memory_order_release);
  195. g_lastHead = {};
  196. g_hasLastHead = false;
  197. }
  198. } // namespace sunrise::client::hooks::queuez::family0