config luaW_checkconfig(lua_State *L, int index) { config result; if (!luaW_toconfig(L, index, result)) luaW_type_error(L, index, "WML table"); return result; }
bool luaW_tovconfig(lua_State *L, int index, vconfig &vcfg) { switch (lua_type(L, index)) { case LUA_TTABLE: { config cfg; bool ok = luaW_toconfig(L, index, cfg); if (!ok) return false; vcfg = vconfig(cfg, true); break; } case LUA_TUSERDATA: if (vconfig * ptr = static_cast<vconfig *> (luaL_testudata(L, index, vconfigKey))) { vcfg = *ptr; } else { return false; } case LUA_TNONE: case LUA_TNIL: break; default: return false; } return true; }
void lua_ai_context::get_persistent_data(config &cfg) const { int top = lua_gettop(L); lua_pushlightuserdata(L, static_cast<void *>(const_cast<char *>(&aisKey))); lua_rawget(L, LUA_REGISTRYINDEX); lua_rawgeti(L, -1, num_); lua_getfield(L, -1, "data"); luaW_toconfig(L, -1, cfg); lua_settop(L, top); }
static int impl_context_accessor(lua_State * L, std::shared_ptr<lua_context_backend> backend, plugins_context::accessor_function func) { if (!backend->valid) { luaL_error(L , "Error, you tried to use an invalid context object in a lua thread"); } if(lua_gettop(L)) { config temp; if(!luaW_toconfig(L, 1, temp)) { luaL_argerror(L, 1, "Error, tried to parse a config but some fields were invalid"); } luaW_pushconfig(L, func(temp)); return 1; } else { luaW_pushconfig(L, func(config())); return 1; } }
config mapgen_lua_kernel::create_scenario(const char * prog, const config & generator, boost::optional<boost::uint32_t> seed) // throws game::lua_error { random_seed_ = seed; run_generator(prog, generator); if (!lua_istable(mState, -1)) { std::string msg = "expected a config (table), found a "; msg += lua_typename(mState, lua_type(mState, -1)); lua_pop(mState, 1); throw game::lua_error(msg.c_str(),"bad return value"); } config result; if (!luaW_toconfig(mState, -1, result)) { std::string msg = "expected a config, but it is malformed "; lua_pop(mState, 1); throw game::lua_error(msg.c_str(),"bad return value"); } return result; }
bool luaW_checkvariable(lua_State *L, variable_access_create& v, int n) { int variabletype = lua_type(L, n); try { switch (variabletype) { case LUA_TBOOLEAN: v.as_scalar() = luaW_toboolean(L, n); return true; case LUA_TNUMBER: v.as_scalar() = lua_tonumber(L, n); return true; case LUA_TSTRING: v.as_scalar() = lua_tostring(L, n); return true; case LUA_TUSERDATA: if (t_string * t_str = static_cast<t_string*> (luaL_testudata(L, n, tstringKey))) { v.as_scalar() = *t_str; return true; } goto default_explicit; case LUA_TTABLE: { config &cfg = v.as_container(); cfg.clear(); if (luaW_toconfig(L, n, cfg)) { return true; } // no break } default: default_explicit: return luaW_type_error(L, n, "WML table or scalar") != 0; } } catch (const invalid_variablename_exception&) { WRN_LUA << v.get_error_message() << " when attempting to write a '" << lua_typename(L, variabletype) << "'\n"; return false; } }
bool luaW_toconfig(lua_State *L, int index, config &cfg) { if (!lua_checkstack(L, LUA_MINSTACK)) return false; // Get the absolute index of the table. index = lua_absindex(L, index); int initial_top = lua_gettop(L); switch (lua_type(L, index)) { case LUA_TTABLE: break; case LUA_TUSERDATA: { if (vconfig * ptr = static_cast<vconfig *> (luaL_testudata(L, index, vconfigKey))) { cfg = ptr->get_parsed_config(); return true; } else { return false; } } case LUA_TNONE: case LUA_TNIL: return true; default: return false; } // First convert the children (integer indices). for (int i = 1, i_end = lua_rawlen(L, index); i <= i_end; ++i) { lua_rawgeti(L, index, i); if (!lua_istable(L, -1)) return_misformed(); lua_rawgeti(L, -1, 1); char const *m = lua_tostring(L, -1); if (!m) return_misformed(); lua_rawgeti(L, -2, 2); if (!luaW_toconfig(L, -1, cfg.add_child(m))) return_misformed(); lua_pop(L, 3); } // Then convert the attributes (string indices). for (lua_pushnil(L); lua_next(L, index); lua_pop(L, 1)) { if (lua_isnumber(L, -2)) continue; if (!lua_isstring(L, -2)) return_misformed(); config::attribute_value &v = cfg[lua_tostring(L, -2)]; if (lua_istable(L, -1)) { int subindex = lua_absindex(L, -1); std::ostringstream str; for (int i = 1, i_end = lua_rawlen(L, subindex); i <= i_end; ++i, lua_pop(L, 1)) { lua_rawgeti(L, -1, i); config::attribute_value item; if (!luaW_toscalar(L, -1, item)) return_misformed(); if (i > 1) str << ','; str << item; } // If there are any string keys, it's misformed for (lua_pushnil(L); lua_next(L, subindex); lua_pop(L, 1)) { if (!lua_isnumber(L, -2)) return_misformed(); } v = str.str(); } else if (!luaW_toscalar(L, -1, v)) return_misformed(); } lua_settop(L, initial_top); return true; }
inline boost::shared_ptr<config> lua_object<config>::to_type(lua_State *L, int n) { boost::shared_ptr<config> cfg = boost::shared_ptr<config>(new config()); luaW_toconfig(L, n, *cfg); return cfg; }