package_item_rows.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include <array>
  2. #include <span>
  3. #include <vector>
  4. #include "../../../../state/build_data/items/details/item_detail_catalog.h"
  5. #include "../../../../state/build_data/runtime.h"
  6. #include "internal.h"
  7. #include "package_socket_plug_build.h"
  8. namespace sunrise::client::content::items::packages {
  9. namespace {
  10. namespace build_details = state::build_data::items::details;
  11. namespace build_items = state::build_data::items;
  12. /** @return True when one extracted detail can join the currently published numeric domains. */
  13. [[nodiscard]] bool publishable_detail(const build_details::Definition& detail) noexcept {
  14. const std::size_t itemCount = state::build_data::item_definition_count();
  15. const std::size_t socketListCount = state::build_data::socket_entry_list_count();
  16. build_items::Definition item{};
  17. if (!build_details::valid(std::span<const build_details::Definition>{&detail, 1})
  18. || detail.definitionIndex >= itemCount || detail.socketEntryListIndex >= socketListCount
  19. || !build_items::find_index(detail.definitionIndex, item)
  20. || item.bucketId != detail.bucketId) {
  21. return false;
  22. }
  23. for (const std::uint16_t plugIndex : detail.initialPlugIndices) {
  24. if (plugIndex != build_details::kUnavailableItemIndex && plugIndex >= itemCount) {
  25. return false;
  26. }
  27. }
  28. return true;
  29. }
  30. } // namespace
  31. /** Walks the located item index table, then publishes every domain that depends on it. */
  32. bool build_item_rows(const reader::Source& source,
  33. Storage& storage,
  34. const tables::Array& table,
  35. std::size_t& rowCount,
  36. const char*& reason) noexcept {
  37. const bool needDefinitions = !state::build_data::item_definitions_ready();
  38. const bool needDetails = !state::build_data::configured_item_details_ready();
  39. const bool needSocketPlugs = !state::build_data::socket_plug_rules_ready();
  40. const bool needBuckets = !state::build_data::inventory_bucket_descriptors_ready();
  41. const bool needDetailRows = needDetails || needSocketPlugs;
  42. // Bucket equipment slots are derived from this same complete item walk, so a partial retry
  43. // must still revisit the table even when definitions and detail domains already published.
  44. const bool needRows = needDefinitions || needDetailRows || needBuckets;
  45. bool published = !needRows;
  46. if (needDetails && storage.details.size() != kDetailCapacity) {
  47. storage.details.assign(kDetailCapacity, build_details::Definition{});
  48. }
  49. const bool detailStorageReady = !needDetails || storage.details.size() == kDetailCapacity;
  50. const std::span<const std::byte> container{storage.child};
  51. reason = "rows";
  52. // The detail closure is gathered during this one walk. Collections can name any installed
  53. // item row, including profile-owned shaders and modifications, so retain every readable row
  54. // rather than only startup-authored/equippable definitions. The fixed request bitset still
  55. // bounds this to the installed 16-bit item-table domain.
  56. storage.detailRequests.reset();
  57. storage.specialPlugCategories.fill(0);
  58. std::size_t detailCount = 0;
  59. // One malformed entry is omitted on its own so the rest of the table still publishes. An
  60. // entry can fail either at its index row or at the definition the row points to, and neither
  61. // says anything about the entries that follow it.
  62. std::uint64_t index = 0;
  63. for (; needRows && index < table.count && rowCount < storage.rows.size(); ++index) {
  64. tables::IndexRow row{};
  65. if (!tables::index_row(container, table, index, row)) {
  66. continue;
  67. }
  68. tables::items::Row item{};
  69. item.definitionHash = row.definitionHash;
  70. item.definitionIndex = static_cast<std::uint16_t>(index);
  71. if (!reader::read_tag(source, storage.scratch, row.targetTag, storage.definition)
  72. || !tables::items::read_definition(std::span<const std::byte>{storage.definition},
  73. item)) {
  74. continue;
  75. }
  76. storage.rows[rowCount++] =
  77. state::build_data::items::Definition{item.definitionHash,
  78. item.definitionIndex,
  79. item.bucketId,
  80. item.insertionMaterialRequirementSetIndex,
  81. item.enabledMaterialRequirementSetIndex};
  82. if (needSocketPlugs) {
  83. storage.specialPlugCategories[item.definitionIndex] =
  84. special_plug_category(item.plugCategoryHash);
  85. }
  86. if (needDetailRows) {
  87. request(item.definitionIndex, storage.detailRequests);
  88. append_initial_plugs(item, table.count, storage.detailRequests);
  89. }
  90. }
  91. bool requestsFit = true;
  92. if (needRows) {
  93. // Every walked entry either published a row or was skipped, so the count of one
  94. // follows from the other rather than being tracked alongside them.
  95. report_row_count(index, rowCount, index - rowCount, index < table.count);
  96. requestsFit = publish_buckets(storage)
  97. && (!needDetailRows
  98. || materialize_requests(
  99. storage.detailRequests, storage.requestedDetailIndices, detailCount));
  100. published = rowCount != 0 && requestsFit && detailStorageReady;
  101. }
  102. if (published && needDefinitions) {
  103. published =
  104. state::build_data::publish_item_definitions(std::span(storage.rows).first(rowCount));
  105. }
  106. if (!published) {
  107. reason = !detailStorageReady ? "detail_storage"
  108. : !requestsFit ? "detail_capacity"
  109. : "publish";
  110. }
  111. SocketPlugBuild socketPlugBuild;
  112. const bool socketStorageReady =
  113. !needSocketPlugs
  114. || socketPlugBuild.prepare(storage.specialPlugCategories,
  115. std::span(storage.rows).first(rowCount));
  116. if (published && !socketStorageReady) {
  117. published = false;
  118. reason = "socket_storage";
  119. }
  120. // Every readable installed row is found through the table this pass just published. One
  121. // malformed row is omitted independently so unrelated Collections categories stay usable.
  122. if (published && needDetailRows) {
  123. reason = "details";
  124. const DetailSource detailSource{
  125. &source, &storage.scratch, container, table, &storage.definition};
  126. std::size_t builtDetailCount = 0;
  127. for (std::size_t slot = 0; slot < detailCount; ++slot) {
  128. build_details::Definition detail{};
  129. tables::items::Row item{};
  130. if (!build_detail(detailSource, storage.requestedDetailIndices[slot], detail, item)
  131. || !publishable_detail(detail)) {
  132. report_detail_failure(slot, storage.requestedDetailIndices[slot]);
  133. continue;
  134. }
  135. if (needDetails) {
  136. storage.details[builtDetailCount++] = detail;
  137. }
  138. if (needSocketPlugs) {
  139. (void)socketPlugBuild.append(item,
  140. std::span<const std::byte>{storage.definition},
  141. std::span<const std::byte>{storage.plugSetTable},
  142. table.count);
  143. }
  144. }
  145. if (needDetails) {
  146. published = state::build_data::publish_configured_item_details(
  147. std::span<build_details::Definition>{storage.details}.first(builtDetailCount));
  148. report_detail_count(detailCount, builtDetailCount);
  149. }
  150. if (published && needSocketPlugs) {
  151. const std::size_t rules = socketPlugBuild.rule_count();
  152. const std::size_t pools = socketPlugBuild.pool_count();
  153. const std::size_t members = socketPlugBuild.member_count();
  154. const std::size_t skipped = socketPlugBuild.skipped();
  155. reason = "socket_plugs";
  156. published = socketPlugBuild.publish();
  157. if (published) {
  158. report_socket_plug_count(rules, pools, members, skipped);
  159. }
  160. }
  161. }
  162. // Ability buckets read the socket entry list table again and depend on the detail domain, so
  163. // they run last.
  164. if (published && !state::build_data::ability_buckets_ready()) {
  165. reason = "abilities";
  166. std::size_t abilityCount = 0;
  167. const bool built = build_character_abilities(source,
  168. storage.scratch,
  169. std::span<const std::byte>{storage.root},
  170. storage.abilityTable,
  171. storage.definition,
  172. storage.abilityPool,
  173. storage.abilityRows,
  174. abilityCount);
  175. published = built
  176. && state::build_data::publish_ability_buckets(
  177. std::span(storage.abilityRows).first(abilityCount));
  178. if (built) {
  179. report_ability_count(abilityCount);
  180. }
  181. }
  182. return published && state::build_data::item_definitions_ready()
  183. && state::build_data::configured_item_details_ready()
  184. && state::build_data::socket_plug_rules_ready()
  185. && state::build_data::ability_buckets_ready();
  186. }
  187. } // namespace sunrise::client::content::items::packages