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