Skip to content

Commit

Permalink
Ensure invalid savename characters are stripped from scenario names e…
Browse files Browse the repository at this point in the history
…arlier (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.
  • Loading branch information
Vultraz committed Jul 1, 2019
1 parent d53ff65 commit 533facb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 42 deletions.
64 changes: 39 additions & 25 deletions src/saved_game.cpp
Expand Up @@ -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())
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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()
Expand Down
15 changes: 1 addition & 14 deletions src/savegame.cpp
Expand Up @@ -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()
Expand Down
3 changes: 0 additions & 3 deletions src/savegame.hpp
Expand Up @@ -208,9 +208,6 @@ class savegame
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. */
Expand Down

0 comments on commit 533facb

Please sign in to comment.