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 | |
---|
29 | #include "ArgumentCompletionFunctions.h" |
---|
30 | |
---|
31 | #include <map> |
---|
32 | #include <boost/version.hpp> |
---|
33 | #include <boost/filesystem.hpp> |
---|
34 | |
---|
35 | #include "util/Convert.h" |
---|
36 | #include "util/StringUtils.h" |
---|
37 | #include "core/Identifier.h" |
---|
38 | #include "core/ConfigFileManager.h" |
---|
39 | #include "core/ConfigValueContainer.h" |
---|
40 | #include "TclThreadManager.h" |
---|
41 | #include "ConsoleCommand.h" |
---|
42 | |
---|
43 | // Boost 1.36 has some issues with deprecated functions that have been omitted |
---|
44 | #if (BOOST_VERSION == 103600) |
---|
45 | # define BOOST_LEAF_FUNCTION filename |
---|
46 | #else |
---|
47 | # define BOOST_LEAF_FUNCTION leaf |
---|
48 | #endif |
---|
49 | |
---|
50 | namespace orxonox |
---|
51 | { |
---|
52 | namespace autocompletion |
---|
53 | { |
---|
54 | ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(fallback)() |
---|
55 | { |
---|
56 | return ArgumentCompletionList(); |
---|
57 | } |
---|
58 | |
---|
59 | ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(groupsandcommands)() |
---|
60 | { |
---|
61 | ArgumentCompletionList groupList; |
---|
62 | |
---|
63 | const std::map<std::string, std::map<std::string, _ConsoleCommand*> >& commands = _ConsoleCommand::getCommands(); |
---|
64 | for (std::map<std::string, std::map<std::string, _ConsoleCommand*> >::const_iterator it_group = commands.begin(); it_group != commands.end(); ++it_group) |
---|
65 | // todo: check if active / not hidden / not denied |
---|
66 | groupList.push_back(ArgumentCompletionListElement(it_group->first, getLowercase(it_group->first))); |
---|
67 | |
---|
68 | std::map<std::string, std::map<std::string, _ConsoleCommand*> >::const_iterator it_group = commands.find(""); |
---|
69 | if (it_group != commands.end()) |
---|
70 | { |
---|
71 | groupList.push_back(ArgumentCompletionListElement("\n")); |
---|
72 | |
---|
73 | for (std::map<std::string, _ConsoleCommand*>::const_iterator it_command = it_group->second.begin(); it_command != it_group->second.end(); ++it_command) |
---|
74 | // todo: check if active / not hidden / not denied |
---|
75 | groupList.push_back(ArgumentCompletionListElement(it_command->first, getLowercase(it_command->first))); |
---|
76 | } |
---|
77 | |
---|
78 | return groupList; |
---|
79 | } |
---|
80 | |
---|
81 | ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(subcommands)(const std::string& fragment, const std::string& group) |
---|
82 | { |
---|
83 | ArgumentCompletionList commandList; |
---|
84 | |
---|
85 | std::string groupLC = getLowercase(group); |
---|
86 | |
---|
87 | std::map<std::string, std::map<std::string, _ConsoleCommand*> >::const_iterator it_group = _ConsoleCommand::getCommands().begin(); |
---|
88 | for ( ; it_group != _ConsoleCommand::getCommands().end(); ++it_group) |
---|
89 | if (getLowercase(it_group->first) == groupLC) |
---|
90 | break; |
---|
91 | |
---|
92 | if (it_group != _ConsoleCommand::getCommands().end()) |
---|
93 | { |
---|
94 | for (std::map<std::string, _ConsoleCommand*>::const_iterator it_command = it_group->second.begin(); it_command != it_group->second.end(); ++it_command) |
---|
95 | // todo: check if active / not hidden / not denied |
---|
96 | commandList.push_back(ArgumentCompletionListElement(it_command->first, getLowercase(it_command->first))); |
---|
97 | } |
---|
98 | |
---|
99 | return commandList; |
---|
100 | } |
---|
101 | |
---|
102 | ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(files)(const std::string& fragment) |
---|
103 | { |
---|
104 | ArgumentCompletionList dirlist; |
---|
105 | ArgumentCompletionList filelist; |
---|
106 | |
---|
107 | try |
---|
108 | { |
---|
109 | boost::filesystem::path input(fragment); |
---|
110 | boost::filesystem::path startdirectory(input.branch_path()); |
---|
111 | |
---|
112 | if (!boost::filesystem::exists(startdirectory)) |
---|
113 | { |
---|
114 | startdirectory = "."; |
---|
115 | } |
---|
116 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
117 | else |
---|
118 | { |
---|
119 | const std::string& dir = startdirectory.string(); |
---|
120 | if (dir.size() > 0 && dir[dir.size() - 1] == ':') |
---|
121 | startdirectory = dir + '/'; |
---|
122 | } |
---|
123 | #endif |
---|
124 | |
---|
125 | boost::filesystem::directory_iterator file(startdirectory); |
---|
126 | boost::filesystem::directory_iterator end; |
---|
127 | |
---|
128 | while (file != end) |
---|
129 | { |
---|
130 | if (boost::filesystem::is_directory(*file)) |
---|
131 | dirlist.push_back(ArgumentCompletionListElement(file->string() + '/', getLowercase(file->string()) + '/', file->BOOST_LEAF_FUNCTION() + '/')); |
---|
132 | else |
---|
133 | filelist.push_back(ArgumentCompletionListElement(file->string(), getLowercase(file->string()), file->BOOST_LEAF_FUNCTION())); |
---|
134 | ++file; |
---|
135 | } |
---|
136 | } |
---|
137 | catch (...) {} |
---|
138 | |
---|
139 | filelist.insert(filelist.begin(), dirlist.begin(), dirlist.end()); |
---|
140 | return filelist; |
---|
141 | } |
---|
142 | |
---|
143 | ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(settingssections)() |
---|
144 | { |
---|
145 | ArgumentCompletionList sectionList; |
---|
146 | |
---|
147 | const std::set<std::string>& names = SettingsConfigFile::getInstance().getSectionNames(); |
---|
148 | for (std::set<std::string>::const_iterator it = names.begin(); it != names.end(); ++it) |
---|
149 | sectionList.push_back(ArgumentCompletionListElement(*it, getLowercase(*it))); |
---|
150 | |
---|
151 | return sectionList; |
---|
152 | } |
---|
153 | |
---|
154 | ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(settingsentries)(const std::string& fragment, const std::string& section) |
---|
155 | { |
---|
156 | ArgumentCompletionList entryList; |
---|
157 | SettingsConfigFile& settings = SettingsConfigFile::getInstance(); |
---|
158 | const std::string& sectionLC = getLowercase(section); |
---|
159 | |
---|
160 | SettingsConfigFile::ContainerMap::const_iterator upper = settings.getContainerUpperBound(sectionLC); |
---|
161 | for (SettingsConfigFile::ContainerMap::const_iterator it = settings.getContainerLowerBound(sectionLC); it != upper; ++it) |
---|
162 | entryList.push_back(ArgumentCompletionListElement(it->second.second->getName(), it->second.first)); |
---|
163 | |
---|
164 | return entryList; |
---|
165 | } |
---|
166 | |
---|
167 | ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(settingsvalue)(const std::string& fragment, const std::string& entry, const std::string& section) |
---|
168 | { |
---|
169 | ArgumentCompletionList oldValue; |
---|
170 | SettingsConfigFile& settings = SettingsConfigFile::getInstance(); |
---|
171 | const std::string& sectionLC = getLowercase(section); |
---|
172 | const std::string& entryLC = getLowercase(entry); |
---|
173 | |
---|
174 | SettingsConfigFile::ContainerMap::const_iterator upper = settings.getContainerUpperBound(sectionLC); |
---|
175 | for (SettingsConfigFile::ContainerMap::const_iterator it = settings.getContainerLowerBound(sectionLC); it != upper; ++it) |
---|
176 | { |
---|
177 | if (it->second.first == entryLC) |
---|
178 | { |
---|
179 | const std::string& valuestring = it->second.second->toString(); |
---|
180 | oldValue.push_back(ArgumentCompletionListElement(valuestring, getLowercase(valuestring), "Old value: " + valuestring)); |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | return oldValue; |
---|
185 | } |
---|
186 | |
---|
187 | ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(tclthreads)() |
---|
188 | { |
---|
189 | std::list<unsigned int> threadnumbers = TclThreadManager::getInstance().getThreadList(); |
---|
190 | ArgumentCompletionList threads; |
---|
191 | |
---|
192 | for (std::list<unsigned int>::const_iterator it = threadnumbers.begin(); it != threadnumbers.end(); ++it) |
---|
193 | threads.push_back(ArgumentCompletionListElement(multi_cast<std::string>(*it))); |
---|
194 | |
---|
195 | return threads; |
---|
196 | } |
---|
197 | } |
---|
198 | } |
---|