MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / GRFLoadConfig

Function GRFLoadConfig

src/settings.cpp:1059–1163  ·  view source on GitHub ↗

* Load a GRF configuration * @param ini The configuration to read from. * @param grpname Group name containing the configuration of the GRF. * @param is_static GRF is static. */

Source from the content-addressed store, hash-verified

1057 * @param is_static GRF is static.
1058 */
1059static GRFConfigList GRFLoadConfig(const IniFile &ini, std::string_view grpname, bool is_static)
1060{
1061 const IniGroup *group = ini.GetGroup(grpname);
1062 GRFConfigList list;
1063
1064 if (group == nullptr) return list;
1065
1066 uint num_grfs = 0;
1067 for (const IniItem &item : group->items) {
1068 std::unique_ptr<GRFConfig> c{};
1069
1070 std::array<uint8_t, 4> grfid_buf;
1071 MD5Hash md5sum;
1072 std::string_view item_name = item.name;
1073 bool has_md5sum = false;
1074
1075 /* Try reading "<grfid>|" and on success, "<md5sum>|". */
1076 auto grfid_pos = item_name.find("|");
1077 if (grfid_pos != std::string_view::npos) {
1078 std::string_view grfid_str = item_name.substr(0, grfid_pos);
1079
1080 if (ConvertHexToBytes(grfid_str, grfid_buf)) {
1081 item_name = item_name.substr(grfid_pos + 1);
1082
1083 auto md5sum_pos = item_name.find("|");
1084 if (md5sum_pos != std::string_view::npos) {
1085 std::string_view md5sum_str = item_name.substr(0, md5sum_pos);
1086
1087 has_md5sum = ConvertHexToBytes(md5sum_str, md5sum);
1088 if (has_md5sum) item_name = item_name.substr(md5sum_pos + 1);
1089 }
1090
1091 uint32_t grfid = grfid_buf[0] | (grfid_buf[1] << 8) | (grfid_buf[2] << 16) | (grfid_buf[3] << 24);
1092 if (has_md5sum) {
1093 const GRFConfig *s = FindGRFConfig(grfid, FGCM_EXACT, &md5sum);
1094 if (s != nullptr) c = std::make_unique<GRFConfig>(*s);
1095 }
1096 if (c == nullptr && !FioCheckFileExists(std::string(item_name), NEWGRF_DIR)) {
1097 const GRFConfig *s = FindGRFConfig(grfid, FGCM_NEWEST_VALID);
1098 if (s != nullptr) c = std::make_unique<GRFConfig>(*s);
1099 }
1100 }
1101 }
1102 std::string filename = std::string(item_name);
1103
1104 if (c == nullptr) c = std::make_unique<GRFConfig>(filename);
1105
1106 /* Parse parameters */
1107 if (item.value.has_value() && !item.value->empty()) {
1108 auto params = ParseIntList(*item.value);
1109 if (params.has_value()) {
1110 c->SetParams(params.value());
1111 } else {
1112 ShowErrorMessage(GetEncodedString(STR_CONFIG_ERROR),
1113 GetEncodedString(STR_CONFIG_ERROR_ARRAY, filename),
1114 WL_CRITICAL);
1115 }
1116 }

Callers 2

LoadFromConfigFunction · 0.85
LoadGRFPresetFromConfigFunction · 0.85

Calls 15

ConvertHexToBytesFunction · 0.85
FindGRFConfigFunction · 0.85
FioCheckFileExistsFunction · 0.85
stringFunction · 0.85
ParseIntListFunction · 0.85
ShowErrorMessageFunction · 0.85
FillGRFDetailsFunction · 0.85
GetGroupMethod · 0.80
substrMethod · 0.80
SetParamsMethod · 0.80
TestMethod · 0.80
push_backMethod · 0.80

Tested by

no test coverage detected