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 "ConsoleCommand.h" |
---|
30 | #include <cassert> |
---|
31 | |
---|
32 | namespace orxonox |
---|
33 | { |
---|
34 | ConsoleCommand::ConsoleCommand(Functor* functor, const std::string& name) : Executor(functor, name) |
---|
35 | { |
---|
36 | this->accessLevel_ = AccessLevel::None; |
---|
37 | this->argumentCompleter_[0] = 0; |
---|
38 | this->argumentCompleter_[1] = 0; |
---|
39 | this->argumentCompleter_[2] = 0; |
---|
40 | this->argumentCompleter_[3] = 0; |
---|
41 | this->argumentCompleter_[4] = 0; |
---|
42 | |
---|
43 | this->keybindMode_ = KeybindMode::OnPress; |
---|
44 | this->inputConfiguredParam_ = -1; |
---|
45 | } |
---|
46 | |
---|
47 | ConsoleCommand& ConsoleCommand::argumentCompleter(unsigned int param, ArgumentCompleter* completer) |
---|
48 | { |
---|
49 | if (param < 5) |
---|
50 | this->argumentCompleter_[param] = completer; |
---|
51 | else |
---|
52 | { |
---|
53 | COUT(2) << "Warning: Couldn't add autocompletion-function for param " << param << ": index out of bound." << std::endl; |
---|
54 | } |
---|
55 | return (*this); |
---|
56 | } |
---|
57 | |
---|
58 | ArgumentCompleter* ConsoleCommand::getArgumentCompleter(unsigned int param) const |
---|
59 | { |
---|
60 | if (param < 5) |
---|
61 | return this->argumentCompleter_[param]; |
---|
62 | else |
---|
63 | return 0; |
---|
64 | } |
---|
65 | |
---|
66 | void ConsoleCommand::createArgumentCompletionList(unsigned int param, const std::string& param1, const std::string& param2, const std::string& param3, const std::string& param4, const std::string& param5) |
---|
67 | { |
---|
68 | if (param < 5 && this->argumentCompleter_[param]) |
---|
69 | this->argumentList_ = (*this->argumentCompleter_[param])(param1, param2, param3, param4, param5); |
---|
70 | else |
---|
71 | this->argumentList_.clear(); |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | #include "BaseObject.h" // remove this |
---|
76 | |
---|
77 | namespace orxonox |
---|
78 | { |
---|
79 | _SetConsoleCommand("BaseObject", "setName", &BaseObject::setName, (BaseObject*)0); |
---|
80 | _ConsoleCommand::_ConsoleCommandManipulator test(_ModifyConsoleCommand("BaseObject", "setName").setFunction(&BaseObject::setActive)); |
---|
81 | |
---|
82 | _ConsoleCommand::_ConsoleCommand(const std::string& group, const std::string& name, Functor* functor, bool bInitialized) : Executor(functor, name), functionHeader_(functor->getHeaderIdentifier()) |
---|
83 | { |
---|
84 | this->bActive_ = true; |
---|
85 | this->bInitialized_ = bInitialized; |
---|
86 | _ConsoleCommand::registerCommand(group, name, this); |
---|
87 | } |
---|
88 | |
---|
89 | _ConsoleCommand& _ConsoleCommand::addShortcut() |
---|
90 | { |
---|
91 | _ConsoleCommand::registerCommand("", this->getName(), this); |
---|
92 | return *this; |
---|
93 | } |
---|
94 | |
---|
95 | _ConsoleCommand& _ConsoleCommand::addShortcut(const std::string& name) |
---|
96 | { |
---|
97 | _ConsoleCommand::registerCommand("", name, this); |
---|
98 | return *this; |
---|
99 | } |
---|
100 | |
---|
101 | _ConsoleCommand& _ConsoleCommand::addGroup(const std::string& group) |
---|
102 | { |
---|
103 | _ConsoleCommand::registerCommand(group, this->getName(), this); |
---|
104 | return *this; |
---|
105 | } |
---|
106 | |
---|
107 | _ConsoleCommand& _ConsoleCommand::addGroup(const std::string& group, const std::string& name) |
---|
108 | { |
---|
109 | _ConsoleCommand::registerCommand(group, name, this); |
---|
110 | return *this; |
---|
111 | } |
---|
112 | |
---|
113 | bool _ConsoleCommand::setFunctor(Functor* functor, bool bForce) |
---|
114 | { |
---|
115 | if (!functor) |
---|
116 | { |
---|
117 | this->bInitialized_ = false; |
---|
118 | return true; |
---|
119 | } |
---|
120 | |
---|
121 | if (!bForce && !this->functionHeaderMatches(functor)) |
---|
122 | { |
---|
123 | COUT(1) << "Error: Couldn't assign new function to console command with name \"" << this->getName() << "\", headers don't match." << std::endl; |
---|
124 | return false; |
---|
125 | } |
---|
126 | |
---|
127 | this->functor_ = functor; |
---|
128 | this->bInitialized_ = true; |
---|
129 | return true; |
---|
130 | } |
---|
131 | |
---|
132 | void _ConsoleCommand::pushFunctor(Functor* functor, bool bForce) |
---|
133 | { |
---|
134 | Functor* oldfunctor = this->getFunctor(); |
---|
135 | |
---|
136 | if (this->setFunctor(functor, bForce)); |
---|
137 | this->functorStack_.push(oldfunctor); |
---|
138 | } |
---|
139 | |
---|
140 | void _ConsoleCommand::popFunctor() |
---|
141 | { |
---|
142 | Functor* newfunctor = 0; |
---|
143 | if (!this->functorStack_.empty()) |
---|
144 | { |
---|
145 | newfunctor = this->functorStack_.top(); |
---|
146 | this->functorStack_.pop(); |
---|
147 | } |
---|
148 | this->setFunctor(newfunctor); |
---|
149 | } |
---|
150 | |
---|
151 | Functor* _ConsoleCommand::getFunctor() const |
---|
152 | { |
---|
153 | if (this->bInitialized_) |
---|
154 | return this->functor_; |
---|
155 | else |
---|
156 | return 0; |
---|
157 | } |
---|
158 | |
---|
159 | bool _ConsoleCommand::functionHeaderMatches(Functor* functor) const |
---|
160 | { |
---|
161 | if (!this->functor_) |
---|
162 | { |
---|
163 | assert(false); |
---|
164 | return true; |
---|
165 | } |
---|
166 | return (functor->getHeaderIdentifier() == this->functionHeader_); |
---|
167 | } |
---|
168 | |
---|
169 | void _ConsoleCommand::setObject(void* object) |
---|
170 | { |
---|
171 | if (this->functor_) |
---|
172 | this->functor_->setRawObjectPointer(object); |
---|
173 | else if (object) |
---|
174 | COUT(0) << "Error: Can't set object in console command \"" << this->getName() << "\", no functor set." << std::endl; |
---|
175 | } |
---|
176 | |
---|
177 | void _ConsoleCommand::pushObject(void* object) |
---|
178 | { |
---|
179 | if (this->functor_) |
---|
180 | { |
---|
181 | this->objectStack_.push(this->getObject()); |
---|
182 | this->setObject(object); |
---|
183 | } |
---|
184 | else |
---|
185 | COUT(0) << "Error: Can't set object in console command \"" << this->getName() << "\", no functor set." << std::endl; |
---|
186 | } |
---|
187 | |
---|
188 | void _ConsoleCommand::popObject() |
---|
189 | { |
---|
190 | void* newobject = 0; |
---|
191 | if (!this->objectStack_.empty()) |
---|
192 | { |
---|
193 | newobject = this->objectStack_.top(); |
---|
194 | this->objectStack_.pop(); |
---|
195 | } |
---|
196 | this->setObject(newobject); |
---|
197 | } |
---|
198 | |
---|
199 | void* _ConsoleCommand::getObject() const |
---|
200 | { |
---|
201 | if (this->functor_) |
---|
202 | return this->functor_->getRawObjectPointer(); |
---|
203 | else |
---|
204 | return 0; |
---|
205 | } |
---|
206 | |
---|
207 | /* static */ const _ConsoleCommand* _ConsoleCommand::getCommand(const std::string& group, const std::string& name, bool bPrintError) |
---|
208 | { |
---|
209 | std::map<std::string, std::map<std::string, _ConsoleCommand*> >::const_iterator it_group = _ConsoleCommand::getCommandMap().find(group); |
---|
210 | if (it_group != _ConsoleCommand::getCommandMap().end()) |
---|
211 | { |
---|
212 | std::map<std::string, _ConsoleCommand*>::const_iterator it_name = it_group->second.find(name); |
---|
213 | if (it_name != it_group->second.end()) |
---|
214 | { |
---|
215 | return it_name->second; |
---|
216 | } |
---|
217 | } |
---|
218 | if (bPrintError) |
---|
219 | { |
---|
220 | if (group == "") |
---|
221 | COUT(0) << "Error: Couldn't find console command with shortcut \"" << name << "\"" << std::endl; |
---|
222 | else |
---|
223 | COUT(0) << "Error: Couldn't find console command with group \"" << group << "\" and name \"" << name << "\"" << std::endl; |
---|
224 | } |
---|
225 | return 0; |
---|
226 | } |
---|
227 | |
---|
228 | /* static */ std::map<std::string, std::map<std::string, _ConsoleCommand*> >& _ConsoleCommand::getCommandMap() |
---|
229 | { |
---|
230 | static std::map<std::string, std::map<std::string, _ConsoleCommand*> > commandMap; |
---|
231 | return commandMap; |
---|
232 | } |
---|
233 | |
---|
234 | /* static */ void _ConsoleCommand::registerCommand(const std::string& group, const std::string& name, _ConsoleCommand* command) |
---|
235 | { |
---|
236 | if (name == "") |
---|
237 | return; |
---|
238 | |
---|
239 | if (_ConsoleCommand::getCommand(group, name) != 0) |
---|
240 | { |
---|
241 | if (group == "") |
---|
242 | COUT(2) << "Warning: A console command with shortcut name \"" << name << "\" already exists." << std::endl; |
---|
243 | else |
---|
244 | COUT(2) << "Warning: A console command with group \"" << group << "\" and name \"" << name << "\" already exists." << std::endl; |
---|
245 | } |
---|
246 | else |
---|
247 | { |
---|
248 | _ConsoleCommand::getCommandMap()[group][name] = command; |
---|
249 | } |
---|
250 | } |
---|
251 | } |
---|