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"
33 /// speed (pixels/s) the console closes
34 static const float FADE_SPEED = 1;
37 : history_position(history.end()), vm(NULL), backgroundOffset(0),
38 height(0), alpha(1.0), offset(0), focused(false), stayOpen(0) {
45 sq_release(Scripting::global_vm, &vm_object);
50 Console::init_graphics()
52 font.reset(new Font("images/engine/fonts/white-small.png",
53 "images/engine/fonts/shadow-small.png", 8, 9, 1));
54 fontheight = font->get_height();
55 background.reset(new Surface("images/engine/console.png"));
56 background2.reset(new Surface("images/engine/console2.png"));
60 Console::flush(ConsoleStreamBuffer* buffer)
62 if (buffer == &outputBuffer) {
63 std::string s = outputBuffer.str();
64 if ((s.length() > 0) && ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r'))) {
65 while ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r')) s.erase(s.length()-1);
67 outputBuffer.str(std::string());
70 if (buffer == &inputBuffer) {
71 std::string s = inputBuffer.str();
72 if ((s.length() > 0) && ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r'))) {
73 while ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r')) s.erase(s.length()-1);
76 inputBuffer.str(std::string());
85 vm = Scripting::global_vm;
86 HSQUIRRELVM new_vm = sq_newthread(vm, 16);
88 throw Scripting::SquirrelError(vm, "Couldn't create new VM thread for console");
90 // store reference to thread
91 sq_resetobject(&vm_object);
92 if(SQ_FAILED(sq_getstackobj(vm, -1, &vm_object)))
93 throw Scripting::SquirrelError(vm, "Couldn't get vm object for console");
94 sq_addref(vm, &vm_object);
97 // create new roottable for thread
99 sq_pushroottable(new_vm);
100 if(SQ_FAILED(sq_setdelegate(new_vm, -2)))
101 throw Scripting::SquirrelError(new_vm, "Couldn't set console_table delegate");
103 sq_setroottable(new_vm);
108 std::string filename = "scripts/console.nut";
109 IFileStream stream(filename);
110 Scripting::compile_and_run(vm, stream, filename);
111 } catch(std::exception& e) {
112 log_warning << "Couldn't load console.nut: " << e.what() << std::endl;
118 Console::execute_script(const std::string& command)
120 using namespace Scripting;
124 SQInteger oldtop = sq_gettop(vm);
126 if(SQ_FAILED(sq_compilebuffer(vm, command.c_str(), command.length(),
128 throw SquirrelError(vm, "Couldn't compile command");
130 sq_pushroottable(vm);
131 if(SQ_FAILED(sq_call(vm, 1, SQTrue, SQTrue)))
132 throw SquirrelError(vm, "Problem while executing command");
134 if(sq_gettype(vm, -1) != OT_NULL)
135 addLines(squirrel2string(vm, -1));
136 } catch(std::exception& e) {
139 SQInteger newtop = sq_gettop(vm);
140 if(newtop < oldtop) {
141 log_fatal << "Script destroyed squirrel stack..." << std::endl;
143 sq_settop(vm, oldtop);
150 std::string s = inputBuffer.str();
151 if (s.length() > 0) {
152 s.erase(s.length()-1);
154 inputBuffer.pubseekoff(0, std::ios_base::end, std::ios_base::out);
159 Console::scroll(int numLines)
162 if (offset > 0) offset = 0;
166 Console::show_history(int offset)
168 while ((offset > 0) && (history_position != history.end())) {
172 while ((offset < 0) && (history_position != history.begin())) {
176 if (history_position == history.end()) {
177 inputBuffer.str(std::string());
179 inputBuffer.str(*history_position);
180 inputBuffer.pubseekoff(0, std::ios_base::end, std::ios_base::out);
185 Console::autocomplete()
187 std::string cmdPart = inputBuffer.str();
188 addLines("> "+cmdPart);
190 std::string cmdList = "";
193 // append all known CCRs to cmdList
194 for (std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.begin(); i != commands.end(); i++) {
195 std::string cmdKnown = i->first;
196 if (cmdKnown.substr(0, cmdPart.length()) == cmdPart) {
197 if (cmdListLen > 0) cmdList = cmdList + ", ";
198 cmdList = cmdList + cmdKnown;
205 // append all keys of the current root table to cmdList
206 sq_pushroottable(vm); // push root table
208 sq_pushnull(vm); // push null
209 while (SQ_SUCCEEDED(sq_next(vm,-2))) {
211 if(SQ_SUCCEEDED(sq_getstring(vm, -2, &s))) {
212 std::string cmdKnown = s;
213 if (cmdKnown.substr(0, cmdPart.length()) == cmdPart) {
214 if (cmdListLen > 0) cmdList = cmdList + ", ";
215 cmdList = cmdList + cmdKnown;
220 sq_pop(vm, 2); // pop key, val
223 // cycle through parent(delegate) table
224 sq_pop(vm, 1); // pop iterator
225 SQInteger oldtop = sq_gettop(vm);
226 if(SQ_FAILED(sq_getdelegate(vm, -1)) || oldtop == sq_gettop(vm)) {
229 sq_remove(vm, -2); // remove old table
231 sq_pop(vm, 1); // remove table
233 // depending on number of hits, show matches or autocomplete
234 if (cmdListLen == 0) addLines("No known command starts with \""+cmdPart+"\"");
235 if (cmdListLen == 1) {
236 inputBuffer.str(cmdList);
237 inputBuffer.pubseekoff(0, std::ios_base::end, std::ios_base::out);
239 if (cmdListLen > 1) addLines(cmdList);
243 Console::addLines(std::string s)
245 std::istringstream iss(s);
247 while (std::getline(iss, line, '\n')) addLine(line);
251 Console::addLine(std::string s)
253 // output line to stderr
254 std::cerr << s << std::endl;
257 std::string overflow;
259 lines.push_front(Font::wrap_to_chars(s, 99, &overflow));
261 } while (s.length() > 0);
263 // trim scrollback buffer
264 while (lines.size() >= 1000)
267 // increase console height if necessary
271 height += fontheight;
274 // reset console to full opacity
277 // increase time that console stays open
283 Console::parse(std::string s)
285 // make sure we actually have something to parse
286 if (s.length() == 0) return;
288 // add line to history
289 history.push_back(s);
290 history_position = history.end();
292 // split line into list of args
293 std::vector<std::string> args;
297 start = s.find_first_not_of(" ,", end);
298 end = s.find_first_of(" ,", start);
299 if (start == s.npos) break;
300 args.push_back(s.substr(start, end-start));
303 // command is args[0]
304 if (args.size() == 0) return;
305 std::string command = args.front();
306 args.erase(args.begin());
308 // ignore if it's an internal command
309 if (consoleCommand(command,args)) return;
311 // look up registered ccr
312 std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.find(command);
313 if ((i == commands.end()) || (i->second.size() == 0)) {
316 } catch(std::exception& e) {
322 // send command to the most recently registered ccr
323 ConsoleCommandReceiver* ccr = i->second.front();
324 if (ccr->consoleCommand(command, args) != true) log_warning << "Sent command to registered ccr, but command was unhandled" << std::endl;
328 Console::consoleCommand(std::string command, std::vector<std::string> arguments)
330 if (command == "ccrs") {
331 if (arguments.size() != 1) {
332 log_info << "Usage: ccrs <command>" << std::endl;
335 std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.find(arguments[0]);
336 if ((i == commands.end()) || (i->second.size() == 0)) {
337 log_info << "unknown command: \"" << arguments[0] << "\"" << std::endl;
341 std::ostringstream ccr_list;
342 std::list<ConsoleCommandReceiver*> &ccrs = i->second;
343 std::list<ConsoleCommandReceiver*>::iterator j;
344 for (j = ccrs.begin(); j != ccrs.end(); j++) {
345 if (j != ccrs.begin()) ccr_list << ", ";
346 ccr_list << "[" << *j << "]";
349 log_info << "registered ccrs for \"" << arguments[0] << "\": " << ccr_list.str() << std::endl;
377 // clear input buffer
378 inputBuffer.str(std::string());
384 if (Console::hasFocus()) {
393 Console::update(float elapsed_time)
396 stayOpen -= elapsed_time;
399 } else if(!focused && height > 0) {
400 alpha -= elapsed_time * FADE_SPEED;
409 Console::draw(DrawingContext& context)
414 int layer = LAYER_GUI + 1;
416 context.push_transform();
417 context.set_alpha(alpha);
418 context.draw_surface(background2.get(), Vector(SCREEN_WIDTH/2 - background->get_width()/2 - background->get_width() + backgroundOffset, height - background->get_height()), layer);
419 context.draw_surface(background2.get(), Vector(SCREEN_WIDTH/2 - background->get_width()/2 + backgroundOffset, height - background->get_height()), layer);
420 context.draw_surface(background.get(), Vector(SCREEN_WIDTH/2 - background->get_width()/2, height - background->get_height()), layer);
421 backgroundOffset+=10;
422 if (backgroundOffset > (int)background->get_width()) backgroundOffset -= (int)background->get_width();
428 float py = height-4-1*9;
429 context.draw_text(font.get(), "> "+inputBuffer.str()+"_", Vector(4, py), LEFT_ALLIGN, layer);
432 int skipLines = -offset;
433 for (std::list<std::string>::iterator i = lines.begin(); i != lines.end(); i++) {
434 if (skipLines-- > 0) continue;
436 float py = height-4-lineNo*9;
438 context.draw_text(font.get(), *i, Vector(4, py), LEFT_ALLIGN, layer);
441 context.pop_transform();
445 Console::registerCommand(std::string command, ConsoleCommandReceiver* ccr)
447 commands[command].push_front(ccr);
451 Console::unregisterCommand(std::string command, ConsoleCommandReceiver* ccr)
453 std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.find(command);
454 if ((i == commands.end()) || (i->second.size() == 0)) {
455 log_warning << "Command \"" << command << "\" not associated with a command receiver. Not dissociated." << std::endl;
458 std::list<ConsoleCommandReceiver*>::iterator j = find(i->second.begin(), i->second.end(), ccr);
459 if (j == i->second.end()) {
460 log_warning << "Command \"" << command << "\" not associated with given command receiver. Not dissociated." << std::endl;
467 Console::unregisterCommands(ConsoleCommandReceiver* ccr)
469 for (std::map<std::string, std::list<ConsoleCommandReceiver*> >::iterator i = commands.begin(); i != commands.end(); i++) {
470 std::list<ConsoleCommandReceiver*> &ccrs = i->second;
471 std::list<ConsoleCommandReceiver*>::iterator j;
472 while ((j = find(ccrs.begin(), ccrs.end(), ccr)) != ccrs.end()) {
478 Console* Console::instance = NULL;
479 ConsoleStreamBuffer Console::inputBuffer;
480 ConsoleStreamBuffer Console::outputBuffer;
481 std::ostream Console::input(&Console::inputBuffer);
482 std::ostream Console::output(&Console::outputBuffer);