[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 | |
---|
| 29 | #include "ConsoleCommandCompilation.h" |
---|
[3196] | 30 | |
---|
| 31 | #include <fstream> |
---|
| 32 | #include <set> |
---|
| 33 | #include <string> |
---|
| 34 | |
---|
[1747] | 35 | #include "util/Debug.h" |
---|
[1505] | 36 | #include "util/ExprParser.h" |
---|
[6417] | 37 | #include "util/StringUtils.h" |
---|
[3196] | 38 | #include "ConsoleCommand.h" |
---|
[7219] | 39 | #include "CommandExecutor.h" |
---|
[1505] | 40 | |
---|
| 41 | namespace orxonox |
---|
| 42 | { |
---|
[7236] | 43 | SetConsoleCommand("source", source).argumentCompleter(0, autocompletion::files()); |
---|
| 44 | SetConsoleCommand("echo", echo); |
---|
| 45 | SetConsoleCommand("puts", puts); |
---|
[1505] | 46 | |
---|
[7236] | 47 | SetConsoleCommand("read", read).argumentCompleter(0, autocompletion::files()); |
---|
| 48 | SetConsoleCommand("append", append).argumentCompleter(0, autocompletion::files()); |
---|
| 49 | SetConsoleCommand("write", write).argumentCompleter(0, autocompletion::files()); |
---|
[1505] | 50 | |
---|
[7236] | 51 | SetConsoleCommand("calculate", calculate); |
---|
[1505] | 52 | |
---|
| 53 | void source(const std::string& filename) |
---|
| 54 | { |
---|
| 55 | static std::set<std::string> executingFiles; |
---|
| 56 | |
---|
| 57 | std::set<std::string>::const_iterator it = executingFiles.find(filename); |
---|
| 58 | if (it != executingFiles.end()) |
---|
| 59 | { |
---|
| 60 | COUT(1) << "Error: Recurring source command in \"" << filename << "\". Stopped execution." << std::endl; |
---|
| 61 | return; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | // Open the file |
---|
| 65 | std::ifstream file; |
---|
| 66 | file.open(filename.c_str(), std::fstream::in); |
---|
| 67 | |
---|
| 68 | if (!file.is_open()) |
---|
| 69 | { |
---|
| 70 | COUT(1) << "Error: Couldn't open file \"" << filename << "\"." << std::endl; |
---|
| 71 | return; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | executingFiles.insert(filename); |
---|
| 75 | |
---|
| 76 | // Iterate through the file and put the lines into the CommandExecutor |
---|
| 77 | while (file.good() && !file.eof()) |
---|
| 78 | { |
---|
[3198] | 79 | std::string line; |
---|
| 80 | std::getline(file, line); |
---|
[1505] | 81 | CommandExecutor::execute(line); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | executingFiles.erase(filename); |
---|
| 85 | file.close(); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | std::string echo(const std::string& text) |
---|
| 89 | { |
---|
| 90 | return text; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | void puts(bool newline, const std::string& text) |
---|
| 94 | { |
---|
| 95 | if (newline) |
---|
| 96 | { |
---|
[3370] | 97 | COUT(0) << stripEnclosingBraces(text) << std::endl; |
---|
[1505] | 98 | } |
---|
| 99 | else |
---|
| 100 | { |
---|
[3370] | 101 | COUT(0) << stripEnclosingBraces(text); |
---|
[1505] | 102 | } |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | void write(const std::string& filename, const std::string& text) |
---|
| 106 | { |
---|
| 107 | std::ofstream file; |
---|
| 108 | file.open(filename.c_str(), std::fstream::out); |
---|
| 109 | |
---|
| 110 | if (!file.is_open()) |
---|
| 111 | { |
---|
| 112 | COUT(1) << "Error: Couldn't write to file \"" << filename << "\"." << std::endl; |
---|
| 113 | return; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | file << text << std::endl; |
---|
| 117 | file.close(); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | void append(const std::string& filename, const std::string& text) |
---|
| 121 | { |
---|
| 122 | std::ofstream file; |
---|
| 123 | file.open(filename.c_str(), std::fstream::app); |
---|
| 124 | |
---|
| 125 | if (!file.is_open()) |
---|
| 126 | { |
---|
| 127 | COUT(1) << "Error: Couldn't append to file \"" << filename << "\"." << std::endl; |
---|
| 128 | return; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | file << text << std::endl; |
---|
| 132 | file.close(); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | std::string read(const std::string& filename) |
---|
| 136 | { |
---|
| 137 | std::ifstream file; |
---|
| 138 | file.open(filename.c_str(), std::fstream::in); |
---|
| 139 | |
---|
| 140 | if (!file.is_open()) |
---|
| 141 | { |
---|
| 142 | COUT(1) << "Error: Couldn't read from file \"" << filename << "\"." << std::endl; |
---|
| 143 | return ""; |
---|
| 144 | } |
---|
| 145 | |
---|
[6417] | 146 | std::string output; |
---|
[1505] | 147 | while (file.good() && !file.eof()) |
---|
| 148 | { |
---|
[3198] | 149 | std::string line; |
---|
| 150 | std::getline(file, line); |
---|
[1505] | 151 | output += line; |
---|
| 152 | output += "\n"; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | file.close(); |
---|
| 156 | |
---|
| 157 | return output; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | float calculate(const std::string& calculation) |
---|
| 161 | { |
---|
[6417] | 162 | ExprParser expr; |
---|
| 163 | expr.parse(calculation); |
---|
[1505] | 164 | if (expr.getSuccess()) |
---|
| 165 | { |
---|
| 166 | if (expr.getResult() == 42.0) |
---|
| 167 | { |
---|
| 168 | COUT(3) << "Greetings from the restaurant at the end of the universe." << std::endl; |
---|
| 169 | } |
---|
[6417] | 170 | if (!expr.getRemains().empty()) |
---|
[1505] | 171 | { |
---|
[6417] | 172 | COUT(2) << "Warning: Expression could not be parsed to the end! Remains: '" << expr.getRemains() << '\'' << std::endl; |
---|
[1505] | 173 | } |
---|
[3196] | 174 | return static_cast<float>(expr.getResult()); |
---|
[1505] | 175 | } |
---|
| 176 | else |
---|
| 177 | { |
---|
| 178 | COUT(1) << "Error: Cannot calculate expression: Parse error." << std::endl; |
---|
| 179 | return 0; |
---|
| 180 | } |
---|
| 181 | } |
---|
| 182 | } |
---|