Command LuaUtils::ParseCommand(lua_State* L, const char* caller, int idIndex) { // cmdID if (!lua_isnumber(L, idIndex)) { luaL_error(L, "%s(): bad command ID", caller); } const int id = lua_toint(L, idIndex); Command cmd(id); // params const int paramTable = (idIndex + 1); if (!lua_istable(L, paramTable)) { luaL_error(L, "%s(): bad param table", caller); } for (lua_pushnil(L); lua_next(L, paramTable) != 0; lua_pop(L, 1)) { if (lua_israwnumber(L, -2)) { // avoid 'n' if (!lua_isnumber(L, -1)) { luaL_error(L, "%s(): bad param table entry", caller); } const float value = lua_tofloat(L, -1); cmd.PushParam(value); } } // options ParseCommandOptions(L, caller, (idIndex + 2), cmd); // XXX should do some sanity checking? return cmd; }
void LuaUtils::ParseCommandTable(lua_State* L, const char* caller, int table, Command& cmd) { // cmdID lua_rawgeti(L, table, 1); if (!lua_isnumber(L, -1)) { luaL_error(L, "%s(): bad command ID", caller); } cmd.id = lua_toint(L, -1); lua_pop(L, 1); // params lua_rawgeti(L, table, 2); if (!lua_istable(L, -1)) { luaL_error(L, "%s(): bad param table", caller); } const int paramTable = lua_gettop(L); for (lua_pushnil(L); lua_next(L, paramTable) != 0; lua_pop(L, 1)) { if (lua_israwnumber(L, -2)) { // avoid 'n' if (!lua_isnumber(L, -1)) { luaL_error(L, "%s(): bad param table entry", caller); } const float value = lua_tofloat(L, -1); cmd.params.push_back(value); } } lua_pop(L, 1); // options lua_rawgeti(L, table, 3); ParseCommandOptions(L, caller, lua_gettop(L), cmd); lua_pop(L, 1); // NOTE: should do some sanity checking? }