The Battle for Wesnoth  1.15.0-dev
game_launcher.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include "game_launcher.hpp"
16 #include "game_errors.hpp"
17 
18 #include "ai/manager.hpp" // for manager
20 #include "commandline_options.hpp" // for commandline_options
21 #include "config.hpp" // for config, etc
22 #include "cursor.hpp" // for set, CURSOR_TYPE::NORMAL
23 #include "exceptions.hpp" // for error
24 #include "filesystem.hpp" // for get_user_config_dir, etc
25 #include "game_classification.hpp" // for game_classification, etc
26 #include "game_config.hpp" // for path, no_delay, revision, etc
27 #include "game_config_manager.hpp" // for game_config_manager
28 #include "game_end_exceptions.hpp" // for LEVEL_RESULT, etc
29 #include "generators/map_generator.hpp" // for mapgen_exception
30 #include "gettext.hpp" // for _
32 #include "gui/dialogs/language_selection.hpp" // for language_selection
34 #include "gui/dialogs/message.hpp" //for show error message
36 #include "gui/dialogs/multiplayer/mp_host_game_prompt.hpp" //for host game prompt
38 #include "gui/dialogs/outro.hpp"
40 #include "gui/dialogs/transient_message.hpp" // for show_transient_message
41 #include "gui/dialogs/title_screen.hpp" // for show_debug_clock_button
42 #include "gui/widgets/settings.hpp" // for new_widgets
43 #include "gui/widgets/retval.hpp" // for window, etc
44 #include "language.hpp" // for language_def, etc
45 #include "log.hpp" // for LOG_STREAM, logger, general, etc
46 #include "map/exception.hpp"
47 #include "game_initialization/multiplayer.hpp" // for start_client, etc
49 #include "game_initialization/playcampaign.hpp" // for play_game, etc
50 #include "preferences/general.hpp" // for disable_preferences_save, etc
51 #include "preferences/display.hpp"
52 #include "savegame.hpp" // for clean_saves, etc
54 #include "sdl/surface.hpp" // for surface
55 #include "serialization/compression.hpp" // for format::NONE
56 #include "serialization/string_utils.hpp" // for split
57 #include "game_initialization/singleplayer.hpp" // for sp_create_mode
58 #include "statistics.hpp"
59 #include "tstring.hpp" // for operator==, operator!=
60 #include "utils/general.hpp" // for clamp
61 #include "video.hpp" // for CVideo
63 #include "wml_exception.hpp" // for wml_exception
64 
65 #include <algorithm> // for copy, max, min, stable_sort
66 #include <cstdlib> // for system
67 #include <iostream> // for operator<<, basic_ostream, etc
68 #include <new>
69 #include <utility> // for pair
70 #include <SDL.h> // for SDL_INIT_JOYSTICK, etc
71 
72 #ifdef DEBUG_WINDOW_LAYOUT_GRAPHS
73 #include "gui/widgets/debug.hpp"
74 #endif
75 
76 // For wesnothd launch code.
77 #ifdef _WIN32
78 
79 #define WIN32_LEAN_AND_MEAN
80 #include <windows.h>
81 
82 #endif // _WIN32
83 
85 
86 static lg::log_domain log_config("config");
87 #define ERR_CONFIG LOG_STREAM(err, log_config)
88 #define WRN_CONFIG LOG_STREAM(warn, log_config)
89 #define LOG_CONFIG LOG_STREAM(info, log_config)
90 
91 #define LOG_GENERAL LOG_STREAM(info, lg::general())
92 #define WRN_GENERAL LOG_STREAM(warn, lg::general())
93 #define DBG_GENERAL LOG_STREAM(debug, lg::general())
94 
95 static lg::log_domain log_mp_create("mp/create");
96 #define DBG_MP LOG_STREAM(debug, log_mp_create)
97 
98 static lg::log_domain log_network("network");
99 #define ERR_NET LOG_STREAM(err, log_network)
100 
101 static lg::log_domain log_enginerefac("enginerefac");
102 #define LOG_RG LOG_STREAM(info, log_enginerefac)
103 
104 game_launcher::game_launcher(const commandline_options& cmdline_opts, const char *appname) :
105  cmdline_opts_(cmdline_opts),
106  video_(new CVideo()),
107  font_manager_(),
108  prefs_manager_(),
109  image_manager_(),
110  main_event_context_(),
111  hotkey_manager_(),
112  music_thinker_(),
113  music_muter_(),
114  test_scenario_("test"),
115  screenshot_map_(),
116  screenshot_filename_(),
117  state_(),
118  play_replay_(false),
119  multiplayer_server_(),
120  jump_to_multiplayer_(false),
121  jump_to_campaign_(false, -1, "", ""),
122  jump_to_editor_(false),
123  load_data_()
124 {
125  bool no_music = false;
126  bool no_sound = false;
127 
128  // The path can be hardcoded and it might be a relative path.
129  if(!game_config::path.empty() &&
130 #ifdef _WIN32
131  // use c_str to ensure that index 1 points to valid element since c_str() returns null-terminated string
132  game_config::path.c_str()[1] != ':'
133 #else
134  game_config::path[0] != '/'
135 #endif
136  )
137  {
139  // font_manager_.update_font_path()
140  // To update the font path, destroy and recreate the manager
142  new (&font_manager_) font::manager();
143  }
144 
145  const std::string app_basename = filesystem::base_name(appname);
146  jump_to_editor_ = app_basename.find("editor") != std::string::npos;
147 
148  if (cmdline_opts_.core_id) {
150  }
151  if (cmdline_opts_.campaign) {
152  jump_to_campaign_.jump_ = true;
154  std::cerr << "selected campaign id: [" << jump_to_campaign_.campaign_id_ << "]\n";
155 
158  std::cerr << "selected difficulty: [" << jump_to_campaign_.difficulty_ << "]\n";
159  }
160  else
161  jump_to_campaign_.difficulty_ = -1; // let the user choose the difficulty
162 
165  std::cerr << "selected scenario id: [" << jump_to_campaign_.scenario_id_ << "]\n";
166  }
167  }
168  if (cmdline_opts_.clock)
170  if (cmdline_opts_.debug) {
172  game_config::mp_debug = true;
173  }
174 #ifdef DEBUG_WINDOW_LAYOUT_GRAPHS
175  if (cmdline_opts_.debug_dot_domain)
176  gui2::debug_layout_graph::set_domain (*cmdline_opts_.debug_dot_domain);
177  if (cmdline_opts_.debug_dot_level)
179 #endif
180  if (cmdline_opts_.editor)
181  {
182  jump_to_editor_ = true;
183  if (!cmdline_opts_.editor->empty())
184  load_data_.reset(new savegame::load_game_metadata{ *cmdline_opts_.editor });
185  }
186  if (cmdline_opts_.fps)
189  video().set_fullscreen(true);
190  if (cmdline_opts_.load)
192  if (cmdline_opts_.max_fps) {
193  int fps = utils::clamp(*cmdline_opts_.max_fps, 1, 1000);
194  fps = 1000 / fps;
195  // increase the delay to avoid going above the maximum
196  if(1000 % fps != 0) {
197  ++fps;
198  }
200  }
202  no_sound = true;
204  }
206  gui2::new_widgets = true;
208  game_config::no_delay = true;
210  no_music = true;
212  no_sound = true;
214  const int xres = std::get<0>(*cmdline_opts_.resolution);
215  const int yres = std::get<1>(*cmdline_opts_.resolution);
216  if(xres > 0 && yres > 0) {
217  preferences::_set_resolution(point(xres, yres));
219  }
220  }
222  //TODO it could be simplified to use cmdline_opts_ directly if there is no other way to enter screenshot mode
225  no_sound = true;
227  }
228  if (cmdline_opts_.server){
229  jump_to_multiplayer_ = true;
230  //Do we have any server specified ?
231  if (!cmdline_opts_.server->empty())
233  else //Pick the first server in config
234  {
237  else
238  multiplayer_server_ = "";
239  }
240  if (cmdline_opts_.username) {
243  if (cmdline_opts_.password) {
245  preferences::set_password(*cmdline_opts.server, *cmdline_opts.username, *cmdline_opts_.password);
246  }
247  }
248  }
249  if (cmdline_opts_.test)
250  {
251  if (!cmdline_opts_.test->empty())
253  }
255  {
256  if (!cmdline_opts_.unit_test->empty()) {
258  }
259 
260  }
262  video().set_fullscreen(false);
264  load_data_->show_replay = true;
267 
268  std::cerr
269  << "\nData directory: " << filesystem::sanitize_path(game_config::path)
270  << "\nUser configuration directory: " << filesystem::sanitize_path(filesystem::get_user_config_dir())
271  << "\nUser data directory: " << filesystem::sanitize_path(filesystem::get_user_data_dir())
272  << "\nCache directory: " << filesystem::sanitize_path(filesystem::get_cache_dir())
273  << '\n';
274  std::cerr << '\n';
275 
276  // disable sound in nosound mode, or when sound engine failed to initialize
277  if (no_sound || ((preferences::sound_on() || preferences::music_on() ||
279  !sound::init_sound())) {
280  preferences::set_sound(false);
281  preferences::set_music(false);
284  }
285  else if (no_music) { // else disable the music in nomusic mode
286  preferences::set_music(false);
287  }
288 }
289 
291 {
292  if(!::load_language_list())
293  return false;
294 
295  language_def locale;
296  if(cmdline_opts_.language) {
297  std::vector<language_def> langs = get_languages();
298  for(const language_def & def : langs) {
299  if(def.localename == *cmdline_opts_.language) {
300  locale = def;
301  break;
302  }
303  }
304  if(locale.localename.empty()) {
305  std::cerr << "Language symbol '" << *cmdline_opts_.language << "' not found.\n";
306  return false;
307  }
308  } else {
309  locale = get_locale();
310  }
311  ::set_language(locale);
312 
313  return true;
314 }
315 
317 {
318  // Handle special commandline launch flags
321  std::cerr << "--nogui flag is only valid with --multiplayer or --screenshot or --plugin flags\n";
322  return false;
323  }
324  video().make_fake();
325  game_config::no_delay = true;
326  return true;
327  }
328 
329  // Initialize a new window
330  video().init_window();
331 
332  // Set window title and icon
334 
335 #if !(defined(__APPLE__))
336  surface icon(image::get_image("icons/icon-game.png", image::UNSCALED));
337  if(icon != nullptr) {
338 
339  video().set_window_icon(icon);
340  }
341 #endif
342  return true;
343 }
344 
346 {
347  bool error = false;
348 
349  std::cerr << "Checking lua scripts... ";
350 
352  plugins_manager::get()->get_kernel_base()->load_package(); //load the "package" package, so that scripts can get what packages they want
353  }
354 
355  // get the application lua kernel, load and execute script file, if script file is present
357  {
359 
360  if (!sf->fail()) {
361  /* Cancel all "jumps" to editor / campaign / multiplayer */
362  jump_to_multiplayer_ = false;
363  jump_to_editor_ = false;
364  jump_to_campaign_.jump_ = false;
365 
366  std::string full_script((std::istreambuf_iterator<char>(*sf)), std::istreambuf_iterator<char>());
367 
368  std::cerr << "\nRunning lua script: " << *cmdline_opts_.script_file << std::endl;
369 
370  plugins_manager::get()->get_kernel_base()->run(full_script.c_str());
371  } else {
372  std::cerr << "Encountered failure when opening script '" << *cmdline_opts_.script_file << "'\n";
373  error = true;
374  }
375  }
376 
378  {
379  std::string filename = *cmdline_opts_.plugin_file;
380 
381  std::cerr << "Loading a plugin file'" << filename << "'...\n";
382 
384 
385  try {
386  if (sf->fail()) {
387  throw std::runtime_error("failed to open plugin file");
388  }
389 
390  /* Cancel all "jumps" to editor / campaign / multiplayer */
391  jump_to_multiplayer_ = false;
392  jump_to_editor_ = false;
393  jump_to_campaign_.jump_ = false;
394 
395  std::string full_plugin((std::istreambuf_iterator<char>(*sf)), std::istreambuf_iterator<char>());
396 
398 
399  std::size_t i = pm.add_plugin(filename, full_plugin);
400 
401  for (std::size_t j = 0 ; j < pm.size(); ++j) {
402  std::cerr << j << ": " << pm.get_name(j) << " -- " << pm.get_detailed_status(j) << std::endl;
403  }
404 
405  std::cerr << "Starting a plugin...\n";
406  pm.start_plugin(i);
407 
408  for (std::size_t j = 0 ; j < pm.size(); ++j) {
409  std::cerr << j << ": " << pm.get_name(j) << " -- " << pm.get_detailed_status(j) << std::endl;
410  }
411 
412  plugins_context pc("init");
413 
414  for (std::size_t repeat = 0; repeat < 5; ++repeat) {
415  std::cerr << "Playing a slice...\n";
416  pc.play_slice();
417 
418  for (std::size_t j = 0 ; j < pm.size(); ++j) {
419  std::cerr << j << ": " << pm.get_name(j) << " -- " << pm.get_detailed_status(j) << std::endl;
420  }
421  }
422 
423  return true;
424  } catch (const std::exception & e) {
425  gui2::show_error_message(std::string("When loading a plugin, error:\n") + e.what());
426  error = true;
427  }
428  }
429 
430  if (!error) {
431  std::cerr << "ok\n";
432  }
433 
434  return !error;
435 }
436 
437 void game_launcher::set_test(const std::string& id)
438 {
439  state_.clear();
440  state_.classification().campaign_type = game_classification::CAMPAIGN_TYPE::TEST;
442 
443  state_.mp_settings().mp_era = "era_default";
444 
446  config {"next_scenario", id}
447  );
448 }
449 
451 {
452  static bool first_time = true;
453 
454  if(!cmdline_opts_.test) {
455  return true;
456  }
457  if(!first_time)
458  return false;
459 
460  first_time = false;
461 
464 
466  load_game_config_for_game(state_.classification());
467 
468  try {
470  ccontroller.play_game();
471  } catch(const savegame::load_game_exception &e) {
472  load_data_.reset(new savegame::load_game_metadata(std::move(e.data_)));
473  return true;
474  }
475 
476  return false;
477 }
478 
479 // Same as play_test except that we return the results of play_game.
481 {
482  static bool first_time_unit = true;
483 
484  if(!cmdline_opts_.unit_test) {
485  return 0;
486  }
487  if(!first_time_unit)
488  return 0;
489 
490  first_time_unit = false;
491 
492  state_.classification().campaign_type = game_classification::CAMPAIGN_TYPE::TEST;
495  config {"next_scenario", test_scenario_}
496  );
497 
498 
500  load_game_config_for_game(state_.classification());
501 
502  try {
503  campaign_controller ccontroller(state_, game_config_manager::get()->game_config(), game_config_manager::get()->terrain_types(), true);
504  LEVEL_RESULT res = ccontroller.play_game();
505  if (!(res == LEVEL_RESULT::VICTORY) || lg::broke_strict()) {
506  return 1;
507  }
508  } catch(const wml_exception& e) {
509  std::cerr << "Caught WML Exception:" << e.dev_message << std::endl;
510  return 1;
511  }
512 
514 
516  return 0; //we passed, huzzah!
517 
519  save.save_game_automatic(false, "unit_test_replay"); //false means don't check for overwrite
520 
521  load_data_.reset(new savegame::load_game_metadata{ "unit_test_replay" , "", true, true, false });
522 
523  if (!load_game()) {
524  std::cerr << "Failed to load the replay!" << std::endl;
525  return 3; //failed to load replay
526  }
527 
528  try {
529  campaign_controller ccontroller(state_, game_config_manager::get()->game_config(), game_config_manager::get()->terrain_types(), true);
530  LEVEL_RESULT res = ccontroller.play_replay();
531  if (!(res == LEVEL_RESULT::VICTORY) || lg::broke_strict()) {
532  std::cerr << "Observed failure on replay" << std::endl;
533  return 4;
534  }
535  } catch(const wml_exception& e) {
536  std::cerr << "WML Exception while playing replay: " << e.dev_message << std::endl;
537  return 4; //failed with an error during the replay
538  }
539 
540  return 0; //we passed, huzzah!
541 }
542 
544 {
546  return true;
547  }
548 
550 
552 
555  return false;
556 }
557 
559 {
561  return true;
562  }
563 
564  state_.classification().campaign_type = game_classification::CAMPAIGN_TYPE::MULTIPLAYER;
565  DBG_GENERAL << "Current campaign type: " << state_.classification().campaign_type << std::endl;
566 
567  try {
569  load_game_config_for_game(state_.classification());
570  } catch(const config::error& e) {
571  std::cerr << "Error loading game config: " << e.what() << std::endl;
572  return false;
573  }
574 
575  // A default output filename
576  std::string outfile = "wesnoth_image.bmp";
577 
578  // If a output path was given as an argument, use that instead
580  outfile = *cmdline_opts_.render_image_dst;
581  }
582 
584  exit(1);
585  }
586 
587  return false;
588 }
589 
591 {
592  return !!load_data_;
593 }
594 
596 {
597  assert(game_config_manager::get());
598 
599  DBG_GENERAL << "Current campaign type: " << state_.classification().campaign_type << std::endl;
600 
602  if (load_data_) {
603  std::unique_ptr<savegame::load_game_metadata> load_data = std::move(load_data_);
604  load.data() = std::move(*load_data);
605  }
606 
607  try {
608  if(!load.load_game()) {
609  return false;
610  }
611 
612  try {
614  load_game_config_for_game(state_.classification());
615  } catch(const config::error&) {
616  return false;
617  }
618 
619  load.set_gamestate();
620 
621  } catch(const config::error& e) {
622  if(e.message.empty()) {
623  gui2::show_error_message(_("The file you have tried to load is corrupt"));
624  }
625  else {
626  gui2::show_error_message(_("The file you have tried to load is corrupt: '") + e.message + '\'');
627  }
628  return false;
629  } catch(const wml_exception& e) {
630  e.show();
631  return false;
632  } catch(const filesystem::io_exception& e) {
633  if(e.message.empty()) {
634  gui2::show_error_message(_("File I/O Error while reading the game"));
635  } else {
636  gui2::show_error_message(_("File I/O Error while reading the game: '") + e.message + '\'');
637  }
638  return false;
639  } catch(const game::error& e) {
640  if(e.message.empty()) {
641  gui2::show_error_message(_("The file you have tried to load is corrupt"));
642  }
643  else {
644  gui2::show_error_message(_("The file you have tried to load is corrupt: '") + e.message + '\'');
645  }
646  return false;
647  }
648 
649  play_replay_ = load.data().show_replay;
650  LOG_CONFIG << "is middle game savefile: " << (state_.is_mid_game_save() ? "yes" : "no") << "\n";
651  LOG_CONFIG << "show replay: " << (play_replay_ ? "yes" : "no") << "\n";
652  // in case load.data().show_replay && !state_.is_mid_game_save()
653  // there won't be any turns to replay, but the
654  // user gets to watch the intro sequence again ...
655 
656  if(state_.is_mid_game_save() && load.data().show_replay)
657  {
659  }
660 
661  if(state_.classification().campaign_type == game_classification::CAMPAIGN_TYPE::MULTIPLAYER) {
663  }
664 
665  if (load.data().cancel_orders) {
667  }
668 
669  return true;
670 }
671 
673 {
674  state_.clear();
675  state_.classification().campaign_type = game_classification::CAMPAIGN_TYPE::TUTORIAL;
676  state_.classification().campaign_define = "TUTORIAL";
677  state_.mp_settings().mp_era = "era_default";
679  config {"next_scenario", "tutorial"}
680  );
681 
682 }
683 
684 void game_launcher::mark_completed_campaigns(std::vector<config> &campaigns)
685 {
686  for (config &campaign : campaigns) {
687  campaign["completed"] = preferences::is_campaign_completed(campaign["id"]);
688  }
689 }
690 
692 {
693  state_.clear();
695  play_replay_ = false;
696 
698 }
699 
701 {
703 }
704 
706 {
708  if(new_campaign()) {
709  jump_to_campaign_.jump_ = false;
711  }else{
712  jump_to_campaign_.jump_ = false;
713  return false;
714  }
715  }
716  return true;
717 }
718 
720 {
722  jump_to_multiplayer_ = false;
724  ;
725  }else{
726  return false;
727  }
728  }
729  return true;
730 }
731 
733 {
734  if(jump_to_editor_){
735  jump_to_editor_ = false;
736  std::unique_ptr<savegame::load_game_metadata> load_data = std::move(load_data_);
737  if (start_editor(filesystem::normalize_path(load_data ? load_data->filename : "")) ==
739  {
740  return false;
741  }
742  }
743  return true;
744 }
745 
747 {
748  const std::string wesnothd_program =
751 
752  std::string config = filesystem::get_user_config_dir() + "/lan_server.cfg";
753  if (!filesystem::file_exists(config)) {
754  // copy file if it isn't created yet
756  }
757 
758 #ifndef _WIN32
759  std::string command = "\"" + wesnothd_program +"\" -c \"" + config + "\" -d -t 2 -T 5";
760 #else
761  // start wesnoth as background job
762  std::string command = "cmd /C start \"wesnoth server\" /B \"" + wesnothd_program + "\" -c \"" + config + "\" -t 2 -T 5";
763  // Make sure wesnothd's console output is visible on the console window by
764  // disabling SDL's stdio redirection code for this and future child
765  // processes. No need to bother cleaning this up because it's only
766  // meaningful to SDL applications during pre-main initialization.
767  SetEnvironmentVariableA("SDL_STDIO_REDIRECT", "0");
768 #endif
769  LOG_GENERAL << "Starting wesnothd: "<< command << "\n";
770  if (std::system(command.c_str()) == 0) {
771  // Give server a moment to start up
772  SDL_Delay(50);
773  return;
774  }
776 
777  // Couldn't start server so throw error
778  WRN_GENERAL << "Failed to run server start script" << std::endl;
779  throw game::mp_server_error("Starting MP server failed!");
780 }
781 
783 {
784  state_.clear();
785  state_.classification().campaign_type = game_classification::CAMPAIGN_TYPE::MULTIPLAYER;
786 
787  try {
788  if (res == MP_HOST)
789  {
790  try {
791  start_wesnothd();
792  } catch(const game::mp_server_error&)
793  {
795 
796  try {
797  start_wesnothd();
798  } catch(const game::mp_server_error&)
799  {
800  return false;
801  }
802  }
803 
804 
805  }
806 
807  // If a server address wasn't specified, prompt for it now.
808  if(res != MP_LOCAL && multiplayer_server_.empty()) {
809  if(!gui2::dialogs::mp_connect::execute()) {
810  return false;
811  }
812 
813  // The prompt saves its input to preferences.
815 
816  if(multiplayer_server_ != preferences::server_list().front().address) {
818  }
819  }
820 
821  //create_engine already calls game_config_manager::get()->load_config but maybe its better to have MULTIPLAYER defined while we are in the lobby.
823 
824  events::discard_input(); // prevent the "keylogger" effect
826 
827  if(res == MP_LOCAL) {
830  } else {
833  multiplayer_server_.clear();
834  }
835 
836  } catch(const wesnothd_rejected_client_error& e) {
838  } catch(const game::mp_server_error& e) {
839  gui2::show_error_message(_("Error while starting server: ") + e.message);
840  } catch(const game::load_game_failed& e) {
841  gui2::show_error_message(_("The game could not be loaded: ") + e.message);
842  } catch(const game::game_error& e) {
843  gui2::show_error_message(_("Error while playing the game: ") + e.message);
844  } catch (const mapgen_exception& e) {
845  gui2::show_error_message(_("Map generator error: ") + e.message);
846  } catch(const wesnothd_error& e) {
847  if(!e.message.empty()) {
848  ERR_NET << "caught network error: " << e.message << std::endl;
849 
850  std::string user_msg;
851  auto conn_err = dynamic_cast<const wesnothd_connection_error*>(&e);
852 
853  if(conn_err) {
854  // The wesnothd_connection_error subclass is only thrown with messages
855  // from boost::system::error_code which we can't translate ourselves.
856  // It's also the originator of the infamous EOF error that happens when
857  // the server dies. <https://github.com/wesnoth/wesnoth/issues/3005>. It
858  // will provide a translated string instead of that when it happens.
859  user_msg = !conn_err->user_message.empty()
860  ? conn_err->user_message
861  : _("Connection failed: ") + e.message;
862  } else {
863  // This will be a message from the server itself, which we can
864  // probably translate.
865  user_msg = translation::gettext(e.message.c_str());
866  }
867 
868  gui2::show_error_message(user_msg);
869  } else {
870  ERR_NET << "caught network error" << std::endl;
871  }
872  } catch(const config::error& e) {
873  if(!e.message.empty()) {
874  ERR_CONFIG << "caught config::error: " << e.message << std::endl;
876  } else {
877  ERR_CONFIG << "caught config::error" << std::endl;
878  }
879  } catch(const incorrect_map_format_error& e) {
880  gui2::show_error_message(_("The game map could not be loaded: ") + e.message);
881  } catch (const savegame::load_game_exception & e) {
882  load_data_.reset(new savegame::load_game_metadata(std::move(e.data_)));
883  //this will make it so next time through the title screen loop, this game is loaded
884  } catch(const wml_exception& e) {
885  e.show();
886  } catch (const game::error & e) {
887  std::cerr << "caught game::error...\n";
888  gui2::show_error_message(_("Error: ") + e.message);
889  }
890 
891  return false;
892 }
893 
895 {
897  return true;
898  }
899 
900  DBG_MP << "starting multiplayer game from the commandline" << std::endl;
901 
902  // These are all the relevant lines taken literally from play_multiplayer() above
903  state_.clear();
904  state_.classification().campaign_type = game_classification::CAMPAIGN_TYPE::MULTIPLAYER;
905 
907  load_game_config_for_game(state_.classification());
908 
909  events::discard_input(); // prevent the "keylogger" effect
911 
914 
915  return false;
916 }
917 
919 {
921  dlg.show();
922  if (dlg.get_retval() != gui2::retval::OK) return false;
923 
926  }
927 
928  return true;
929 }
930 
932 {
934 }
935 
937 {
938  assert(!load_data_);
939  if(play_replay_)
940  {
941  play_replay();
942  return;
943  }
944 
945  gui2::dialogs::loading_screen::display([this, reload]() {
946 
948  if(reload == RELOAD_DATA) {
949  try {
951  load_game_config_for_game(state_.classification());
952  } catch(const config::error&) {
953  return;
954  }
955  }
956  });
957 
958  try {
960  LEVEL_RESULT result = ccontroller.play_game();
961  ai::manager::singleton_ = nullptr;
962  // don't show The End for multiplayer scenario
963  // change this if MP campaigns are implemented
964  if(result == LEVEL_RESULT::VICTORY && !state_.classification().is_normal_mp_game()) {
966 
967  gui2::dialogs::outro::display(state_.classification().end_text, state_.classification().end_text_duration);
968 
970  gui2::dialogs::end_credits::display(state_.classification().campaign);
971  }
972  }
973  } catch (const savegame::load_game_exception &e) {
974  load_data_.reset(new savegame::load_game_metadata(std::move(e.data_)));
975  //this will make it so next time through the title screen loop, this game is loaded
976  } catch(const wml_exception& e) {
977  e.show();
978  } catch(const mapgen_exception& e) {
979  gui2::show_error_message(_("Map generator error: ") + e.message);
980  }
981 }
982 
984 {
985  assert(!load_data_);
986  try {
988  ccontroller.play_replay();
989  } catch (const savegame::load_game_exception &e) {
990  load_data_.reset(new savegame::load_game_metadata(std::move(e.data_)));
991  //this will make it so next time through the title screen loop, this game is loaded
992  } catch(const wml_exception& e) {
993  e.show();
994  }
995 }
996 
998 {
999  while(true){
1001 
1003 
1005  game_config_manager::get()->game_config(), filename);
1006 
1007  if(res != editor::EXIT_RELOAD_DATA)
1008  return res;
1009 
1011  }
1012  return editor::EXIT_ERROR; // not supposed to happen
1013 }
1014 
1016 {
1017  load_data_.reset();
1018 }
1019 
1021 {
1022  try {
1024  } catch (...) {}
1025 }
boost::optional< std::string > core_id
Non-empty if –core was given on the command line. Chooses the core to be loaded. ...
constexpr const T & clamp(const T &value, const T &min, const T &max)
Definition: general.hpp:31
an error occurred inside the underlying network communication code (boost asio) TODO: find a short na...
CVideo & video()
boost::optional< std::string > script_file
File to load lua script from.
An error occurred during when trying to coommunicate with the wesnothd server.
bool new_widgets
Do we wish to use the new library or not.
Definition: settings.cpp:21
surface get_image(const image::locator &i_locator, TYPE type)
function to get the surface corresponding to an image.
Definition: image.cpp:989
unsigned int end_text_duration
for how long the end-of-campaign text is shown
void set_window_icon(surface &icon)
Sets the icon of the main window.
Definition: video.cpp:303
bool set_sound(bool ison)
Definition: general.cpp:665
void close_sound()
Definition: sound.cpp:457
std::string get_program_invocation(const std::string &program_name)
Returns the appropriate invocation for a Wesnoth-related binary, assuming that it is located in the s...
LEVEL_RESULT play_replay()
bool load_game()
Load a game with pre-setting information for the load-game dialog.
Definition: savegame.cpp:158
static lg::log_domain log_mp_create("mp/create")
bool turn_bell()
Definition: general.cpp:607
void discard_input()
Discards all input events.
Definition: events.cpp:729
bool end_credits
whether to show the standard credits at the end
void start_local_game_commandline(const config &game_config, saved_game &state, const commandline_options &cmdline_opts)
Starts a multiplayer game in single-user mode.
Error used when the client is rejected by the MP server.
LEVEL_RESULT play_game()
bool is_mid_game_save() const
Definition: saved_game.hpp:93
bool nogui
True if –nogui was given on the command line. Disables GUI.
The class for loading a savefile.
Definition: savegame.hpp:96
bool script_unsafe_mode
Whether to load the "package" package for the scripting environment. (This allows to load arbitrary l...
void set(CURSOR_TYPE type)
Use the default parameter to reset cursors.
Definition: cursor.cpp:173
void _set_maximized(bool ison)
Definition: general.cpp:407
std::string label
Name of the game (e.g.
void disable_preferences_save()
Definition: general.cpp:248
std::string filename
Name of the savefile to be loaded.
Definition: savegame.hpp:40
void set_gamestate()
Generate the gamestate out of the loaded game config.
Definition: savegame.cpp:262
bool cancel_orders
State of the "cancel_orders" checkbox in the load-game dialog.
Definition: savegame.hpp:49
std::vector< game_tip > load(const config &cfg)
Loads the tips from a config.
Definition: tips.cpp:35
Add a special kind of assert to validate whether the input from WML doesn&#39;t contain any problems that...
static bool file_exists(const fs::path &fpath)
Definition: filesystem.cpp:300
#define DBG_MP
static l_noret error(LoadState *S, const char *why)
Definition: lundump.cpp:39
Error used when game loading fails.
Definition: game_errors.hpp:30
void show_wesnothd_server_search()
Definition: display.cpp:150
void show_preferences()
Definition: video.hpp:36
bool is_campaign_completed(const std::string &campaign_id)
Definition: game.cpp:323
std::string network_host()
Definition: game.cpp:390
static void progress(loading_stage stage=loading_stage::none)
void unify_controllers()
Definition: saved_game.cpp:640
const language_def & get_locale()
Definition: language.cpp:263
filesystem::scoped_istream istream_file(const std::string &fname, bool treat_failure_as_error)
Definition: filesystem.cpp:894
void _set_resolution(const point &res)
Definition: general.cpp:401
void set_language(const std::string &language, const std::vector< std::string > *)
Definition: gettext.cpp:449
boost::optional< std::string > username
Non-empty if –username was given on the command line. Forces Wesnoth to use this network username...
void mark_completed_campaigns(std::vector< config > &campaigns)
save_result save_image(const locator &i_locator, const std::string &filename)
Definition: image.cpp:1257
boost::optional< std::string > campaign_scenario
Non-empty if –campaign-scenario was given on the command line. Chooses starting scenario in the camp...
std::string get_default_title_string()
void set_min_translation_percent(int percent)
Definition: language.cpp:132
void clear()
Definition: saved_game.cpp:730
boost::optional< int > max_fps
Max FPS specified by –max-fps option.
bool fps
True if –fps was given on the command line. Shows number of fps.
bool change_language()
void set_network_host(const std::string &host)
Definition: game.cpp:400
std::string normalize_path(const std::string &fpath, bool normalize_separators, bool resolve_dot_entries)
Returns the absolute path of a file.
bool noreplaycheck
True if –noreplaycheck was given on the command line. Dependent on –unit.
Contains the exception interfaces used to signal completion of a scenario, campaign or turn...
bool show(const unsigned auto_close_time=0)
Shows the window.
std::string jump_to_campaign_id() const
-file util.hpp
Definitions for the interface to Wesnoth Markup Language (WML).
load_game_metadata & data()
Definition: savegame.hpp:115
boost::optional< std::string > plugin_file
File to load a lua plugin (similar to a script) from. Experimental / may replace script.
bool sound_on()
Definition: general.cpp:660
#define DBG_GENERAL
load_game_metadata data_
Definition: savegame.hpp:89
std::string user_message
User-friendly and potentially translated message for use in the UI.
jump_to_campaign_info jump_to_campaign_
boost::optional< std::string > load
Non-empty if –load was given on the command line. Savegame specified to load after start...
bool init_sound()
Definition: sound.cpp:405
static lg::log_domain log_config("config")
bool broke_strict()
Definition: log.cpp:171
bool play_multiplayer(mp_selection res)
std::string get_cwd()
Definition: filesystem.cpp:783
bool with_replay
True if –with-replay was given on the command line. Shows replay of the loaded file.
void set_mp_server_program_name(const std::string &path)
Definition: game.cpp:512
void set_window_title(const std::string &title)
Sets the title of the main window.
Definition: video.cpp:297
void show() const
Shows the error in a dialog.
bool play_render_image_mode()
bool headless_unit_test
True if –unit is used and –showgui is not present.
static game_config_manager * get()
void make_fake()
Definition: video.cpp:108
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:89
#define ERR_CONFIG
font::manager font_manager_
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
static lg::log_domain log_enginerefac("enginerefac")
void start_plugin(std::size_t idx)
Definition: manager.cpp:101
This file contains the settings handling of the widget library.
std::string get_name(std::size_t idx)
Definition: manager.cpp:94
bool nodelay
True if –nodelay was given on the command line.
std::string get_user_data_dir()
Definition: filesystem.cpp:749
void write_file(const std::string &fname, const std::string &data)
Throws io_exception if an error occurs.
Definition: filesystem.cpp:955
void show_transient_message(const std::string &title, const std::string &message, const std::string &image, const bool message_use_markup, const bool title_use_markup)
Shows a transient message to the user.
static lg::log_domain log_network("network")
std::string multiplayer_server_
std::string campaign_define
If there is a define the campaign uses to customize data.
editor::EXIT_STATUS start_editor()
void set_login(const std::string &login)
boost::optional< unsigned int > translation_percent
Non-empty if –all-translations or –translations-over is given on the command line.
std::string localename
Definition: language.hpp:52
void load_package()
Loads the package library into lua environment.
bool windowed
True if –windowed was given on the command line. Starts Wesnoth in windowed mode.
boost::optional< std::string > editor
Non-empty if –editor was given on the command line. Goes directly into editor. If string is longer t...
void set_show_fps(bool value)
Definition: general.cpp:832
const std::vector< game_config::server_info > & server_list()
Definition: game.cpp:373
std::unique_ptr< std::istream > scoped_istream
Definition: filesystem.hpp:39
void set_test(const std::string &id)
std::string end_text
end-of-campaign text
void set_password(const std::string &server, const std::string &login, const std::string &key)
void set_core_id(const std::string &core_id)
Definition: general.cpp:321
std::string path
Definition: game_config.cpp:39
boost::optional< std::string > test
Non-empty if –test was given on the command line. Goes directly into test mode, into a scenario...
void init_textdomains(const config &cfg)
Initializes the list of textdomains from a configuration object.
Definition: language.cpp:303
bool multiplayer
True if –multiplayer was given on the command line. Goes directly into multiplayer mode...
Managing the AIs lifecycle - headers.
bool UI_sound_on()
Definition: general.cpp:631
std::string sanitize_path(const std::string &path)
Sanitizes a path to remove references to the user&#39;s name.
if(win_locale=="af") win_locale
game_launcher(const commandline_options &cmdline_opts, const char *appname)
std::string campaign
The id of the campaign being played.
const char * what() const noexcept
Definition: exceptions.hpp:37
bool clock
True if –clock was given on the command line. Enables.
static UNUSEDNOWARN std::string gettext(const char *str)
Definition: gettext.hpp:66
Error used for any general game error, e.g.
Definition: game_errors.hpp:46
std::string read_file(const std::string &fname)
Basic disk I/O - read file.
Definition: filesystem.cpp:886
std::string screenshot_map_
std::unique_ptr< savegame::load_game_metadata > load_data_
#define LOG_CONFIG
void set_draw_delay(int value)
Definition: general.cpp:842
boost::optional< std::string > render_image
Image path to render. First parameter after –render-image.
void start_client(const config &game_config, saved_game &state, const std::string &host)
Starts a multiplayer game in client mode.
void set_level(const std::string &value)
Definition: game.cpp:714
boost::optional< int > campaign_difficulty
Non-empty if –campaign-difficulty was given on the command line. Numerical difficulty of the campaig...
Helper class, don&#39;t construct this directly.
bool nomusic
True if –nomusic was given on the command line. Disables music.
void run(char const *prog, int nArgs=0)
Runs a plain script.
std::string dev_message
The message for developers telling which problem was triggered, this shouldn&#39;t be translated...
std::string get_cache_dir()
Definition: filesystem.cpp:754
Exception used to signal that the user has decided to abortt a game, and to load another game instead...
Definition: savegame.hpp:74
static void display(const config &game_cfg, const preferences::PREFERENCE_VIEW initial_view=preferences::VIEW_DEFAULT)
The display function – see modal_dialog for more information.
#define WRN_GENERAL
boost::optional< std::string > password
Non-empty if –password was given on the command line. Forces Wesnoth to use this network password...
bool is_loading() const
bool debug
True if –debug was given on the command line. Enables debug mode.
void start_local_game(const config &game_config, saved_game &state)
Starts a multiplayer game in single-user mode.
std::size_t i
Definition: function.cpp:933
std::size_t add_plugin(const std::string &name, const std::string &prog)
Definition: manager.cpp:118
std::string get_mp_server_program_name()
Definition: game.cpp:524
boost::optional< std::string > screenshot_map_file
Map file to make a screenshot of. First parameter given after –screenshot.
std::string get_wml_location(const std::string &filename, const std::string &current_dir)
Returns a complete path to the actual WML file or directory or an empty string if the file isn&#39;t pres...
void clean_saves(const std::string &label)
Delete all autosaves of a certain scenario.
Definition: savegame.cpp:67
void load_game_config_for_create(bool is_mp, bool is_test=false)
Game configuration data as global variables.
Definition: build_info.cpp:46
bool new_widgets
True if –new-widgets was given on the command line. Hidden option to enable the new widget toolkit...
#define ERR_NET
void init_window()
Initializes a new SDL window instance, taking into account any preiously saved states.
Definition: video.cpp:121
EXIT_STATUS start(const config &game_conf, const std::string &filename, bool take_screenshot, const std::string &screenshot_filename)
Main interface for launching the editor from the title screen.
Definition: editor_main.cpp:28
An exception object used when an IO error occurs.
Definition: filesystem.hpp:48
bool music_on()
Definition: general.cpp:683
void add_completed_campaign(const std::string &campaign_id, const std::string &difficulty_level)
Definition: game.cpp:319
bool load_language_list()
Definition: language.cpp:92
language_list get_languages()
Definition: language.cpp:115
void set_fullscreen(bool ison)
Definition: video.cpp:426
Holds a 2D point.
Definition: point.hpp:23
bool set_UI_sound(bool ison)
Definition: general.cpp:636
bool goto_multiplayer()
Declarations for File-IO.
static void save(LexState *ls, int c)
Definition: llex.cpp:57
void launch_game(RELOAD_GAME_DATA reload=RELOAD_DATA)
Class for replay saves (either manually or automatically).
Definition: savegame.hpp:256
-file mapgen.hpp
std::string difficulty
The difficulty level the game is being played on.
boost::optional< std::string > render_image_dst
Output file to put rendered image path in. Optional second parameter after –render-image.
std::string base_name(const std::string &file, const bool remove_extension)
Returns the base filename of a file, with directory name stripped.
static manager * singleton_
Definition: manager.hpp:455
std::string get_user_config_dir()
Definition: filesystem.cpp:720
bool screenshot
True if –screenshot was given on the command line. Starts Wesnoth in screenshot mode.
void cancel_orders()
Definition: saved_game.cpp:623
boost::optional< std::string > unit_test
Non-empty if –unit was given on the command line. Goes directly into unit test mode, into a scenario, if specified.
std::string get_detailed_status(std::size_t idx)
Definition: manager.cpp:83
bool show_replay
State of the "show_replay" checkbox in the load-game dialog.
Definition: savegame.hpp:46
static void display(std::function< void()> f)
const commandline_options & cmdline_opts_
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:29
game_classification & classification()
Definition: saved_game.hpp:55
std::string screenshot_filename_
void set_carryover_sides_start(config carryover_sides_start)
Definition: saved_game.cpp:125
saved_game state_
bool set_music(bool ison)
Definition: general.cpp:688
Standard logging facilities (interface).
boost::optional< std::string > screenshot_output_file
Output file to put screenshot in. Second parameter given after –screenshot.
bool set_turn_bell(bool ison)
Definition: general.cpp:612
void clear_loaded_game()
std::string message
Definition: exceptions.hpp:31
bool fullscreen
True if –fullscreen was given on the command line. Starts Wesnoth in fullscreen mode.
void show_error_message(const std::string &msg, bool message_use_markup)
Shows an error message to the user.
Definition: message.cpp:203
boost::optional< std::string > language
Non-empty if –language was given on the command line. Sets the language for this session...
#define e
std::string test_scenario_
bool nosound
True if –nosound was given on the command line. Disables sound.
boost::optional< std::pair< int, int > > resolution
Pair of AxB values specified after –resolution. Changes Wesnoth resolution.
Dialog was closed with the OK button.
Definition: retval.hpp:34
bool play_screenshot_mode()
lua_kernel_base * get_kernel_base()
Definition: manager.cpp:63
boost::optional< std::string > campaign
Non-empty if –campaign was given on the command line. ID of the campaign we want to start...
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
void clear_current_scenario()
Definition: statistics.cpp:646
mp_game_settings & mp_settings()
Multiplayer parameters for this game.
Definition: saved_game.hpp:59
bool enter_create_mode(saved_game &state, jump_to_campaign_info jump_to_campaign)
bool init_lua_script()
std::vector< server_info > server_list
Definition: game_config.cpp:93
bool save_game_automatic(bool ask_for_overwrite=false, const std::string &filename="")
Saves a game without user interaction, unless the file exists and it should be asked to overwrite it...
Definition: savegame.cpp:337
boost::optional< std::string > server
Non-empty if –server was given on the command line. Connects Wesnoth to specified server...
bool play_multiplayer_commandline()
void set_debug(bool new_debug)
static plugins_manager * get()
Definition: manager.cpp:58
void play_slice()
Definition: context.cpp:96
#define LOG_GENERAL
bool show_debug_clock_button
Do we wish to show the button for the debug clock.