display_parser.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include <bitset>
  2. #include <limits>
  3. #include "../parser.h"
  4. namespace sunrise::core::settings::parser {
  5. /** Parses screen and renderer settings under stable Sunrise-owned names. */
  6. bool Parser::display_settings(state::account::settings::Display& output) noexcept {
  7. enum class Field : std::size_t {
  8. brightness,
  9. showFps,
  10. hdrMode,
  11. calibrationPrimary,
  12. calibrationAlpha,
  13. count,
  14. };
  15. output = {};
  16. if (!consume('{')) {
  17. return false;
  18. }
  19. std::bitset<static_cast<std::size_t>(Field::count)> supplied;
  20. // Tracked apart from the bitset because these two keys are optional: an enumerated field is
  21. // required by the supplied.all() below, and an older settings file does not carry them.
  22. bool hasVerticalSyncInterval = false;
  23. bool hasFieldOfView = false;
  24. const auto mark = [&supplied](Field field) noexcept {
  25. const std::size_t index = static_cast<std::size_t>(field);
  26. if (supplied.test(index)) {
  27. return false;
  28. }
  29. supplied.set(index);
  30. return true;
  31. };
  32. if (consume('}')) {
  33. return false;
  34. }
  35. for (;;) {
  36. std::string_view key;
  37. if (!string(key) || !consume(':')) {
  38. return false;
  39. }
  40. if (key == "brightness") {
  41. if (!mark(Field::brightness) || !signed_byte(output.brightness)) {
  42. return false;
  43. }
  44. } else if (key == "show_fps") {
  45. if (!mark(Field::showFps) || !boolean(output.showFps)) {
  46. return false;
  47. }
  48. } else if (key == "hdr_mode") {
  49. if (!mark(Field::hdrMode) || !signed_byte(output.hdrMode)) {
  50. return false;
  51. }
  52. } else if (key == "vertical_sync_interval") {
  53. std::uint64_t value = 0;
  54. // Bounds the narrowing only. The 0 to 4 presentation domain is checked with the rest
  55. // of the account settings, so it is stated once.
  56. if (hasVerticalSyncInterval || !unsigned_integer(value)
  57. || value > (std::numeric_limits<std::uint8_t>::max)()) {
  58. return false;
  59. }
  60. output.verticalSyncInterval = static_cast<std::uint8_t>(value);
  61. hasVerticalSyncInterval = true;
  62. } else if (key == "field_of_view") {
  63. if (hasFieldOfView || !signed_32(output.fieldOfView)) {
  64. return false;
  65. }
  66. hasFieldOfView = true;
  67. } else if (key == "calibration_primary") {
  68. if (!mark(Field::calibrationPrimary) || !floating_point(output.calibrationPrimary)) {
  69. return false;
  70. }
  71. } else if (key == "calibration_alpha") {
  72. if (!mark(Field::calibrationAlpha) || !floating_point(output.calibrationAlpha)) {
  73. return false;
  74. }
  75. } else if (!skip_value(0)) {
  76. return false;
  77. }
  78. if (consume('}')) {
  79. return supplied.all();
  80. }
  81. if (!consume(',')) {
  82. return false;
  83. }
  84. }
  85. }
  86. } // namespace sunrise::core::settings::parser