Skip to content

Commit

Permalink
Game Events: some safeguards
Browse files Browse the repository at this point in the history
* Don't register an empty event
* Raise a WML error if a event is missing a name= key (the wiki declares it mandatory)
  • Loading branch information
Vultraz committed Feb 1, 2018
1 parent b888713 commit c916a85
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/game_events/manager_impl.cpp
Expand Up @@ -91,6 +91,11 @@ handler_list& event_handlers::get(const std::string& name)
*/
void event_handlers::add_event_handler(const config& cfg, bool is_menu_item)
{
// Someone decided to register an empty event... bail.
if(cfg.empty()) {
return;
}

std::string name = cfg["name"];
std::string id = cfg["id"];

Expand All @@ -104,12 +109,17 @@ void event_handlers::add_event_handler(const config& cfg, bool is_menu_item)
}
}

if(name.empty()) {
lg::wml_error() << "[event] is missing name field\n";
return;
}

// Make a copy of the event cfg here in order to do some standardization on the
// name field. Will be moved into the handler.
config event_cfg = cfg;

// Split the name field...
std::vector<std::string> standardized_names = utils::split(cfg["name"]);
std::vector<std::string> standardized_names = utils::split(name);

// ...and standardize each one individually. This ensures they're all valid for by-name lookup.
for(std::string& single_name : standardized_names) {
Expand All @@ -118,7 +128,7 @@ void event_handlers::add_event_handler(const config& cfg, bool is_menu_item)
}
}

assert(standardized_names.size() != 0);
assert(!standardized_names.empty());

// Write the new name back to the config.
name = utils::join(standardized_names);
Expand Down

0 comments on commit c916a85

Please sign in to comment.