[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 | |
---|
[7401] | 29 | /** |
---|
| 30 | @file |
---|
| 31 | @brief Implementation of all argument completion functions |
---|
| 32 | */ |
---|
| 33 | |
---|
[2710] | 34 | #include "ArgumentCompletionFunctions.h" |
---|
| 35 | |
---|
[1505] | 36 | #include <map> |
---|
[2087] | 37 | #include <boost/filesystem.hpp> |
---|
[1505] | 38 | |
---|
[3196] | 39 | #include "util/Convert.h" |
---|
[3280] | 40 | #include "util/StringUtils.h" |
---|
[9667] | 41 | #include "core/class/Identifier.h" |
---|
| 42 | #include "core/config/SettingsConfigFile.h" |
---|
| 43 | #include "core/config/ConfigValueContainer.h" |
---|
[7233] | 44 | #include "CommandExecutor.h" |
---|
| 45 | #include "ConsoleCommand.h" |
---|
[10624] | 46 | #include "ConsoleCommandManager.h" |
---|
[1505] | 47 | #include "TclThreadManager.h" |
---|
| 48 | |
---|
[8351] | 49 | // Differentiate Boost Filesystem v2 and v3 |
---|
| 50 | #if (BOOST_FILESYSTEM_VERSION < 3) |
---|
| 51 | # define BF_LEAF leaf |
---|
| 52 | # define BF_GENERIC_STRING string |
---|
| 53 | # define BF_DICTIONARY_ENTRY_NAME string |
---|
[2728] | 54 | #else |
---|
[8351] | 55 | # define BF_LEAF path().filename().string |
---|
| 56 | # define BF_GENERIC_STRING generic_string |
---|
| 57 | # define BF_DICTIONARY_ENTRY_NAME path().string |
---|
[2728] | 58 | #endif |
---|
| 59 | |
---|
[1505] | 60 | namespace orxonox |
---|
| 61 | { |
---|
| 62 | namespace autocompletion |
---|
| 63 | { |
---|
[7401] | 64 | /** |
---|
| 65 | @brief Fallback implementation, returns an empty list. |
---|
| 66 | */ |
---|
[1505] | 67 | ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(fallback)() |
---|
| 68 | { |
---|
| 69 | return ArgumentCompletionList(); |
---|
| 70 | } |
---|
| 71 | |
---|
[7233] | 72 | namespace detail |
---|
[7232] | 73 | { |
---|
[7401] | 74 | /** |
---|
| 75 | @brief Returns true if a group of console commands is visible (i.e. if at least one command in this group is visible). |
---|
| 76 | */ |
---|
[7236] | 77 | bool groupIsVisible(const std::map<std::string, ConsoleCommand*>& group, bool bOnlyShowHidden) |
---|
[7233] | 78 | { |
---|
[11071] | 79 | for (const auto& mapEntry : group) |
---|
| 80 | if (mapEntry.second->isActive() && mapEntry.second->hasAccess() && (!mapEntry.second->isHidden())^bOnlyShowHidden) |
---|
[7233] | 81 | return true; |
---|
[7232] | 82 | |
---|
[7233] | 83 | return false; |
---|
| 84 | } |
---|
[7232] | 85 | |
---|
[7401] | 86 | /** |
---|
| 87 | @brief Returns a list of all console command groups AND all console command shortcuts. |
---|
| 88 | @param fragment The last argument |
---|
| 89 | @param bOnlyShowHidden If true, only hidden groups and commands are returned |
---|
| 90 | */ |
---|
[7235] | 91 | ArgumentCompletionList _groupsandcommands(const std::string& fragment, bool bOnlyShowHidden) |
---|
[7233] | 92 | { |
---|
[8858] | 93 | // note: this function returns only arguments that begin with "fragment", which wouldn't be necessary for the |
---|
[7401] | 94 | // auto-completion, but it's necessary to place the line-break "\n" between groups and commands |
---|
| 95 | // only if both groups AND commands are in the list. |
---|
| 96 | |
---|
[7233] | 97 | ArgumentCompletionList groupList; |
---|
[7235] | 98 | std::string fragmentLC = getLowercase(fragment); |
---|
[7228] | 99 | |
---|
[7401] | 100 | // get all the groups that are visible (except the shortcut group "") |
---|
[11071] | 101 | const std::map<std::string, std::map<std::string, ConsoleCommand*>>& commands = ConsoleCommandManager::getInstance().getCommands(); |
---|
| 102 | for (const auto& mapEntry : commands) |
---|
| 103 | if (groupIsVisible(mapEntry.second, bOnlyShowHidden) && mapEntry.first != "" && (fragmentLC == "" || getLowercase(mapEntry.first).find(fragmentLC) == 0)) |
---|
| 104 | groupList.emplace_back(mapEntry.first, getLowercase(mapEntry.first)); |
---|
[7228] | 105 | |
---|
[7401] | 106 | // now add all shortcuts (in group "") |
---|
[11071] | 107 | std::map<std::string, std::map<std::string, ConsoleCommand*>>::const_iterator it_group = commands.find(""); |
---|
[7233] | 108 | if (it_group != commands.end()) |
---|
| 109 | { |
---|
[7401] | 110 | // add a line-break if the list isn't empty |
---|
[7235] | 111 | if (!groupList.empty()) |
---|
[11071] | 112 | groupList.emplace_back("", "", "\n"); |
---|
[7233] | 113 | |
---|
[7401] | 114 | // add the shortcuts |
---|
[11071] | 115 | for (const auto& mapEntry : it_group->second) |
---|
| 116 | if (mapEntry.second->isActive() && mapEntry.second->hasAccess() && (!mapEntry.second->isHidden())^bOnlyShowHidden && (fragmentLC == "" || getLowercase(mapEntry.first).find(fragmentLC) == 0)) |
---|
| 117 | groupList.emplace_back(mapEntry.first, getLowercase(mapEntry.first)); |
---|
[7233] | 118 | } |
---|
| 119 | |
---|
[7401] | 120 | // if no shortcut was added, remove the line-break again |
---|
[7235] | 121 | if (!groupList.empty() && groupList.back().getDisplay() == "\n") |
---|
| 122 | groupList.pop_back(); |
---|
| 123 | |
---|
[7233] | 124 | return groupList; |
---|
| 125 | } |
---|
| 126 | |
---|
[7401] | 127 | /** |
---|
| 128 | @brief Returns a list of all console commands in a given group. |
---|
| 129 | @param fragment The last argument |
---|
| 130 | @param group The group's name |
---|
| 131 | @param bOnlyShowHidden If true, only hidden console commands are returned |
---|
| 132 | */ |
---|
[7233] | 133 | ArgumentCompletionList _subcommands(const std::string& fragment, const std::string& group, bool bOnlyShowHidden) |
---|
[7228] | 134 | { |
---|
[7233] | 135 | ArgumentCompletionList commandList; |
---|
[7228] | 136 | |
---|
[7233] | 137 | std::string groupLC = getLowercase(group); |
---|
| 138 | |
---|
[7401] | 139 | // find the iterator of the given group |
---|
[11071] | 140 | std::map<std::string, std::map<std::string, ConsoleCommand*>>::const_iterator it_group = ConsoleCommandManager::getInstance().getCommands().begin(); |
---|
[10624] | 141 | for ( ; it_group != ConsoleCommandManager::getInstance().getCommands().end(); ++it_group) |
---|
[7233] | 142 | if (getLowercase(it_group->first) == groupLC) |
---|
| 143 | break; |
---|
| 144 | |
---|
[7401] | 145 | // add all commands in the group to the list |
---|
[10624] | 146 | if (it_group != ConsoleCommandManager::getInstance().getCommands().end()) |
---|
[7233] | 147 | { |
---|
[11071] | 148 | for (const auto& mapEntry : it_group->second) |
---|
| 149 | if (mapEntry.second->isActive() && mapEntry.second->hasAccess() && (!mapEntry.second->isHidden())^bOnlyShowHidden) |
---|
| 150 | commandList.emplace_back(mapEntry.first, getLowercase(mapEntry.first)); |
---|
[7233] | 151 | } |
---|
| 152 | |
---|
| 153 | return commandList; |
---|
[7228] | 154 | } |
---|
[7233] | 155 | } |
---|
[7228] | 156 | |
---|
[7401] | 157 | /** |
---|
| 158 | @brief Returns a list of all console command groups AND all console command shortcuts. |
---|
| 159 | */ |
---|
[7235] | 160 | ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(groupsandcommands)(const std::string& fragment) |
---|
[7233] | 161 | { |
---|
[7235] | 162 | return detail::_groupsandcommands(fragment, false); |
---|
[7228] | 163 | } |
---|
| 164 | |
---|
[7401] | 165 | /** |
---|
| 166 | @brief Returns a list of all console commands in a given group. |
---|
| 167 | */ |
---|
[7228] | 168 | ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(subcommands)(const std::string& fragment, const std::string& group) |
---|
| 169 | { |
---|
[7233] | 170 | return detail::_subcommands(fragment, group, false); |
---|
| 171 | } |
---|
[7228] | 172 | |
---|
[7401] | 173 | /** |
---|
| 174 | @brief Returns a list of commands and groups and also supports auto-completion of the arguments of these commands. |
---|
| 175 | |
---|
| 176 | This is a multi-word function, because commands are composed of 1-2 words and additional arguments. |
---|
| 177 | */ |
---|
[7233] | 178 | ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION_MULTI(command)(const std::string& fragment) |
---|
| 179 | { |
---|
| 180 | CommandEvaluation evaluation = CommandExecutor::evaluate(fragment); |
---|
| 181 | const std::string& hint = evaluation.hint(); |
---|
| 182 | |
---|
[7235] | 183 | if (evaluation.getPossibleArguments().size() > 0 && evaluation.getPossibleArgumentsSize() > 0) |
---|
[7228] | 184 | { |
---|
[7233] | 185 | return evaluation.getPossibleArguments(); |
---|
[7228] | 186 | } |
---|
[7233] | 187 | else |
---|
| 188 | { |
---|
| 189 | ArgumentCompletionList list; |
---|
[11071] | 190 | list.emplace_back("", "", hint); |
---|
[7233] | 191 | return list; |
---|
| 192 | } |
---|
[7228] | 193 | } |
---|
| 194 | |
---|
[7401] | 195 | /** |
---|
| 196 | @brief Returns a list of hidden commands and groups and also supports auto-completion of the arguments of these commands. |
---|
| 197 | |
---|
| 198 | This is a multi-word function, because commands are composed of 1-2 words and additional arguments. |
---|
| 199 | |
---|
| 200 | This function makes commands visible that would usually be hidden. |
---|
| 201 | */ |
---|
[7234] | 202 | ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION_MULTI(hiddencommand)(const std::string& fragment) |
---|
| 203 | { |
---|
[7276] | 204 | SubString tokens(fragment, " ", SubString::WhiteSpaces, false, '\\', true, '"', true, '{', '}', true, '\0'); |
---|
[7234] | 205 | |
---|
| 206 | if (tokens.size() == 0) |
---|
[7235] | 207 | return detail::_groupsandcommands(fragment, true); |
---|
[7234] | 208 | |
---|
[10624] | 209 | if (ConsoleCommandManager::getInstance().getCommandLC(getLowercase(tokens[0]))) |
---|
[7234] | 210 | return ARGUMENT_COMPLETION_FUNCTION_CALL(command)(fragment); |
---|
| 211 | |
---|
| 212 | if (tokens.size() == 1) |
---|
| 213 | { |
---|
[11071] | 214 | std::map<std::string, std::map<std::string, ConsoleCommand*>>::const_iterator it_group = ConsoleCommandManager::getInstance().getCommands().find(tokens[0]); |
---|
[10624] | 215 | if (it_group != ConsoleCommandManager::getInstance().getCommands().end()) |
---|
[7234] | 216 | return detail::_subcommands(fragment, tokens[0], true); |
---|
| 217 | else |
---|
[7235] | 218 | return detail::_groupsandcommands(fragment, true); |
---|
[7234] | 219 | } |
---|
| 220 | |
---|
[10624] | 221 | if (ConsoleCommandManager::getInstance().getCommandLC(getLowercase(tokens[0]), getLowercase(tokens[1]))) |
---|
[7234] | 222 | return ARGUMENT_COMPLETION_FUNCTION_CALL(command)(fragment); |
---|
| 223 | |
---|
| 224 | return ArgumentCompletionList(); |
---|
| 225 | } |
---|
| 226 | |
---|
[7401] | 227 | /** |
---|
| 228 | @brief Returns possible files and directories and also supports files in arbitrary deeply nested subdirectories. |
---|
| 229 | |
---|
| 230 | This function returns files and directories in the given path. This allows to |
---|
| 231 | navigate iteratively through the file system. The first argument @a fragment |
---|
| 232 | is used to get the current path. |
---|
| 233 | */ |
---|
[1505] | 234 | ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(files)(const std::string& fragment) |
---|
| 235 | { |
---|
| 236 | ArgumentCompletionList dirlist; |
---|
| 237 | ArgumentCompletionList filelist; |
---|
| 238 | |
---|
| 239 | try |
---|
| 240 | { |
---|
| 241 | boost::filesystem::path input(fragment); |
---|
| 242 | boost::filesystem::path startdirectory(input.branch_path()); |
---|
| 243 | |
---|
| 244 | if (!boost::filesystem::exists(startdirectory)) |
---|
| 245 | { |
---|
| 246 | startdirectory = "."; |
---|
| 247 | } |
---|
[2710] | 248 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
[1505] | 249 | else |
---|
| 250 | { |
---|
[8351] | 251 | const std::string& dir = startdirectory.BF_GENERIC_STRING(); |
---|
[1505] | 252 | if (dir.size() > 0 && dir[dir.size() - 1] == ':') |
---|
[2759] | 253 | startdirectory = dir + '/'; |
---|
[1505] | 254 | } |
---|
| 255 | #endif |
---|
| 256 | |
---|
| 257 | boost::filesystem::directory_iterator file(startdirectory); |
---|
| 258 | boost::filesystem::directory_iterator end; |
---|
| 259 | |
---|
| 260 | while (file != end) |
---|
| 261 | { |
---|
| 262 | if (boost::filesystem::is_directory(*file)) |
---|
[11071] | 263 | dirlist.emplace_back(file->BF_DICTIONARY_ENTRY_NAME() + '/', getLowercase(file->BF_DICTIONARY_ENTRY_NAME()) + '/', file->BF_LEAF() + '/'); |
---|
[1505] | 264 | else |
---|
[11071] | 265 | filelist.emplace_back(file->BF_DICTIONARY_ENTRY_NAME(), getLowercase(file->BF_DICTIONARY_ENTRY_NAME()), file->BF_LEAF()); |
---|
[1505] | 266 | ++file; |
---|
| 267 | } |
---|
| 268 | } |
---|
| 269 | catch (...) {} |
---|
| 270 | |
---|
| 271 | filelist.insert(filelist.begin(), dirlist.begin(), dirlist.end()); |
---|
| 272 | return filelist; |
---|
| 273 | } |
---|
| 274 | |
---|
[7401] | 275 | /** |
---|
| 276 | @brief Returns the sections of the config file. |
---|
| 277 | */ |
---|
[6536] | 278 | ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(settingssections)() |
---|
[1505] | 279 | { |
---|
[6536] | 280 | ArgumentCompletionList sectionList; |
---|
[1505] | 281 | |
---|
[6536] | 282 | const std::set<std::string>& names = SettingsConfigFile::getInstance().getSectionNames(); |
---|
[11071] | 283 | for (const std::string& name : names) |
---|
| 284 | sectionList.emplace_back(name, getLowercase(name)); |
---|
[1505] | 285 | |
---|
[6536] | 286 | return sectionList; |
---|
[1505] | 287 | } |
---|
| 288 | |
---|
[7401] | 289 | /** |
---|
| 290 | @brief Returns the entries in a given section of the config file. |
---|
| 291 | */ |
---|
[6536] | 292 | ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(settingsentries)(const std::string& fragment, const std::string& section) |
---|
[1505] | 293 | { |
---|
[6536] | 294 | ArgumentCompletionList entryList; |
---|
| 295 | SettingsConfigFile& settings = SettingsConfigFile::getInstance(); |
---|
| 296 | const std::string& sectionLC = getLowercase(section); |
---|
[1505] | 297 | |
---|
[6536] | 298 | SettingsConfigFile::ContainerMap::const_iterator upper = settings.getContainerUpperBound(sectionLC); |
---|
| 299 | for (SettingsConfigFile::ContainerMap::const_iterator it = settings.getContainerLowerBound(sectionLC); it != upper; ++it) |
---|
[11071] | 300 | entryList.emplace_back(it->second.second->getName(), it->second.first); |
---|
[1505] | 301 | |
---|
[6536] | 302 | return entryList; |
---|
[1505] | 303 | } |
---|
| 304 | |
---|
[7401] | 305 | /** |
---|
| 306 | @brief Returns the current value of a given value in a given section of the config file. |
---|
| 307 | */ |
---|
[6536] | 308 | ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(settingsvalue)(const std::string& fragment, const std::string& entry, const std::string& section) |
---|
[1505] | 309 | { |
---|
[6536] | 310 | ArgumentCompletionList oldValue; |
---|
| 311 | SettingsConfigFile& settings = SettingsConfigFile::getInstance(); |
---|
| 312 | const std::string& sectionLC = getLowercase(section); |
---|
| 313 | const std::string& entryLC = getLowercase(entry); |
---|
| 314 | |
---|
| 315 | SettingsConfigFile::ContainerMap::const_iterator upper = settings.getContainerUpperBound(sectionLC); |
---|
| 316 | for (SettingsConfigFile::ContainerMap::const_iterator it = settings.getContainerLowerBound(sectionLC); it != upper; ++it) |
---|
[1505] | 317 | { |
---|
[6536] | 318 | if (it->second.first == entryLC) |
---|
[1505] | 319 | { |
---|
[6536] | 320 | const std::string& valuestring = it->second.second->toString(); |
---|
[11071] | 321 | oldValue.emplace_back(valuestring, getLowercase(valuestring), "Old value: " + valuestring); |
---|
[1505] | 322 | } |
---|
| 323 | } |
---|
[6536] | 324 | |
---|
| 325 | return oldValue; |
---|
[1505] | 326 | } |
---|
| 327 | |
---|
[7401] | 328 | /** |
---|
| 329 | @brief Returns a list of indexes of the available Tcl threads (see TclThreadManager). |
---|
| 330 | */ |
---|
[1505] | 331 | ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(tclthreads)() |
---|
| 332 | { |
---|
| 333 | std::list<unsigned int> threadnumbers = TclThreadManager::getInstance().getThreadList(); |
---|
| 334 | ArgumentCompletionList threads; |
---|
| 335 | |
---|
[11071] | 336 | for (unsigned int threadnumber : threadnumbers) |
---|
| 337 | threads.emplace_back(multi_cast<std::string>(threadnumber)); |
---|
[1505] | 338 | |
---|
| 339 | return threads; |
---|
| 340 | } |
---|
| 341 | } |
---|
| 342 | } |
---|