[1505] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[2710] | 29 | #include "ArgumentCompletionFunctions.h" |
---|
| 30 | |
---|
[1505] | 31 | #include <map> |
---|
[2728] | 32 | #include <boost/version.hpp> |
---|
[2087] | 33 | #include <boost/filesystem.hpp> |
---|
[1505] | 34 | |
---|
[3196] | 35 | #include "util/Convert.h" |
---|
[3280] | 36 | #include "util/StringUtils.h" |
---|
[7203] | 37 | #include "core/Identifier.h" |
---|
| 38 | #include "core/ConfigFileManager.h" |
---|
| 39 | #include "core/ConfigValueContainer.h" |
---|
[7233] | 40 | #include "CommandExecutor.h" |
---|
| 41 | #include "ConsoleCommand.h" |
---|
[1505] | 42 | #include "TclThreadManager.h" |
---|
| 43 | |
---|
[2728] | 44 | // Boost 1.36 has some issues with deprecated functions that have been omitted |
---|
| 45 | #if (BOOST_VERSION == 103600) |
---|
| 46 | # define BOOST_LEAF_FUNCTION filename |
---|
| 47 | #else |
---|
| 48 | # define BOOST_LEAF_FUNCTION leaf |
---|
| 49 | #endif |
---|
| 50 | |
---|
[1505] | 51 | namespace orxonox |
---|
| 52 | { |
---|
| 53 | namespace autocompletion |
---|
| 54 | { |
---|
| 55 | ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(fallback)() |
---|
| 56 | { |
---|
| 57 | return ArgumentCompletionList(); |
---|
| 58 | } |
---|
| 59 | |
---|
[7233] | 60 | namespace detail |
---|
[7232] | 61 | { |
---|
[7236] | 62 | bool groupIsVisible(const std::map<std::string, ConsoleCommand*>& group, bool bOnlyShowHidden) |
---|
[7233] | 63 | { |
---|
[7236] | 64 | for (std::map<std::string, ConsoleCommand*>::const_iterator it_command = group.begin(); it_command != group.end(); ++it_command) |
---|
[7233] | 65 | if (it_command->second->isActive() && it_command->second->hasAccess() && (!it_command->second->isHidden())^bOnlyShowHidden) |
---|
| 66 | return true; |
---|
[7232] | 67 | |
---|
[7233] | 68 | return false; |
---|
| 69 | } |
---|
[7232] | 70 | |
---|
[7235] | 71 | ArgumentCompletionList _groupsandcommands(const std::string& fragment, bool bOnlyShowHidden) |
---|
[7233] | 72 | { |
---|
| 73 | ArgumentCompletionList groupList; |
---|
[7235] | 74 | std::string fragmentLC = getLowercase(fragment); |
---|
[7228] | 75 | |
---|
[7236] | 76 | const std::map<std::string, std::map<std::string, ConsoleCommand*> >& commands = ConsoleCommand::getCommands(); |
---|
| 77 | for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = commands.begin(); it_group != commands.end(); ++it_group) |
---|
[7235] | 78 | if (groupIsVisible(it_group->second, bOnlyShowHidden) && it_group->first != "" && (fragmentLC == "" || getLowercase(it_group->first).find_first_of(fragmentLC) == 0)) |
---|
[7233] | 79 | groupList.push_back(ArgumentCompletionListElement(it_group->first, getLowercase(it_group->first))); |
---|
[7228] | 80 | |
---|
[7236] | 81 | std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = commands.find(""); |
---|
[7233] | 82 | if (it_group != commands.end()) |
---|
| 83 | { |
---|
[7235] | 84 | if (!groupList.empty()) |
---|
| 85 | groupList.push_back(ArgumentCompletionListElement("", "", "\n")); |
---|
[7233] | 86 | |
---|
[7236] | 87 | for (std::map<std::string, ConsoleCommand*>::const_iterator it_command = it_group->second.begin(); it_command != it_group->second.end(); ++it_command) |
---|
[7235] | 88 | if (it_command->second->isActive() && it_command->second->hasAccess() && (!it_command->second->isHidden())^bOnlyShowHidden && (fragmentLC == "" || getLowercase(it_command->first).find_first_of(fragmentLC) == 0)) |
---|
[7233] | 89 | groupList.push_back(ArgumentCompletionListElement(it_command->first, getLowercase(it_command->first))); |
---|
| 90 | } |
---|
| 91 | |
---|
[7235] | 92 | if (!groupList.empty() && groupList.back().getDisplay() == "\n") |
---|
| 93 | groupList.pop_back(); |
---|
| 94 | |
---|
[7233] | 95 | return groupList; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | ArgumentCompletionList _subcommands(const std::string& fragment, const std::string& group, bool bOnlyShowHidden) |
---|
[7228] | 99 | { |
---|
[7233] | 100 | ArgumentCompletionList commandList; |
---|
[7228] | 101 | |
---|
[7233] | 102 | std::string groupLC = getLowercase(group); |
---|
| 103 | |
---|
[7236] | 104 | std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommand::getCommands().begin(); |
---|
| 105 | for ( ; it_group != ConsoleCommand::getCommands().end(); ++it_group) |
---|
[7233] | 106 | if (getLowercase(it_group->first) == groupLC) |
---|
| 107 | break; |
---|
| 108 | |
---|
[7236] | 109 | if (it_group != ConsoleCommand::getCommands().end()) |
---|
[7233] | 110 | { |
---|
[7236] | 111 | for (std::map<std::string, ConsoleCommand*>::const_iterator it_command = it_group->second.begin(); it_command != it_group->second.end(); ++it_command) |
---|
[7233] | 112 | if (it_command->second->isActive() && it_command->second->hasAccess() && (!it_command->second->isHidden())^bOnlyShowHidden) |
---|
| 113 | commandList.push_back(ArgumentCompletionListElement(it_command->first, getLowercase(it_command->first))); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | return commandList; |
---|
[7228] | 117 | } |
---|
[7233] | 118 | } |
---|
[7228] | 119 | |
---|
[7235] | 120 | ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(groupsandcommands)(const std::string& fragment) |
---|
[7233] | 121 | { |
---|
[7235] | 122 | return detail::_groupsandcommands(fragment, false); |
---|
[7228] | 123 | } |
---|
| 124 | |
---|
| 125 | ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(subcommands)(const std::string& fragment, const std::string& group) |
---|
| 126 | { |
---|
[7233] | 127 | return detail::_subcommands(fragment, group, false); |
---|
| 128 | } |
---|
[7228] | 129 | |
---|
[7233] | 130 | ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION_MULTI(command)(const std::string& fragment) |
---|
| 131 | { |
---|
| 132 | CommandEvaluation evaluation = CommandExecutor::evaluate(fragment); |
---|
| 133 | const std::string& hint = evaluation.hint(); |
---|
| 134 | |
---|
[7235] | 135 | if (evaluation.getPossibleArguments().size() > 0 && evaluation.getPossibleArgumentsSize() > 0) |
---|
[7228] | 136 | { |
---|
[7233] | 137 | return evaluation.getPossibleArguments(); |
---|
[7228] | 138 | } |
---|
[7233] | 139 | else |
---|
| 140 | { |
---|
| 141 | ArgumentCompletionList list; |
---|
| 142 | list.push_back(ArgumentCompletionListElement("", "", hint)); |
---|
| 143 | return list; |
---|
| 144 | } |
---|
[7228] | 145 | } |
---|
| 146 | |
---|
[7234] | 147 | ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION_MULTI(hiddencommand)(const std::string& fragment) |
---|
| 148 | { |
---|
| 149 | SubString tokens(fragment, " ", SubString::WhiteSpaces, false, '\\', true, '"', true, '(', ')', true, '\0'); |
---|
| 150 | |
---|
| 151 | if (tokens.size() == 0) |
---|
[7235] | 152 | return detail::_groupsandcommands(fragment, true); |
---|
[7234] | 153 | |
---|
[7236] | 154 | if (ConsoleCommand::getCommandLC(getLowercase(tokens[0]))) |
---|
[7234] | 155 | return ARGUMENT_COMPLETION_FUNCTION_CALL(command)(fragment); |
---|
| 156 | |
---|
| 157 | if (tokens.size() == 1) |
---|
| 158 | { |
---|
[7236] | 159 | std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommand::getCommands().find(tokens[0]); |
---|
| 160 | if (it_group != ConsoleCommand::getCommands().end()) |
---|
[7234] | 161 | return detail::_subcommands(fragment, tokens[0], true); |
---|
| 162 | else |
---|
[7235] | 163 | return detail::_groupsandcommands(fragment, true); |
---|
[7234] | 164 | } |
---|
| 165 | |
---|
[7236] | 166 | if (ConsoleCommand::getCommandLC(getLowercase(tokens[0]), getLowercase(tokens[1]))) |
---|
[7234] | 167 | return ARGUMENT_COMPLETION_FUNCTION_CALL(command)(fragment); |
---|
| 168 | |
---|
| 169 | return ArgumentCompletionList(); |
---|
| 170 | } |
---|
| 171 | |
---|
[1505] | 172 | ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(files)(const std::string& fragment) |
---|
| 173 | { |
---|
| 174 | ArgumentCompletionList dirlist; |
---|
| 175 | ArgumentCompletionList filelist; |
---|
| 176 | |
---|
| 177 | try |
---|
| 178 | { |
---|
| 179 | boost::filesystem::path input(fragment); |
---|
| 180 | boost::filesystem::path startdirectory(input.branch_path()); |
---|
| 181 | |
---|
| 182 | if (!boost::filesystem::exists(startdirectory)) |
---|
| 183 | { |
---|
| 184 | startdirectory = "."; |
---|
| 185 | } |
---|
[2710] | 186 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
[1505] | 187 | else |
---|
| 188 | { |
---|
[6417] | 189 | const std::string& dir = startdirectory.string(); |
---|
[1505] | 190 | if (dir.size() > 0 && dir[dir.size() - 1] == ':') |
---|
[2759] | 191 | startdirectory = dir + '/'; |
---|
[1505] | 192 | } |
---|
| 193 | #endif |
---|
| 194 | |
---|
| 195 | boost::filesystem::directory_iterator file(startdirectory); |
---|
| 196 | boost::filesystem::directory_iterator end; |
---|
| 197 | |
---|
| 198 | while (file != end) |
---|
| 199 | { |
---|
| 200 | if (boost::filesystem::is_directory(*file)) |
---|
[6417] | 201 | dirlist.push_back(ArgumentCompletionListElement(file->string() + '/', getLowercase(file->string()) + '/', file->BOOST_LEAF_FUNCTION() + '/')); |
---|
[1505] | 202 | else |
---|
[6417] | 203 | filelist.push_back(ArgumentCompletionListElement(file->string(), getLowercase(file->string()), file->BOOST_LEAF_FUNCTION())); |
---|
[1505] | 204 | ++file; |
---|
| 205 | } |
---|
| 206 | } |
---|
| 207 | catch (...) {} |
---|
| 208 | |
---|
| 209 | filelist.insert(filelist.begin(), dirlist.begin(), dirlist.end()); |
---|
| 210 | return filelist; |
---|
| 211 | } |
---|
| 212 | |
---|
[6536] | 213 | ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(settingssections)() |
---|
[1505] | 214 | { |
---|
[6536] | 215 | ArgumentCompletionList sectionList; |
---|
[1505] | 216 | |
---|
[6536] | 217 | const std::set<std::string>& names = SettingsConfigFile::getInstance().getSectionNames(); |
---|
| 218 | for (std::set<std::string>::const_iterator it = names.begin(); it != names.end(); ++it) |
---|
| 219 | sectionList.push_back(ArgumentCompletionListElement(*it, getLowercase(*it))); |
---|
[1505] | 220 | |
---|
[6536] | 221 | return sectionList; |
---|
[1505] | 222 | } |
---|
| 223 | |
---|
[6536] | 224 | ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(settingsentries)(const std::string& fragment, const std::string& section) |
---|
[1505] | 225 | { |
---|
[6536] | 226 | ArgumentCompletionList entryList; |
---|
| 227 | SettingsConfigFile& settings = SettingsConfigFile::getInstance(); |
---|
| 228 | const std::string& sectionLC = getLowercase(section); |
---|
[1505] | 229 | |
---|
[6536] | 230 | SettingsConfigFile::ContainerMap::const_iterator upper = settings.getContainerUpperBound(sectionLC); |
---|
| 231 | for (SettingsConfigFile::ContainerMap::const_iterator it = settings.getContainerLowerBound(sectionLC); it != upper; ++it) |
---|
| 232 | entryList.push_back(ArgumentCompletionListElement(it->second.second->getName(), it->second.first)); |
---|
[1505] | 233 | |
---|
[6536] | 234 | return entryList; |
---|
[1505] | 235 | } |
---|
| 236 | |
---|
[6536] | 237 | ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(settingsvalue)(const std::string& fragment, const std::string& entry, const std::string& section) |
---|
[1505] | 238 | { |
---|
[6536] | 239 | ArgumentCompletionList oldValue; |
---|
| 240 | SettingsConfigFile& settings = SettingsConfigFile::getInstance(); |
---|
| 241 | const std::string& sectionLC = getLowercase(section); |
---|
| 242 | const std::string& entryLC = getLowercase(entry); |
---|
| 243 | |
---|
| 244 | SettingsConfigFile::ContainerMap::const_iterator upper = settings.getContainerUpperBound(sectionLC); |
---|
| 245 | for (SettingsConfigFile::ContainerMap::const_iterator it = settings.getContainerLowerBound(sectionLC); it != upper; ++it) |
---|
[1505] | 246 | { |
---|
[6536] | 247 | if (it->second.first == entryLC) |
---|
[1505] | 248 | { |
---|
[6536] | 249 | const std::string& valuestring = it->second.second->toString(); |
---|
| 250 | oldValue.push_back(ArgumentCompletionListElement(valuestring, getLowercase(valuestring), "Old value: " + valuestring)); |
---|
[1505] | 251 | } |
---|
| 252 | } |
---|
[6536] | 253 | |
---|
| 254 | return oldValue; |
---|
[1505] | 255 | } |
---|
| 256 | |
---|
| 257 | ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(tclthreads)() |
---|
| 258 | { |
---|
| 259 | std::list<unsigned int> threadnumbers = TclThreadManager::getInstance().getThreadList(); |
---|
| 260 | ArgumentCompletionList threads; |
---|
| 261 | |
---|
| 262 | for (std::list<unsigned int>::const_iterator it = threadnumbers.begin(); it != threadnumbers.end(); ++it) |
---|
[3280] | 263 | threads.push_back(ArgumentCompletionListElement(multi_cast<std::string>(*it))); |
---|
[1505] | 264 | |
---|
| 265 | return threads; |
---|
| 266 | } |
---|
| 267 | } |
---|
| 268 | } |
---|