Skip to content

Commit

Permalink
Lua: Add float mutator macros and some fixups/cleanup to other mutato…
Browse files Browse the repository at this point in the history
…r/accessor macros

- Enclose all macro arguments in parentheses, except those meant to be statements
- Use pushlstring to allow strings with nulls
- New float mutators
  • Loading branch information
CelticMinstrel committed Apr 22, 2017
1 parent 1cdf0ed commit f1e4ad2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/scripting/game_lua_kernel.cpp
Expand Up @@ -1286,8 +1286,8 @@ int game_lua_kernel::impl_game_config_set(lua_State *L)
modify_int_attrib("kill_experience", game_config::kill_experience = value);
modify_int_attrib("last_turn", tod_man().set_number_of_turns_by_wml(value));
modify_string_attrib("next_scenario", gamedata().set_next_scenario(value));
modify_vector_string_attrib("defeat_music", gamedata().set_defeat_music(std::move(vector)));
modify_vector_string_attrib("victory_music", gamedata().set_victory_music(std::move(vector)));
modify_vector_string_attrib("defeat_music", gamedata().set_defeat_music(std::move(value)));
modify_vector_string_attrib("victory_music", gamedata().set_victory_music(std::move(value)));
std::string err_msg = "unknown modifiable property of game_config: ";
err_msg += m;
return luaL_argerror(L, 2, err_msg.c_str());
Expand Down
81 changes: 50 additions & 31 deletions src/scripting/lua_common.hpp
Expand Up @@ -187,115 +187,134 @@ int luaW_pcall_internal(lua_State *L, int nArgs, int nRets);
int luaW_type_error (lua_State *L, int narg, const char *tname);

