Skip to content

Commit

Permalink
Removed sanity checks for missing variable=, [case] and value= in [sw…
Browse files Browse the repository at this point in the history
…itch]
  • Loading branch information
Elvish-Hunter committed Apr 12, 2014
1 parent c35e08d commit 082992b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 58 deletions.
28 changes: 7 additions & 21 deletions data/lua/wml-tags.lua
Expand Up @@ -367,37 +367,23 @@ wml_actions["while"] = function( cfg )
end

function wml_actions.switch(cfg)
-- check if variable= is missing
if not cfg.variable then
helper.wml_error "[switch] missing required variable= attribute"
end
local variable = wesnoth.get_variable(cfg.variable)
local value = wesnoth.get_variable(cfg.variable)
local found = false

-- check if the [case] sub-tag is missing, and raise error if so
if not helper.get_child( cfg, "case" ) then
helper.wml_error "[switch] missing required [case] tag"
end

-- Execute all the [case]s where the value matches.
for case_child in helper.child_range(cfg, "case") do
-- warn if value= isn't present; it may be false, so check only for nil
if case_child.value == nil then
helper.wml_error "[case] missing required value= attribute"
end
for v in helper.child_range(cfg, "case") do
local match = false
for w in string.gmatch(case_child.value, "[^%s,][^,]*") do
if w == tostring(variable) then match = true ; break end
for w in string.gmatch(v.value, "[^%s,][^,]*") do
if w == tostring(value) then match = true ; break end
end
if match then
handle_event_commands(case_child)
handle_event_commands(v)
found = true
end
end
-- Otherwise execute [else] statements.
if not found then
for else_child in helper.child_range(cfg, "else") do
handle_event_commands(else_child)
for v in helper.child_range(cfg, "else") do
handle_event_commands(v)
end
end
end
Expand Down
18 changes: 0 additions & 18 deletions src/storyscreen/controller.cpp
Expand Up @@ -106,30 +106,12 @@ void controller::resolve_wml(const vconfig& cfg)
}
// [switch]
else if(key == "switch") {
// raise a WML error and exit if variable= is missing
if (!node.has_attribute("variable")) {
lg::wml_error << "[switch] missing required variable= attribute\n";
return;
}
const std::string var_name = node["variable"];
const std::string var_actual_value = resources::gamedata->get_variable_const(var_name);
bool case_not_found = true;

// check if the [switch] tag has a [case] child;
// if not, raise a WML error and exit to make the mistake as much evident as possible
if (!node.has_child("case")) {
lg::wml_error << "[switch] missing required [case] tag\n";
return;
}

for(vconfig::all_children_iterator j = node.ordered_begin(); j != node.ordered_end(); ++j) {
if(j->first != "case") continue;

// raise a WML error and exit if value= is missing
if (!(j->second).has_attribute("value")) {
lg::wml_error << "[case] missing required value= attribute\n";
return;
}

// Enter all matching cases.
const std::string var_expected_value = (j->second)["value"];
Expand Down
19 changes: 0 additions & 19 deletions src/storyscreen/part.cpp
Expand Up @@ -299,34 +299,15 @@ void part::resolve_wml(const vconfig &cfg)
}
// [switch]
else if(key == "switch") {
// raise a WML error and exit if variable= is missing
if (!node.has_attribute("variable")) {
lg::wml_error << "[switch] missing required variable= attribute\n";
return;
}
const std::string var_name = node["variable"];
const std::string var_actual_value = resources::gamedata->get_variable_const(var_name);
bool case_not_found = true;

// check if the [switch] tag has a [case] child;
// if not, raise a WML error and exit to make the mistake as much evident as possible
if (!node.has_child("case")) {
lg::wml_error << "[switch] missing required [case] tag\n";
return;
}

for(vconfig::all_children_iterator j = node.ordered_begin(); j != node.ordered_end(); ++j) {
if(j->first != "case") continue;

// raise a WML error and exit if value= is missing
if (!(j->second).has_attribute("value")) {
lg::wml_error << "[case] missing required value= attribute\n";
return;
}

// Enter all matching cases.
const std::string var_expected_value = (j->second)["value"];

if(var_actual_value == var_expected_value) {
case_not_found = false;
resolve_wml(j->second);
Expand Down

0 comments on commit 082992b

Please sign in to comment.