internal.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. #pragma once
  2. #include <array>
  3. #include <bitset>
  4. #include <cstddef>
  5. #include <cstdint>
  6. #include <span>
  7. #include <vector>
  8. #include "../../../../core/filesystem/path.h"
  9. #include "../../../../middleware/content/packages/reader/reader.h"
  10. #include "../../../../middleware/content/packages/tables/definition_index_table.h"
  11. #include "../../../../middleware/content/packages/tables/items.h"
  12. #include "../../../../state/build_data/abilities/definition.h"
  13. #include "../../../../state/build_data/bounties/definition.h"
  14. #include "../../../../state/build_data/collectibles/collectible_catalog.h"
  15. #include "../../../../state/build_data/constants/definition.h"
  16. #include "../../../../state/build_data/inventory/buckets/definition.h"
  17. #include "../../../../state/build_data/items/catalysts/definition.h"
  18. #include "../../../../state/build_data/items/details/definition.h"
  19. #include "../../../../state/build_data/items/item_catalog.h"
  20. #include "../../../../state/build_data/material_requirements/material_requirement_catalog.h"
  21. #include "../../../../state/build_data/progressions/definition.h"
  22. #include "../../../../state/build_data/runtime.h"
  23. #include "../../../../state/build_data/season_pass/definition.h"
  24. #include "../../../../state/build_data/sobjects/sobject_catalog.h"
  25. namespace sunrise::client::content::items::packages {
  26. namespace reader = middleware::content::packages::reader;
  27. namespace tables = middleware::content::packages::tables;
  28. /** Equippable rows, their initial plugs, and authored override plugs. */
  29. inline constexpr std::size_t kDetailCapacity =
  30. state::build_data::items::details::kDefinitionCapacity;
  31. /** Native item indices selected for the deduplicated detail closure. */
  32. using DetailRequests = std::bitset<state::build_data::items::kDefinitionCapacity>;
  33. /** Authored definition hashes one pass looks for while walking the item index table. */
  34. struct AuthoredHashes {
  35. std::array<std::uint32_t, kDetailCapacity> values{};
  36. std::size_t count{};
  37. };
  38. /** Everything the detail pass needs to read one requested definition from the packages. */
  39. struct DetailSource {
  40. const reader::Source* source{};
  41. reader::Scratch* scratch{};
  42. /** Item index table blob owning the located array. */
  43. std::span<const std::byte> table{};
  44. tables::Array array{};
  45. std::vector<std::byte>* definition{};
  46. };
  47. /** The container name is not always unique, so every match is a candidate. */
  48. inline constexpr std::size_t kContainerCandidates = 16;
  49. /** Lock-owned storage kept off the caller stack, shared by every stage of the pass. */
  50. struct Storage {
  51. reader::Scratch scratch{};
  52. /** Node rows held until the value slot and owned records are resolved. */
  53. std::array<state::build_data::nodes::Definition, state::build_data::nodes::kDefinitionCapacity>
  54. nodeRows{};
  55. /** Record rows held until the completion flag mapping is resolved. */
  56. std::array<state::build_data::records::Definition,
  57. state::build_data::records::kDefinitionCapacity>
  58. recordRows{};
  59. /** Flat objective, interval and reward banks the record rows name by range. */
  60. std::array<state::build_data::records::Objective,
  61. state::build_data::records::kObjectiveCapacity>
  62. recordObjectives{};
  63. std::array<state::build_data::records::Interval, state::build_data::records::kIntervalCapacity>
  64. recordIntervals{};
  65. std::array<state::build_data::records::Reward, state::build_data::records::kRewardCapacity>
  66. recordRewards{};
  67. std::size_t nodeCount{};
  68. std::size_t recordCount{};
  69. std::size_t recordObjectiveCount{};
  70. std::size_t recordIntervalCount{};
  71. std::size_t recordRewardCount{};
  72. /** Objective and record display tables, held while the record pass joins them. */
  73. std::vector<std::byte> objectiveTable{};
  74. std::vector<std::byte> displayTable{};
  75. /** Progression, item index, per-item strings and unlock slot tables, held across one join. */
  76. std::vector<std::byte> progressionTable{};
  77. std::vector<std::byte> itemIndexTable{};
  78. std::vector<std::byte> itemStringsTable{};
  79. std::vector<std::byte> unlockSlotTable{};
  80. std::vector<std::byte> container{};
  81. std::vector<std::byte> child{};
  82. std::vector<std::byte> root{};
  83. std::vector<std::byte> definition{};
  84. /** Shared reusable/randomized plug-set table read from investment-root slot 51. */
  85. std::vector<std::byte> plugSetTable{};
  86. /** Dense item-indexed catalyst completion expressions for this package pass. */
  87. std::vector<state::build_data::items::catalysts::CompletionCondition>
  88. catalystCompletionConditions{};
  89. /** Dense socket-type-indexed acquired-state gates for this package pass. */
  90. std::vector<state::build_data::items::catalysts::AcquisitionGate> catalystAcquisitionGates{};
  91. /** Dense native objective completion values used by legacy catalyst progress items. */
  92. std::vector<std::int32_t> catalystObjectiveValues{};
  93. /** Compact 0..3 special plug-category code of every dense installed item row. */
  94. std::array<std::uint8_t, state::build_data::items::kDefinitionCapacity> specialPlugCategories{};
  95. /** Inventory routing rows held until the paired bucket-definition table is resolved. */
  96. std::array<state::build_data::inventory::buckets::Descriptor,
  97. state::build_data::inventory::buckets::kDescriptorCapacity>
  98. bucketRows{};
  99. std::size_t bucketCount{};
  100. std::array<std::int8_t, state::build_data::inventory::buckets::kDescriptorCapacity>
  101. equipmentSlotByBucket{};
  102. DetailRequests detailRequests{};
  103. std::array<std::uint16_t, kDetailCapacity> requestedDetailIndices{};
  104. std::vector<state::build_data::items::details::Definition> details{};
  105. AuthoredHashes authoredHashes{};
  106. std::vector<std::byte> abilityTable{};
  107. std::vector<std::byte> abilityPool{};
  108. std::array<state::build_data::abilities::Definition,
  109. state::build_data::abilities::kDefinitionCapacity>
  110. abilityRows{};
  111. std::array<state::build_data::socket_entry_buckets::Definition,
  112. state::build_data::socket_entry_buckets::kDefinitionCapacity>
  113. entryBucketRows{};
  114. std::array<state::build_data::progressions::Definition,
  115. state::build_data::progressions::kDefinitionCapacity>
  116. progressionRows{};
  117. std::array<state::build_data::progressions::Step,
  118. state::build_data::progressions::kStepCapacity>
  119. progressionSteps{};
  120. std::size_t progressionStepCount{};
  121. /** Season pass reward rows, their raw claim slots, and the wrapper items they grant. */
  122. std::array<state::build_data::season_pass::Reward,
  123. state::build_data::season_pass::kRewardCapacity>
  124. seasonPassRewards{};
  125. std::array<std::uint16_t, state::build_data::season_pass::kRewardCapacity>
  126. seasonPassClaimSlots{};
  127. std::array<state::build_data::season_pass::Package,
  128. state::build_data::season_pass::kPackageCapacity>
  129. seasonPassPackages{};
  130. std::size_t seasonPassRewardCount{};
  131. std::size_t seasonPassPackageCount{};
  132. /** Repeatable bounty rows, keyed by the item-type pair their pool shares. */
  133. std::array<state::build_data::bounties::Definition,
  134. state::build_data::bounties::kDefinitionCapacity>
  135. bountyRows{};
  136. std::size_t bountyCount{};
  137. std::array<state::build_data::collectibles::Definition,
  138. state::build_data::collectibles::kDefinitionCapacity>
  139. collectibleRows{};
  140. std::array<state::build_data::material_requirements::Definition,
  141. state::build_data::material_requirements::kDefinitionCapacity>
  142. materialRequirementRows{};
  143. std::array<state::build_data::items::Definition, state::build_data::items::kDefinitionCapacity>
  144. rows{};
  145. /** Incident-target rows held until the sobject table is published. */
  146. std::array<state::build_data::sobjects::Definition,
  147. state::build_data::sobjects::kDefinitionCapacity>
  148. sobjectRows{};
  149. };
  150. /**
  151. * Collects the authored equipment and plug hashes every configured character names.
  152. * Hashes are sorted so the index-table walk can test each row by binary search instead of
  153. * rescanning the whole authored set per row.
  154. * @param output Receives the sorted unique authored hashes.
  155. * @return True when the account is good and every hash fits.
  156. */
  157. [[nodiscard]] bool collect_authored_hashes(AuthoredHashes& output) noexcept;
  158. /** @param hashes Sorted authored hashes. @param hash Row hash. @return True when authored. */
  159. [[nodiscard]] bool authored(const AuthoredHashes& hashes, std::uint32_t hash) noexcept;
  160. /**
  161. * @param row Installed item row.
  162. * @return True when its bucket maps to an equipment slot
  163. * supported by the generated loadout.
  164. */
  165. [[nodiscard]] bool equippable(const tables::items::Row& row) noexcept;
  166. /** Publishes parsed inventory buckets after applying the extracted item-slot relation. */
  167. [[nodiscard]] bool publish_buckets(Storage& storage) noexcept;
  168. /** @return True when the catalyst catalog is published or cannot exist on this executable. */
  169. [[nodiscard]] bool exotic_catalysts_settled() noexcept;
  170. /** Adds one native definition index to the deduplicated request set. */
  171. void request(std::uint16_t definitionIndex, DetailRequests& requested) noexcept;
  172. /**
  173. * Adds every socket lane's initial plug to the requested set.
  174. * A lane using native defaults falls back to this plug, so its detail must exist.
  175. * @param row Item row already read from its definition blob.
  176. * @param itemDefinitionCount Installed item-table bound.
  177. * @param requested Requested-set storage.
  178. */
  179. void append_initial_plugs(const tables::items::Row& row,
  180. std::uint64_t itemDefinitionCount,
  181. DetailRequests& requested) noexcept;
  182. /**
  183. * Materializes requested native indices in ascending order.
  184. * @param requested Deduplicated native-index set.
  185. * @param output Fixed detail-index storage.
  186. * @param count Receives the number of selected rows, or zero when output is too small.
  187. * @return True when every selected row fits.
  188. */
  189. [[nodiscard]] bool materialize_requests(const DetailRequests& requested,
  190. std::span<std::uint16_t> output,
  191. std::size_t& count) noexcept;
  192. /**
  193. * Reads one requested definition and turns it into its cached detail form.
  194. * @param source Located item index table and package reader state.
  195. * @param definitionIndex Native item index.
  196. * @param detail Receives the cached detail.
  197. * @param item Receives the raw definition row the detail was built from.
  198. * @return True when the row is found and its definition blob reads.
  199. */
  200. [[nodiscard]] bool build_detail(const DetailSource& source,
  201. std::uint16_t definitionIndex,
  202. state::build_data::items::details::Definition& detail,
  203. tables::items::Row& item) noexcept;
  204. /**
  205. * Reads the stat rows the installed investment constants blob names.
  206. * @param source Package source.
  207. * @param scratch Reader scratch.
  208. * @param root Investment root bytes.
  209. * @param blob Scratch storage for the constants blob.
  210. * @param output Receives the extracted stat rows.
  211. * @return True when the blob reads and carries every named row.
  212. */
  213. [[nodiscard]] bool
  214. read_investment_constants(const reader::Source& source,
  215. reader::Scratch& scratch,
  216. std::span<const std::byte> root,
  217. std::vector<std::byte>& blob,
  218. state::build_data::constants::InvestmentConstants& output) noexcept;
  219. /**
  220. * Builds the ability buckets one subclass publishes under one ability selection.
  221. * @param source Package source.
  222. * @param scratch Reader scratch.
  223. * @param listDefinition Socket-entry-list definition bytes of the subclass.
  224. * @param blob Scratch storage reused for every pool blob.
  225. * @param selection The character's 5 selected socket entries.
  226. * @param output Receives the 12 buckets and the overflow bank.
  227. * @return True when every selected entry reaches a bucket of its own.
  228. */
  229. [[nodiscard]] bool build_ability_buckets(const reader::Source& source,
  230. reader::Scratch& scratch,
  231. std::span<const std::byte> listDefinition,
  232. std::vector<std::byte>& blob,
  233. const state::build_data::abilities::Selection& selection,
  234. state::build_data::abilities::Definition& output) noexcept;
  235. /**
  236. * Builds one ability bucket row per distinct subclass and ability selection in use.
  237. * Two characters on the same subclass with the same ability selection publish identical
  238. * buckets, so the row is keyed by both and built once.
  239. * @param source Package source.
  240. * @param scratch Reader scratch.
  241. * @param root Investment root bytes.
  242. * @param table Scratch storage for the socket entry list table.
  243. * @param definition Scratch storage for one socket entry list definition.
  244. * @param blob Scratch storage reused for every pool blob.
  245. * @param output Row storage.
  246. * @param count Receives the number of rows built.
  247. * @return True when the table reads; a subclass that fails is skipped, not fatal.
  248. */
  249. [[nodiscard]] bool build_character_abilities(
  250. const reader::Source& source,
  251. reader::Scratch& scratch,
  252. std::span<const std::byte> root,
  253. std::vector<std::byte>& table,
  254. std::vector<std::byte>& definition,
  255. std::vector<std::byte>& blob,
  256. std::span<state::build_data::abilities::Definition> output,
  257. std::size_t& count,
  258. std::span<state::build_data::socket_entry_buckets::Definition> entryBucketOutput,
  259. std::size_t& entryBucketCount) noexcept;
  260. /**
  261. * Resolves which of the 12 semantic ability buckets every entry in one socket-entry list reaches.
  262. * @param source Package source.
  263. * @param scratch Reader scratch.
  264. * @param listDefinition One socket-entry list's definition bytes.
  265. * @param blob Scratch storage reused for every pool blob.
  266. * @param output Receives one resolved bucket per entry, or the no-destination sentinel.
  267. * @return True when the list's entries read.
  268. */
  269. [[nodiscard]] bool resolve_entry_buckets(
  270. const reader::Source& source,
  271. reader::Scratch& scratch,
  272. std::span<const std::byte> listDefinition,
  273. std::vector<std::byte>& blob,
  274. std::array<std::uint8_t, state::build_data::socket_entry_lists::kEntryCapacity>&
  275. output) noexcept;
  276. /**
  277. * Reads nodes and resolves their value slots, owned records and lore parent bars.
  278. * The record rows must already be built: a node's lore-book flag and parent bar come from them.
  279. * @param source Package source.
  280. * @param storage Pass storage holding the record rows and the objective bank.
  281. * @param root Investment root bytes.
  282. * @return True when the node table read and produced at least one row.
  283. */
  284. [[nodiscard]] bool build_nodes(const reader::Source& source,
  285. Storage& storage,
  286. std::span<const std::byte> root) noexcept;
  287. /**
  288. * Reads records and resolves their flags, objectives, intervals and rewards.
  289. * @param source Package source.
  290. * @param storage Pass storage receiving the record rows and all three flat banks.
  291. * @param root Investment root bytes.
  292. * @return True when the record table read and produced at least one row.
  293. */
  294. [[nodiscard]] bool build_records(const reader::Source& source,
  295. Storage& storage,
  296. std::span<const std::byte> root) noexcept;
  297. /**
  298. * Reads progression definitions, their rank steps, and their replicated-object slots.
  299. * @param source Package source.
  300. * @param scratch Reader scratch.
  301. * @param root Investment root bytes.
  302. * @param blob Scratch storage for the progression table.
  303. * @param output Definition storage.
  304. * @param count Receives the definitions read.
  305. * @param steps Flat step bank storage the definitions name by range.
  306. * @param stepCount Receives the steps read.
  307. * @return True when the table read and produced at least one row.
  308. */
  309. [[nodiscard]] bool build_progressions(const reader::Source& source,
  310. reader::Scratch& scratch,
  311. std::span<const std::byte> root,
  312. std::vector<std::byte>& blob,
  313. std::span<state::build_data::progressions::Definition> output,
  314. std::size_t& count,
  315. std::span<state::build_data::progressions::Step> steps,
  316. std::size_t& stepCount) noexcept;
  317. /** Reads the season pass reward list and the wrapper items it grants. */
  318. [[nodiscard]] bool build_season_pass(const reader::Source& source,
  319. Storage& storage,
  320. std::span<const std::byte> root) noexcept;
  321. /** Reads the item-type of every repeatable bounty, which is what groups a vendor pool. */
  322. [[nodiscard]] bool
  323. build_bounties(const reader::Source& source, Storage& storage, std::size_t itemCount) noexcept;
  324. /**
  325. * Copies the block key material this pass borrows.
  326. * @param keys Receives the primary, alternate and nonce material.
  327. * @return True when the installed key table and the bootstrap token are both there.
  328. */
  329. [[nodiscard]] bool collect_keys(reader::BlockKeys& keys) noexcept;
  330. /**
  331. * Collects every catalogue tag carrying the container name.
  332. * @param candidates Receives the candidate tags.
  333. * @param count Receives the number of candidates.
  334. * @return True when the catalogue names at least one.
  335. */
  336. [[nodiscard]] bool
  337. investment_globals_tags(std::array<std::uint32_t, kContainerCandidates>& candidates,
  338. std::size_t& count) noexcept;
  339. /** @param directory Receives the installed packages directory. @return True when it exists. */
  340. [[nodiscard]] bool package_directory(core::path::Buffer& directory) noexcept;
  341. /** @param slot Requested-set position. @param definitionIndex Native item index that failed. */
  342. void report_detail_failure(std::size_t slot, std::uint16_t definitionIndex) noexcept;
  343. /** @param count Ability bucket rows the pass built, one per subclass and ability selection. */
  344. void report_ability_count(std::size_t count) noexcept;
  345. /** Reports a bounded number of exact subclass/ability extraction failures per process. */
  346. void report_ability_failure(const char* stage,
  347. std::size_t character,
  348. std::size_t first,
  349. std::size_t second) noexcept;
  350. /** Reports requested, retained, and skipped detail closure rows. */
  351. void report_detail_count(std::size_t requested, std::size_t built) noexcept;
  352. /**
  353. * Reports the item index-table walk, including rows the pass could not read.
  354. * @param walked Table entries the pass visited.
  355. * @param rows Readable rows it retained.
  356. * @param skipped Entries omitted because their index row or definition was malformed.
  357. * @param truncated True when the walk stopped early because the row storage filled.
  358. */
  359. void report_row_count(std::size_t walked,
  360. std::size_t rows,
  361. std::size_t skipped,
  362. bool truncated) noexcept;
  363. /** Reports the exact socket-rule, deduplicated-pool, member, and skipped-lane counts. */
  364. void report_socket_plug_count(std::size_t rules,
  365. std::size_t pools,
  366. std::size_t members,
  367. std::size_t skipped) noexcept;
  368. /**
  369. * Reports released, placeholder, and unsupported catalyst catalog counts.
  370. * @param report Complete catalog report from the build pass.
  371. * @param built True when all released catalyst relations were safe.
  372. */
  373. void report_catalyst_catalog(const state::build_data::items::catalysts::Report& report,
  374. bool built) noexcept;
  375. /** Reports the validated installed bucket/equipment-slot coverage. */
  376. void report_bucket_equipment_mapping(std::size_t mappedSlots) noexcept;
  377. /** Reports the first rejected bucket-definition invariant in one process. */
  378. void report_bucket_equipment_failure(const char* stage,
  379. std::size_t first,
  380. std::size_t second) noexcept;
  381. /**
  382. * Reports the pass outcome once.
  383. * @param published Rows published, or zero on failure.
  384. * @param reason Stage the pass reached, named in the line.
  385. */
  386. void report(std::size_t published, const char* reason) noexcept;
  387. /**
  388. * Publishes the inventory bucket descriptors from the root's bucket table.
  389. * @param source Package source.
  390. * @param storage Pass storage.
  391. * @param root Investment root bytes.
  392. * @return True when the table reads and publishes.
  393. */
  394. [[nodiscard]] bool build_buckets(const reader::Source& source,
  395. Storage& storage,
  396. std::span<const std::byte> root) noexcept;
  397. /**
  398. * Publishes the socket entry list table from the root.
  399. * @param source Package source.
  400. * @param storage Pass storage.
  401. * @param root Investment root bytes.
  402. * @return True when the table reads and publishes.
  403. */
  404. [[nodiscard]] bool build_socket_entry_lists(const reader::Source& source,
  405. Storage& storage,
  406. std::span<const std::byte> root) noexcept;
  407. /** Reads and publishes the dense collectible-to-item mapping. */
  408. [[nodiscard]] bool build_collectibles(const reader::Source& source,
  409. Storage& storage,
  410. std::span<const std::byte> root,
  411. std::uint64_t itemDefinitionCount) noexcept;
  412. /**
  413. * Walks the located item index table, then publishes every domain that depends on it.
  414. * @param source Package source.
  415. * @param storage Pass storage holding the located table blob.
  416. * @param table Located item index array.
  417. * @param rowCount Receives the dense item rows published.
  418. * @param reason Receives the step name when a stage fails.
  419. * @return True when the dense table and every dependent domain publish.
  420. */
  421. [[nodiscard]] bool build_item_rows(const reader::Source& source,
  422. Storage& storage,
  423. const tables::Array& table,
  424. std::size_t& rowCount,
  425. const char*& reason) noexcept;
  426. /** Reads and publishes every native material-requirement set from investment-root slot 96. */
  427. [[nodiscard]] bool build_material_requirements(const reader::Source& source,
  428. Storage& storage,
  429. std::span<const std::byte> root,
  430. std::uint64_t itemDefinitionCount) noexcept;
  431. } // namespace sunrise::client::content::items::packages