src/config/json.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "config/json_io.h" | ||
| 4 | |||
| 5 | #include "terraingen/sources/upsampling.h" | ||
| 6 | |||
| 7 | #include <array> | ||
| 8 | #include <cmath> | ||
| 9 | #include <cstdint> | ||
| 10 | #include <filesystem> | ||
| 11 | #include <fstream> | ||
| 12 | #include <limits> | ||
| 13 | #include <sstream> | ||
| 14 | #include <string> | ||
| 15 | |||
| 16 | namespace terraingen { | ||
| 17 | |||
| 18 | namespace config { | ||
| 19 | |||
| 20 | 5767 | Reader::Reader(const Json& json, std::string path) : m_json(json), m_path(std::move(path)) { | |
| 21 | 5767 | if (!m_json.is_object()) { | |
| 22 | 15 | throw ConfigError(m_path, std::string("expected an object, not ") + m_json.type_name()); | |
| 23 | } | ||
| 24 | 5772 | } | |
| 25 | |||
| 26 | 5577 | void Reader::Finish() const { | |
| 27 | 35848 | for (const auto& [key, value] : m_json.items()) { | |
| 28 | 60596 | if (m_known.count(key) == 0) { | |
| 29 | 54 | std::string expected; | |
| 30 | 732 | for (const std::string& name : m_known) { | |
| 31 | 2034 | expected += (expected.empty() ? "" : ", ") + name; | |
| 32 | } | ||
| 33 | 108 | throw ConfigError(FieldPath(m_path, key), "unknown key; expected one of: " + expected); | |
| 34 | 54 | } | |
| 35 | 5631 | } | |
| 36 | 5523 | if (!m_missing.empty()) { | |
| 37 | 8 | throw ConfigError(FieldPath(m_path, m_missing.front()), "required"); | |
| 38 | } | ||
| 39 | 5519 | } | |
| 40 | |||
| 41 | 15323 | void Read(const Json& json, const std::string& path, double& value) { | |
| 42 | 15323 | if (!json.is_number()) { | |
| 43 | 12 | throw ConfigError(path, std::string("expected a number, not ") + json.type_name()); | |
| 44 | } | ||
| 45 | 15319 | value = json.get<double>(); | |
| 46 | 15319 | if (!std::isfinite(value)) { | |
| 47 | ✗ | throw ConfigError(path, "expected a finite number"); | |
| 48 | } | ||
| 49 | 15319 | } | |
| 50 | |||
| 51 | 336 | void Read(const Json& json, const std::string& path, float& value) { | |
| 52 | 336 | double number = 0.0; | |
| 53 | 336 | Read(json, path, number); | |
| 54 | 336 | if (std::fabs(number) > static_cast<double>(std::numeric_limits<float>::max())) { | |
| 55 | 2 | throw ConfigError(path, "number is too large"); | |
| 56 | } | ||
| 57 | 335 | value = static_cast<float>(number); | |
| 58 | 335 | } | |
| 59 | |||
| 60 | 894 | void Read(const Json& json, const std::string& path, bool& value) { | |
| 61 | 894 | if (!json.is_boolean()) { | |
| 62 | 3 | throw ConfigError(path, std::string("expected true or false, not ") + json.type_name()); | |
| 63 | } | ||
| 64 | 893 | value = json.get<bool>(); | |
| 65 | 893 | } | |
| 66 | |||
| 67 | 2796 | void Read(const Json& json, const std::string& path, std::string& value) { | |
| 68 | 2796 | if (!json.is_string()) { | |
| 69 | 3 | throw ConfigError(path, std::string("expected a string, not ") + json.type_name()); | |
| 70 | } | ||
| 71 | 2795 | value = json.get<std::string>(); | |
| 72 | 2795 | } | |
| 73 | |||
| 74 | 2371 | void Read(const Json& json, const std::string& path, int& value) { | |
| 75 | 2371 | if (!json.is_number_integer()) { | |
| 76 | 6 | throw ConfigError(path, std::string("expected an integer, not ") + json.type_name()); | |
| 77 | } | ||
| 78 | 2369 | if (json.is_number_unsigned() && | |
| 79 | 2368 | json.get<std::uint64_t>() > static_cast<std::uint64_t>(std::numeric_limits<int>::max())) { | |
| 80 | 2 | throw ConfigError(path, "integer is too large"); | |
| 81 | } | ||
| 82 | 2368 | const auto number = json.get<std::int64_t>(); | |
| 83 | 2368 | if (number < std::numeric_limits<int>::min() || number > std::numeric_limits<int>::max()) { | |
| 84 | 2 | throw ConfigError(path, "integer is out of range"); | |
| 85 | } | ||
| 86 | 2367 | value = static_cast<int>(number); | |
| 87 | 2367 | } | |
| 88 | |||
| 89 | 544 | void Read(const Json& json, const std::string& path, std::array<double, 2>& value) { | |
| 90 | 544 | if (!json.is_array() || json.size() != 2) { | |
| 91 | 10 | throw ConfigError(path, std::string("expected a list of two numbers, not ") + | |
| 92 | 13 | (json.is_array() ? "a list of " + std::to_string(json.size()) | |
| 93 | 20 | : std::string(json.type_name()))); | |
| 94 | } | ||
| 95 | 539 | std::array<double, 2> pair{}; | |
| 96 | 1614 | for (std::size_t i = 0; i < 2; ++i) { | |
| 97 | 2152 | Read(json[i], ItemPath(path, i), pair[i]); | |
| 98 | } | ||
| 99 | 537 | value = pair; | |
| 100 | 537 | } | |
| 101 | |||
| 102 | 44 | void Read(const Json& json, const std::string& path, UpsamplingSpec& value) { | |
| 103 | 44 | UpsamplingSpec spec; | |
| 104 | 44 | if (json.is_string()) { | |
| 105 | 24 | spec.type = json.get<std::string>(); | |
| 106 | 24 | if (spec.type.empty()) { | |
| 107 | 2 | throw ConfigError(path, "must not be empty"); | |
| 108 | } | ||
| 109 | 23 | value = std::move(spec); | |
| 110 | 23 | return; | |
| 111 | } | ||
| 112 | 20 | if (!json.is_object()) { | |
| 113 | 2 | throw ConfigError( | |
| 114 | 4 | path, std::string("expected an upsampling name or an object with a \"type\", not ") + | |
| 115 | 8 | json.type_name()); | |
| 116 | } | ||
| 117 | 18 | const auto type = json.find("type"); | |
| 118 | 53 | if (type == json.end() || !type->is_string() || type->get<std::string>().empty()) { | |
| 119 | 2 | throw ConfigError(FieldPath(path, "type"), "expected an upsampling name"); | |
| 120 | } | ||
| 121 | 17 | spec.type = type->get<std::string>(); | |
| 122 | 17 | if (KernelUpsamplerMethod(spec.type)) { | |
| 123 | 9 | Reader reader(json, path); | |
| 124 | 2 | reader.Accept("type"); | |
| 125 | 2 | reader.Finish(); | |
| 126 | 17 | } else if (spec.type == "diamond_square") { | |
| 127 | 11 | ReadObject(json, path, spec.diamondSquare, true); | |
| 128 | } else { | ||
| 129 | 4 | Json settings = json; | |
| 130 | 4 | settings.erase("type"); | |
| 131 | 4 | if (!settings.empty()) { | |
| 132 | 4 | spec.settings = settings.dump(); | |
| 133 | } | ||
| 134 | 4 | } | |
| 135 | 14 | value = std::move(spec); | |
| 136 | 37 | } | |
| 137 | |||
| 138 | 4014 | Json TypedJson(std::string_view type, const Json& fields) { | |
| 139 | 4014 | Json json = Json::object(); | |
| 140 | 8028 | json["type"] = std::string(type); | |
| 141 | 23973 | for (const auto& item : fields.items()) { | |
| 142 | 19959 | json[item.key()] = item.value(); | |
| 143 | 4014 | } | |
| 144 | 4014 | return json; | |
| 145 | } | ||
| 146 | |||
| 147 | 190 | Json ToJson(const UpsamplingSpec& value, bool compact) { | |
| 148 | 190 | if (value.type == "diamond_square") { | |
| 149 | 26 | return TypedJson(value.type, FieldsJson(value.diamondSquare, false, compact)); | |
| 150 | } | ||
| 151 | 164 | if (value.settings.empty()) { | |
| 152 | 157 | return value.type; | |
| 153 | } | ||
| 154 | 7 | return TypedJson(value.type, Json::parse(value.settings)); | |
| 155 | } | ||
| 156 | |||
| 157 | namespace { | ||
| 158 | |||
| 159 | /// Checks a configuration's "schema_version" before any other key, which a newer version may add. | ||
| 160 | /// @throws ConfigError at "schema_version" when the object `json` has none, or another version | ||
| 161 | /// than this library reads. | ||
| 162 | 463 | void CheckSchemaVersion(const Json& json) { | |
| 163 | 463 | if (!json.is_object()) { | |
| 164 | 2 | return; | |
| 165 | } | ||
| 166 | 461 | const std::string supported = | |
| 167 | 461 | "this library reads version " + std::to_string(kConfigSchemaVersion); | |
| 168 | 461 | const auto found = json.find("schema_version"); | |
| 169 | 922 | if (found == json.end()) { | |
| 170 | 4 | throw ConfigError("schema_version", "required; " + supported); | |
| 171 | } | ||
| 172 | 459 | int version = 0; | |
| 173 | 924 | Read(*found, "schema_version", version); | |
| 174 | 456 | if (version != kConfigSchemaVersion) { | |
| 175 | 4 | throw ConfigError("schema_version", | |
| 176 | 12 | "version " + std::to_string(version) + " is not supported; " + supported); | |
| 177 | } | ||
| 178 | 452 | } | |
| 179 | |||
| 180 | } // namespace | ||
| 181 | |||
| 182 | } // namespace config | ||
| 183 | |||
| 184 | 440 | ConfigError::ConfigError(std::string path, const std::string& message) | |
| 185 | 1313 | : std::runtime_error(path.empty() ? message : path + ": " + message), m_path(std::move(path)) {} | |
| 186 | |||
| 187 | 465 | TerrainGenConfig ParseConfig(std::string_view json) { | |
| 188 | 465 | config::Json document; | |
| 189 | 465 | try { | |
| 190 | 467 | document = config::Json::parse(json); | |
| 191 | 2 | } catch (const config::Json::exception& error) { | |
| 192 | 6 | throw ConfigError("", std::string("invalid JSON: ") + error.what()); | |
| 193 | 2 | } | |
| 194 | 463 | config::CheckSchemaVersion(document); | |
| 195 | 454 | ConfigSpec parsed; | |
| 196 | 908 | config::Read(document, "", parsed); | |
| 197 | 674 | return parsed; | |
| 198 | 582 | } | |
| 199 | |||
| 200 | 18 | TerrainGenConfig LoadConfig(const std::filesystem::path& path) { | |
| 201 | 18 | std::ifstream file(path, std::ios::binary); | |
| 202 | 18 | if (!file) { | |
| 203 | 6 | throw ConfigError("", "cannot read " + path.string()); | |
| 204 | } | ||
| 205 | 15 | std::ostringstream text; | |
| 206 | 15 | text << file.rdbuf(); | |
| 207 | 44 | ConfigSpec config = ParseConfig(text.str()); | |
| 208 | 14 | config.directory = path.parent_path(); | |
| 209 | 28 | return config; | |
| 210 | 19 | } | |
| 211 | |||
| 212 | 107 | std::string ConfigToJson(const TerrainGenConfig& terrainConfig, int indent) { | |
| 213 | 214 | return config::FieldsJson(terrainConfig.Spec(), true, true).dump(indent) + "\n"; | |
| 214 | } | ||
| 215 | |||
| 216 | 74 | bool operator==(const TerrainGenConfig& a, const TerrainGenConfig& b) { | |
| 217 | 74 | return config::FieldsJson(a.Spec()) == config::FieldsJson(b.Spec()); | |
| 218 | } | ||
| 219 | |||
| 220 | 2 | bool operator!=(const TerrainGenConfig& a, const TerrainGenConfig& b) { | |
| 221 | 2 | return !(a == b); | |
| 222 | } | ||
| 223 | |||
| 224 | } // namespace terraingen | ||
| 225 |