* Load town data from _file_to_saveload, place towns at the appropriate locations, and expand them to their target populations. */
| 357 | * Load town data from _file_to_saveload, place towns at the appropriate locations, and expand them to their target populations. |
| 358 | */ |
| 359 | void LoadTownData() |
| 360 | { |
| 361 | /* Load the JSON file as a string initially. We'll parse it soon. */ |
| 362 | size_t filesize; |
| 363 | auto f = FioFOpenFile(_file_to_saveload.name, "rb", HEIGHTMAP_DIR, &filesize); |
| 364 | |
| 365 | if (!f.has_value()) { |
| 366 | ShowErrorMessage(GetEncodedString(STR_TOWN_DATA_ERROR_LOAD_FAILED), |
| 367 | GetEncodedString(STR_TOWN_DATA_ERROR_JSON_FORMATTED_INCORRECTLY), WL_ERROR); |
| 368 | return; |
| 369 | } |
| 370 | |
| 371 | std::string text(filesize, '\0'); |
| 372 | size_t len = fread(text.data(), filesize, 1, *f); |
| 373 | f.reset(); |
| 374 | if (len != 1) { |
| 375 | ShowErrorMessage(GetEncodedString(STR_TOWN_DATA_ERROR_LOAD_FAILED), |
| 376 | GetEncodedString(STR_TOWN_DATA_ERROR_JSON_FORMATTED_INCORRECTLY), WL_ERROR); |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | /* Now parse the JSON. */ |
| 381 | nlohmann::json town_data; |
| 382 | try { |
| 383 | town_data = nlohmann::json::parse(text); |
| 384 | } catch (nlohmann::json::exception &) { |
| 385 | ShowErrorMessage(GetEncodedString(STR_TOWN_DATA_ERROR_LOAD_FAILED), GetEncodedString(STR_TOWN_DATA_ERROR_JSON_FORMATTED_INCORRECTLY), WL_ERROR); |
| 386 | return; |
| 387 | } |
| 388 | |
| 389 | /* Check for JSON formatting errors with the array of towns. */ |
| 390 | if (!town_data.is_array()) { |
| 391 | ShowErrorMessage(GetEncodedString(STR_TOWN_DATA_ERROR_LOAD_FAILED), GetEncodedString(STR_TOWN_DATA_ERROR_JSON_FORMATTED_INCORRECTLY), WL_ERROR); |
| 392 | return; |
| 393 | } |
| 394 | |
| 395 | std::vector<std::pair<Town *, uint> > towns; |
| 396 | uint failed_towns = 0; |
| 397 | |
| 398 | /* Iterate through towns and attempt to found them. */ |
| 399 | for (auto &feature : town_data) { |
| 400 | std::string name; // The name of the town. |
| 401 | uint population; // The target population of the town when created in OpenTTD. If input is blank, defaults to 0. |
| 402 | bool is_city; // Should it be created as a city in OpenTTD? If input is blank, defaults to false. |
| 403 | float x_proportion; // The X coordinate of the town, as a proportion 0..1 of the maximum X coordinate. |
| 404 | float y_proportion; // The Y coordinate of the town, as a proportion 0..1 of the maximum Y coordinate. |
| 405 | |
| 406 | /* Ensure JSON is formatted properly. */ |
| 407 | if (!feature.is_object()) { |
| 408 | ShowErrorMessage(GetEncodedString(STR_TOWN_DATA_ERROR_LOAD_FAILED), GetEncodedString(STR_TOWN_DATA_ERROR_JSON_FORMATTED_INCORRECTLY), WL_ERROR); |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | /* Check to ensure all fields exist and are of the correct type. |
| 413 | * If the town name is formatted wrong, all we can do is give a general warning. */ |
| 414 | if (!feature.contains("name") || !feature.at("name").is_string()) { |
| 415 | ShowErrorMessage(GetEncodedString(STR_TOWN_DATA_ERROR_LOAD_FAILED), GetEncodedString(STR_TOWN_DATA_ERROR_JSON_FORMATTED_INCORRECTLY), WL_ERROR); |
| 416 | return; |
no test coverage detected