[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 some console commands. |
---|
| 32 | */ |
---|
| 33 | |
---|
[1505] | 34 | #include "ConsoleCommandCompilation.h" |
---|
[3196] | 35 | |
---|
| 36 | #include <fstream> |
---|
| 37 | #include <set> |
---|
| 38 | #include <string> |
---|
| 39 | |
---|
[8858] | 40 | #include "util/Output.h" |
---|
[1505] | 41 | #include "util/ExprParser.h" |
---|
[6417] | 42 | #include "util/StringUtils.h" |
---|
[10624] | 43 | #include "ConsoleCommandIncludes.h" |
---|
[7219] | 44 | #include "CommandExecutor.h" |
---|
[1505] | 45 | |
---|
| 46 | namespace orxonox |
---|
| 47 | { |
---|
[7236] | 48 | SetConsoleCommand("echo", echo); |
---|
[1505] | 49 | |
---|
[8858] | 50 | SetConsoleCommand("orxout", orxout_level); |
---|
| 51 | SetConsoleCommand("orxout_context", orxout_level_context); |
---|
| 52 | |
---|
| 53 | SetConsoleCommand("log" , log ); |
---|
| 54 | SetConsoleCommand("error" , error ).hide(); |
---|
| 55 | SetConsoleCommand("warning", warning).hide(); |
---|
| 56 | SetConsoleCommand("status" , status ).hide(); |
---|
| 57 | SetConsoleCommand("info" , info ).hide(); |
---|
| 58 | SetConsoleCommand("debug" , debug ).hide(); |
---|
| 59 | |
---|
| 60 | // SetConsoleCommand("source", source).argumentCompleter(0, autocompletion::files()); // disabled because we use the implementation in Tcl |
---|
[8079] | 61 | // SetConsoleCommand("read", read).argumentCompleter(0, autocompletion::files()); // disabled because we use the implementation in Tcl |
---|
| 62 | // SetConsoleCommand("append", append).argumentCompleter(0, autocompletion::files()); // disabled because we use the implementation in Tcl |
---|
| 63 | // SetConsoleCommand("write", write).argumentCompleter(0, autocompletion::files()); // disabled because we use the implementation in Tcl |
---|
[1505] | 64 | |
---|
[7236] | 65 | SetConsoleCommand("calculate", calculate); |
---|
[1505] | 66 | |
---|
[7401] | 67 | /** |
---|
[8858] | 68 | @brief Simply returns the arguments. |
---|
| 69 | */ |
---|
| 70 | std::string echo(const std::string& text) |
---|
| 71 | { |
---|
| 72 | return text; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | /** |
---|
| 76 | @brief Builds a map that maps the levels of all output levels to their ID. |
---|
| 77 | */ |
---|
| 78 | std::map<std::string, OutputLevel> getOutputLevelsMap() |
---|
| 79 | { |
---|
| 80 | std::map<std::string, OutputLevel> levels; |
---|
| 81 | |
---|
| 82 | levels["message"] = level::message; |
---|
| 83 | levels["debug_output"] = level::debug_output; |
---|
| 84 | levels["user_error"] = level::user_error; |
---|
| 85 | levels["user_warning"] = level::user_warning; |
---|
| 86 | levels["user_status"] = level::user_status; |
---|
| 87 | levels["user_info"] = level::user_info; |
---|
| 88 | levels["internal_error"] = level::internal_error; |
---|
| 89 | levels["internal_warning"] = level::internal_warning; |
---|
| 90 | levels["internal_status"] = level::internal_status; |
---|
| 91 | levels["internal_info"] = level::internal_info; |
---|
| 92 | levels["verbose"] = level::verbose; |
---|
| 93 | levels["verbose_more"] = level::verbose_more; |
---|
| 94 | levels["verbose_ultra"] = level::verbose_ultra; |
---|
| 95 | |
---|
| 96 | return levels; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /** |
---|
| 100 | @brief Prints text to the console. |
---|
| 101 | @param level_name The name of the output level |
---|
| 102 | */ |
---|
| 103 | void orxout_level(const std::string& level_name, const std::string& text) |
---|
| 104 | { |
---|
| 105 | static std::map<std::string, OutputLevel> levels = getOutputLevelsMap(); |
---|
| 106 | |
---|
| 107 | OutputLevel level = level::debug_output; |
---|
| 108 | std::map<std::string, OutputLevel>::iterator it = levels.find(level_name); |
---|
| 109 | if (it != levels.end()) |
---|
| 110 | level = it->second; |
---|
| 111 | else |
---|
| 112 | orxout(internal_warning) << "'" << level_name << "' is not a valid output level" << endl; |
---|
| 113 | |
---|
| 114 | orxout(level) << text << endl; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | /** |
---|
| 118 | @brief Prints text to the console. |
---|
| 119 | @param level_name The name of the output level |
---|
| 120 | @param context_name The name of the output context |
---|
| 121 | */ |
---|
| 122 | void orxout_level_context(const std::string& level_name, const std::string& context_name, const std::string& text) |
---|
| 123 | { |
---|
| 124 | static std::map<std::string, OutputLevel> levels = getOutputLevelsMap(); |
---|
| 125 | |
---|
| 126 | OutputLevel level = level::debug_output; |
---|
| 127 | std::map<std::string, OutputLevel>::iterator it = levels.find(level_name); |
---|
| 128 | if (it != levels.end()) |
---|
| 129 | level = it->second; |
---|
| 130 | else |
---|
| 131 | orxout(internal_warning) << "'" << level_name << "' is not a valid output level" << endl; |
---|
| 132 | |
---|
| 133 | OutputContextContainer context = registerContext(context_name); |
---|
| 134 | |
---|
| 135 | orxout(level, context) << text << endl; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | /// @brief Prints text to the console and the logfile. |
---|
| 139 | void log(const std::string& text) |
---|
| 140 | { orxout() << text << endl; } |
---|
| 141 | |
---|
| 142 | /// @brief Prints output with error level. |
---|
| 143 | void error(const std::string& text) |
---|
| 144 | { orxout(user_error) << text << endl; } |
---|
| 145 | |
---|
| 146 | /// @brief Prints output with warning level. |
---|
| 147 | void warning(const std::string& text) |
---|
| 148 | { orxout(user_warning) << text << endl; } |
---|
| 149 | |
---|
| 150 | /// @brief Prints output with status level. |
---|
| 151 | void status(const std::string& text) |
---|
| 152 | { orxout(user_status) << text << endl; } |
---|
| 153 | |
---|
| 154 | /// @brief Prints output with info level. |
---|
| 155 | void info(const std::string& text) |
---|
| 156 | { orxout(user_info) << text << endl; } |
---|
| 157 | |
---|
| 158 | /// @brief Prints debug output with verbose level. |
---|
| 159 | void debug(const std::string& text) |
---|
| 160 | { orxout(verbose, context::tcl) << text << endl; } |
---|
| 161 | |
---|
| 162 | /** |
---|
[7401] | 163 | @brief Reads the content of a file and executes the commands in it line by line. |
---|
| 164 | */ |
---|
[1505] | 165 | void source(const std::string& filename) |
---|
| 166 | { |
---|
| 167 | static std::set<std::string> executingFiles; |
---|
| 168 | |
---|
| 169 | std::set<std::string>::const_iterator it = executingFiles.find(filename); |
---|
| 170 | if (it != executingFiles.end()) |
---|
| 171 | { |
---|
[8858] | 172 | orxout(user_error) << "Recurring source command in \"" << filename << "\". Stopped execution." << endl; |
---|
[1505] | 173 | return; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | // Open the file |
---|
| 177 | std::ifstream file; |
---|
| 178 | file.open(filename.c_str(), std::fstream::in); |
---|
| 179 | |
---|
| 180 | if (!file.is_open()) |
---|
| 181 | { |
---|
[8858] | 182 | orxout(user_error) << "Couldn't open file \"" << filename << "\"." << endl; |
---|
[1505] | 183 | return; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | executingFiles.insert(filename); |
---|
| 187 | |
---|
| 188 | // Iterate through the file and put the lines into the CommandExecutor |
---|
| 189 | while (file.good() && !file.eof()) |
---|
| 190 | { |
---|
[3198] | 191 | std::string line; |
---|
| 192 | std::getline(file, line); |
---|
[1505] | 193 | CommandExecutor::execute(line); |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | executingFiles.erase(filename); |
---|
| 197 | file.close(); |
---|
| 198 | } |
---|
| 199 | |
---|
[7401] | 200 | /** |
---|
| 201 | @brief Writes text to a file. |
---|
| 202 | */ |
---|
[1505] | 203 | void write(const std::string& filename, const std::string& text) |
---|
| 204 | { |
---|
| 205 | std::ofstream file; |
---|
| 206 | file.open(filename.c_str(), std::fstream::out); |
---|
| 207 | |
---|
| 208 | if (!file.is_open()) |
---|
| 209 | { |
---|
[8858] | 210 | orxout(user_error) << "Couldn't write to file \"" << filename << "\"." << endl; |
---|
[1505] | 211 | return; |
---|
| 212 | } |
---|
| 213 | |
---|
[8858] | 214 | file << text << endl; |
---|
[1505] | 215 | file.close(); |
---|
| 216 | } |
---|
| 217 | |
---|
[7401] | 218 | /** |
---|
| 219 | @brief Appends text to a file. |
---|
| 220 | */ |
---|
[1505] | 221 | void append(const std::string& filename, const std::string& text) |
---|
| 222 | { |
---|
| 223 | std::ofstream file; |
---|
| 224 | file.open(filename.c_str(), std::fstream::app); |
---|
| 225 | |
---|
| 226 | if (!file.is_open()) |
---|
| 227 | { |
---|
[8858] | 228 | orxout(user_error) << "Couldn't append to file \"" << filename << "\"." << endl; |
---|
[1505] | 229 | return; |
---|
| 230 | } |
---|
| 231 | |
---|
[8858] | 232 | file << text << endl; |
---|
[1505] | 233 | file.close(); |
---|
| 234 | } |
---|
| 235 | |
---|
[7401] | 236 | /** |
---|
| 237 | @brief Reads text from a file |
---|
| 238 | */ |
---|
[1505] | 239 | std::string read(const std::string& filename) |
---|
| 240 | { |
---|
| 241 | std::ifstream file; |
---|
| 242 | file.open(filename.c_str(), std::fstream::in); |
---|
| 243 | |
---|
| 244 | if (!file.is_open()) |
---|
| 245 | { |
---|
[8858] | 246 | orxout(user_error) << "Couldn't read from file \"" << filename << "\"." << endl; |
---|
[1505] | 247 | return ""; |
---|
| 248 | } |
---|
| 249 | |
---|
[6417] | 250 | std::string output; |
---|
[1505] | 251 | while (file.good() && !file.eof()) |
---|
| 252 | { |
---|
[3198] | 253 | std::string line; |
---|
| 254 | std::getline(file, line); |
---|
[1505] | 255 | output += line; |
---|
| 256 | output += "\n"; |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | file.close(); |
---|
| 260 | |
---|
| 261 | return output; |
---|
| 262 | } |
---|
| 263 | |
---|
[7401] | 264 | /** |
---|
| 265 | @brief Parses the mathematical expression and returns the result. |
---|
| 266 | */ |
---|
[1505] | 267 | float calculate(const std::string& calculation) |
---|
| 268 | { |
---|
[6417] | 269 | ExprParser expr; |
---|
| 270 | expr.parse(calculation); |
---|
[1505] | 271 | if (expr.getSuccess()) |
---|
| 272 | { |
---|
| 273 | if (expr.getResult() == 42.0) |
---|
| 274 | { |
---|
[8858] | 275 | orxout(user_info) << "Greetings from the restaurant at the end of the universe." << endl; |
---|
[1505] | 276 | } |
---|
[6417] | 277 | if (!expr.getRemains().empty()) |
---|
[1505] | 278 | { |
---|
[8858] | 279 | orxout(user_warning) << "Expression could not be parsed to the end! Remains: '" << expr.getRemains() << '\'' << endl; |
---|
[1505] | 280 | } |
---|
[3196] | 281 | return static_cast<float>(expr.getResult()); |
---|
[1505] | 282 | } |
---|
| 283 | else |
---|
| 284 | { |
---|
[8858] | 285 | orxout(user_error) << "Cannot calculate expression: Parse error." << endl; |
---|
[1505] | 286 | return 0; |
---|
| 287 | } |
---|
| 288 | } |
---|
| 289 | } |
---|