Merge remote-tracking branch 'upstream/master'
This commit is contained in:
119
src/settings.cpp
119
src/settings.cpp
@@ -82,7 +82,7 @@ ClientSettings _settings_client;
|
||||
GameSettings _settings_game; ///< Game settings of a running game or the scenario editor.
|
||||
GameSettings _settings_newgame; ///< Game settings for new games (updated from the intro screen).
|
||||
VehicleDefaultSettings _old_vds; ///< Used for loading default vehicles settings from old savegames
|
||||
char *_config_file; ///< Configuration file of OpenTTD
|
||||
std::string _config_file; ///< Configuration file of OpenTTD
|
||||
|
||||
typedef std::list<ErrorMessageData> ErrorList;
|
||||
static ErrorList _settings_error_list; ///< Errors while loading minimal settings.
|
||||
@@ -416,6 +416,7 @@ static const void *StringToVal(const SettingDescBase *desc, const char *orig_str
|
||||
return desc->def;
|
||||
}
|
||||
|
||||
case SDT_STDSTRING:
|
||||
case SDT_STRING: return orig_str;
|
||||
case SDT_INTLIST: return str;
|
||||
default: break;
|
||||
@@ -507,10 +508,6 @@ static void IniLoadSettings(IniFile *ini, const SettingDesc *sd, const char *grp
|
||||
{
|
||||
IniGroup *group;
|
||||
IniGroup *group_def = ini->GetGroup(grpname);
|
||||
IniItem *item;
|
||||
const void *p;
|
||||
void *ptr;
|
||||
const char *s;
|
||||
|
||||
for (; sd->save.cmd != SL_END; sd++) {
|
||||
const SettingDescBase *sdb = &sd->desc;
|
||||
@@ -519,30 +516,30 @@ static void IniLoadSettings(IniFile *ini, const SettingDesc *sd, const char *grp
|
||||
if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to)) continue;
|
||||
|
||||
/* For settings.xx.yy load the settings from [xx] yy = ? */
|
||||
s = strchr(sdb->name, '.');
|
||||
if (s != nullptr) {
|
||||
group = ini->GetGroup(sdb->name, s - sdb->name);
|
||||
s++;
|
||||
std::string s{ sdb->name };
|
||||
auto sc = s.find('.');
|
||||
if (sc != std::string::npos) {
|
||||
group = ini->GetGroup(s.substr(0, sc));
|
||||
s = s.substr(sc + 1);
|
||||
} else {
|
||||
s = sdb->name;
|
||||
group = group_def;
|
||||
}
|
||||
|
||||
item = group->GetItem(s, false);
|
||||
IniItem *item = group->GetItem(s, false);
|
||||
if (item == nullptr && group != group_def) {
|
||||
/* For settings.xx.yy load the settings from [settingss] yy = ? in case the previous
|
||||
/* For settings.xx.yy load the settings from [settings] yy = ? in case the previous
|
||||
* did not exist (e.g. loading old config files with a [settings] section */
|
||||
item = group_def->GetItem(s, false);
|
||||
}
|
||||
if (item == nullptr) {
|
||||
/* For settings.xx.zz.yy load the settings from [zz] yy = ? in case the previous
|
||||
* did not exist (e.g. loading old config files with a [yapf] section */
|
||||
const char *sc = strchr(s, '.');
|
||||
if (sc != nullptr) item = ini->GetGroup(s, sc - s)->GetItem(sc + 1, false);
|
||||
sc = s.find('.');
|
||||
if (sc != std::string::npos) item = ini->GetGroup(s.substr(0, sc))->GetItem(s.substr(sc + 1), false);
|
||||
}
|
||||
|
||||
p = (item == nullptr) ? sdb->def : StringToVal(sdb, item->value);
|
||||
ptr = GetVariableAddress(object, sld);
|
||||
const void *p = (item == nullptr) ? sdb->def : StringToVal(sdb, item->value.has_value() ? item->value->c_str() : nullptr);
|
||||
void *ptr = GetVariableAddress(object, sld);
|
||||
|
||||
switch (sdb->cmd) {
|
||||
case SDT_BOOLX: // All four are various types of (integer) numbers
|
||||
@@ -571,6 +568,22 @@ static void IniLoadSettings(IniFile *ini, const SettingDesc *sd, const char *grp
|
||||
}
|
||||
break;
|
||||
|
||||
case SDT_STDSTRING:
|
||||
switch (GetVarMemType(sld->conv)) {
|
||||
case SLE_VAR_STR:
|
||||
case SLE_VAR_STRQ:
|
||||
if (p != nullptr) {
|
||||
reinterpret_cast<std::string *>(ptr)->assign((const char *)p);
|
||||
} else {
|
||||
reinterpret_cast<std::string *>(ptr)->clear();
|
||||
}
|
||||
break;
|
||||
|
||||
default: NOT_REACHED();
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case SDT_INTLIST: {
|
||||
if (!LoadIntList((const char*)p, ptr, sld->length, GetVarMemType(sld->conv))) {
|
||||
ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_ARRAY);
|
||||
@@ -606,7 +619,6 @@ static void IniSaveSettings(IniFile *ini, const SettingDesc *sd, const char *grp
|
||||
IniGroup *group_def = nullptr, *group;
|
||||
IniItem *item;
|
||||
char buf[512];
|
||||
const char *s;
|
||||
void *ptr;
|
||||
|
||||
for (; sd->save.cmd != SL_END; sd++) {
|
||||
@@ -619,22 +631,22 @@ static void IniSaveSettings(IniFile *ini, const SettingDesc *sd, const char *grp
|
||||
if (sld->conv & SLF_NOT_IN_CONFIG) continue;
|
||||
|
||||
/* XXX - wtf is this?? (group override?) */
|
||||
s = strchr(sdb->name, '.');
|
||||
if (s != nullptr) {
|
||||
group = ini->GetGroup(sdb->name, s - sdb->name);
|
||||
s++;
|
||||
std::string s{ sdb->name };
|
||||
auto sc = s.find('.');
|
||||
if (sc != std::string::npos) {
|
||||
group = ini->GetGroup(s.substr(0, sc));
|
||||
s = s.substr(sc + 1);
|
||||
} else {
|
||||
if (group_def == nullptr) group_def = ini->GetGroup(grpname);
|
||||
s = sdb->name;
|
||||
group = group_def;
|
||||
}
|
||||
|
||||
item = group->GetItem(s, true);
|
||||
ptr = GetVariableAddress(object, sld);
|
||||
|
||||
if (item->value != nullptr) {
|
||||
if (item->value.has_value()) {
|
||||
/* check if the value is the same as the old value */
|
||||
const void *p = StringToVal(sdb, item->value);
|
||||
const void *p = StringToVal(sdb, item->value->c_str());
|
||||
|
||||
/* The main type of a variable/setting is in bytes 8-15
|
||||
* The subtype (what kind of numbers do we have there) is in 0-7 */
|
||||
@@ -708,6 +720,22 @@ static void IniSaveSettings(IniFile *ini, const SettingDesc *sd, const char *grp
|
||||
}
|
||||
break;
|
||||
|
||||
case SDT_STDSTRING:
|
||||
switch (GetVarMemType(sld->conv)) {
|
||||
case SLE_VAR_STR: strecpy(buf, reinterpret_cast<std::string *>(ptr)->c_str(), lastof(buf)); break;
|
||||
|
||||
case SLE_VAR_STRQ:
|
||||
if (reinterpret_cast<std::string *>(ptr)->empty()) {
|
||||
buf[0] = '\0';
|
||||
} else {
|
||||
seprintf(buf, lastof(buf), "\"%s\"", reinterpret_cast<std::string *>(ptr)->c_str());
|
||||
}
|
||||
break;
|
||||
|
||||
default: NOT_REACHED();
|
||||
}
|
||||
break;
|
||||
|
||||
case SDT_INTLIST:
|
||||
MakeIntList(buf, lastof(buf), ptr, sld->length, sld->conv);
|
||||
break;
|
||||
@@ -716,8 +744,7 @@ static void IniSaveSettings(IniFile *ini, const SettingDesc *sd, const char *grp
|
||||
}
|
||||
|
||||
/* The value is different, that means we have to write it to the ini */
|
||||
free(item->value);
|
||||
item->value = stredup(buf);
|
||||
item->value.emplace(buf);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -739,7 +766,7 @@ static void IniLoadSettingList(IniFile *ini, const char *grpname, StringList &li
|
||||
list.clear();
|
||||
|
||||
for (const IniItem *item = group->item; item != nullptr; item = item->next) {
|
||||
if (item->name != nullptr) list.emplace_back(item->name);
|
||||
if (!item->name.empty()) list.push_back(item->name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1439,14 +1466,14 @@ static void AILoadConfig(IniFile *ini, const char *grpname)
|
||||
for (item = group->item; c < MAX_COMPANIES && item != nullptr; c++, item = item->next) {
|
||||
AIConfig *config = AIConfig::GetConfig(c, AIConfig::SSS_FORCE_NEWGAME);
|
||||
|
||||
config->Change(item->name);
|
||||
config->Change(item->name.c_str());
|
||||
if (!config->HasScript()) {
|
||||
if (strcmp(item->name, "none") != 0) {
|
||||
DEBUG(script, 0, "The AI by the name '%s' was no longer found, and removed from the list.", item->name);
|
||||
if (item->name != "none") {
|
||||
DEBUG(script, 0, "The AI by the name '%s' was no longer found, and removed from the list.", item->name.c_str());
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (item->value != nullptr) config->StringToSettings(item->value);
|
||||
if (item->value.has_value()) config->StringToSettings(item->value->c_str());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1466,14 +1493,14 @@ static void GameLoadConfig(IniFile *ini, const char *grpname)
|
||||
|
||||
GameConfig *config = GameConfig::GetConfig(AIConfig::SSS_FORCE_NEWGAME);
|
||||
|
||||
config->Change(item->name);
|
||||
config->Change(item->name.c_str());
|
||||
if (!config->HasScript()) {
|
||||
if (strcmp(item->name, "none") != 0) {
|
||||
DEBUG(script, 0, "The GameScript by the name '%s' was no longer found, and removed from the list.", item->name);
|
||||
if (item->name != "none") {
|
||||
DEBUG(script, 0, "The GameScript by the name '%s' was no longer found, and removed from the list.", item->name.c_str());
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (item->value != nullptr) config->StringToSettings(item->value);
|
||||
if (item->value.has_value()) config->StringToSettings(item->value->c_str());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1497,7 +1524,7 @@ static int DecodeHexNibble(char c)
|
||||
* @param dest_size Number of bytes in \a dest.
|
||||
* @return Whether reading was successful.
|
||||
*/
|
||||
static bool DecodeHexText(char *pos, uint8 *dest, size_t dest_size)
|
||||
static bool DecodeHexText(const char *pos, uint8 *dest, size_t dest_size)
|
||||
{
|
||||
while (dest_size > 0) {
|
||||
int hi = DecodeHexNibble(pos[0]);
|
||||
@@ -1529,7 +1556,7 @@ static GRFConfig *GRFLoadConfig(IniFile *ini, const char *grpname, bool is_stati
|
||||
GRFConfig *c = nullptr;
|
||||
|
||||
uint8 grfid_buf[4], md5sum[16];
|
||||
char *filename = item->name;
|
||||
const char *filename = item->name.c_str();
|
||||
bool has_grfid = false;
|
||||
bool has_md5sum = false;
|
||||
|
||||
@@ -1553,8 +1580,8 @@ static GRFConfig *GRFLoadConfig(IniFile *ini, const char *grpname, bool is_stati
|
||||
if (c == nullptr) c = new GRFConfig(filename);
|
||||
|
||||
/* Parse parameters */
|
||||
if (!StrEmpty(item->value)) {
|
||||
int count = ParseIntList(item->value, c->param, lengthof(c->param));
|
||||
if (item->value.has_value() && !item->value->empty()) {
|
||||
int count = ParseIntList(item->value->c_str(), c->param, lengthof(c->param));
|
||||
if (count < 0) {
|
||||
SetDParamStr(0, filename);
|
||||
ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_ARRAY, WL_CRITICAL);
|
||||
@@ -1577,7 +1604,7 @@ static GRFConfig *GRFLoadConfig(IniFile *ini, const char *grpname, bool is_stati
|
||||
SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_UNKNOWN);
|
||||
}
|
||||
|
||||
SetDParamStr(0, StrEmpty(filename) ? item->name : filename);
|
||||
SetDParamStr(0, StrEmpty(filename) ? item->name.c_str() : filename);
|
||||
ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_GRF, WL_CRITICAL);
|
||||
delete c;
|
||||
continue;
|
||||
@@ -1788,8 +1815,8 @@ StringList GetGRFPresetList()
|
||||
|
||||
std::unique_ptr<IniFile> ini(IniLoadConfig());
|
||||
for (IniGroup *group = ini->group; group != nullptr; group = group->next) {
|
||||
if (strncmp(group->name, "preset-", 7) == 0) {
|
||||
list.emplace_back(group->name + 7);
|
||||
if (group->name.compare(0, 7, "preset-") == 0) {
|
||||
list.push_back(group->name.substr(7));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1898,6 +1925,8 @@ CommandCost CmdChangeSetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uin
|
||||
}
|
||||
|
||||
SetWindowClassesDirty(WC_GAME_OPTIONS);
|
||||
|
||||
if (_save_config) SaveToConfig();
|
||||
}
|
||||
|
||||
return CommandCost();
|
||||
@@ -1966,12 +1995,15 @@ bool SetSettingValue(uint index, int32 value, bool force_newgame)
|
||||
|
||||
SetWindowClassesDirty(WC_GAME_OPTIONS);
|
||||
|
||||
if (_save_config) SaveToConfig();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (force_newgame) {
|
||||
void *var2 = GetVariableAddress(&_settings_newgame, &sd->save);
|
||||
Write_ValidateSetting(var2, sd, value);
|
||||
|
||||
if (_save_config) SaveToConfig();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -2038,6 +2070,7 @@ uint GetCompanySettingIndex(const char *name)
|
||||
{
|
||||
uint i;
|
||||
const SettingDesc *sd = GetSettingFromName(name, &i);
|
||||
(void)sd; // Unused without asserts
|
||||
assert(sd != nullptr && (sd->desc.flags & SGF_PER_COMPANY) != 0);
|
||||
return i;
|
||||
}
|
||||
@@ -2064,6 +2097,7 @@ bool SetSettingValue(uint index, const char *value, bool force_newgame)
|
||||
}
|
||||
if (sd->desc.proc != nullptr) sd->desc.proc(0);
|
||||
|
||||
if (_save_config) SaveToConfig();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -2144,6 +2178,7 @@ void IConsoleSetSetting(const char *name, int value)
|
||||
{
|
||||
uint index;
|
||||
const SettingDesc *sd = GetSettingFromName(name, &index);
|
||||
(void)sd; // Unused without asserts
|
||||
assert(sd != nullptr);
|
||||
SetSettingValue(index, value);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user