#define return_tstring_attrib(name, accessor) \
if (strcmp(m, name) == 0) { \
luaW_pushtstring(L, accessor); \
if (strcmp(m, (name)) == 0) { \
luaW_pushtstring(L, (accessor)); \
return 1; \
}

#define return_cstring_attrib(name, accessor) \
if (strcmp(m, name) == 0) { \
lua_pushstring(L, accessor); \
if (strcmp(m, (name)) == 0) { \
lua_pushstring(L, (accessor)); \
return 1; \
}

#define return_string_attrib(name, accessor) \
return_cstring_attrib(name, (accessor).c_str())
if (strcmp(m, (name)) == 0) { \
const std::string& str = (accessor); \
lua_pushlstring(L, str.c_str(), str.length()); \
return 1; \
}

#define return_int_attrib(name, accessor) \
if (strcmp(m, name) == 0) { \
lua_pushinteger(L, accessor); \
if (strcmp(m, (name)) == 0) { \
lua_pushinteger(L, (accessor)); \
return 1; \
}

#define return_float_attrib(name, accessor) \
if (strcmp(m, name) == 0) { \
lua_pushnumber(L, accessor); \
if (strcmp(m, (name)) == 0) { \
lua_pushnumber(L, (accessor)); \
return 1; \
}

#define return_bool_attrib(name, accessor) \
if (strcmp(m, name) == 0) { \
lua_pushboolean(L, accessor); \
if (strcmp(m, (name)) == 0) { \
lua_pushboolean(L, (accessor)); \
return 1; \
}

#define return_cfg_attrib(name, accessor) \
if (strcmp(m, name) == 0) { \
if (strcmp(m, (name)) == 0) { \
config cfg; \
accessor; \
{accessor;} \
luaW_pushconfig(L, cfg); \
return 1; \
}

#define return_cfgref_attrib(name, accessor) \
if (strcmp(m, name) == 0) { \
luaW_pushconfig(L, accessor); \
if (strcmp(m, (name)) == 0) { \
luaW_pushconfig(L, (accessor)); \
return 1; \
}

#define return_vector_string_attrib(name, accessor) \
if (strcmp(m, name) == 0) { \
const std::vector<std::string>& vector = accessor; \
if (strcmp(m, (name)) == 0) { \
const std::vector<std::string>& vector = (accessor); \
lua_createtable(L, vector.size(), 0); \
int i = 1; \
for (const std::string& s : vector) { \
lua_pushstring(L, s.c_str()); \
lua_pushlstring(L, s.c_str(), s.length()); \
lua_rawseti(L, -2, i); \
++i; \
} \
return 1; \
}

#define modify_tstring_attrib(name, accessor) \
if (strcmp(m, name) == 0) { \
if (strcmp(m, (name)) == 0) { \
t_string value = luaW_checktstring(L, 3); \
accessor; \
{accessor;} \
return 0; \
}

#define modify_string_attrib(name, accessor) \
if (strcmp(m, name) == 0) { \
if (strcmp(m, (name)) == 0) { \
const char *value = luaL_checkstring(L, 3); \
accessor; \
{accessor;} \
return 0; \
}

#define modify_int_attrib(name, accessor) \
if (strcmp(m, name) == 0) { \
if (strcmp(m, (name)) == 0) { \
int value = static_cast<int>(luaL_checknumber(L, 3)); \
accessor; \
{accessor;} \
return 0; \
}

#define modify_int_attrib_check_range(name, accessor, allowed_min, allowed_max) \
if (strcmp(m, name) == 0) { \
if (strcmp(m, (name)) == 0) { \
int value = static_cast<int>(luaL_checknumber(L, 3)); \
if (value < allowed_min || allowed_max < value) return luaL_argerror(L, 3, "out of bounds"); \
accessor; \
if (value < (allowed_min) || (allowed_max) < value) return luaL_argerror(L, 3, "out of bounds"); \
{accessor;} \
return 0; \
}

#define modify_float_attrib(name, accessor) \
if (strcmp(m, (name)) == 0) { \
lua_Number value = luaL_checknumber(L, 3); \
{accessor;} \
return 0; \
}

#define modify_float_attrib_check_range(name, accessor, allowed_min, allowed_max) \
if (strcmp(m, (name)) == 0) { \
lua_Number value = luaL_checknumber(L, 3); \
if (value < (allowed_min) || (allowed_max) < value) return luaL_argerror(L, 3, "out of bounds"); \
{accessor;} \
return 0; \
}

#define modify_bool_attrib(name, accessor) \
if (strcmp(m, name) == 0) { \
bool value = luaW_toboolean(L, 3); \
accessor; \
{accessor;} \
return 0; \
}

#define modify_vector_string_attrib(name, accessor) \
if (strcmp(m, name) == 0) { \
std::vector<std::string> vector; \
std::vector<std::string> value; \
char const* message = "table with unnamed indices holding strings expected"; \
if (!lua_istable(L, 3)) return luaL_argerror(L, 3, message); \
unsigned length = lua_rawlen(L, 3); \
for (unsigned i = 1; i <= length; ++i) { \
lua_rawgeti(L, 3, i); \
char const* string = lua_tostring(L, 4); \
if(!string) return luaL_argerror(L, 2 + i, message); \
vector.push_back(string); \
value.push_back(string); \
lua_pop(L, 1); \
} \
accessor; \
{accessor;} \
return 0; \
}

Expand Down
4 changes: 2 additions & 2 deletions src/scripting/lua_unit.cpp
Expand Up @@ -399,8 +399,8 @@ static int impl_unit_set(lua_State *L)
modify_bool_attrib("zoc", u.set_emit_zoc(value));
modify_bool_attrib("canrecruit", u.set_can_recruit(value));

modify_vector_string_attrib("extra_recruit", u.set_recruits(vector));
modify_vector_string_attrib("advances_to", u.set_advances_to(vector));
modify_vector_string_attrib("extra_recruit", u.set_recruits(value));
modify_vector_string_attrib("advances_to", u.set_advances_to(value));
if(strcmp(m, "alignment") == 0) {
u.set_alignment(lua_check<unit_type::ALIGNMENT>(L, 3));
return 0;
Expand Down

0 comments on commit f1e4ad2

Please sign in to comment.