Skip to content

Commit

Permalink
Add more Lua functions for working with WML values
Browse files Browse the repository at this point in the history
  • Loading branch information
CelticMinstrel committed Nov 13, 2019
1 parent 8374306 commit f2f9bc5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Expand Up @@ -41,6 +41,8 @@
* unit:transform() now takes an optional variation parameter
* Support side.variables for access to side variables, similar to unit.variables
* New read-write keys in unit userdata: ellipse, halo, description, renamable
* New functions for working with WML: wml.merge, wml.diff, wml.patch
* wesnoth.wml_matches_filter renamed to wml.matches_filter (the old name still works)
### WML engine
* Support upkeep in StandardUnitFilter
* [effect]apply_to=variation now supports heal_full
Expand Down
40 changes: 40 additions & 0 deletions src/scripting/lua_kernel_base.cpp
Expand Up @@ -293,6 +293,42 @@ static int intf_wml_matches_filter(lua_State* L)
return 1;
}

static int intf_wml_merge(lua_State* L) {
config base = luaW_checkconfig(L, 1);
config merge = luaW_checkconfig(L, 2);
const std::string mode = lua_isstring(L, 3) ? luaL_checkstring(L, 3) : "merge";
if(mode == "append") {
base.merge_attributes(merge);
base.append_children(merge);
} else {
if(mode == "replace") {
for(const auto& c : merge.all_children_range()) {
base.clear_children(c.key);
}
} else if(mode != "merge") {
return luaL_argerror(L, 3, "invalid merge mode - must be merge, append, or replace");
}
base.merge_with(merge);
}
luaW_pushconfig(L, base);
return 1;
}

static int intf_wml_diff(lua_State* L) {
config lhs = luaW_checkconfig(L, 1);
config rhs = luaW_checkconfig(L, 2);
luaW_pushconfig(L, lhs.get_diff(rhs));
return 1;
}

static int intf_wml_patch(lua_State* L) {
config base = luaW_checkconfig(L, 1);
config patch = luaW_checkconfig(L, 2);
base.apply_diff(patch);
luaW_pushconfig(L, base);
return 1;
}

/**
* Logs a message
* Arg 1: (optional) Logger
Expand Down Expand Up @@ -649,6 +685,10 @@ lua_kernel_base::lua_kernel_base()
{ "load", &intf_load_wml},
{ "parse", &intf_parse_wml},
{ "clone", &intf_clone_wml},
{ "merge", &intf_wml_merge},
{ "diff", &intf_wml_diff},
{ "patch", &intf_wml_patch},
{ "matches_filter", &intf_wml_matches_filter},
{ nullptr, nullptr },
};
lua_newtable(L);
Expand Down

0 comments on commit f2f9bc5

Please sign in to comment.