package_item_rows.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #include <array>
  2. #include <span>
  3. #include "../../../../middleware/content/packages/tables/quest_initialization_reader.h"
  4. #include "../../../../state/build_data/items/catalysts/exotic_catalyst_builder.h"
  5. #include "../../../../state/build_data/items/details/item_detail_catalog.h"
  6. #include "../../../../state/build_data/runtime.h"
  7. #include "internal.h"
  8. #include "package_socket_plug_build.h"
  9. namespace sunrise::client::content::items::packages {
  10. namespace {
  11. namespace build_details = state::build_data::items::details;
  12. namespace build_items = state::build_data::items;
  13. /**
  14. * Set when a derivation reports the installed executable is not the one the catalyst facts pin.
  15. * A build identity cannot change while the process runs, so the catalog will never derive here.
  16. * TODO: replace with a build_data predicate for catalyst build support; nothing exports one yet.
  17. */
  18. bool g_catalystsUnsupported = false;
  19. /** @return True when one extracted detail can join the currently published numeric domains. */
  20. [[nodiscard]] bool publishable_detail(const build_details::Definition& detail) noexcept {
  21. const std::size_t itemCount = state::build_data::item_definition_count();
  22. const std::size_t socketListCount = state::build_data::socket_entry_list_count();
  23. build_items::Definition item{};
  24. if (!build_details::valid(std::span<const build_details::Definition>{&detail, 1})
  25. || detail.definitionIndex >= itemCount || detail.socketEntryListIndex >= socketListCount
  26. || !build_items::find_index(detail.definitionIndex, item)
  27. || item.bucketId != detail.bucketId) {
  28. return false;
  29. }
  30. for (const std::uint16_t plugIndex : detail.initialPlugIndices) {
  31. if (plugIndex != build_details::kUnavailableItemIndex && plugIndex >= itemCount) {
  32. return false;
  33. }
  34. }
  35. return true;
  36. }
  37. } // namespace
  38. /** @return True when the catalyst catalog is published or cannot exist on this executable. */
  39. bool exotic_catalysts_settled() noexcept {
  40. return state::build_data::exotic_catalysts_ready() || g_catalystsUnsupported;
  41. }
  42. /**
  43. * Publishes missing item domains from the located index table and its definitions.
  44. * @param source Borrowed package source.
  45. * @param storage Pass storage holding the table in child, root data, maps, and output rows.
  46. * @param table Located item index array within storage.child.
  47. * @param rowCount Starts at zero; receives the rows retained even if publication fails.
  48. * @param reason Receives the last stage reached or its failure reason.
  49. * @return True when this pass's required item domains are ready; failure may retain prior results.
  50. */
  51. bool build_item_rows(const reader::Source& source,
  52. Storage& storage,
  53. const tables::Array& table,
  54. std::size_t& rowCount,
  55. const char*& reason) noexcept {
  56. const bool needDefinitions = !state::build_data::item_definitions_ready();
  57. const bool needDetails = !state::build_data::configured_item_details_ready();
  58. const bool needSocketPlugs = !state::build_data::socket_plug_rules_ready();
  59. const bool needCatalysts = !exotic_catalysts_settled();
  60. const bool needBuckets = !state::build_data::inventory_bucket_descriptors_ready();
  61. const bool retainDetails = needDetails || needCatalysts;
  62. const bool needSocketRows = needSocketPlugs || needCatalysts;
  63. const bool needDetailRows = needDetails || needSocketRows;
  64. // Bucket equipment slots are derived from this same complete item walk, so a partial retry
  65. // must still revisit the table even when definitions and detail domains already published.
  66. const bool needRows = needDefinitions || needDetailRows || needBuckets;
  67. bool published = !needRows;
  68. if (retainDetails && storage.details.size() != kDetailCapacity) {
  69. storage.details.assign(kDetailCapacity, build_details::Definition{});
  70. }
  71. if (needCatalysts) {
  72. storage.catalystCompletionConditions.assign(
  73. static_cast<std::size_t>(table.count),
  74. state::build_data::items::catalysts::CompletionCondition{});
  75. for (std::size_t item = 0; item < storage.catalystCompletionConditions.size(); ++item) {
  76. storage.catalystCompletionConditions[item].itemDefinitionIndex =
  77. static_cast<std::uint16_t>(item);
  78. }
  79. }
  80. const bool detailStorageReady = !retainDetails || storage.details.size() == kDetailCapacity;
  81. const std::span<const std::byte> container{storage.child};
  82. reason = "rows";
  83. // The detail closure is gathered during this one walk. Collections can name any installed
  84. // item row, including profile-owned shaders and modifications, so retain every readable row.
  85. // The fixed request bitset bounds this to the installed 16-bit item-table domain.
  86. storage.detailRequests.reset();
  87. storage.specialPlugCategories.fill(0);
  88. std::size_t detailCount = 0;
  89. // One malformed entry is omitted on its own so the rest of the table still publishes. An
  90. // entry can fail either at its index row or at the definition the row points to, and neither
  91. // says anything about the entries that follow it.
  92. std::uint64_t index = 0;
  93. for (; needRows && index < table.count && rowCount < storage.rows.size(); ++index) {
  94. tables::IndexRow row{};
  95. if (!tables::index_row(container, table, index, row)) {
  96. continue;
  97. }
  98. tables::items::Row item{};
  99. item.definitionHash = row.definitionHash;
  100. item.definitionIndex = static_cast<std::uint16_t>(index);
  101. std::uint32_t itemClass = 0;
  102. if (!reader::read_tag(source, storage.scratch, row.targetTag, storage.definition, itemClass)
  103. || !tables::items::read_definition(std::span<const std::byte>{storage.definition},
  104. item)) {
  105. continue;
  106. }
  107. const std::uint32_t plugCategoryHash =
  108. corrected_plug_category(item.definitionHash, item.plugCategoryHash);
  109. build_items::QuestInitialization quest{};
  110. const auto parentIndex = tables::items::quest_parent(storage.definition);
  111. if (needDefinitions && itemClass == tables::kItemDefinitionClass
  112. && parentIndex < table.count) {
  113. std::span<const std::byte> parent = storage.definition;
  114. tables::IndexRow parentRow{};
  115. std::uint32_t parentClass = itemClass;
  116. const bool parentReady = parentIndex == item.definitionIndex
  117. || (tables::index_row(container, table, parentIndex, parentRow)
  118. && reader::read_tag(source,
  119. storage.scratch,
  120. parentRow.targetTag,
  121. storage.questParentDefinition,
  122. parentClass));
  123. if (parentIndex != item.definitionIndex) {
  124. parent = storage.questParentDefinition;
  125. }
  126. if (parentReady && parentClass == tables::kItemDefinitionClass) {
  127. quest =
  128. tables::items::read_quest_initialization(storage.definition,
  129. item.definitionIndex,
  130. parent,
  131. static_cast<std::size_t>(table.count),
  132. storage.questValueMap);
  133. }
  134. }
  135. storage.rows[rowCount++] =
  136. state::build_data::items::Definition{item.definitionHash,
  137. item.definitionIndex,
  138. item.bucketId,
  139. item.insertionMaterialRequirementSetIndex,
  140. item.enabledMaterialRequirementSetIndex,
  141. item.tier,
  142. plugCategoryHash,
  143. item.rollSetIndex,
  144. item.linkedPlugIndex,
  145. quest};
  146. if (needSocketRows) {
  147. storage.specialPlugCategories[item.definitionIndex] =
  148. special_plug_category(plugCategoryHash);
  149. }
  150. if (needDetailRows) {
  151. request(item.definitionIndex, storage.detailRequests);
  152. append_initial_plugs(item, table.count, storage.detailRequests);
  153. }
  154. }
  155. bool requestsFit = true;
  156. if (needRows) {
  157. // Every walked entry either published a row or was skipped, so the count of one
  158. // follows from the other rather than being tracked alongside them.
  159. report_row_count(index, rowCount, index - rowCount, index < table.count);
  160. requestsFit = publish_buckets(storage)
  161. && (!needDetailRows
  162. || materialize_requests(
  163. storage.detailRequests, storage.requestedDetailIndices, detailCount));
  164. published = rowCount != 0 && requestsFit && detailStorageReady;
  165. }
  166. if (published && needDefinitions) {
  167. published =
  168. state::build_data::publish_item_definitions(std::span(storage.rows).first(rowCount));
  169. }
  170. if (!published) {
  171. reason = !detailStorageReady ? "detail_storage"
  172. : !requestsFit ? "detail_capacity"
  173. : "publish";
  174. }
  175. SocketPlugBuild socketPlugBuild;
  176. const bool socketStorageReady =
  177. !needSocketRows
  178. || socketPlugBuild.prepare(storage.specialPlugCategories,
  179. std::span(storage.rows).first(rowCount));
  180. if (published && !socketStorageReady) {
  181. published = false;
  182. reason = "socket_storage";
  183. }
  184. // Every readable installed row is found through the table this pass just published. One
  185. // malformed row is omitted independently so unrelated Collections categories stay usable.
  186. if (published && needDetailRows) {
  187. reason = "details";
  188. const DetailSource detailSource{
  189. &source, &storage.scratch, container, table, &storage.definition};
  190. std::size_t builtDetailCount = 0;
  191. for (std::size_t slot = 0; slot < detailCount; ++slot) {
  192. build_details::Definition detail{};
  193. tables::items::Row item{};
  194. if (!build_detail(detailSource, storage.requestedDetailIndices[slot], detail, item)
  195. || !publishable_detail(detail)) {
  196. report_detail_failure(slot, storage.requestedDetailIndices[slot]);
  197. continue;
  198. }
  199. if (retainDetails) {
  200. storage.details[builtDetailCount++] = detail;
  201. }
  202. if (needCatalysts
  203. && detail.definitionIndex < storage.catalystCompletionConditions.size()) {
  204. read_catalyst_completion_condition(
  205. std::span<const std::byte>{storage.definition},
  206. detail.definitionIndex,
  207. storage.catalystCompletionConditions[detail.definitionIndex]);
  208. }
  209. if (needSocketRows) {
  210. (void)socketPlugBuild.append(item,
  211. std::span<const std::byte>{storage.definition},
  212. std::span<const std::byte>{storage.plugSetTable},
  213. table.count);
  214. }
  215. }
  216. if (needDetails) {
  217. published = state::build_data::publish_configured_item_details(
  218. std::span<build_details::Definition>{storage.details}.first(builtDetailCount));
  219. report_detail_count(detailCount, builtDetailCount);
  220. }
  221. std::array<state::build_data::items::catalysts::Definition,
  222. state::build_data::items::catalysts::kDefinitionCapacity>
  223. catalystRows{};
  224. std::size_t catalystCount = 0;
  225. state::build_data::items::catalysts::Report catalystReport{};
  226. const state::build_data::items::catalysts::Source catalystSource{
  227. {},
  228. std::span(storage.rows).first(rowCount),
  229. std::span(storage.details).first(builtDetailCount),
  230. socketPlugBuild.rules(),
  231. socketPlugBuild.pools(),
  232. socketPlugBuild.members(),
  233. storage.catalystCompletionConditions,
  234. storage.catalystAcquisitionGates,
  235. storage.catalystObjectiveValues,
  236. storage.slotMaps.accountFlag,
  237. };
  238. bool catalystBuilt = false;
  239. if (published && needCatalysts) {
  240. reason = "exotic_catalysts";
  241. catalystBuilt = state::build_data::derive_exotic_catalysts(
  242. catalystSource, catalystRows, catalystCount, catalystReport);
  243. report_catalyst_catalog(catalystReport, catalystBuilt);
  244. // An unsupported executable settles the domain: retrying repeats the whole item walk
  245. // on every refresh slice and reaches the same verdict.
  246. g_catalystsUnsupported =
  247. catalystReport.error
  248. == state::build_data::items::catalysts::Error::unsupportedBuild;
  249. published = catalystBuilt || g_catalystsUnsupported;
  250. }
  251. if (published && needSocketPlugs) {
  252. const std::size_t rules = socketPlugBuild.rule_count();
  253. const std::size_t pools = socketPlugBuild.pool_count();
  254. const std::size_t members = socketPlugBuild.member_count();
  255. const std::size_t skipped = socketPlugBuild.skipped();
  256. reason = "socket_plugs";
  257. published = socketPlugBuild.publish();
  258. if (published) {
  259. report_socket_plug_count(rules, pools, members, skipped);
  260. }
  261. }
  262. if (published && needCatalysts && catalystBuilt) {
  263. reason = "exotic_catalysts";
  264. published = state::build_data::publish_exotic_catalysts(
  265. catalystSource, std::span(catalystRows).first(catalystCount));
  266. }
  267. }
  268. // Ability buckets read the socket entry list table again and depend on the detail domain, so
  269. // they run last. The entry-bucket table never joins the on-disk cache, so a warm boot still
  270. // has to run this once to fill it in for the session.
  271. if (published
  272. && (!state::build_data::ability_buckets_ready()
  273. || !state::build_data::socket_entry_buckets_ready())) {
  274. reason = "abilities";
  275. std::size_t abilityCount = 0;
  276. std::size_t entryBucketCount = 0;
  277. const bool built = build_character_abilities(source,
  278. storage.scratch,
  279. std::span<const std::byte>{storage.root},
  280. storage.abilityTable,
  281. storage.definition,
  282. storage.abilityPool,
  283. storage.abilityRows,
  284. abilityCount,
  285. storage.entryBucketRows,
  286. entryBucketCount);
  287. published = built
  288. && state::build_data::publish_ability_buckets(
  289. std::span(storage.abilityRows).first(abilityCount))
  290. && state::build_data::publish_socket_entry_buckets(
  291. std::span(storage.entryBucketRows).first(entryBucketCount));
  292. if (built) {
  293. report_ability_count(abilityCount);
  294. }
  295. }
  296. return published && state::build_data::item_definitions_ready()
  297. && state::build_data::configured_item_details_ready()
  298. && state::build_data::socket_plug_rules_ready() && exotic_catalysts_settled()
  299. && state::build_data::ability_buckets_ready();
  300. }
  301. } // namespace sunrise::client::content::items::packages