package_item_rows.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #include <array>
  2. #include <span>
  3. #include "../../../../state/build_data/items/catalysts/exotic_catalyst_builder.h"
  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 needCatalysts = !state::build_data::exotic_catalysts_ready();
  41. const bool needBuckets = !state::build_data::inventory_bucket_descriptors_ready();
  42. const bool retainDetails = needDetails || needCatalysts;
  43. const bool needSocketRows = needSocketPlugs || needCatalysts;
  44. const bool needDetailRows = needDetails || needSocketRows;
  45. // Bucket equipment slots are derived from this same complete item walk, so a partial retry
  46. // must still revisit the table even when definitions and detail domains already published.
  47. const bool needRows = needDefinitions || needDetailRows || needBuckets;
  48. bool published = !needRows;
  49. if (retainDetails && storage.details.size() != kDetailCapacity) {
  50. storage.details.assign(kDetailCapacity, build_details::Definition{});
  51. }
  52. const bool detailStorageReady = !retainDetails || storage.details.size() == kDetailCapacity;
  53. const std::span<const std::byte> container{storage.child};
  54. reason = "rows";
  55. // The detail closure is gathered during this one walk. Collections can name any installed
  56. // item row, including profile-owned shaders and modifications, so retain every readable row.
  57. // The fixed request bitset bounds this to the installed 16-bit item-table domain.
  58. storage.detailRequests.reset();
  59. storage.specialPlugCategories.fill(0);
  60. std::size_t detailCount = 0;
  61. // One malformed entry is omitted on its own so the rest of the table still publishes. An
  62. // entry can fail either at its index row or at the definition the row points to, and neither
  63. // says anything about the entries that follow it.
  64. std::uint64_t index = 0;
  65. for (; needRows && index < table.count && rowCount < storage.rows.size(); ++index) {
  66. tables::IndexRow row{};
  67. if (!tables::index_row(container, table, index, row)) {
  68. continue;
  69. }
  70. tables::items::Row item{};
  71. item.definitionHash = row.definitionHash;
  72. item.definitionIndex = static_cast<std::uint16_t>(index);
  73. if (!reader::read_tag(source, storage.scratch, row.targetTag, storage.definition)
  74. || !tables::items::read_definition(std::span<const std::byte>{storage.definition},
  75. item)) {
  76. continue;
  77. }
  78. storage.rows[rowCount++] =
  79. state::build_data::items::Definition{item.definitionHash,
  80. item.definitionIndex,
  81. item.bucketId,
  82. item.insertionMaterialRequirementSetIndex,
  83. item.enabledMaterialRequirementSetIndex,
  84. item.tier,
  85. item.plugCategoryHash,
  86. item.rollSetIndex,
  87. item.linkedPlugIndex};
  88. if (needSocketRows) {
  89. storage.specialPlugCategories[item.definitionIndex] =
  90. special_plug_category(item.plugCategoryHash);
  91. }
  92. if (needDetailRows) {
  93. request(item.definitionIndex, storage.detailRequests);
  94. append_initial_plugs(item, table.count, storage.detailRequests);
  95. }
  96. }
  97. bool requestsFit = true;
  98. if (needRows) {
  99. // Every walked entry either published a row or was skipped, so the count of one
  100. // follows from the other rather than being tracked alongside them.
  101. report_row_count(index, rowCount, index - rowCount, index < table.count);
  102. requestsFit = publish_buckets(storage)
  103. && (!needDetailRows
  104. || materialize_requests(
  105. storage.detailRequests, storage.requestedDetailIndices, detailCount));
  106. published = rowCount != 0 && requestsFit && detailStorageReady;
  107. }
  108. if (published && needDefinitions) {
  109. published =
  110. state::build_data::publish_item_definitions(std::span(storage.rows).first(rowCount));
  111. }
  112. if (!published) {
  113. reason = !detailStorageReady ? "detail_storage"
  114. : !requestsFit ? "detail_capacity"
  115. : "publish";
  116. }
  117. SocketPlugBuild socketPlugBuild;
  118. const bool socketStorageReady =
  119. !needSocketRows
  120. || socketPlugBuild.prepare(storage.specialPlugCategories,
  121. std::span(storage.rows).first(rowCount));
  122. if (published && !socketStorageReady) {
  123. published = false;
  124. reason = "socket_storage";
  125. }
  126. // Every readable installed row is found through the table this pass just published. One
  127. // malformed row is omitted independently so unrelated Collections categories stay usable.
  128. if (published && needDetailRows) {
  129. reason = "details";
  130. const DetailSource detailSource{
  131. &source, &storage.scratch, container, table, &storage.definition};
  132. std::size_t builtDetailCount = 0;
  133. for (std::size_t slot = 0; slot < detailCount; ++slot) {
  134. build_details::Definition detail{};
  135. tables::items::Row item{};
  136. if (!build_detail(detailSource, storage.requestedDetailIndices[slot], detail, item)
  137. || !publishable_detail(detail)) {
  138. report_detail_failure(slot, storage.requestedDetailIndices[slot]);
  139. continue;
  140. }
  141. if (retainDetails) {
  142. storage.details[builtDetailCount++] = detail;
  143. }
  144. if (needSocketRows) {
  145. (void)socketPlugBuild.append(item,
  146. std::span<const std::byte>{storage.definition},
  147. std::span<const std::byte>{storage.plugSetTable},
  148. table.count);
  149. }
  150. }
  151. if (needDetails) {
  152. published = state::build_data::publish_configured_item_details(
  153. std::span<build_details::Definition>{storage.details}.first(builtDetailCount));
  154. report_detail_count(detailCount, builtDetailCount);
  155. }
  156. std::array<state::build_data::items::catalysts::Definition,
  157. state::build_data::items::catalysts::kDefinitionCapacity>
  158. catalystRows{};
  159. std::size_t catalystCount = 0;
  160. state::build_data::items::catalysts::Report catalystReport{};
  161. const state::build_data::items::catalysts::Source catalystSource{
  162. {},
  163. std::span(storage.rows).first(rowCount),
  164. std::span(storage.details).first(builtDetailCount),
  165. socketPlugBuild.rules(),
  166. socketPlugBuild.pools(),
  167. socketPlugBuild.members(),
  168. };
  169. bool catalystBuilt = false;
  170. if (published && needCatalysts) {
  171. reason = "exotic_catalysts";
  172. catalystBuilt = state::build_data::derive_exotic_catalysts(
  173. catalystSource, catalystRows, catalystCount, catalystReport);
  174. report_catalyst_catalog(catalystReport, catalystBuilt);
  175. }
  176. if (published && needSocketPlugs) {
  177. const std::size_t rules = socketPlugBuild.rule_count();
  178. const std::size_t pools = socketPlugBuild.pool_count();
  179. const std::size_t members = socketPlugBuild.member_count();
  180. const std::size_t skipped = socketPlugBuild.skipped();
  181. reason = "socket_plugs";
  182. published = socketPlugBuild.publish();
  183. if (published) {
  184. report_socket_plug_count(rules, pools, members, skipped);
  185. }
  186. }
  187. if (published && needCatalysts && catalystBuilt) {
  188. reason = "exotic_catalysts";
  189. published = state::build_data::publish_exotic_catalysts(
  190. catalystSource, std::span(catalystRows).first(catalystCount));
  191. }
  192. }
  193. // Ability buckets read the socket entry list table again and depend on the detail domain, so
  194. // they run last. The entry-bucket table never joins the on-disk cache, so a warm boot still
  195. // has to run this once to fill it in for the session.
  196. if (published
  197. && (!state::build_data::ability_buckets_ready()
  198. || !state::build_data::socket_entry_buckets_ready())) {
  199. reason = "abilities";
  200. std::size_t abilityCount = 0;
  201. std::size_t entryBucketCount = 0;
  202. const bool built = build_character_abilities(source,
  203. storage.scratch,
  204. std::span<const std::byte>{storage.root},
  205. storage.abilityTable,
  206. storage.definition,
  207. storage.abilityPool,
  208. storage.abilityRows,
  209. abilityCount,
  210. storage.entryBucketRows,
  211. entryBucketCount);
  212. published = built
  213. && state::build_data::publish_ability_buckets(
  214. std::span(storage.abilityRows).first(abilityCount))
  215. && state::build_data::publish_socket_entry_buckets(
  216. std::span(storage.entryBucketRows).first(entryBucketCount));
  217. if (built) {
  218. report_ability_count(abilityCount);
  219. }
  220. }
  221. return published && state::build_data::item_definitions_ready()
  222. && state::build_data::configured_item_details_ready()
  223. && state::build_data::socket_plug_rules_ready()
  224. && state::build_data::ability_buckets_ready();
  225. }
  226. } // namespace sunrise::client::content::items::packages