Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ipv6/src/libraries/core/command/ConsoleCommandCompilation.cc @ 7320

Last change on this file since 7320 was 7284, checked in by landauf, 14 years ago

merged consolecommands3 branch back to trunk.

note: the console command interface has changed completely, but the documentation is not yet up to date. just copy an existing command and change it to your needs, it's pretty self-explanatory. also the include files related to console commands are now located in core/command/. in the game it should work exactly like before, except for some changes in the auto-completion.

  • Property svn:eol-style set to native
File size: 5.0 KB
Line 
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"
30
31#include <fstream>
32#include <set>
33#include <string>
34
35#include "util/Debug.h"
36#include "util/ExprParser.h"
37#include "util/StringUtils.h"
38#include "ConsoleCommand.h"
39#include "CommandExecutor.h"
40
41namespace orxonox
42{
43    SetConsoleCommand("source", source).argumentCompleter(0, autocompletion::files());
44    SetConsoleCommand("echo", echo);
45    SetConsoleCommand("puts", puts);
46
47    SetConsoleCommand("read", read).argumentCompleter(0, autocompletion::files());
48    SetConsoleCommand("append", append).argumentCompleter(0, autocompletion::files());
49    SetConsoleCommand("write", write).argumentCompleter(0, autocompletion::files());
50
51    SetConsoleCommand("calculate", calculate);
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        {
79            std::string line;
80            std::getline(file, line);
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        {
97            COUT(0) << stripEnclosingBraces(text) << std::endl;
98        }
99        else
100        {
101            COUT(0) << stripEnclosingBraces(text);
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
146        std::string output;
147        while (file.good() && !file.eof())
148        {
149            std::string line;
150            std::getline(file, line);
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    {
162        ExprParser expr;
163        expr.parse(calculation);
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            }
170            if (!expr.getRemains().empty())
171            {
172                COUT(2) << "Warning: Expression could not be parsed to the end! Remains: '" << expr.getRemains() << '\'' << std::endl;
173            }
174            return static_cast<float>(expr.getResult());
175        }
176        else
177        {
178            COUT(1) << "Error: Cannot calculate expression: Parse error." << std::endl;
179            return 0;
180        }
181    }
182}
Note: See TracBrowser for help on using the repository browser.