cache_payload_reader.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #pragma once
  2. #include <Windows.h>
  3. #include <cstdint>
  4. #include "../../definition.h"
  5. #include "../records/domains.h"
  6. #include "../records/format.h"
  7. namespace sunrise::state::build_data::cache::read {
  8. /**
  9. * Reads one whole object at the current file position.
  10. * @tparam Value Trivially copied cache object.
  11. * @param file Open cache handle.
  12. * @param value Receives the whole object.
  13. * @return True when Windows reads every byte.
  14. */
  15. template <typename Value> [[nodiscard]] bool read_value(HANDLE file, Value& value) noexcept {
  16. DWORD transferred = 0;
  17. return ReadFile(file, &value, static_cast<DWORD>(sizeof value), &transferred, nullptr) != FALSE
  18. && transferred == sizeof value;
  19. }
  20. /** Clears every row in the caller's storage after a failed load. */
  21. void clear(records::MutableDomains output) noexcept;
  22. /**
  23. * Computes the exact cache file size for every record array.
  24. * @param counts Header row counts.
  25. * @param size Receives the exact byte count.
  26. * @return True when the total size fits.
  27. */
  28. [[nodiscard]] bool expected_size(const records::DomainCounts& counts, std::uint64_t& size) noexcept;
  29. /**
  30. * Reads, checksums, and checks every payload array.
  31. * @param file Open cache handle, positioned after its header.
  32. * @param build Cache build identity.
  33. * @param constants Header constants, held until the whole load commits.
  34. * @param counts Checked row counts.
  35. * @param output Fixed caller storage for every domain.
  36. * @param checksum Receives the checksum over the packed record bytes.
  37. * @return True when all records decode and pass their checks together.
  38. */
  39. [[nodiscard]] bool read_payload(HANDLE file,
  40. const BuildIdentity& build,
  41. const records::InvestmentConstants& constants,
  42. const gameplay::entity_position_profiles::Fingerprint& fingerprint,
  43. const records::DomainCounts& counts,
  44. records::MutableDomains output,
  45. std::uint64_t& checksum) noexcept;
  46. } // namespace sunrise::state::build_data::cache::read