console.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. // SuperTux - Console
  2. // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include "supertux/console.hpp"
  17. #include "math/sizef.hpp"
  18. #include "physfs/ifile_stream.hpp"
  19. #include "squirrel/squirrel_virtual_machine.hpp"
  20. #include "squirrel/squirrel_util.hpp"
  21. #include "supertux/gameconfig.hpp"
  22. #include "supertux/globals.hpp"
  23. #include "supertux/resources.hpp"
  24. #include "util/log.hpp"
  25. #include "video/drawing_context.hpp"
  26. #include "video/surface.hpp"
  27. /// speed (pixels/s) the console closes
  28. static const float FADE_SPEED = 1;
  29. ConsoleBuffer::ConsoleBuffer() :
  30. m_lines(),
  31. m_console(nullptr)
  32. {
  33. }
  34. void
  35. ConsoleBuffer::set_console(Console* console)
  36. {
  37. assert((console && !m_console) ||
  38. (m_console && !console));
  39. m_console = console;
  40. }
  41. void
  42. ConsoleBuffer::addLines(const std::string& s)
  43. {
  44. std::istringstream iss(s);
  45. std::string line;
  46. while (std::getline(iss, line, '\n'))
  47. {
  48. addLine(line);
  49. }
  50. }
  51. void
  52. ConsoleBuffer::addLine(const std::string& s_)
  53. {
  54. std::string s = s_;
  55. // Output line to stderr.
  56. get_logging_instance(false) << s << std::endl;
  57. // Wrap long lines.
  58. std::string overflow;
  59. int line_count = 0;
  60. do {
  61. m_lines.push_front(Font::wrap_to_chars(s, 99, &overflow));
  62. line_count += 1;
  63. s = overflow;
  64. } while (s.length() > 0);
  65. // Trim scrollback buffer.
  66. while (m_lines.size() >= 1000)
  67. {
  68. m_lines.pop_back();
  69. }
  70. if (m_console)
  71. {
  72. m_console->on_buffer_change(line_count);
  73. }
  74. }
  75. void
  76. ConsoleBuffer::flush(ConsoleStreamBuffer& buffer)
  77. {
  78. if (&buffer == &s_outputBuffer)
  79. {
  80. std::string s = s_outputBuffer.str();
  81. if ((s.length() > 0) && ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r')))
  82. {
  83. while ((s[s.length()-1] == '\n') || (s[s.length()-1] == '\r'))
  84. {
  85. s.erase(s.length()-1);
  86. }
  87. addLines(s);
  88. s_outputBuffer.str("");
  89. }
  90. }
  91. }
  92. Console::Console(ConsoleBuffer& buffer) :
  93. m_buffer(buffer),
  94. m_inputBuffer(),
  95. m_inputBufferPosition(0),
  96. m_history(),
  97. m_history_position(m_history.end()),
  98. m_background(Surface::from_file("images/engine/console.png")),
  99. m_background2(Surface::from_file("images/engine/console2.png")),
  100. m_vm(nullptr),
  101. m_vm_object(),
  102. m_backgroundOffset(0),
  103. m_height(0),
  104. m_alpha(1.0),
  105. m_offset(0),
  106. m_focused(false),
  107. m_font(Resources::console_font),
  108. m_stayOpen(0)
  109. {
  110. m_buffer.set_console(this);
  111. }
  112. Console::~Console()
  113. {
  114. if (m_vm != nullptr && SquirrelVirtualMachine::current() != nullptr)
  115. {
  116. sq_release(SquirrelVirtualMachine::current()->get_vm().get_vm(), &m_vm_object);
  117. }
  118. m_buffer.set_console(nullptr);
  119. }
  120. void
  121. Console::on_buffer_change(int line_count)
  122. {
  123. // Increase console height if necessary.
  124. if (m_stayOpen > 0 && m_height < 64)
  125. {
  126. if (m_height < 4)
  127. {
  128. m_height = 4;
  129. }
  130. m_height += m_font->get_height() * static_cast<float>(line_count);
  131. }
  132. // Reset console to full opacity.
  133. m_alpha = 1.0;
  134. }
  135. void
  136. Console::ready_vm()
  137. {
  138. if (m_vm == nullptr) {
  139. m_vm = SquirrelVirtualMachine::current()->get_vm().get_vm();
  140. HSQUIRRELVM new_vm = sq_newthread(m_vm, 16);
  141. if (new_vm == nullptr)
  142. throw SquirrelError(m_vm, "Couldn't create new VM thread for console");
  143. // Store reference to thread.
  144. sq_resetobject(&m_vm_object);
  145. if (SQ_FAILED(sq_getstackobj(m_vm, -1, &m_vm_object)))
  146. throw SquirrelError(m_vm, "Couldn't get vm object for console");
  147. sq_addref(m_vm, &m_vm_object);
  148. sq_pop(m_vm, 1);
  149. // Create new roottable for thread.
  150. sq_newtable(new_vm);
  151. sq_pushroottable(new_vm);
  152. if (SQ_FAILED(sq_setdelegate(new_vm, -2)))
  153. throw SquirrelError(new_vm, "Couldn't set console_table delegate");
  154. sq_setroottable(new_vm);
  155. m_vm = new_vm;
  156. try {
  157. std::string filename = "scripts/console.nut";
  158. IFileStream stream(filename);
  159. compile_and_run(m_vm, stream, filename);
  160. } catch(std::exception& e) {
  161. log_warning << "Couldn't load console.nut: " << e.what() << std::endl;
  162. }
  163. }
  164. }
  165. void
  166. Console::execute_script(const std::string& command)
  167. {
  168. ready_vm();
  169. SQInteger oldtop = sq_gettop(m_vm);
  170. try {
  171. if (SQ_FAILED(sq_compilebuffer(m_vm, command.c_str(), command.length(),
  172. "", SQTrue)))
  173. throw SquirrelError(m_vm, "Couldn't compile command");
  174. sq_pushroottable(m_vm);
  175. if (SQ_FAILED(sq_call(m_vm, 1, SQTrue, SQTrue)))
  176. throw SquirrelError(m_vm, "Problem while executing command");
  177. if (sq_gettype(m_vm, -1) != OT_NULL)
  178. m_buffer.addLines(squirrel2string(m_vm, -1));
  179. } catch(std::exception& e) {
  180. m_buffer.addLines(e.what());
  181. }
  182. SQInteger newtop = sq_gettop(m_vm);
  183. if (newtop < oldtop) {
  184. log_fatal << "Script destroyed squirrel stack..." << std::endl;
  185. } else {
  186. sq_settop(m_vm, oldtop);
  187. }
  188. }
  189. void
  190. Console::input(char c)
  191. {
  192. m_inputBuffer.insert(m_inputBufferPosition, 1, c);
  193. m_inputBufferPosition++;
  194. }
  195. void
  196. Console::backspace()
  197. {
  198. if ((m_inputBufferPosition > 0) && (m_inputBuffer.length() > 0)) {
  199. m_inputBuffer.erase(m_inputBufferPosition-1, 1);
  200. m_inputBufferPosition--;
  201. }
  202. }
  203. void
  204. Console::eraseChar()
  205. {
  206. if (m_inputBufferPosition < static_cast<int>(m_inputBuffer.length())) {
  207. m_inputBuffer.erase(m_inputBufferPosition, 1);
  208. }
  209. }
  210. void
  211. Console::enter()
  212. {
  213. m_buffer.addLines("> " + m_inputBuffer);
  214. parse(m_inputBuffer);
  215. m_inputBuffer = "";
  216. m_inputBufferPosition = 0;
  217. }
  218. void
  219. Console::scroll(int numLines)
  220. {
  221. m_offset += numLines;
  222. if (m_offset > 0) m_offset = 0;
  223. }
  224. void
  225. Console::show_history(int offset_)
  226. {
  227. while ((offset_ > 0) && (m_history_position != m_history.end())) {
  228. ++m_history_position;
  229. offset_--;
  230. }
  231. while ((offset_ < 0) && (m_history_position != m_history.begin())) {
  232. --m_history_position;
  233. offset_++;
  234. }
  235. if (m_history_position == m_history.end()) {
  236. m_inputBuffer = "";
  237. m_inputBufferPosition = 0;
  238. } else {
  239. m_inputBuffer = *m_history_position;
  240. m_inputBufferPosition = static_cast<int>(m_inputBuffer.length());
  241. }
  242. }
  243. void
  244. Console::move_cursor(int offset_)
  245. {
  246. if (offset_ == -65535) m_inputBufferPosition = 0;
  247. if (offset_ == +65535) m_inputBufferPosition = static_cast<int>(m_inputBuffer.length());
  248. m_inputBufferPosition+=offset_;
  249. if (m_inputBufferPosition < 0) m_inputBufferPosition = 0;
  250. if (m_inputBufferPosition > static_cast<int>(m_inputBuffer.length())) m_inputBufferPosition = static_cast<int>(m_inputBuffer.length());
  251. }
  252. // Helper functions for Console::autocomplete.
  253. // TODO: Fix rough documentation.
  254. namespace {
  255. void sq_insert_commands(std::list<std::string>& cmds, HSQUIRRELVM vm, const std::string& table_prefix, const std::string& search_prefix);
  256. /**
  257. * Acts upon key,value on top of stack:
  258. * Appends key (plus type-dependent suffix) to cmds if table_prefix+key starts with search_prefix;
  259. * Calls sq_insert_commands if search_prefix starts with table_prefix+key (and value is a table/class/instance);
  260. */
  261. void
  262. sq_insert_command(std::list<std::string>& cmds, HSQUIRRELVM vm, const std::string& table_prefix, const std::string& search_prefix)
  263. {
  264. const SQChar* key_chars;
  265. if (SQ_FAILED(sq_getstring(vm, -2, &key_chars))) return;
  266. std::string key_string = table_prefix + key_chars;
  267. switch (sq_gettype(vm, -1)) {
  268. case OT_INSTANCE:
  269. key_string+=".";
  270. if (search_prefix.substr(0, key_string.length()) == key_string) {
  271. sq_getclass(vm, -1);
  272. sq_insert_commands(cmds, vm, key_string, search_prefix);
  273. sq_pop(vm, 1);
  274. }
  275. break;
  276. case OT_TABLE:
  277. case OT_CLASS:
  278. key_string+=".";
  279. if (search_prefix.substr(0, key_string.length()) == key_string) {
  280. sq_insert_commands(cmds, vm, key_string, search_prefix);
  281. }
  282. break;
  283. case OT_CLOSURE:
  284. case OT_NATIVECLOSURE:
  285. key_string+="()";
  286. break;
  287. default:
  288. break;
  289. }
  290. if (key_string.substr(0, search_prefix.length()) == search_prefix) {
  291. cmds.push_back(key_string);
  292. }
  293. }
  294. /**
  295. * Calls sq_insert_command for all entries of table/class on top of stack.
  296. */
  297. void
  298. sq_insert_commands(std::list<std::string>& cmds, HSQUIRRELVM vm, const std::string& table_prefix, const std::string& search_prefix)
  299. {
  300. sq_pushnull(vm); // push iterator.
  301. while (SQ_SUCCEEDED(sq_next(vm,-2))) {
  302. sq_insert_command(cmds, vm, table_prefix, search_prefix);
  303. sq_pop(vm, 2); // pop key, val.
  304. }
  305. sq_pop(vm, 1); // pop iterator.
  306. }
  307. }
  308. // End of Console::autocomplete helper functions.
  309. void
  310. Console::autocomplete()
  311. {
  312. // int autocompleteFrom = m_inputBuffer.find_last_of(" ();+", m_inputBufferPosition);
  313. int autocompleteFrom = static_cast<int>(m_inputBuffer.find_last_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_->.", m_inputBufferPosition));
  314. if (autocompleteFrom != static_cast<int>(std::string::npos)) {
  315. autocompleteFrom += 1;
  316. } else {
  317. autocompleteFrom = 0;
  318. }
  319. std::string prefix = m_inputBuffer.substr(autocompleteFrom, m_inputBufferPosition - autocompleteFrom);
  320. m_buffer.addLines("> " + prefix);
  321. std::list<std::string> cmds;
  322. ready_vm();
  323. // Append all keys of the current root table to list.
  324. sq_pushroottable(m_vm); // push root table.
  325. while (true) {
  326. // Check all keys (and their children) for matches.
  327. sq_insert_commands(cmds, m_vm, "", prefix);
  328. // Cycle through parent(delegate) table.
  329. SQInteger oldtop = sq_gettop(m_vm);
  330. if (SQ_FAILED(sq_getdelegate(m_vm, -1)) || oldtop == sq_gettop(m_vm)) {
  331. break;
  332. }
  333. sq_remove(m_vm, -2); // Remove old table.
  334. }
  335. sq_pop(m_vm, 1); // Remove table.
  336. // Depending on number of hits, show matches or autocomplete.
  337. if (cmds.empty())
  338. {
  339. m_buffer.addLines("No known command starts with \"" + prefix + "\"");
  340. }
  341. if (cmds.size() == 1)
  342. {
  343. // One match: just replace input buffer with full command.
  344. std::string replaceWith = cmds.front();
  345. m_inputBuffer.replace(autocompleteFrom, prefix.length(), replaceWith);
  346. m_inputBufferPosition += static_cast<int>(replaceWith.length() - prefix.length());
  347. }
  348. if (cmds.size() > 1)
  349. {
  350. // Multiple matches: show all matches and set input buffer to longest common prefix.
  351. std::string commonPrefix = cmds.front();
  352. while (cmds.begin() != cmds.end()) {
  353. std::string cmd = cmds.front();
  354. cmds.pop_front();
  355. m_buffer.addLines(cmd);
  356. for (int n = static_cast<int>(commonPrefix.length()); n >= 1; n--) {
  357. if (cmd.compare(0, n, commonPrefix) != 0) commonPrefix.resize(n-1); else break;
  358. }
  359. }
  360. std::string replaceWith = commonPrefix;
  361. m_inputBuffer.replace(autocompleteFrom, prefix.length(), replaceWith);
  362. m_inputBufferPosition += static_cast<int>(replaceWith.length() - prefix.length());
  363. }
  364. }
  365. void
  366. Console::parse(const std::string& s)
  367. {
  368. // Make sure we actually have something to parse.
  369. if (s.length() == 0) return;
  370. // Add line to history.
  371. m_history.push_back(s);
  372. m_history_position = m_history.end();
  373. // Split line into list of args.
  374. std::vector<std::string> args;
  375. size_t end = 0;
  376. while (1) {
  377. size_t start = s.find_first_not_of(" ,", end);
  378. end = s.find_first_of(" ,", start);
  379. if (start == s.npos) break;
  380. args.push_back(s.substr(start, end-start));
  381. }
  382. // Command is args[0].
  383. if (args.size() == 0) return;
  384. std::string command = args.front();
  385. args.erase(args.begin());
  386. // Ignore if it's an internal command.
  387. if (consoleCommand(command,args)) return;
  388. try {
  389. execute_script(s);
  390. } catch(std::exception& e) {
  391. m_buffer.addLines(e.what());
  392. }
  393. }
  394. bool
  395. Console::consoleCommand(const std::string& /*command*/, const std::vector<std::string>& /*arguments*/)
  396. {
  397. return false;
  398. }
  399. bool
  400. Console::hasFocus() const
  401. {
  402. return m_focused;
  403. }
  404. void
  405. Console::show()
  406. {
  407. if (!g_config->developer_mode)
  408. return;
  409. m_focused = true;
  410. m_height = 256;
  411. m_alpha = 1.0;
  412. }
  413. void
  414. Console::open()
  415. {
  416. if (m_stayOpen < 2)
  417. m_stayOpen += 1.5f;
  418. }
  419. void
  420. Console::hide()
  421. {
  422. m_focused = false;
  423. m_height = 0;
  424. m_stayOpen = 0;
  425. // Clear the input buffer.
  426. }
  427. void
  428. Console::toggle()
  429. {
  430. if (Console::hasFocus()) {
  431. Console::hide();
  432. }
  433. else {
  434. Console::show();
  435. }
  436. }
  437. void
  438. Console::update(float dt_sec)
  439. {
  440. if (m_stayOpen > 0) {
  441. m_stayOpen -= dt_sec;
  442. if (m_stayOpen < 0)
  443. m_stayOpen = 0;
  444. } else if (!m_focused && m_height > 0) {
  445. m_alpha -= dt_sec * FADE_SPEED;
  446. if (m_alpha < 0) {
  447. m_alpha = 0;
  448. m_height = 0;
  449. }
  450. }
  451. m_backgroundOffset += static_cast<int>(600.0f * dt_sec);
  452. if (m_backgroundOffset > static_cast<int>(m_background->get_width())) m_backgroundOffset -= static_cast<int>(m_background->get_width());
  453. }
  454. void
  455. Console::draw(DrawingContext& context) const
  456. {
  457. if (m_height == 0)
  458. return;
  459. const int layer = LAYER_GUI + 1;
  460. const float context_center_x = context.get_width() / 2;
  461. const int background_center_x = m_background->get_width() / 2;
  462. context.push_transform();
  463. context.set_alpha(m_alpha);
  464. context.color().draw_surface(m_background2,
  465. Vector(context_center_x - static_cast<float>(background_center_x + m_background->get_width() - m_backgroundOffset),
  466. m_height - static_cast<float>(m_background->get_height())),
  467. layer);
  468. context.color().draw_surface(m_background2,
  469. Vector(context_center_x - static_cast<float>(background_center_x - m_backgroundOffset),
  470. m_height - static_cast<float>(m_background->get_height())),
  471. layer);
  472. for (int x = (static_cast<int>(context_center_x) - background_center_x
  473. - (static_cast<int>(ceilf(context.get_width() /
  474. static_cast<float>(m_background->get_width())) - 1) * m_background->get_width()));
  475. x < static_cast<int>(context.get_width());
  476. x += m_background->get_width())
  477. {
  478. context.color().draw_surface(m_background, Vector(static_cast<float>(x),
  479. m_height - static_cast<float>(m_background->get_height())),
  480. layer);
  481. }
  482. int lineNo = 0;
  483. if (m_focused) {
  484. lineNo++;
  485. float py = m_height-4-1 * m_font->get_height();
  486. std::string line = "> " + m_inputBuffer;
  487. context.color().draw_text(m_font, line, Vector(4, py), ALIGN_LEFT, layer);
  488. if (SDL_GetTicks() % 500 < 250) {
  489. std::string::size_type p = 2 + m_inputBufferPosition;
  490. float cursor_x;
  491. if (p >= line.size())
  492. {
  493. cursor_x = m_font->get_text_width(line);
  494. }
  495. else
  496. {
  497. cursor_x = m_font->get_text_width(line.substr(0, p));
  498. }
  499. context.color().draw_filled_rect(Rectf(Vector(3 + cursor_x, py),
  500. Sizef(2.0f, m_font->get_height() - 2)),
  501. Color(1.0f, 1.0f, 1.0f, 0.75f), layer);
  502. }
  503. }
  504. int skipLines = -m_offset;
  505. for (std::list<std::string>::iterator i = m_buffer.m_lines.begin(); i != m_buffer.m_lines.end(); ++i)
  506. {
  507. if (skipLines-- > 0) continue;
  508. lineNo++;
  509. float py = static_cast<float>(m_height - 4.0f - static_cast<float>(lineNo) * m_font->get_height());
  510. if (py < -m_font->get_height()) break;
  511. context.color().draw_text(m_font, *i, Vector(4.0f, py), ALIGN_LEFT, layer);
  512. }
  513. context.pop_transform();
  514. }
  515. ConsoleStreamBuffer ConsoleBuffer::s_outputBuffer;
  516. std::ostream ConsoleBuffer::output(&ConsoleBuffer::s_outputBuffer);
  517. /* EOF */