Ensure invalid savename characters are stripped from scenario names earlier (fixes #4116)

This ensures the invalid characters are never saved internally in the first place, so this should
cover any case besides the aforementioned bug that might crop up.
This commit is contained in:
Charles Dang 2019-07-01 18:29:58 +11:00
parent d53ff6568c
commit 533facbe83
3 changed files with 40 additions and 42 deletions

View file

@ -85,6 +85,38 @@ static lg::log_domain log_engine("engine");
#define LOG_NG LOG_STREAM(info, log_engine)
#define DBG_NG LOG_STREAM(debug, log_engine)
namespace
{
bool variable_to_bool(const config& vars, const std::string& expression)
{
std::string res = utils::interpolate_variables_into_string(expression, config_variable_set(vars));
return res == "true" || res == "yes" || res == "1";
}
// helper objects for saved_game::expand_mp_events()
struct modevents_entry
{
modevents_entry(const std::string& _type, const std::string& _id)
: type(_type)
, id(_id)
{
}
std::string type;
std::string id;
};
bool is_illegal_file_char(char c)
{
return c == '/' || c == '\\' || c == ':' || (c >= 0x00 && c < 0x20)
#ifdef _WIN32
|| c == '?' || c == '|' || c == '<' || c == '>' || c == '*' || c == '"'
#endif
;
}
} // end anon namespace
saved_game::saved_game()
: has_carryover_expanded_(false)
, carryover_(carryover_info().to_config())
@ -275,29 +307,6 @@ void saved_game::check_require_scenario()
mp_settings_.update_addon_requirements(required_scenario);
}
namespace
{
bool variable_to_bool(const config& vars, const std::string& expression)
{
std::string res = utils::interpolate_variables_into_string(expression, config_variable_set(vars));
return res == "true" || res == "yes" || res == "1";
}
// helper objects for saved_game::expand_mp_events()
struct modevents_entry
{
modevents_entry(const std::string& _type, const std::string& _id)
: type(_type)
, id(_id)
{
}
std::string type;
std::string id;
};
} // end anon namespace
void saved_game::load_mod(const std::string& type, const std::string& id, size_t pos)
{
if(const config& cfg = game_config_manager::get()->game_config().find_child(type, "id", id)) {
@ -620,11 +629,16 @@ bool saved_game::not_corrupt() const
void saved_game::update_label()
{
std::string& label = classification().label;
if(classification().abbrev.empty()) {
classification().label = starting_point_["name"].str();
label = starting_point_["name"].str();
} else {
classification().label = classification().abbrev + "-" + starting_point_["name"];
label = classification().abbrev + "-" + starting_point_["name"];
}
label.erase(std::remove_if(label.begin(), label.end(), is_illegal_file_char), label.end());
std::replace(label.begin(), label.end(), '_', ' ');
}
void saved_game::cancel_orders()

View file

@ -414,22 +414,9 @@ bool savegame::check_filename(const std::string& filename)
return true;
}
bool savegame::is_illegal_file_char(char c)
{
return c == '/' || c == '\\' || c == ':' || (c >= 0x00 && c < 0x20)
#ifdef _WIN32
|| c == '?' || c == '|' || c == '<' || c == '>' || c == '*' || c == '"'
#endif
;
}
std::string savegame::create_filename(unsigned int turn_number) const
{
std::string filename = create_initial_filename(turn_number);
filename.erase(std::remove_if(filename.begin(), filename.end(),
is_illegal_file_char), filename.end());
std::replace(filename.begin(), filename.end(), '_', ' ');
return filename;
return create_initial_filename(turn_number);
}
void savegame::before_save()

View file

@ -208,9 +208,6 @@ protected:
std::string title_;
private:
/** Checks if a certain character is allowed in a savefile name. */
static bool is_illegal_file_char(char c);
/** Subclass-specific part of filename building. */
virtual std::string create_initial_filename(unsigned int turn_number) const = 0;
/** Display the save game dialog. */