4 // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include "console.hpp"
23 #include "video/drawing_context.hpp"
24 #include "video/surface.hpp"
25 #include "scripting/squirrel_error.hpp"
26 #include "scripting/squirrel_util.hpp"
27 #include "physfs/physfs_stream.hpp"
28 #include "player_status.hpp"
31 #include "resources.hpp"
32 #include "gameconfig.hpp"
34 /// speed (pixels/s) the console closes
35 static const float FADE_SPEED = 1;
38 : history_position(history.end()), vm(NULL), backgroundOffset(0),
39 height(0), alpha(1.0), offset(0), focused(false), stayOpen(0) {
46 sq_release(Scripting::global_vm, &vm_object);
51 Console::init_graphics()
53 font.reset(new Font("images/engine/fonts/white-small.png",
54 "images/engine/fonts/shadow-small.png", 8, 9, 1));
55 fontheight = font->get_height();
56 background.reset(new Surface("images/engine/console.png"));
57 background2.reset(new Surface("images/engine/console2.png"));
61 Console::flush(ConsoleStreamBuffer* buffer)
63 if (buffer == &outputBuffer) {
64 std::string s = outputBuffer.str();
65 if ((s.length() > 0) && ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r'))) {
66 while ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r')) s.erase(s.length()-1);
68 outputBuffer.str(std::string());
71 if (buffer == &inputBuffer) {
72 std::string s = inputBuffer.str();
73 if ((s.length() > 0) && ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r'))) {
74 while ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r')) s.erase(s.length()-1);
77 inputBuffer.str(std::string());
86 vm = Scripting::global_vm;
87 HSQUIRRELVM new_vm = sq_newthread(vm, 16);
89 throw Scripting::SquirrelError(vm, "Couldn't create new VM thread for console");
91 // store reference to thread
92 sq_resetobject(&vm_object);
93 if(SQ_FAILED(sq_getstackobj(vm, -1, &vm_object)))
94 throw Scripting::SquirrelError(vm, "Couldn't get vm object for console");
95 sq_addref(vm, &vm_object);
98 // create new roottable for thread
100 sq_pushroottable(new_vm);
101 if(SQ_FAILED(sq_setdelegate(new_vm, -2)))
102 throw Scripting::SquirrelError(new_vm, "Couldn't set console_table delegate");
104 sq_setroottable(new_vm);
109 std::string filename = "scripts/console.nut";
110 IFileStream stream(filename);
111 Scripting::compile_and_run(vm, stream, filename);
112 } catch(std::exception& e) {
113 log_warning << "Couldn't load console.nut: " << e.what() << std::endl;
119 Console::execute_script(const std::string& command)
121 using namespace Scripting;
125 SQInteger oldtop = sq_gettop(vm);
127 if(SQ_FAILED(sq_compilebuffer(vm, command.c_str(), command.length(),
129 throw SquirrelError(vm, "Couldn't compile command");
131 sq_pushroottable(vm);
132 if(SQ_FAILED(sq_call(vm, 1, SQTrue, SQTrue)))
133 throw SquirrelError(vm, "Problem while executing command");
135 if(sq_gettype(vm, -1) != OT_NULL)
136 addLines(squirrel2string(vm, -1));
137 } catch(std::exception& e) {
140 SQInteger newtop = sq_gettop(vm);
141 if(newtop < oldtop) {
142 log_fatal << "Script destroyed squirrel stack..." << std::endl;
144 sq_settop(vm, oldtop);
151 std::string s = inputBuffer.str();
152 if (s.length() > 0) {
153 s.erase(s.length()-1);
155 inputBuffer.pubseekoff(0, std::ios_base::end, std::ios_base::out);
160 Console::scroll(int numLines)
163 if (offset > 0) offset = 0;
167 Console::show_history(int offset)
169 while ((offset > 0) && (history_position != history.end())) {
173 while ((offset < 0) && (history_position != history.begin())) {
177 if (history_position == history.end()) {
178 inputBuffer.str(std::string());
180 inputBuffer.str(*history_position);
181 inputBuffer.pubseekoff(0, std::ios_base::end, std::ios_base::out);
185 // Helper functions for Console::autocomplete
186 // TODO: Fix rough documentation
189 void sq_insert_commands(std::list<std::string>& cmds, HSQUIRRELVM vm, std::string table_prefix, std::string search_prefix);
192 * Acts upon key,value on top of stack:
193 * Appends key (plus type-dependent suffix) to cmds if table_prefix+key starts with search_prefix;
194 * Calls sq_insert_commands if search_prefix starts with table_prefix+key (and value is a table/class/instance);
197 sq_insert_command(std::list<std::string>& cmds, HSQUIRRELVM vm, std::string table_prefix, std::string search_prefix)
199 const SQChar* key_chars;
200 if (SQ_FAILED(sq_getstring(vm, -2, &key_chars))) return;
201 std::string key_string = table_prefix + key_chars;
203 switch (sq_gettype(vm, -1)) {
206 if (search_prefix.substr(0, key_string.length()) == key_string) {
208 sq_insert_commands(cmds, vm, key_string, search_prefix);
215 if (search_prefix.substr(0, key_string.length()) == key_string) {
216 sq_insert_commands(cmds, vm, key_string, search_prefix);
220 case OT_NATIVECLOSURE:
227 if (key_string.substr(0, search_prefix.length()) == search_prefix) {
228 cmds.push_back(key_string);
234 * calls sq_insert_command for all entries of table/class on top of stack
237 sq_insert_commands(std::list<std::string>& cmds, HSQUIRRELVM vm, std::string table_prefix, std::string search_prefix)
239 sq_pushnull(vm); // push iterator
240 while (SQ_SUCCEEDED(sq_next(vm,-2))) {
241 sq_insert_command(cmds, vm, table_prefix, search_prefix);
242 sq_pop(vm, 2); // pop key, val
244 sq_pop(vm, 1); // pop iterator
249 // End of Console::autocomplete helper functions
252 Console::autocomplete()
254 std::string prefix = inputBuffer.str();
255 addLines("> "+prefix);
257 std::list<std::string> cmds;
261 // append all keys of the current root table to list
262 sq_pushroottable(vm); // push root table
264 // check all keys (and their children) for matches
265 sq_insert_commands(cmds, vm, "", prefix);
267 // cycle through parent(delegate) table
268 SQInteger oldtop = sq_gettop(vm);
269 if(SQ_FAILED(sq_getdelegate(vm, -1)) || oldtop == sq_gettop(vm)) {
272 sq_remove(vm, -2); // remove old table
274 sq_pop(vm, 1); // remove table
276 // depending on number of hits, show matches or autocomplete
277 if (cmds.size() == 0) addLines("No known command starts with \""+prefix+"\"");
278 if (cmds.size() == 1) {
279 inputBuffer.str(cmds.front());
280 inputBuffer.pubseekoff(0, std::ios_base::end, std::ios_base::out);
282 if (cmds.size() > 1) {
283 while (cmds.begin() != cmds.end()) {
284 addLines(cmds.front());
291 Console::addLines(std::string s)
293 std::istringstream iss(s);
295 while (std::getline(iss, line, '\n')) addLine(line);
299 Console::addLine(std::string s)
301 // output line to stderr
302 std::cerr << s << std::endl;
305 std::string overflow;
307 lines.push_front(Font::wrap_to_chars(s, 99, &overflow));
309 } while (s.length() > 0);
311 // trim scrollback buffer
312 while (lines.size() >= 1000)
315 // increase console height if necessary
319 height += fontheight;
322 // reset console to full opacity
325 // increase time that console stays open
331 Console::parse(std::string s)
333 // make sure we actually have something to parse
334 if (s.length() == 0) return;
336 // add line to history
337 history.push_back(s);
338 history_position = history.end();
340 // split line into list of args
341 std::vector<std::string> args;
345 start = s.find_first_not_of(" ,", end);
346 end = s.find_first_of(" ,", start);
347 if (start == s.npos) break;
348 args.push_back(s.substr(start, end-start));
351 // command is args[0]
352 if (args.size() == 0) return;
353 std::string command = args.front();
354 args.erase(args.begin());
356 // ignore if it's an internal command
357 if (consoleCommand(command,args)) return;
361 } catch(std::exception& e) {
368 Console::consoleCommand(std::string /*command*/, std::vector<std::string> /*arguments*/)
382 if(!config->console_enabled)
388 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
398 // clear input buffer
399 inputBuffer.str(std::string());
400 SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
406 if (Console::hasFocus()) {
415 Console::update(float elapsed_time)
418 stayOpen -= elapsed_time;
421 } else if(!focused && height > 0) {
422 alpha -= elapsed_time * FADE_SPEED;
431 Console::draw(DrawingContext& context)
436 int layer = LAYER_GUI + 1;
438 context.push_transform();
439 context.set_alpha(alpha);
440 context.draw_surface(background2.get(), Vector(SCREEN_WIDTH/2 - background->get_width()/2 - background->get_width() + backgroundOffset, height - background->get_height()), layer);
441 context.draw_surface(background2.get(), Vector(SCREEN_WIDTH/2 - background->get_width()/2 + backgroundOffset, height - background->get_height()), layer);
442 context.draw_surface(background.get(), Vector(SCREEN_WIDTH/2 - background->get_width()/2, height - background->get_height()), layer);
443 backgroundOffset+=10;
444 if (backgroundOffset > (int)background->get_width()) backgroundOffset -= (int)background->get_width();
450 float py = height-4-1*9;
451 context.draw_text(font.get(), "> "+inputBuffer.str()+"_", Vector(4, py), LEFT_ALLIGN, layer);
454 int skipLines = -offset;
455 for (std::list<std::string>::iterator i = lines.begin(); i != lines.end(); i++) {
456 if (skipLines-- > 0) continue;
458 float py = height-4-lineNo*9;
460 context.draw_text(font.get(), *i, Vector(4, py), LEFT_ALLIGN, layer);
462 context.pop_transform();
465 Console* Console::instance = NULL;
466 ConsoleStreamBuffer Console::inputBuffer;
467 ConsoleStreamBuffer Console::outputBuffer;
468 std::ostream Console::input(&Console::inputBuffer);
469 std::ostream Console::output(&Console::outputBuffer);