vector MissionSpec::getAllowedCommands()

in Malmo/src/MissionSpec.cpp [563:643]


    vector<string> MissionSpec::getAllowedCommands(int role,const string& command_handler) const
    {
        vector<string> allowed_commands;

        const boost::property_tree::ptree& m = mission.get_child("Mission");
        for (auto& e : m) {
            if (e.first != "AgentSection")
                continue;

            if (role-- == 0) {
                const auto& commands = e.second.get_child_optional("AgentHandlers." + command_handler + "Commands");
                if (commands == boost::none)
                    return allowed_commands;

                bool explicit_allow = false;
                // Collect all allowed verbs first and then remove any that are denied.
                for (auto& ml : commands.get()) {
                    if (ml.first == "ModifierList") {
                        const auto& t = ml.second.get_optional<std::string>("<xmlattr>.type");
                        if (t != boost::none && t.get() == "allow-list") {
                            explicit_allow = true;
                            for (auto& c : ml.second) {
                                if (c.first == "command") {
                                    allowed_commands.push_back(c.second.data());
                                }
                            }
                        }
                    }
                } 
                if (!explicit_allow) {
                    // Command defaulting.
                    if (command_handler == "ContinuousMovement") {
                        allowed_commands = all_continuous_movement_commands;
                    }
                    else if (command_handler == "AbsoluteMovement") {
                        allowed_commands = all_absolute_movement_commands;
                    }
                    else if (command_handler == "DiscreteMovement") {
                        allowed_commands = all_discrete_movement_commands;
                    }
                    else if (command_handler == "Inventory") {
                        allowed_commands = all_inventory_commands;
                    }
                    else if (command_handler == "SimpleCraft") {
                        allowed_commands = all_simplecraft_commands;
                    }
                    else if (command_handler == "NearbyCraft") {
                        allowed_commands = all_nearbycraft_commands;
                    }
                    else if (command_handler == "NearbySmelt") {
                        allowed_commands = all_nearbysmelt_commands;
                    }
                    else if (command_handler == "Chat") {
                        allowed_commands = all_chat_commands;
                    }
                    else if (command_handler == "MissionQuit") {
                        allowed_commands = all_mission_quit_commands;
                    }
                    else if (command_handler == "HumanLevel") {
                        allowed_commands = all_human_level_commands;
                    }
                    else
                        throw runtime_error("Unknown command handler");
                }
                for (auto& ml : commands.get()) {
                    if (ml.first == "ModifierList") {
                        const auto& t = ml.second.get_optional<std::string>("<xmlattr>.type");
                        if (t == boost::none || t.get() != "allow-list") {
                            for (auto& c : ml.second) {
                                if (c.first == "command") {
                                    allowed_commands.erase(std::remove(allowed_commands.begin(), allowed_commands.end(), c.second.data()), allowed_commands.end());
                                }
                            }
                        }
                    }
                }
            }
        }

        return allowed_commands;
    }