internal.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. #pragma once
  2. #include <array>
  3. #include <bitset>
  4. #include <cstddef>
  5. #include <cstdint>
  6. #include <memory>
  7. #include <span>
  8. #include <vector>
  9. #include "../../../../core/filesystem/path.h"
  10. #include "../../../../middleware/content/packages/reader/reader.h"
  11. #include "../../../../middleware/content/packages/tables/definition_index_table.h"
  12. #include "../../../../middleware/content/packages/tables/items.h"
  13. #include "../../../../state/build_data/abilities/definition.h"
  14. #include "../../../../state/build_data/collectibles/collectible_catalog.h"
  15. #include "../../../../state/build_data/constants/definition.h"
  16. #include "../../../../state/build_data/items/details/definition.h"
  17. #include "../../../../state/build_data/items/item_catalog.h"
  18. #include "../../../../state/build_data/material_requirements/material_requirement_catalog.h"
  19. #include "../../../../state/build_data/progressions/definition.h"
  20. namespace sunrise::client::content::items::packages {
  21. namespace reader = middleware::content::packages::reader;
  22. namespace tables = middleware::content::packages::tables;
  23. /** Equippable rows, their initial plugs, and authored override plugs. */
  24. inline constexpr std::size_t kDetailCapacity =
  25. state::build_data::items::details::kDefinitionCapacity;
  26. /** Native item indices selected for the deduplicated detail closure. */
  27. using DetailRequests = std::bitset<state::build_data::items::kDefinitionCapacity>;
  28. /** Authored definition hashes one pass looks for while walking the item index table. */
  29. struct AuthoredHashes {
  30. std::array<std::uint32_t, kDetailCapacity> values{};
  31. std::size_t count{};
  32. };
  33. /** Everything the detail pass needs to read one requested definition from the packages. */
  34. struct DetailSource {
  35. const reader::Source* source{};
  36. reader::Scratch* scratch{};
  37. /** Item index table blob owning the located array. */
  38. std::span<const std::byte> table{};
  39. tables::Array array{};
  40. std::vector<std::byte>* definition{};
  41. };
  42. /** The container name is not always unique, so every match is a candidate. */
  43. inline constexpr std::size_t kContainerCandidates = 16;
  44. /** Lock-owned storage kept off the caller stack, shared by every stage of the pass. */
  45. struct Storage {
  46. reader::Scratch scratch{};
  47. std::vector<std::byte> container{};
  48. std::vector<std::byte> child{};
  49. std::vector<std::byte> root{};
  50. std::vector<std::byte> definition{};
  51. /** Shared reusable/randomized plug-set table read from investment-root slot 51. */
  52. std::vector<std::byte> plugSetTable{};
  53. /** Compact 0..3 special plug-category code of every dense installed item row. */
  54. std::array<std::uint8_t, state::build_data::items::kDefinitionCapacity> specialPlugCategories{};
  55. DetailRequests detailRequests{};
  56. std::array<std::uint16_t, kDetailCapacity> requestedDetailIndices{};
  57. std::unique_ptr<state::build_data::items::details::Definition[]> details{};
  58. AuthoredHashes authoredHashes{};
  59. std::vector<std::byte> abilityTable{};
  60. std::vector<std::byte> abilityPool{};
  61. std::array<state::build_data::abilities::Definition,
  62. state::build_data::abilities::kDefinitionCapacity>
  63. abilityRows{};
  64. std::array<state::build_data::progressions::Definition,
  65. state::build_data::progressions::kDefinitionCapacity>
  66. progressionRows{};
  67. std::array<state::build_data::collectibles::Definition,
  68. state::build_data::collectibles::kDefinitionCapacity>
  69. collectibleRows{};
  70. std::array<state::build_data::material_requirements::Definition,
  71. state::build_data::material_requirements::kDefinitionCapacity>
  72. materialRequirementRows{};
  73. std::array<state::build_data::items::Definition, state::build_data::items::kDefinitionCapacity>
  74. rows{};
  75. };
  76. /**
  77. * Collects the authored equipment and plug hashes every configured character names.
  78. * Hashes are sorted so the index-table walk can test each row by binary search instead of
  79. * rescanning the whole authored set per row.
  80. * @param output Receives the sorted unique authored hashes.
  81. * @return True when the account is good and every hash fits.
  82. */
  83. [[nodiscard]] bool collect_authored_hashes(AuthoredHashes& output) noexcept;
  84. /** @param hashes Sorted authored hashes. @param hash Row hash. @return True when authored. */
  85. [[nodiscard]] bool authored(const AuthoredHashes& hashes, std::uint32_t hash) noexcept;
  86. /**
  87. * @param row Installed item row.
  88. * @return True when its bucket maps to an equipment slot
  89. * supported by the generated loadout.
  90. */
  91. [[nodiscard]] bool equippable(const tables::items::Row& row) noexcept;
  92. /**
  93. * Adds one definition index to the deduplicated requested set.
  94. * @param definitionIndex Native
  95. * item index.
  96. * @param requested Requested-set storage.
  97. */
  98. void request(std::uint16_t definitionIndex, DetailRequests& requested) noexcept;
  99. /**
  100. * Adds every socket lane's initial plug to the requested set.
  101. * A lane using native defaults falls back to this plug, so its detail must exist.
  102. * @param row
  103. * Item row already read from its definition blob.
  104. * @param itemDefinitionCount Installed
  105. * item-table bound.
  106. * @param requested Requested-set storage.
  107. */
  108. void append_initial_plugs(const tables::items::Row& row,
  109. std::uint64_t itemDefinitionCount,
  110. DetailRequests& requested) noexcept;
  111. /**
  112. * Materializes requested native indices in ascending order.
  113. * @param requested Deduplicated
  114. * native-index set.
  115. * @param output Fixed detail-index storage.
  116. * @param count Receives the
  117. * number of selected rows, or zero when output is too small.
  118. * @return True when every selected
  119. * row fits.
  120. */
  121. [[nodiscard]] bool materialize_requests(const DetailRequests& requested,
  122. std::span<std::uint16_t> output,
  123. std::size_t& count) noexcept;
  124. /**
  125. * Reads one requested definition and turns it into its cached detail form.
  126. * @param source Located item index table and package reader state.
  127. * @param definitionIndex Native item index.
  128. * @param detail Receives the cached detail.
  129. * @return True when the row is found and its definition blob reads.
  130. */
  131. [[nodiscard]] bool build_detail(const DetailSource& source,
  132. std::uint16_t definitionIndex,
  133. state::build_data::items::details::Definition& detail,
  134. tables::items::Row& item) noexcept;
  135. /**
  136. * Reads the stat rows the installed investment constants blob names.
  137. * @param source Package source.
  138. * @param scratch Reader scratch.
  139. * @param root Investment root bytes.
  140. * @param blob Scratch storage for the constants blob.
  141. * @param output Receives the extracted stat rows.
  142. * @return True when the blob reads and carries every named row.
  143. */
  144. [[nodiscard]] bool
  145. read_investment_constants(const reader::Source& source,
  146. reader::Scratch& scratch,
  147. std::span<const std::byte> root,
  148. std::vector<std::byte>& blob,
  149. state::build_data::constants::InvestmentConstants& output) noexcept;
  150. /**
  151. * Builds the ability buckets one subclass publishes under one ability selection.
  152. * @param source Package source.
  153. * @param scratch Reader scratch.
  154. * @param listDefinition Socket-entry-list definition bytes of the subclass.
  155. * @param blob Scratch storage reused for every pool blob.
  156. * @param selection The character's 5 selected socket entries.
  157. * @param output Receives the 12 buckets and the overflow bank.
  158. * @return True when every selected entry reaches a bucket of its own.
  159. */
  160. [[nodiscard]] bool build_ability_buckets(const reader::Source& source,
  161. reader::Scratch& scratch,
  162. std::span<const std::byte> listDefinition,
  163. std::vector<std::byte>& blob,
  164. const state::build_data::abilities::Selection& selection,
  165. state::build_data::abilities::Definition& output) noexcept;
  166. /**
  167. * Builds one ability bucket row per distinct subclass and ability selection in use.
  168. * Two characters on the same subclass with the same ability selection publish identical
  169. * buckets, so the row is keyed by both and built once.
  170. * @param source Package source.
  171. * @param scratch Reader scratch.
  172. * @param root Investment root bytes.
  173. * @param table Scratch storage for the socket entry list table.
  174. * @param definition Scratch storage for one socket entry list definition.
  175. * @param blob Scratch storage reused for every pool blob.
  176. * @param output Row storage.
  177. * @param count Receives the number of rows built.
  178. * @return True when the table reads; a subclass that fails is skipped, not fatal.
  179. */
  180. [[nodiscard]] bool
  181. build_character_abilities(const reader::Source& source,
  182. reader::Scratch& scratch,
  183. std::span<const std::byte> root,
  184. std::vector<std::byte>& table,
  185. std::vector<std::byte>& definition,
  186. std::vector<std::byte>& blob,
  187. std::span<state::build_data::abilities::Definition> output,
  188. std::size_t& count) noexcept;
  189. /**
  190. * Reads the progression definition table and the object array each definition routes to.
  191. * The table is inline rows, not index rows. The scope byte in a row picks the replicated object
  192. * holding that progression, and the row's place among rows of that scope is its slot there.
  193. * @param source Package source.
  194. * @param scratch Reader scratch.
  195. * @param root Investment root bytes.
  196. * @param blob Scratch storage for the table.
  197. * @param output Row storage in native definition order.
  198. * @param count Receives the number of rows read.
  199. * @return True when the table reads and every row fits.
  200. */
  201. [[nodiscard]] bool build_progressions(const reader::Source& source,
  202. reader::Scratch& scratch,
  203. std::span<const std::byte> root,
  204. std::vector<std::byte>& blob,
  205. std::span<state::build_data::progressions::Definition> output,
  206. std::size_t& count) noexcept;
  207. /**
  208. * Copies the block key material this pass borrows.
  209. * @param keys Receives the primary, alternate and nonce material.
  210. * @return True when the installed key table and the bootstrap token are both there.
  211. */
  212. [[nodiscard]] bool collect_keys(reader::BlockKeys& keys) noexcept;
  213. /**
  214. * Collects every catalogue tag carrying the container name.
  215. * @param candidates Receives the candidate tags.
  216. * @param count Receives the number of candidates.
  217. * @return True when the catalogue names at least one.
  218. */
  219. [[nodiscard]] bool
  220. investment_globals_tags(std::array<std::uint32_t, kContainerCandidates>& candidates,
  221. std::size_t& count) noexcept;
  222. /** @param directory Receives the installed packages directory. @return True when it exists. */
  223. [[nodiscard]] bool package_directory(core::path::Buffer& directory) noexcept;
  224. /** @param slot Requested-set position. @param definitionIndex Native item index that failed. */
  225. void report_detail_failure(std::size_t slot, std::uint16_t definitionIndex) noexcept;
  226. /** @param count Ability bucket rows the pass built, one per subclass and ability selection. */
  227. void report_ability_count(std::size_t count) noexcept;
  228. /** Reports requested, retained, and skipped detail closure rows. */
  229. void report_detail_count(std::size_t requested, std::size_t built) noexcept;
  230. /** Reports the exact socket-rule, deduplicated-pool, member, and skipped-lane counts. */
  231. void report_socket_plug_count(std::size_t rules,
  232. std::size_t pools,
  233. std::size_t members,
  234. std::size_t skipped) noexcept;
  235. /** Reports the pass outcome once. @param published Rows published, or zero on failure. */
  236. void report(std::size_t published, const char* reason) noexcept;
  237. /**
  238. * Publishes the inventory bucket descriptors from the root's bucket table.
  239. * @param source Package source.
  240. * @param storage Pass storage.
  241. * @param root Investment root bytes.
  242. * @return True when the table reads and publishes.
  243. */
  244. [[nodiscard]] bool build_buckets(const reader::Source& source,
  245. Storage& storage,
  246. std::span<const std::byte> root) noexcept;
  247. /**
  248. * Publishes the socket entry list table from the root.
  249. * @param source Package source.
  250. * @param storage Pass storage.
  251. * @param root Investment root bytes.
  252. * @return True when the table reads and publishes.
  253. */
  254. [[nodiscard]] bool build_socket_entry_lists(const reader::Source& source,
  255. Storage& storage,
  256. std::span<const std::byte> root) noexcept;
  257. /**
  258. * Reads and publishes the root's dense collectible-to-item mapping table.
  259. * @param source
  260. * Package source.
  261. * @param storage Pass storage, including scratch bytes and bounded row storage.
  262. * * @param root Investment root bytes.
  263. * @param itemDefinitionCount Number of rows in the
  264. * installed item index table.
  265. * @return True when every tag, class, bound, and item link validates
  266. * and publishes.
  267. */
  268. [[nodiscard]] bool build_collectibles(const reader::Source& source,
  269. Storage& storage,
  270. std::span<const std::byte> root,
  271. std::uint64_t itemDefinitionCount) noexcept;
  272. /**
  273. * Walks the located item index table, then publishes every domain that depends on it.
  274. * @param source Package source.
  275. * @param storage Pass storage holding the located table blob.
  276. * @param table Located item index array.
  277. * @param rowCount Receives the dense item rows published.
  278. * @param reason Receives the step name when a stage fails.
  279. * @return True when the dense table and every dependent domain publish.
  280. */
  281. [[nodiscard]] bool build_item_rows(const reader::Source& source,
  282. Storage& storage,
  283. const tables::Array& table,
  284. std::size_t& rowCount,
  285. const char*& reason) noexcept;
  286. /** Reads and publishes every native material-requirement set from investment-root slot 96. */
  287. [[nodiscard]] bool build_material_requirements(const reader::Source& source,
  288. Storage& storage,
  289. std::span<const std::byte> root,
  290. std::uint64_t itemDefinitionCount) noexcept;
  291. } // namespace sunrise::client::content::items::packages