The Battle for Wesnoth  1.17.17+dev
game_launcher.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2023
3  by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #include "game_launcher.hpp"
17 #include "game_errors.hpp"
18 
19 #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
29 #include "game_initialization/multiplayer.hpp" // for start_client, etc
30 #include "game_initialization/playcampaign.hpp" // for play_game, etc
31 #include "game_initialization/singleplayer.hpp" // for sp_create_mode
32 #include "generators/map_generator.hpp" // for mapgen_exception
33 #include "gettext.hpp" // for _
34 #include "gui/dialogs/language_selection.hpp" // for language_selection
36 #include "gui/dialogs/message.hpp" // for show error message
38 #include "gui/dialogs/multiplayer/mp_host_game_prompt.hpp" // for host game prompt
40 #include "gui/dialogs/title_screen.hpp" // for show_debug_clock_button
41 #include "gui/dialogs/transient_message.hpp" // for show_transient_message
42 #include "gui/widgets/retval.hpp" // for window, etc
43 #include "gui/widgets/settings.hpp" // for new_widgets
44 #include "language.hpp" // for language_def, etc
45 #include "log.hpp" // for LOG_STREAM, logger, general, etc
46 #include "map/exception.hpp"
48 #include "preferences/display.hpp"
49 #include "preferences/general.hpp" // for disable_preferences_save, etc
50 #include "save_index.hpp"
52 #include "sdl/surface.hpp" // for surface
53 #include "serialization/compression.hpp" // for format::NONE
54 #include "serialization/string_utils.hpp" // for split
55 #include "tstring.hpp" // for operator==, operator!=
56 #include "video.hpp"
58 #include "wml_exception.hpp" // for wml_exception
59 
60 #include <algorithm> // for copy, max, min, stable_sort
61 #include <cstdlib> // for system
62 #include <new>
63 #include <utility> // for pair
64 
65 #include <SDL2/SDL.h> // for SDL_INIT_JOYSTICK, etc
66 
67 #ifdef DEBUG_WINDOW_LAYOUT_GRAPHS
68 #include "gui/widgets/debug.hpp"
69 #endif
70 
71 // For wesnothd launch code.
72 #ifdef _WIN32
73 
74 #define WIN32_LEAN_AND_MEAN
75 #include <windows.h>
76 
77 #endif // _WIN32
78 
80 
81 static lg::log_domain log_config("config");
82 #define ERR_CONFIG LOG_STREAM(err, log_config)
83 #define WRN_CONFIG LOG_STREAM(warn, log_config)
84 #define LOG_CONFIG LOG_STREAM(info, log_config)
85 
86 #define ERR_GENERAL LOG_STREAM(err, lg::general())
87 #define LOG_GENERAL LOG_STREAM(info, lg::general())
88 #define WRN_GENERAL LOG_STREAM(warn, lg::general())
89 #define DBG_GENERAL LOG_STREAM(debug, lg::general())
90 
91 #define LOG_TEST FORCE_LOG_TO(lg::general(), log_config)
92 
93 static lg::log_domain log_mp_create("mp/create");
94 #define DBG_MP LOG_STREAM(debug, log_mp_create)
95 
96 static lg::log_domain log_network("network");
97 #define ERR_NET LOG_STREAM(err, log_network)
98 
99 static lg::log_domain log_enginerefac("enginerefac");
100 #define LOG_RG LOG_STREAM(info, log_enginerefac)
101 
103  : cmdline_opts_(cmdline_opts)
104  , font_manager_()
105  , prefs_manager_()
106  , image_manager_()
107  , main_event_context_()
108  , hotkey_manager_()
109  , music_thinker_()
110  , music_muter_()
111  , test_scenarios_{"test"}
112  , screenshot_map_()
113  , screenshot_filename_()
114  , state_()
115  , play_replay_(false)
116  , multiplayer_server_()
117  , jump_to_multiplayer_(false)
118  , jump_to_campaign_{}
119  , jump_to_editor_(false)
120  , load_data_()
121 {
122  bool no_music = false;
123  bool no_sound = false;
124 
125  // The path can be hardcoded and it might be a relative path.
126  if(!game_config::path.empty() &&
127 #ifdef _WIN32
128  // use c_str to ensure that index 1 points to valid element since c_str() returns null-terminated string
129  game_config::path.c_str()[1] != ':'
130 #else
131  game_config::path[0] != '/'
132 #endif
133  )
134  {
136  // font_manager_.update_font_path()
137  // To update the font path, destroy and recreate the manager
139  new (&font_manager_) font::manager();
140  }
141 
142  if(cmdline_opts_.core_id) {
144  }
145  if(cmdline_opts_.campaign) {
146  jump_to_campaign_.jump = true;
148  PLAIN_LOG << "selected campaign id: [" << jump_to_campaign_.campaign_id << "]";
149 
152  PLAIN_LOG << "selected difficulty: [" << jump_to_campaign_.difficulty << "]";
153  } else {
154  jump_to_campaign_.difficulty = -1; // let the user choose the difficulty
155  }
156 
159  PLAIN_LOG << "selected scenario id: [" << jump_to_campaign_.scenario_id << "]";
160  }
161 
164  }
165  }
166  if(cmdline_opts_.clock)
168  if(cmdline_opts_.debug) {
170  game_config::mp_debug = true;
171  }
172 #ifdef DEBUG_WINDOW_LAYOUT_GRAPHS
173  if(cmdline_opts_.debug_dot_domain)
174  gui2::debug_layout_graph::set_domain(*cmdline_opts_.debug_dot_domain);
175  if(cmdline_opts_.debug_dot_level)
177 #endif
178  if(cmdline_opts_.editor) {
179  jump_to_editor_ = true;
180  if(!cmdline_opts_.editor->empty()) {
183  }
184  }
185  if(cmdline_opts_.fps)
188  start_in_fullscreen_ = true;
189  if(cmdline_opts_.load)
192  if(cmdline_opts_.max_fps) {
193  int fps = std::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 {
234  // Pick the first server in config
235  if(game_config::server_list.size() > 0) {
237  } else {
238  multiplayer_server_ = "";
239  }
240  }
241  if(cmdline_opts_.username) {
244  if(cmdline_opts_.password) {
246  preferences::set_password(*cmdline_opts.server, *cmdline_opts.username, *cmdline_opts_.password);
247  }
248  }
249  }
250  if(cmdline_opts_.test) {
251  if(!cmdline_opts_.test->empty()) {
253  }
254  }
255  if(!cmdline_opts_.unit_test.empty()) {
257  }
259  start_in_fullscreen_ = false;
261  load_data_->show_replay = true;
264 
265  if(!cmdline_opts.nobanner) {
266  PLAIN_LOG
267  << "\nData directory: " << game_config::path
268  << "\nUser configuration directory: " << filesystem::get_user_config_dir()
269  << "\nUser data directory: " << filesystem::get_user_data_dir()
270  << "\nCache directory: " << filesystem::get_cache_dir()
271  << "\n\n";
272  }
273 
274  // disable sound in nosound mode, or when sound engine failed to initialize
275  if(no_sound || ((preferences::sound_on() || preferences::music_on() ||
277  !sound::init_sound())) {
278  preferences::set_sound(false);
279  preferences::set_music(false);
282  } else if(no_music) { // else disable the music in nomusic mode
283  preferences::set_music(false);
284  }
285 }
286 
288 {
289  if(!::load_language_list()) {
290  return false;
291  }
292 
293  language_def locale;
294  if(cmdline_opts_.language) {
295  std::vector<language_def> langs = get_languages(true);
296  for(const language_def& def : langs) {
297  if(def.localename == *cmdline_opts_.language) {
298  locale = def;
299  break;
300  }
301  }
302  if(locale.localename.empty()) {
303  PLAIN_LOG << "Language symbol '" << *cmdline_opts_.language << "' not found.";
304  return false;
305  }
306  } else {
307  locale = get_locale();
308  }
309  ::set_language(locale);
310 
311  return true;
312 }
313 
315 {
316  // Handle special commandline launch flags
320  {
326  {
327  PLAIN_LOG << "--nogui flag is only valid with --multiplayer or --screenshot or --plugin flags";
328  return false;
329  }
331  game_config::no_delay = true;
332  return true;
333  }
334 
335  // Initialize video subsystem, and create a new window.
336  video::init();
337 
338  // Set window title and icon
340 
341 #if !(defined(__APPLE__))
342  surface icon(image::get_surface(image::locator{"icons/icon-game.png"}, image::UNSCALED));
343  if(icon != nullptr) {
345  }
346 #endif
347  return true;
348 }
349 
351 {
352  bool error = false;
353 
354  if(!cmdline_opts_.nobanner) {
355  STREAMING_LOG << "Checking lua scripts... ";
356  }
357 
359  // load the "package" package, so that scripts can get what packages they want
361  }
362 
363  // get the application lua kernel, load and execute script file, if script file is present
366 
367  if(!sf->fail()) {
368  /* Cancel all "jumps" to editor / campaign / multiplayer */
369  jump_to_multiplayer_ = false;
370  jump_to_editor_ = false;
371  jump_to_campaign_.jump = false;
372 
373  std::string full_script((std::istreambuf_iterator<char>(*sf)), std::istreambuf_iterator<char>());
374 
375  PLAIN_LOG << "\nRunning lua script: " << *cmdline_opts_.script_file;
376 
378  } else {
379  PLAIN_LOG << "Encountered failure when opening script '" << *cmdline_opts_.script_file << '\'';
380  error = true;
381  }
382  }
383 
385  std::string filename = *cmdline_opts_.plugin_file;
386 
387  PLAIN_LOG << "Loading a plugin file'" << filename << "'...";
388 
390 
391  try {
392  if(sf->fail()) {
393  throw std::runtime_error("failed to open plugin file");
394  }
395 
396  /* Cancel all "jumps" to editor / campaign / multiplayer */
397  jump_to_multiplayer_ = false;
398  jump_to_editor_ = false;
399  jump_to_campaign_.jump = false;
400 
401  std::string full_plugin((std::istreambuf_iterator<char>(*sf)), std::istreambuf_iterator<char>());
402 
404 
405  std::size_t i = pm.add_plugin(filename, full_plugin);
406 
407  for(std::size_t j = 0; j < pm.size(); ++j) {
408  PLAIN_LOG << j << ": " << pm.get_name(j) << " -- " << pm.get_detailed_status(j);
409  }
410 
411  PLAIN_LOG << "Starting a plugin...";
412  pm.start_plugin(i);
413 
414  for(std::size_t j = 0; j < pm.size(); ++j) {
415  PLAIN_LOG << j << ": " << pm.get_name(j) << " -- " << pm.get_detailed_status(j);
416  }
417 
418  plugins_context pc("init");
419 
420  for(std::size_t repeat = 0; repeat < 5; ++repeat) {
421  PLAIN_LOG << "Playing a slice...";
422  pc.play_slice();
423 
424  for(std::size_t j = 0; j < pm.size(); ++j) {
425  PLAIN_LOG << j << ": " << pm.get_name(j) << " -- " << pm.get_detailed_status(j);
426  }
427  }
428 
429  return true;
430  } catch(const std::exception& e) {
431  gui2::show_error_message(std::string("When loading a plugin, error:\n") + e.what());
432  error = true;
433  }
434  }
435 
436  if(!error && !cmdline_opts_.nobanner) {
437  PLAIN_LOG << "ok";
438  }
439 
440  return !error;
441 }
442 
443 void game_launcher::set_test(const std::string& id)
444 {
445  state_.clear();
446  state_.classification().type = campaign_type::type::test;
448  state_.classification().era_id = "era_default";
449 
450  state_.set_carryover_sides_start(config{"next_scenario", id});
451 }
452 
454 {
455  // This first_time variable was added in 70f3c80a3e2 so that using the GUI
456  // menu to load a game works. That seems to have edge-cases, for example if
457  // you try to load a game a second time then Wesnoth exits.
458  static bool first_time = true;
459 
460  if(!cmdline_opts_.test) {
461  return true;
462  }
463 
464  if(!first_time) {
465  return false;
466  }
467 
468  first_time = false;
469 
470  if(test_scenarios_.size() == 0) {
471  // shouldn't happen, as test_scenarios_ is initialised to {"test"}
472  PLAIN_LOG << "Error in the test handling code";
473  return false;
474  }
475 
476  if(test_scenarios_.size() > 1) {
477  PLAIN_LOG << "You can't run more than one unit test in interactive mode";
478  }
479 
480  set_test(test_scenarios_.at(0));
481 
483 
484  try {
485  campaign_controller ccontroller(state_);
486  ccontroller.play_game();
487  } catch(savegame::load_game_exception& e) {
488  load_data_ = std::move(e.data_);
489  return true;
490  }
491 
492  return false;
493 }
494 
495 /**
496  * Runs unit tests specified on the command line.
497  *
498  * If multiple unit tests were specified, then this will stop at the first test
499  * which returns a non-zero status.
500  */
501 // Same as play_test except that we return the results of play_game.
502 // \todo "same ... except" ... and many other changes, such as testing the replay
504 {
505  // There's no copy of play_test's first_time variable. That seems to be for handling
506  // the player loading a game via the GUI, which makes no sense in a non-interactive test.
507  if(cmdline_opts_.unit_test.empty()) {
509  }
510 
511  auto ret = unit_test_result::TEST_FAIL; // will only be returned if no test is run
512  for(const auto& scenario : test_scenarios_) {
513  set_test(scenario);
514  ret = single_unit_test();
515  const char* describe_result;
516  switch(ret) {
518  describe_result = "PASS TEST";
519  break;
521  describe_result = "FAIL TEST (INVALID REPLAY)";
522  break;
524  describe_result = "FAIL TEST (ERRORED REPLAY)";
525  break;
527  describe_result = "FAIL TEST (WML EXCEPTION)";
528  break;
530  describe_result = "FAIL TEST (DEFEAT)";
531  break;
533  describe_result = "PASS TEST (VICTORY)";
534  break;
536  describe_result = "BROKE STRICT (PASS)";
537  break;
539  describe_result = "BROKE STRICT (FAIL)";
540  break;
542  describe_result = "BROKE STRICT (DEFEAT)";
543  break;
545  describe_result = "BROKE STRICT (VICTORY)";
546  break;
547  default:
548  describe_result = "FAIL TEST (UNKNOWN)";
549  break;
550  }
551 
552  PLAIN_LOG << describe_result << " (" << int(ret) << "): " << scenario;
553  if(ret != unit_test_result::TEST_PASS) {
554  break;
555  }
556  }
557 
558  return ret;
559 }
560 
562 {
564 
565  level_result::type game_res = level_result::type::fail;
566  try {
567  campaign_controller ccontroller(state_, true);
568  game_res = ccontroller.play_game();
569  if(game_res == level_result::type::fail) {
570  if(lg::broke_strict()) {
572  } else {
574  }
575  }
576  } catch(const wml_exception& e) {
577  PLAIN_LOG << "Caught WML Exception:" << e.dev_message;
579  }
580 
582 
584  return pass_victory_or_defeat(game_res);
585  }
586 
588  save.save_game_automatic(false, "unit_test_replay");
589 
591  savegame::save_index_class::default_saves_dir(), save.filename(), "", true, true, false};
592 
593  if(!load_game()) {
594  PLAIN_LOG << "Failed to load the replay!";
595  return unit_test_result::TEST_FAIL_LOADING_REPLAY; // failed to load replay
596  }
597 
598  try {
599  const bool was_strict_broken = lg::broke_strict();
600  campaign_controller ccontroller(state_, true);
601  ccontroller.play_replay();
602  if(!was_strict_broken && lg::broke_strict()) {
603  PLAIN_LOG << "Observed failure on replay";
605  }
606  } catch(const wml_exception& e) {
607  PLAIN_LOG << "WML Exception while playing replay: " << e.dev_message;
609  }
610 
611  return pass_victory_or_defeat(game_res);
612 }
613 
615 {
616  if(res == level_result::type::defeat) {
617  if(lg::broke_strict()) {
619  } else {
621  }
622  } else if(res == level_result::type::victory) {
623  if(lg::broke_strict()) {
625  } else {
627  }
628  }
629 
630  if(lg::broke_strict()) {
632  } else {
634  }
635 }
636 
638 {
640  return true;
641  }
642 
644 
646 
648  return false;
649 }
650 
652 {
654  return true;
655  }
656 
657  state_.classification().type = campaign_type::type::multiplayer;
658  DBG_GENERAL << "Current campaign type: " << campaign_type::get_string(state_.classification().type);
659 
660  try {
662  } catch(const config::error& e) {
663  PLAIN_LOG << "Error loading game config: " << e.what();
664  return false;
665  }
666 
667  // A default output filename
668  std::string outfile = "wesnoth_image.png";
669 
670  // If a output path was given as an argument, use that instead
672  outfile = *cmdline_opts_.render_image_dst;
673  }
674 
676  exit(1);
677  }
678 
679  return false;
680 }
681 
683 {
684  return load_data_.has_value();
685 }
686 
688 {
689  assert(game_config_manager::get());
690 
691  DBG_GENERAL << "Current campaign type: " << campaign_type::get_string(state_.classification().type);
692 
694  if(load_data_) {
695  load.data() = std::move(*load_data_);
697  }
698 
699  try {
700  if(!load.load_game()) {
701  return false;
702  }
703 
704  load.set_gamestate();
705  try {
707  } catch(const config::error&) {
708  return false;
709  }
710 
711  } catch(const config::error& e) {
712  if(e.message.empty()) {
713  gui2::show_error_message(_("The file you have tried to load is corrupt"));
714  } else {
715  gui2::show_error_message(_("The file you have tried to load is corrupt: '") + e.message + '\'');
716  }
717 
718  return false;
719  } catch(const wml_exception& e) {
720  e.show();
721  return false;
722  } catch(const filesystem::io_exception& e) {
723  if(e.message.empty()) {
724  gui2::show_error_message(_("File I/O Error while reading the game"));
725  } else {
726  gui2::show_error_message(_("File I/O Error while reading the game: '") + e.message + '\'');
727  }
728 
729  return false;
730  } catch(const game::error& e) {
731  if(e.message.empty()) {
732  gui2::show_error_message(_("The file you have tried to load is corrupt"));
733  } else {
734  gui2::show_error_message(_("The file you have tried to load is corrupt: '") + e.message + '\'');
735  }
736 
737  return false;
738  }
739 
740  play_replay_ = load.data().show_replay;
741  LOG_CONFIG << "is middle game savefile: " << (state_.is_mid_game_save() ? "yes" : "no");
742  LOG_CONFIG << "show replay: " << (play_replay_ ? "yes" : "no");
743  // in case load.data().show_replay && state_.is_start_of_scenario
744  // there won't be any turns to replay, but the
745  // user gets to watch the intro sequence again ...
746 
747  if(!state_.is_start_of_scenario() && load.data().show_replay) {
749  }
750 
753  }
754 
755  if(load.data().cancel_orders) {
757  }
758 
759  return true;
760 }
761 
763 {
764  state_.clear();
765  state_.classification().type = campaign_type::type::scenario;
766  play_replay_ = false;
767 
769 }
770 
772 {
774 }
775 
777 {
778  if(jump_to_campaign_.jump) {
779  if(new_campaign()) {
781  jump_to_campaign_.jump = false;
783  } else {
784  jump_to_campaign_.jump = false;
785  return false;
786  }
787  }
788 
789  return true;
790 }
791 
793 {
795  jump_to_multiplayer_ = false;
797  ;
798  } else {
799  return false;
800  }
801  }
802 
803  return true;
804 }
805 
807 {
808  if(jump_to_editor_) {
809  jump_to_editor_ = false;
810 
811  const std::string to_open = load_data_ ? filesystem::normalize_path(load_data_->filename) : "";
813 
815  return false;
816  }
817  }
818 
819  return true;
820 }
821 
823 {
824  const std::string wesnothd_program = preferences::get_mp_server_program_name().empty()
827 
828  std::string config = filesystem::get_user_config_dir() + "/lan_server.cfg";
830  // copy file if it isn't created yet
832  }
833 
834 #ifndef _WIN32
835  std::string command = "\"" + wesnothd_program +"\" -c \"" + config + "\" -d -t 2 -T 5";
836 #else
837  // start wesnoth as background job
838  std::string command = "cmd /C start \"wesnoth server\" /B \"" + wesnothd_program + "\" -c \"" + config + "\" -t 2 -T 5";
839  // Make sure wesnothd's console output is visible on the console window by
840  // disabling SDL's stdio redirection code for this and future child
841  // processes. No need to bother cleaning this up because it's only
842  // meaningful to SDL applications during pre-main initialization.
843  SetEnvironmentVariableA("SDL_STDIO_REDIRECT", "0");
844 #endif
845  LOG_GENERAL << "Starting wesnothd: "<< command;
846  if (std::system(command.c_str()) == 0) {
847  // Give server a moment to start up
848  SDL_Delay(50);
849  return;
850  }
852 
853  // Couldn't start server so throw error
854  WRN_GENERAL << "Failed to run server start script";
855  throw game::mp_server_error("Starting MP server failed!");
856 }
857 
859 {
860  try {
861  if(mode == mp_mode::HOST) {
862  try {
863  start_wesnothd();
864  } catch(const game::mp_server_error&) {
866 
867  try {
868  start_wesnothd();
869  } catch(const game::mp_server_error&) {
870  return false;
871  }
872  }
873  }
874 
875  // If a server address wasn't specified, prompt for it now.
876  if(mode != mp_mode::LOCAL && multiplayer_server_.empty()) {
877  if(!gui2::dialogs::mp_connect::execute()) {
878  return false;
879  }
880 
881  // The prompt saves its input to preferences.
883 
884  if(multiplayer_server_ != preferences::builtin_servers_list().front().address) {
886  }
887  }
888 
889  // create_engine already calls game_config_manager::get()->load_config but maybe its better to have MULTIPLAYER
890  // defined while we are in the lobby.
892 
893  events::discard_input(); // prevent the "keylogger" effect
895 
896  if(mode == mp_mode::LOCAL) {
898  } else {
900  multiplayer_server_.clear();
901  }
902 
903  } catch(const wesnothd_rejected_client_error& e) {
904  gui2::show_error_message(e.message);
905  } catch(const game::mp_server_error& e) {
906  gui2::show_error_message(_("Error while starting server: ") + e.message);
907  } catch(const game::load_game_failed& e) {
908  gui2::show_error_message(_("The game could not be loaded: ") + e.message);
909  } catch(const game::game_error& e) {
910  gui2::show_error_message(_("Error while playing the game: ") + e.message);
911  } catch(const mapgen_exception& e) {
912  gui2::show_error_message(_("Map generator error: ") + e.message);
913  } catch(const wesnothd_error& e) {
914  if(!e.message.empty()) {
915  ERR_NET << "caught network error: " << e.message;
916 
917  std::string user_msg;
918  auto conn_err = dynamic_cast<const wesnothd_connection_error*>(&e);
919 
920  if(conn_err) {
921  // The wesnothd_connection_error subclass is only thrown with messages
922  // from boost::system::error_code which we can't translate ourselves.
923  // It's also the originator of the infamous EOF error that happens when
924  // the server dies. <https://github.com/wesnoth/wesnoth/issues/3005>. It
925  // will provide a translated string instead of that when it happens.
926  user_msg = !conn_err->user_message.empty()
927  ? conn_err->user_message
928  : _("Connection failed: ") + e.message;
929  } else {
930  // This will be a message from the server itself, which we can
931  // probably translate.
932  user_msg = translation::gettext(e.message.c_str());
933  }
934 
935  gui2::show_error_message(user_msg);
936  } else {
937  ERR_NET << "caught network error";
938  }
939  } catch(const config::error& e) {
940  if(!e.message.empty()) {
941  ERR_CONFIG << "caught config::error: " << e.message;
942  gui2::show_transient_message("", e.message);
943  } else {
944  ERR_CONFIG << "caught config::error";
945  }
946  } catch(const incorrect_map_format_error& e) {
947  gui2::show_error_message(_("The game map could not be loaded: ") + e.message);
948  } catch(savegame::load_game_exception& e) {
949  load_data_ = std::move(e.data_);
950  // this will make it so next time through the title screen loop, this game is loaded
951  } catch(const wml_exception& e) {
952  e.show();
953  } catch(const game::error& e) {
954  PLAIN_LOG << "caught game::error...";
955  gui2::show_error_message(_("Error: ") + e.message);
956  }
957 
958  return true;
959 }
960 
962 {
964  return true;
965  }
966 
967  DBG_MP << "starting multiplayer game from the commandline";
968 
970 
971  events::discard_input(); // prevent the "keylogger" effect
973 
975 
976  return false;
977 }
978 
980 {
981  if(!gui2::dialogs::language_selection::execute()) {
982  return false;
983  }
984 
987  }
988 
993 
994  return true;
995 }
996 
998 {
999  assert(!load_data_);
1000  if(play_replay_) {
1001  play_replay();
1002  return;
1003  }
1004 
1005  gui2::dialogs::loading_screen::display([this, reload]() {
1007 
1008  if(reload == reload_mode::RELOAD_DATA) {
1009  try {
1012  } catch(const config::error&) {
1013  return;
1014  }
1015  }
1016  });
1017 
1018  try {
1019  campaign_controller ccontroller(state_);
1020  ccontroller.play_game();
1021  ai::manager::singleton_ = nullptr;
1022  } catch(savegame::load_game_exception& e) {
1023  load_data_ = std::move(e.data_);
1024  // this will make it so next time through the title screen loop, this game is loaded
1025  } catch(const wml_exception& e) {
1026  e.show();
1027  } catch(const mapgen_exception& e) {
1028  gui2::show_error_message(_("Map generator error: ") + e.message);
1029  }
1030 }
1031 
1033 {
1034  assert(!load_data_);
1035  try {
1036  campaign_controller ccontroller(state_);
1037  ccontroller.play_replay();
1038  } catch(savegame::load_game_exception& e) {
1039  load_data_ = std::move(e.data_);
1040  // this will make it so next time through the title screen loop, this game is loaded
1041  } catch(const wml_exception& e) {
1042  e.show();
1043  }
1044 }
1045 
1047 {
1048  while(true) {
1050 
1052 
1053  editor::EXIT_STATUS res = editor::start(filename);
1054 
1055  if(res != editor::EXIT_RELOAD_DATA) {
1056  return res;
1057  }
1058 
1060  }
1061 
1062  return editor::EXIT_ERROR; // not supposed to happen
1063 }
1064 
1066 {
1067  load_data_.reset();
1068 }
1069 
1071 {
1072  try {
1074  video::deinit();
1075  } catch(std::exception& e) {
1076  ERR_GENERAL << "Suppressing exception thrown during ~game_launcher: " << e.what();
1077  } catch(...) {
1078  ERR_GENERAL << "Suppressing exception " << utils::get_unknown_exception_type() << " thrown during ~game_launcher";
1079  }
1080 }
Managing the AIs lifecycle - headers TODO: Refactor history handling and internal commands.
static manager * singleton_
Definition: manager.hpp:442
level_result::type play_game()
level_result::type play_replay()
std::optional< std::string > render_image_dst
Output file to put rendered image path in.
bool nogui
True if –nogui was given on the command line.
std::optional< std::string > server
Non-empty if –server was given on the command line.
std::optional< std::string > plugin_file
File to load a lua plugin (similar to a script) from.
bool headless_unit_test
True if –unit is used and –showgui is not present.
bool windowed
True if –windowed was given on the command line.
bool noreplaycheck
True if –noreplaycheck was given on the command line.
std::optional< std::string > render_image
Image path to render.
std::optional< int > campaign_difficulty
Non-empty if –campaign-difficulty was given on the command line.
std::optional< unsigned int > translation_percent
Non-empty if –all-translations or –translations-over is given on the command line.
std::optional< std::string > campaign_scenario
Non-empty if –campaign-scenario was given on the command line.
std::optional< std::string > password
Non-empty if –password was given on the command line.
bool debug
True if –debug was given on the command line.
bool nosound
True if –nosound was given on the command line.
bool multiplayer
True if –multiplayer was given on the command line.
bool script_unsafe_mode
Whether to load the "package" package for the scripting environment.
bool nobanner
True if –nobanner was given on the command line.
bool nomusic
True if –nomusic was given on the command line.
bool clock
True if –clock was given on the command line.
bool nodelay
True if –nodelay was given on the command line.
std::optional< std::string > username
Non-empty if –username was given on the command line.
bool new_widgets
True if –new-widgets was given on the command line.
std::optional< std::string > load
Non-empty if –load was given on the command line.
std::optional< std::string > campaign
Non-empty if –campaign was given on the command line.
bool fps
True if –fps was given on the command line.
std::optional< std::string > script_file
File to load lua script from.
bool campaign_skip_story
True if –skip-story was given on the command line.
bool with_replay
True if –with-replay was given on the command line.
std::optional< std::string > core_id
Non-empty if –core was given on the command line.
bool screenshot
True if –screenshot was given on the command line.
std::vector< std::string > unit_test
Non-empty if –unit was given on the command line.
bool fullscreen
True if –fullscreen was given on the command line.
std::optional< std::string > test
Non-empty if –test was given on the command line.
std::optional< std::string > language
Non-empty if –language was given on the command line.
std::optional< int > max_fps
Max FPS specified by –max-fps option.
std::optional< std::pair< int, int > > resolution
Pair of AxB values specified after –resolution.
std::optional< std::string > screenshot_map_file
Map file to make a screenshot of.
std::optional< std::string > screenshot_output_file
Output file to put screenshot in.
std::optional< std::string > editor
Non-empty if –editor was given on the command line.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
campaign_type::type type
std::string label
Name of the game (e.g.
std::string campaign_define
If there is a define the campaign uses to customize data.
static game_config_manager * get()
void load_game_config_for_game(const game_classification &classification, const std::string &scenario_id)
void load_game_config_for_create(bool is_mp, bool is_test=false)
game_launcher(const commandline_options &cmdline_opts)
unit_test_result single_unit_test()
Internal to the implementation of unit_test().
std::string multiplayer_server_
std::string jump_to_campaign_id() const
Return the ID of the campaign to jump to (skipping the main menu).
std::optional< savegame::load_game_metadata > load_data_
bool play_screenshot_mode()
jump_to_campaign_info jump_to_campaign_
std::string screenshot_filename_
void launch_game(reload_mode reload=reload_mode::RELOAD_DATA)
bool goto_multiplayer()
bool change_language()
saved_game state_
unit_test_result pass_victory_or_defeat(level_result::type res)
bool has_load_data() const
bool play_render_image_mode()
void clear_loaded_game()
bool init_lua_script()
std::vector< std::string > test_scenarios_
unit_test_result
Status code after running a unit test, should match the run_wml_tests script and the documentation fo...
std::string screenshot_map_
const commandline_options & cmdline_opts_
font::manager font_manager_
void set_test(const std::string &id)
bool play_multiplayer_commandline()
unit_test_result unit_test()
Runs unit tests specified on the command line.
bool play_multiplayer(mp_mode mode)
editor::EXIT_STATUS start_editor()
static void progress(loading_stage stage=loading_stage::none)
Report what is being loaded to the loading screen.
static void display(std::function< void()> f)
Generic locator abstracting the location of an image.
Definition: picture.hpp:64
void run(char const *prog, const std::string &name, int nArgs=0)
Runs a plain script.
void load_package()
Loads the package library into lua environment.
void play_slice()
Definition: context.cpp:97
std::size_t size()
Definition: manager.cpp:69
std::string get_name(std::size_t idx)
Definition: manager.cpp:95
static plugins_manager * get()
Definition: manager.cpp:59
void start_plugin(std::size_t idx)
Definition: manager.cpp:102
std::string get_detailed_status(std::size_t idx)
Definition: manager.cpp:84
lua_kernel_base * get_kernel_base()
Definition: manager.cpp:64
std::size_t add_plugin(const std::string &name, const std::string &prog)
Definition: manager.cpp:119
game_classification & classification()
Definition: saved_game.hpp:56
void set_skip_story(bool skip_story)
Definition: saved_game.hpp:147
bool is_mid_game_save() const
Definition: saved_game.hpp:106
bool is_start_of_scenario() const
Definition: saved_game.hpp:110
void unify_controllers()
Definition: saved_game.cpp:718
void set_carryover_sides_start(config carryover_sides_start)
Definition: saved_game.cpp:165
std::string get_scenario_id() const
Definition: saved_game.cpp:664
void clear()
Definition: saved_game.cpp:799
statistics_record::campaign_stats_t & statistics()
Definition: saved_game.hpp:143
void cancel_orders()
Definition: saved_game.cpp:701
Exception used to signal that the user has decided to abortt a game, and to load another game instead...
Definition: savegame.hpp:85
The class for loading a savefile.
Definition: savegame.hpp:100
Class for replay saves (either manually or automatically).
Definition: savegame.hpp:267
static std::shared_ptr< save_index_class > default_saves_dir()
Returns an instance for managing saves in filesystem::get_saves_dir()
Definition: save_index.cpp:210
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:366
const std::string & filename() const
Definition: savegame.hpp:177
static void reset_translations()
Definition: tstring.cpp:652
Declarations for File-IO.
std::size_t i
Definition: function.cpp:968
#define LOG_CONFIG
#define ERR_GENERAL
#define DBG_GENERAL
static lg::log_domain log_enginerefac("enginerefac")
#define DBG_MP
static lg::log_domain log_network("network")
static lg::log_domain log_mp_create("mp/create")
#define ERR_CONFIG
#define WRN_GENERAL
#define ERR_NET
#define LOG_GENERAL
static lg::log_domain log_config("config")
static std::string _(const char *str)
Definition: gettext.hpp:93
const language_def & get_locale()
Definition: language.cpp:329
void init_textdomains(const game_config_view &cfg)
Initializes the list of textdomains from a configuration object.
Definition: language.cpp:367
bool load_language_list()
Definition: language.cpp:104
language_list get_languages(bool all)
Return a list of available translations.
Definition: language.cpp:127
void set_min_translation_percent(int percent)
Definition: language.cpp:144
Standard logging facilities (interface).
#define STREAMING_LOG
Definition: log.hpp:262
#define PLAIN_LOG
Definition: log.hpp:261
@ NORMAL
Definition: cursor.hpp:29
void set(CURSOR_TYPE type)
Use the default parameter to reset cursors.
Definition: cursor.cpp:176
void point(int x, int y)
Draw a single point.
Definition: draw.cpp:193
@ EXIT_ERROR
Definition: editor_main.hpp:28
@ EXIT_QUIT_TO_DESKTOP
Definition: editor_main.hpp:26
@ EXIT_RELOAD_DATA
Definition: editor_main.hpp:27
EXIT_STATUS start(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:30
void discard_input()
Discards all input events.
Definition: events.cpp:799
std::string get_cache_dir()
Definition: filesystem.cpp:867
filesystem::scoped_istream istream_file(const std::string &fname, bool treat_failure_as_error)
std::string get_user_config_dir()
Definition: filesystem.cpp:828
std::string get_user_data_dir()
Definition: filesystem.cpp:857
static bool file_exists(const bfs::path &fpath)
Definition: filesystem.cpp:320
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't pres...
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...
std::string read_file(const std::string &fname)
Basic disk I/O - read file.
std::unique_ptr< std::istream > scoped_istream
Definition: filesystem.hpp:51
void write_file(const std::string &fname, const std::string &data, std::ios_base::openmode mode)
Throws io_exception if an error occurs.
std::string get_cwd()
Definition: filesystem.cpp:949
std::string normalize_path(const std::string &fpath, bool normalize_separators, bool resolve_dot_entries)
Returns the absolute path of a file.
bool load_font_config()
Definition: font_config.cpp:89
Game configuration data as global variables.
Definition: build_info.cpp:63
std::string path
Definition: filesystem.cpp:83
std::string get_default_title_string()
std::vector< server_info > server_list
Definition: game_config.cpp:72
void set_debug(bool new_debug)
Definition: game_config.cpp:93
bool show_debug_clock_button
Do we wish to show the button for the debug clock.
std::vector< game_tip > load(const config &cfg)
Loads the tips from a config.
Definition: tips.cpp:36
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.
bool new_widgets
Do we wish to use the new library or not.
Definition: settings.cpp:23
void show_error_message(const std::string &msg, bool message_use_markup)
Shows an error message to the user.
Definition: message.cpp:204
void flush_cache()
Purges all image caches.
Definition: picture.cpp:225
surface get_surface(const image::locator &i_locator, TYPE type, bool skip_cache)
Returns an image surface suitable for software manipulation.
Definition: picture.cpp:686
save_result save_image(const locator &i_locator, const std::string &filename)
Definition: picture.cpp:949
@ UNSCALED
Unmodified original-size image.
Definition: picture.hpp:233
bool broke_strict()
Definition: log.cpp:313
void start_local_game()
Starts a multiplayer game in single-user mode.
void start_local_game_commandline(const commandline_options &cmdline_opts)
Starts a multiplayer game in single-user mode using command line settings.
void start_client(const std::string &host)
Pubic entry points for the MP workflow.
void disable_preferences_save()
Definition: general.cpp:231
std::string get_mp_server_program_name()
Definition: game.cpp:510
void set_core_id(const std::string &core_id)
Definition: general.cpp:338
void set_mp_server_program_name(const std::string &path)
Definition: game.cpp:501
bool set_turn_bell(bool ison)
Definition: general.cpp:680
bool set_sound(bool ison)
Definition: general.cpp:733
bool music_on()
Definition: general.cpp:751
void set_draw_delay(int value)
Definition: general.cpp:910
void set_show_fps(bool value)
Definition: general.cpp:900
bool sound_on()
Definition: general.cpp:728
std::string network_host()
Definition: game.cpp:387
bool set_music(bool ison)
Definition: general.cpp:756
void set_network_host(const std::string &host)
Definition: game.cpp:397
bool UI_sound_on()
Definition: general.cpp:699
void _set_maximized(bool ison)
Definition: general.cpp:450
const std::vector< game_config::server_info > & builtin_servers_list()
Definition: game.cpp:356
bool set_UI_sound(bool ison)
Definition: general.cpp:704
void set_level(const std::string &value)
Definition: game.cpp:696
void set_login(const std::string &login)
bool turn_bell()
Definition: general.cpp:675
void set_password(const std::string &server, const std::string &login, const std::string &key)
void _set_resolution(const point &res)
Definition: general.cpp:444
void show_wesnothd_server_search()
Definition: display.cpp:99
void clean_saves(const std::string &label)
Delete all autosaves of a certain scenario from the default save directory.
Definition: savegame.cpp:68
bool init_sound()
Definition: sound.cpp:443
void close_sound()
Definition: sound.cpp:495
void flush_cache()
Definition: sound.cpp:195
bool select_campaign(saved_game &state, jump_to_campaign_info jump_to_campaign)
void set_language(const std::string &language, const std::vector< std::string > *)
Definition: gettext.cpp:492
static std::string gettext(const char *str)
Definition: gettext.hpp:60
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:87
std::string get_unknown_exception_type()
Utility function for finding the type of thing caught with catch(...).
Definition: general.cpp:23
void set_window_title(const std::string &title)
Sets the title of the main window.
Definition: video.cpp:621
void set_window_icon(surface &icon)
Sets the icon of the main window.
Definition: video.cpp:627
void init(fake type)
Initialize the video subsystem.
Definition: video.cpp:86
void deinit()
Deinitialize the video subsystem.
Definition: video.cpp:112
This file contains the settings handling of the widget library.
An exception object used when an IO error occurs.
Definition: filesystem.hpp:65
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:29
Error used for any general game error, e.g.
Definition: game_errors.hpp:47
Error used when game loading fails.
Definition: game_errors.hpp:31
std::string campaign_id
The ID of the campaign to launch.
bool jump
Whether the game should immediately start a campaign.
std::string scenario_id
The ID of the scenario within the campaign to jump to.
bool skip_story
Whether the story screen should be skipped.
int difficulty
The difficulty at which to launch the campaign.
std::string localename
Definition: language.hpp:55
void clear_current_scenario()
Delete the current scenario from the stats.
static std::string get_string(enum_type key)
Converts a enum to its string equivalent.
Definition: enum_base.hpp:46
An error occurred inside the underlying network communication code (boost asio) TODO: find a short na...
std::string user_message
User-friendly and potentially translated message for use in the UI.
An error occurred during when trying to communicate with the wesnothd server.
Error used when the client is rejected by the MP server.
Helper class, don't construct this directly.
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
#define e