package_item_rows.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. // The fixed request bitset bounds this to the installed 16-bit item-table domain.
  55. storage.detailRequests.reset();
  56. storage.specialPlugCategories.fill(0);
  57. std::size_t detailCount = 0;
  58. // One malformed entry is omitted on its own so the rest of the table still publishes. An
  59. // entry can fail either at its index row or at the definition the row points to, and neither
  60. // says anything about the entries that follow it.
  61. std::uint64_t index = 0;
  62. for (; needRows && index < table.count && rowCount < storage.rows.size(); ++index) {
  63. tables::IndexRow row{};
  64. if (!tables::index_row(container, table, index, row)) {
  65. continue;
  66. }
  67. tables::items::Row item{};
  68. item.definitionHash = row.definitionHash;
  69. item.definitionIndex = static_cast<std::uint16_t>(index);
  70. if (!reader::read_tag(source, storage.scratch, row.targetTag, storage.definition)
  71. || !tables::items::read_definition(std::span<const std::byte>{storage.definition},
  72. item)) {
  73. continue;
  74. }
  75. storage.rows[rowCount++] =
  76. state::build_data::items::Definition{item.definitionHash,
  77. item.definitionIndex,
  78. item.bucketId,
  79. item.insertionMaterialRequirementSetIndex,
  80. item.enabledMaterialRequirementSetIndex,
  81. item.tier,
  82. item.plugCategoryHash,
  83. item.rollSetIndex,
  84. item.linkedPlugIndex};
  85. if (needSocketPlugs) {
  86. storage.specialPlugCategories[item.definitionIndex] =
  87. special_plug_category(item.plugCategoryHash);
  88. }
  89. if (needDetailRows) {
  90. request(item.definitionIndex, storage.detailRequests);
  91. append_initial_plugs(item, table.count, storage.detailRequests);
  92. }
  93. }
  94. bool requestsFit = true;
  95. if (needRows) {
  96. // Every walked entry either published a row or was skipped, so the count of one
  97. // follows from the other rather than being tracked alongside them.
  98. report_row_count(index, rowCount, index - rowCount, index < table.count);
  99. requestsFit = publish_buckets(storage)
  100. && (!needDetailRows
  101. || materialize_requests(
  102. storage.detailRequests, storage.requestedDetailIndices, detailCount));
  103. published = rowCount != 0 && requestsFit && detailStorageReady;
  104. }
  105. if (published && needDefinitions) {
  106. published =
  107. state::build_data::publish_item_definitions(std::span(storage.rows).first(rowCount));
  108. }
  109. if (!published) {
  110. reason = !detailStorageReady ? "detail_storage"
  111. : !requestsFit ? "detail_capacity"
  112. : "publish";
  113. }
  114. SocketPlugBuild socketPlugBuild;
  115. const bool socketStorageReady =
  116. !needSocketPlugs
  117. || socketPlugBuild.prepare(storage.specialPlugCategories,
  118. std::span(storage.rows).first(rowCount));
  119. if (published && !socketStorageReady) {
  120. published = false;
  121. reason = "socket_storage";
  122. }
  123. // Every readable installed row is found through the table this pass just published. One
  124. // malformed row is omitted independently so unrelated Collections categories stay usable.
  125. if (published && needDetailRows) {
  126. reason = "details";
  127. const DetailSource detailSource{
  128. &source, &storage.scratch, container, table, &storage.definition};
  129. std::size_t builtDetailCount = 0;
  130. for (std::size_t slot = 0; slot < detailCount; ++slot) {
  131. build_details::Definition detail{};
  132. tables::items::Row item{};
  133. if (!build_detail(detailSource, storage.requestedDetailIndices[slot], detail, item)
  134. || !publishable_detail(detail)) {
  135. report_detail_failure(slot, storage.requestedDetailIndices[slot]);
  136. continue;
  137. }
  138. if (needDetails) {
  139. storage.details[builtDetailCount++] = detail;
  140. }
  141. if (needSocketPlugs) {
  142. (void)socketPlugBuild.append(item,
  143. std::span<const std::byte>{storage.definition},
  144. std::span<const std::byte>{storage.plugSetTable},
  145. table.count);
  146. }
  147. }
  148. if (needDetails) {
  149. published = state::build_data::publish_configured_item_details(
  150. std::span<build_details::Definition>{storage.details}.first(builtDetailCount));
  151. report_detail_count(detailCount, builtDetailCount);
  152. }
  153. if (published && needSocketPlugs) {
  154. const std::size_t rules = socketPlugBuild.rule_count();
  155. const std::size_t pools = socketPlugBuild.pool_count();
  156. const std::size_t members = socketPlugBuild.member_count();
  157. const std::size_t skipped = socketPlugBuild.skipped();
  158. reason = "socket_plugs";
  159. published = socketPlugBuild.publish();
  160. if (published) {
  161. report_socket_plug_count(rules, pools, members, skipped);
  162. }
  163. }
  164. }
  165. // Ability buckets read the socket entry list table again and depend on the detail domain, so
  166. // they run last. The entry-bucket table never joins the on-disk cache, so a warm boot still
  167. // has to run this once to fill it in for the session.
  168. if (published
  169. && (!state::build_data::ability_buckets_ready()
  170. || !state::build_data::socket_entry_buckets_ready())) {
  171. reason = "abilities";
  172. std::size_t abilityCount = 0;
  173. std::size_t entryBucketCount = 0;
  174. const bool built = build_character_abilities(source,
  175. storage.scratch,
  176. std::span<const std::byte>{storage.root},
  177. storage.abilityTable,
  178. storage.definition,
  179. storage.abilityPool,
  180. storage.abilityRows,
  181. abilityCount,
  182. storage.entryBucketRows,
  183. entryBucketCount);
  184. published = built
  185. && state::build_data::publish_ability_buckets(
  186. std::span(storage.abilityRows).first(abilityCount))
  187. && state::build_data::publish_socket_entry_buckets(
  188. std::span(storage.entryBucketRows).first(entryBucketCount));
  189. if (built) {
  190. report_ability_count(abilityCount);
  191. }
  192. }
  193. return published && state::build_data::item_definitions_ready()
  194. && state::build_data::configured_item_details_ready()
  195. && state::build_data::socket_plug_rules_ready()
  196. && state::build_data::ability_buckets_ready();
  197. }
  198. } // namespace sunrise::client::content::items::packages