The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
game_launcher.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2003 - 2025
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_data_dir, etc
25#include "game_classification.hpp" // for game_classification, etc
26#include "game_config.hpp" // for path, etc
27#include "game_initialization/multiplayer.hpp" // for start_client, etc
28#include "game_initialization/playcampaign.hpp" // for play_game, etc
29#include "game_initialization/singleplayer.hpp" // for sp_create_mode
30#include "generators/map_generator.hpp" // for mapgen_exception
31#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/title_screen.hpp" // for show_debug_clock_button
37#include "gui/dialogs/transient_message.hpp" // for show_transient_message
38#include "gui/widgets/settings.hpp" // for new_widgets
39#include "language.hpp" // for language_def, etc
40#include "log.hpp" // for LOG_STREAM, logger, general, etc
41#include "map/exception.hpp"
43#include "save_index.hpp"
45#include "sdl/surface.hpp" // for surface
46#include "serialization/compression.hpp" // for format::NONE
47#include "tstring.hpp" // for operator==, operator!=
48#include "video.hpp"
50#include "wml_exception.hpp" // for wml_exception
51
52#ifdef __APPLE__
53#include <TargetConditionals.h>
54#endif
55
56// Subprocess creation isn't supported on iOS
57#if !TARGET_OS_IPHONE
58#if BOOST_VERSION >= 108600
59
60// boost::asio (via boost::process) complains about winsock.h otherwise
61#ifdef _WIN32
62#define WIN32_LEAN_AND_MEAN
63#endif
64#include <boost/process/v2/process.hpp>
65
66#else
67
68// process::v1 only. The v1 folders do not exist until 1.86
69#ifdef _WIN32
70#include <boost/process/windows.hpp>
71#endif
72#include <boost/process/child.hpp>
73
74#endif
75#endif // !TARGET_OS_IPHONE
76
77#include <algorithm> // for copy, max, min, stable_sort
78#include <new>
79#include <thread>
80#include <utility> // for pair
81
82#ifdef DEBUG_WINDOW_LAYOUT_GRAPHS
83#include "gui/widgets/debug.hpp"
84#endif
85
86static 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 ERR_GENERAL LOG_STREAM(err, lg::general())
92#define LOG_GENERAL LOG_STREAM(info, lg::general())
93#define WRN_GENERAL LOG_STREAM(warn, lg::general())
94#define DBG_GENERAL LOG_STREAM(debug, lg::general())
95
96#define LOG_TEST FORCE_LOG_TO(lg::general(), log_config)
97
98static lg::log_domain log_mp_create("mp/create");
99#define DBG_MP LOG_STREAM(debug, log_mp_create)
100
101static lg::log_domain log_network("network");
102#define ERR_NET LOG_STREAM(err, log_network)
103
104static lg::log_domain log_enginerefac("enginerefac");
105#define LOG_RG LOG_STREAM(info, log_enginerefac)
106
108 : cmdline_opts_(cmdline_opts)
109 , config_manager_(cmdline_opts)
110 , font_manager_()
111 , image_manager_()
113 , hotkey_manager_()
114 , music_thinker_()
115 , music_muter_()
116 , test_scenarios_{"test"}
117 , screenshot_map_()
118 , screenshot_filename_()
119 , state_()
120 , play_replay_(false)
121 , multiplayer_server_()
122 , jump_to_multiplayer_(false)
123 , jump_to_campaign_{}
124 , jump_to_editor_()
125 , load_data_()
126{
127 bool no_music = false;
128 bool no_sound = false;
129
131 prefs::get().set_core(*cmdline_opts_.core_id);
132 }
134 jump_to_campaign_.jump = true;
136 PLAIN_LOG << "selected campaign id: [" << jump_to_campaign_.campaign_id << "]";
137
140 PLAIN_LOG << "selected difficulty: [" << jump_to_campaign_.difficulty << "]";
141 } else {
142 jump_to_campaign_.difficulty = -1; // let the user choose the difficulty
143 }
144
147 PLAIN_LOG << "selected scenario id: [" << jump_to_campaign_.scenario_id << "]";
148 }
149
152 }
153 }
156 if(cmdline_opts_.debug) {
159 }
160#ifdef DEBUG_WINDOW_LAYOUT_GRAPHS
161 if(cmdline_opts_.debug_dot_domain)
162 gui2::debug_layout_graph::set_domain(*cmdline_opts_.debug_dot_domain);
163 if(cmdline_opts_.debug_dot_level)
164 gui2::debug_layout_graph::set_level(*cmdline_opts_.debug_dot_level);
165#endif
169 prefs::get().set_show_fps(true);
171 prefs::get().set_fullscreen(true);
172 if(cmdline_opts_.load) {
173#ifdef __cpp_aggregate_paren_init
174 load_data_.emplace(
176#else
179#endif
180 try {
181 load_data_->read_file();
182 } catch(const game::load_game_failed&) {
183 load_data_.reset();
184 }
185 }
187 prefs::get().set_refresh_rate(std::clamp(*cmdline_opts_.max_fps, 1, 1000));
188 }
190 no_sound = true;
192 }
194 gui2::new_widgets = true;
196 no_music = true;
198 no_sound = true;
200 const int xres = std::get<0>(*cmdline_opts_.resolution);
201 const int yres = std::get<1>(*cmdline_opts_.resolution);
202 if(xres > 0 && yres > 0) {
203 prefs::get().set_resolution(point(xres, yres));
204 prefs::get().set_maximized(false);
205 }
206 }
208 // TODO it could be simplified to use cmdline_opts_ directly if there is no other way to enter screenshot mode
211 no_sound = true;
213 }
216 // Do we have any server specified ?
217 if(!cmdline_opts_.server->empty()) {
219 } else {
220 // Pick the first server in config
221 if(game_config::server_list.size() > 0) {
223 } else {
225 }
226 }
232 prefs::get().set_password(*cmdline_opts.server, *cmdline_opts.username, *cmdline_opts_.password);
233 }
234 }
235 }
236 if(cmdline_opts_.test) {
237 if(!cmdline_opts_.test->empty()) {
239 }
240 }
241 if(!cmdline_opts_.unit_test.empty()) {
243 }
245 prefs::get().set_fullscreen(false);
247 load_data_->show_replay = true;
250
251 if(!cmdline_opts.nobanner) {
253 << "\nGame data: " << game_config::path
254 << "\nUser data: " << filesystem::get_user_data_dir()
255 << "\nCache: " << filesystem::get_cache_dir()
256 << "\n";
257 }
258
259 // disable sound in nosound mode, or when sound engine failed to initialize
260 if(no_sound || !sound::init_sound()) {
261 prefs::get().set_sound(false);
262 prefs::get().set_music(false);
263 prefs::get().set_turn_bell(false);
264 prefs::get().set_ui_sound(false);
265 } else if(no_music) { // else disable the music in nomusic mode
266 prefs::get().set_music(false);
267 }
268}
269
271{
272 if(!::load_language_list()) {
273 return false;
274 }
275
276 language_def locale;
278 std::vector<language_def> langs = get_languages(true);
279 for(const language_def& def : langs) {
280 if(def.localename == *cmdline_opts_.language) {
281 locale = def;
282 break;
283 }
284 }
285 if(locale.localename.empty()) {
286 PLAIN_LOG << "Language symbol '" << *cmdline_opts_.language << "' not found.";
287 return false;
288 }
289 } else {
290 locale = get_locale();
291 }
292 ::set_language(locale);
293
294 return true;
295}
296
298{
299 // Handle special commandline launch flags
305 {
312 {
313 PLAIN_LOG << "--nogui flag is only valid with --multiplayer or --screenshot or --plugin flags";
314 return false;
315 }
317 // Screenshots require a rendering context, and thus a window,
318 // so we create one but hidden.
320 } else {
321 // Other functions don't require a window at all.
323 }
324 return true;
325 }
326
327 // Initialize video subsystem, and create a new window.
328 video::init();
329
330 // Set window title and icon
332
333#if !(defined(__APPLE__))
334 surface icon(image::get_surface(image::locator{"icons/icon-game.png"}, image::UNSCALED));
335 if(icon != nullptr) {
337 }
338#endif
339
340#ifndef __ANDROID__
341 SDL_StartTextInput(video::get_window());
342#endif
343
344 return true;
345}
346
348{
349 bool error = false;
350
352 STREAMING_LOG << "Checking lua scripts... ";
353 }
354
356 // load the "package" package, so that scripts can get what packages they want
358 }
359
361 std::string filename = *cmdline_opts_.plugin_file;
362
363 PLAIN_LOG << "Loading a plugin file'" << filename << "'...";
364
366
367 try {
368 if(sf->fail()) {
369 throw std::runtime_error("failed to open plugin file");
370 }
371
372 /* Cancel all "jumps" to editor / campaign / multiplayer */
373 jump_to_multiplayer_ = false;
374 jump_to_editor_ = utils::nullopt;
375 jump_to_campaign_.jump = false;
376
377 std::string full_plugin((std::istreambuf_iterator<char>(*sf)), std::istreambuf_iterator<char>());
378
380
381 std::size_t i = pm.add_plugin(filename, full_plugin);
382
383 for(std::size_t j = 0; j < pm.size(); ++j) {
384 PLAIN_LOG << j << ": " << pm.get_name(j) << " -- " << pm.get_detailed_status(j);
385 }
386
387 PLAIN_LOG << "Starting a plugin...";
388 pm.start_plugin(i);
389
390 for(std::size_t j = 0; j < pm.size(); ++j) {
391 PLAIN_LOG << j << ": " << pm.get_name(j) << " -- " << pm.get_detailed_status(j);
392 }
393
394 plugins_context pc("init");
395
396 for(std::size_t repeat = 0; repeat < 5; ++repeat) {
397 PLAIN_LOG << "Playing a slice...";
398 pc.play_slice();
399
400 for(std::size_t j = 0; j < pm.size(); ++j) {
401 PLAIN_LOG << j << ": " << pm.get_name(j) << " -- " << pm.get_detailed_status(j);
402 }
403 }
404
405 return true;
406 } catch(const std::exception& e) {
407 gui2::show_error_message(std::string("When loading a plugin, error:\n") + e.what());
408 error = true;
409 }
410 }
411
412 if(!error && !cmdline_opts_.nobanner) {
413 PLAIN_LOG << "ok";
414 }
415
416 return !error;
417}
418
419void game_launcher::set_test(const std::string& id)
420{
421 state_.clear();
422 state_.classification().type = campaign_type::type::test;
424 state_.classification().era_id = "era_default";
425
426 state_.set_carryover_sides_start(config{"next_scenario", id});
427}
428
430{
431 // This first_time variable was added in 70f3c80a3e2 so that using the GUI
432 // menu to load a game works. That seems to have edge-cases, for example if
433 // you try to load a game a second time then Wesnoth exits.
434 static bool first_time = true;
435
436 if(!cmdline_opts_.test) {
437 return true;
438 }
439
440 if(!first_time) {
441 return false;
442 }
443
444 first_time = false;
445
446 if(test_scenarios_.size() == 0) {
447 // shouldn't happen, as test_scenarios_ is initialised to {"test"}
448 PLAIN_LOG << "Error in the test handling code";
449 return false;
450 }
451
452 if(test_scenarios_.size() > 1) {
453 PLAIN_LOG << "You can't run more than one unit test in interactive mode";
454 }
455
457
459
460 try {
461 campaign_controller ccontroller(state_);
462 ccontroller.play_game();
464 load_data_ = std::move(e.data_);
465 return true;
466 }
467
468 return false;
469}
470
471/**
472 * Runs unit tests specified on the command line.
473 *
474 * If multiple unit tests were specified, then this will stop at the first test
475 * which returns a non-zero status.
476 */
477// Same as play_test except that we return the results of play_game.
478// \todo "same ... except" ... and many other changes, such as testing the replay
480{
481 // There's no copy of play_test's first_time variable. That seems to be for handling
482 // the player loading a game via the GUI, which makes no sense in a non-interactive test.
483 if(cmdline_opts_.unit_test.empty()) {
485 }
486
487 auto ret = unit_test_result::TEST_FAIL; // will only be returned if no test is run
488 for(const auto& scenario : test_scenarios_) {
489 set_test(scenario);
490 ret = single_unit_test();
491 const char* describe_result;
492 switch(ret) {
494 describe_result = "PASS TEST";
495 break;
497 describe_result = "FAIL TEST (INVALID REPLAY)";
498 break;
500 describe_result = "FAIL TEST (ERRORED REPLAY)";
501 break;
503 describe_result = "FAIL TEST (WML EXCEPTION)";
504 break;
506 describe_result = "FAIL TEST (DEFEAT)";
507 break;
509 describe_result = "PASS TEST (VICTORY)";
510 break;
512 describe_result = "BROKE STRICT (PASS)";
513 break;
515 describe_result = "BROKE STRICT (FAIL)";
516 break;
518 describe_result = "BROKE STRICT (DEFEAT)";
519 break;
521 describe_result = "BROKE STRICT (VICTORY)";
522 break;
523 default:
524 describe_result = "FAIL TEST (UNKNOWN)";
525 break;
526 }
527
528 PLAIN_LOG << describe_result << " (" << int(ret) << "): " << scenario;
529 if(ret != unit_test_result::TEST_PASS) {
530 break;
531 }
532 }
533
534 return ret;
535}
536
538{
540
541 level_result::type game_res = level_result::type::fail;
542 try {
543 campaign_controller ccontroller(state_, true);
544 game_res = ccontroller.play_game();
545 if(game_res == level_result::type::fail) {
546 if(lg::broke_strict()) {
548 } else {
550 }
551 }
552 } catch(const wml_exception& e) {
553 PLAIN_LOG << "Caught WML Exception:" << e.dev_message;
555 }
556
558
560 return pass_victory_or_defeat(game_res);
561 }
562
564 save.save_game_automatic(false, "unit_test_replay");
565
566#ifdef __cpp_aggregate_paren_init
567 load_data_.emplace(
569#else
572#endif
573 load_data_->read_file();
574
575 if(!load_prepared_game()) {
576 PLAIN_LOG << "Failed to load the replay!";
577 return unit_test_result::TEST_FAIL_LOADING_REPLAY; // failed to load replay
578 }
579
580 try {
581 const bool was_strict_broken = lg::broke_strict();
582 campaign_controller ccontroller(state_, true);
583 ccontroller.play_replay();
584 if(!was_strict_broken && lg::broke_strict()) {
585 PLAIN_LOG << "Observed failure on replay";
587 }
588 } catch(const wml_exception& e) {
589 PLAIN_LOG << "WML Exception while playing replay: " << e.dev_message;
591 }
592
593 return pass_victory_or_defeat(game_res);
594}
595
597{
598 if(res == level_result::type::defeat) {
599 if(lg::broke_strict()) {
601 } else {
603 }
604 } else if(res == level_result::type::victory) {
605 if(lg::broke_strict()) {
607 } else {
609 }
610 }
611
612 if(lg::broke_strict()) {
614 } else {
616 }
617}
618
620{
622 return true;
623 }
624
626
628
630 if (res != editor::EXIT_NORMAL) {
631 PLAIN_LOG << "Error while taking screenshot of map: " << screenshot_map_;
632 }
633 return false;
634}
635
637{
639 return true;
640 }
641
642 state_.classification().type = campaign_type::type::multiplayer;
643 DBG_GENERAL << "Current campaign type: " << campaign_type::get_string(state_.classification().type);
644
645 try {
647 } catch(const config::error& e) {
648 PLAIN_LOG << "Error loading game config: " << e.what();
649 return false;
650 }
651
652 // A default output filename
653 std::string outfile = "wesnoth_image.png";
654
655 // If a output path was given as an argument, use that instead
658 }
659
661 exit(1);
662 }
663
664 return false;
665}
666
668{
669 return load_data_.has_value();
670}
671
673{
674 return std::exchange(load_data_, utils::nullopt).value();
675}
676
678{
679 DBG_GENERAL << "Current campaign type: " << campaign_type::get_string(state_.classification().type);
680 assert(!load_data_);
681
682 // FIXME: evaluate which of these damn catch blocks are still necessary
683 try {
685
686 } catch(const config::error& e) {
687 if(e.message.empty()) {
688 gui2::show_error_message(_("The file you have tried to load is corrupt"));
689 } else {
690 gui2::show_error_message(_("The file you have tried to load is corrupt: '") + e.message + '\'');
691 }
692
693 return false;
694 } catch(const wml_exception& e) {
695 e.show();
696 return false;
697 } catch(const filesystem::io_exception& e) {
698 if(e.message.empty()) {
699 gui2::show_error_message(_("File I/O Error while reading the game"));
700 } else {
701 gui2::show_error_message(_("File I/O Error while reading the game: '") + e.message + '\'');
702 }
703
704 return false;
705 } catch(const game::error& e) {
706 if(e.message.empty()) {
707 gui2::show_error_message(_("The file you have tried to load is corrupt"));
708 } else {
709 gui2::show_error_message(_("The file you have tried to load is corrupt: '") + e.message + '\'');
710 }
711
712 return false;
713 }
714
715 // If the player canceled loading, we won't have any load data at this point
716 return has_load_data();
717}
718
720{
723
724 play_replay_ = load_data.show_replay;
725
726 // in case show_replay && is_start_of_scenario
727 // there won't be any turns to replay, but the
728 // user gets to watch the intro sequence again ...
729 if(load_data.show_replay && !state_.is_start_of_scenario()) {
731 }
732
733 if(load_data.cancel_orders) {
735 }
736
737 try {
739 } catch(const config::error&) {
740 return false;
741 }
742
743 return true;
744}
745
747{
748 state_.clear();
749 state_.classification().type = campaign_type::type::scenario;
750 play_replay_ = false;
751
753}
754
756{
758}
759
761 jump_to_campaign_.jump = false;
762 if(new_campaign()) {
765 return true;
766 }
767 return false;
768}
769
771{
773 return play_campaign();
774 }
775
776 return true;
777}
778
780{
782 return true;
783 }
784
785 jump_to_multiplayer_ = false;
787}
788
790{
791 if(jump_to_editor_) {
792 // If no filename was specified, this will be an empty string.
793 const std::string to_open = std::exchange(jump_to_editor_, utils::nullopt).value();
794
796 return false;
797 }
798 }
799
800 return true;
801}
802
804{
805#if defined(__APPLE__) && TARGET_OS_IPHONE
806 throw game::mp_server_error("Starting MP server is not supported on iOS builds.");
807#else
808 std::string wesnothd_program = "";
809 if(!prefs::get().get_mp_server_program_name().empty()) {
810 wesnothd_program = prefs::get().get_mp_server_program_name();
811 } else {
812 wesnothd_program = filesystem::get_wesnothd_name();
813 }
814
815 std::string config = filesystem::get_user_data_dir() + "/lan_server.cfg";
817 // copy file if it isn't created yet
819 }
820
821 LOG_GENERAL << "Starting wesnothd";
822 try
823 {
824#if BOOST_VERSION >= 108600
825 boost::asio::io_context io_context;
826 auto c = boost::process::v2::process{io_context, wesnothd_program, { "-c", config }};
827#else
828#ifndef _WIN32
829 boost::process::child c(wesnothd_program, "-c", config);
830#else
831 boost::process::child c(wesnothd_program, "-c", config, boost::process::windows::create_no_window);
832#endif
833#endif
834 c.detach();
835 // Give server a moment to start up
836 using namespace std::chrono_literals;
837 std::this_thread::sleep_for(50ms);
838 return;
839 }
840#if BOOST_VERSION >= 108600
841 catch(const std::exception& e)
842#else
843 catch(const boost::process::process_error& e)
844#endif
845 {
847
848 // Couldn't start server so throw error
849 WRN_GENERAL << "Failed to start server " << wesnothd_program << ":\n" << e.what();
850 throw game::mp_server_error("Starting MP server failed!");
851 }
852#endif
853}
854
856{
858 try {
859 if(mode == mp_mode::HOST) {
860 try {
862 } catch(const game::mp_server_error&) {
864
865 try {
867 } catch(const game::mp_server_error&) {
868 return false;
869 }
870 }
871 }
872
873 // If a server address wasn't specified, prompt for it now.
874 if(mode != mp_mode::LOCAL && multiplayer_server_.empty()) {
875 if(!gui2::dialogs::mp_connect::execute()) {
876 return false;
877 }
878
879 // The prompt saves its input to preferences.
881
882 if(multiplayer_server_ != prefs::get().builtin_servers_list().front().address) {
884 }
885 }
886
887 // create_engine already calls config_manager_.load_config but maybe its better to have MULTIPLAYER
888 // defined while we are in the lobby.
890
891 events::discard_input(); // prevent the "keylogger" effect
893
894 if(mode == mp_mode::LOCAL) {
896 } else {
898 multiplayer_server_.clear();
899 }
900
901 } catch(const wesnothd_rejected_client_error& e) {
903 } catch(const game::mp_server_error& e) {
904 gui2::show_error_message(_("Error while starting server: ") + e.message);
905 } catch(const game::load_game_failed& e) {
906 gui2::show_error_message(_("The game could not be loaded: ") + e.message);
907 } catch(const game::game_error& e) {
908 gui2::show_error_message(_("Error while playing the game: ") + e.message);
909 } catch(const mapgen_exception& e) {
910 gui2::show_error_message(_("Map generator error: ") + e.message);
911 } catch(const wesnothd_error& e) {
912 if(!e.message.empty()) {
913 ERR_NET << "caught network error: " << e.message;
914
915 std::string user_msg;
916 auto conn_err = dynamic_cast<const wesnothd_connection_error*>(&e);
917
918 if(conn_err) {
919 // The wesnothd_connection_error subclass is only thrown with messages
920 // from boost::system::error_code which we can't translate ourselves.
921 // It's also the originator of the infamous EOF error that happens when
922 // the server dies. <https://github.com/wesnoth/wesnoth/issues/3005>. It
923 // will provide a translated string instead of that when it happens.
924 user_msg = !conn_err->user_message.empty()
925 ? conn_err->user_message
926 : _("Connection failed: ") + e.message;
927 } else {
928 // This will be a message from the server itself, which we can
929 // probably translate.
930 user_msg = translation::gettext(e.message.c_str());
931 }
932
933 gui2::show_error_message(user_msg);
934 } else {
935 ERR_NET << "caught network error";
936 }
937 } catch(const config::error& e) {
938 if(!e.message.empty()) {
939 ERR_CONFIG << "caught config::error: " << e.message;
940 gui2::show_transient_message("", e.message);
941 } else {
942 ERR_CONFIG << "caught config::error";
943 }
944 } catch(const incorrect_map_format_error& e) {
945 gui2::show_error_message(_("The game map could not be loaded: ") + e.message);
947 load_data_ = std::move(e.data_);
948 // this will make it so next time through the title screen loop, this game is loaded
949 } catch(const wml_exception& e) {
950 e.show();
951 } catch(const game::error& e) {
952 PLAIN_LOG << "caught game::error...";
953 gui2::show_error_message(_("Error: ") + e.message);
954 }
955
956 return true;
957}
958
960{
962 return true;
963 }
964
965 DBG_MP << "starting multiplayer game from the commandline";
966
968
969 events::discard_input(); // prevent the "keylogger" effect
971
972 try {
975 load_data_ = std::move(e.data_);
976 return true;
977 }
978
979 return false;
980}
981
983{
984 if(!gui2::dialogs::language_selection::execute()) {
985 return false;
986 }
987
990 }
991
996
997 return true;
998}
999
1001{
1002 assert(!load_data_);
1003 if(play_replay_) {
1004 play_replay();
1005 return;
1006 }
1007
1010
1011 if(reload == reload_mode::RELOAD_DATA) {
1012 try {
1015 } catch(const config::error&) {
1016 return;
1017 }
1018 }
1019 });
1020
1021 try {
1022 campaign_controller ccontroller(state_);
1023 ccontroller.play_game();
1024 ai::manager::singleton_ = nullptr;
1026 load_data_ = std::move(e.data_);
1027 // this will make it so next time through the title screen loop, this game is loaded
1028 } catch(const wml_exception& e) {
1029 e.show();
1030 } catch(const mapgen_exception& e) {
1031 gui2::show_error_message(_("Map generator error: ") + e.message);
1032 }
1033}
1034
1036{
1037 assert(!load_data_);
1038 try {
1039 campaign_controller ccontroller(state_);
1040 ccontroller.play_replay();
1042 load_data_ = std::move(e.data_);
1043 // this will make it so next time through the title screen loop, this game is loaded
1044 } catch(const wml_exception& e) {
1045 e.show();
1046 }
1047}
1048
1050{
1052 while(true) {
1054
1056
1058
1059 if(res != editor::EXIT_RELOAD_DATA) {
1060 return res;
1061 }
1062
1064 }
1065
1066 return editor::EXIT_ERROR; // not supposed to happen
1067}
1068
1070{
1071 load_data_.reset();
1072}
1073
1075{
1076 try {
1078 video::deinit();
1079 } catch(std::exception& e) {
1080 ERR_GENERAL << "Suppressing exception thrown during ~game_launcher: " << e.what();
1081 } catch(...) {
1082 ERR_GENERAL << "Suppressing exception " << utils::get_unknown_exception_type() << " thrown during ~game_launcher";
1083 }
1084}
#define ERR_CONFIG
#define ERR_NET
Definition manager.cpp:38
Managing the AIs lifecycle - headers TODO: Refactor history handling and internal commands.
static manager * singleton_
Definition manager.hpp:422
level_result::type play_game()
level_result::type play_replay()
bool nogui
True if –nogui was given on the command line.
utils::optional< std::pair< int, int > > resolution
Pair of AxB values specified after –resolution.
bool headless_unit_test
True if –unit is used and –showgui is not present.
utils::optional< std::string > screenshot_map_file
Map file to make a screenshot of.
utils::optional< std::string > test
Non-empty if –test was given on the command line.
bool windowed
True if –windowed was given on the command line.
utils::optional< std::string > language
Non-empty if –language was given on the command line.
bool noreplaycheck
True if –noreplaycheck was given on the command line.
utils::optional< std::string > screenshot_output_file
Output file to put screenshot in.
utils::optional< int > max_fps
Max FPS specified by –max-fps option.
utils::optional< std::string > core_id
Non-empty if –core was given on the command line.
utils::optional< int > campaign_difficulty
Non-empty if –campaign-difficulty was given on the command line.
bool list_wml_tests
True if –list-wml-tests was given.
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.
utils::optional< std::string > server
Non-empty if –server was given on the command line.
bool script_unsafe_mode
Whether to load the "package" package for the scripting environment.
utils::optional< unsigned int > translation_percent
Non-empty if –all-translations or –translations-over is given on the command line.
utils::optional< std::string > plugin_file
File to load a lua plugin script from.
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.
utils::optional< std::string > render_image_dst
Output file to put rendered image path in.
utils::optional< std::string > render_image
Image path to render.
utils::optional< std::string > campaign
Non-empty if –campaign was given on the command line.
bool new_widgets
True if –new-widgets was given on the command line.
bool fps
True if –fps was given on the command line.
utils::optional< std::string > username
Non-empty if –username was given on the command line.
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.
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.
utils::optional< std::string > editor
Non-empty if –editor was given on the command line.
utils::optional< std::string > password
Non-empty if –password was given on the command line.
bool fullscreen
True if –fullscreen was given on the command line.
utils::optional< std::string > campaign_scenario
Non-empty if –campaign-scenario was given on the command line.
utils::optional< std::string > load
Non-empty if –load 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:157
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.
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)
const game_config_view & game_config() const
game_launcher(const commandline_options &cmdline_opts)
bool load_prepared_game()
utils::optional< std::string > jump_to_editor_
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).
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)
saved_game state_
savegame::load_game_metadata extract_load_data()
Returns the load_game_metadata object stored in load_data_.
unit_test_result pass_victory_or_defeat(level_result::type res)
game_config_manager config_manager_
bool has_load_data() const
bool play_render_image_mode()
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_
utils::optional< savegame::load_game_metadata > load_data_
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(const std::function< void()> &f)
Generic locator abstracting the location of an image.
Definition picture.hpp:59
void load_package()
Loads the package library into lua environment.
void play_slice()
Definition context.cpp:103
std::size_t size()
Definition manager.cpp:68
std::string get_name(std::size_t idx)
Definition manager.cpp:94
static plugins_manager * get()
Definition manager.cpp:58
void start_plugin(std::size_t idx)
Definition manager.cpp:101
std::string get_detailed_status(std::size_t idx)
Definition manager.cpp:83
lua_kernel_base * get_kernel_base()
Definition manager.cpp:63
std::size_t add_plugin(const std::string &name, const std::string &prog)
Definition manager.cpp:118
void set_network_host(const std::string &host)
bool set_music(bool ison)
static prefs & get()
std::string network_host()
bool set_ui_sound(bool ison)
void set_login(const std::string &login)
void show_wesnothd_server_search()
void set_password(const std::string &server, const std::string &login, const std::string &key)
bool set_turn_bell(bool ison)
void set_show_fps(bool value)
bool set_sound(bool ison)
void set_resolution(const point &res)
static void disable_preferences_save()
std::string get_mp_server_program_name()
void set_mp_server_program_name(const std::string &)
void set_skip_story(bool skip_story)
bool is_start_of_scenario() const
void set_carryover_sides_start(config carryover_sides_start)
std::string get_scenario_id() const
game_classification & classification()
void cancel_orders()
statistics_record::campaign_stats_t & statistics()
Exception used to signal that the user has decided to abort a game, and to load another game instead.
Definition savegame.hpp:88
Class for replay saves (either manually or automatically).
Definition savegame.hpp:244
static std::shared_ptr< save_index_class > default_saves_dir()
Returns an instance for managing saves in filesystem::get_saves_dir()
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:254
const std::string & filename() const
Definition savegame.hpp:154
static void reset_translations()
Definition tstring.cpp:665
Definitions for the interface to Wesnoth Markup Language (WML).
#define DBG_MP
const events::event_context main_event_context_
Declarations for File-IO.
#define ERR_GENERAL
#define DBG_GENERAL
static lg::log_domain log_enginerefac("enginerefac")
static lg::log_domain log_network("network")
static lg::log_domain log_mp_create("mp/create")
#define WRN_GENERAL
#define LOG_GENERAL
static lg::log_domain log_config("config")
static std::string _(const char *str)
Definition gettext.hpp:100
std::vector< language_def > get_languages(bool all)
Return a list of available translations.
Definition language.cpp:159
void init_textdomains(const game_config_view &cfg)
Initializes the list of textdomains from a configuration object.
Definition language.cpp:339
bool load_language_list()
Definition language.cpp:138
const language_def & get_locale()
Definition language.cpp:319
void set_language(const language_def &locale)
Definition language.cpp:259
void set_min_translation_percent(int percent)
Definition language.cpp:189
Standard logging facilities (interface).
#define STREAMING_LOG
Definition log.hpp:297
#define PLAIN_LOG
Definition log.hpp:296
@ NORMAL
Definition cursor.hpp:28
void set(CURSOR_TYPE type)
Use the default parameter to reset cursors.
Definition cursor.cpp:161
@ EXIT_QUIT_TO_DESKTOP
@ EXIT_RELOAD_DATA
EXIT_STATUS start(bool clear_id, const std::string &filename, bool take_screenshot, const std::string &screenshot_filename)
Main interface for launching the editor from the title screen.
void discard_input()
Discards all input events.
Definition events.cpp:758
std::string get_cache_dir()
bool file_exists(const std::string &name)
Returns true if a file or directory with such name already exists.
std::string get_user_data_dir()
utils::optional< std::string > get_wml_location(const std::string &path, const utils::optional< std::string > &current_dir=utils::nullopt)
Returns a translated path to the actual file or directory, if it exists.
std::string read_file(const std::string &fname)
Basic disk I/O - read file.
std::unique_ptr< std::istream > scoped_istream
void write_file(const std::string &fname, const std::string &data, std::ios_base::openmode mode=std::ios_base::binary)
Throws io_exception if an error occurs.
std::string get_wesnothd_name()
filesystem::scoped_istream istream_file(const std::string &fname, bool treat_failure_as_error=true)
std::string normalize_path(const std::string &path, bool normalize_separators=false, bool resolve_dot_entries=false)
Returns the absolute path of a file.
bool load_font_config()
std::string path
std::string get_default_title_string()
std::vector< server_info > server_list
void set_debug(bool new_debug)
bool show_debug_clock_button
Do we wish to show the button for the debug clock.
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:201
void flush_cache()
Purges all image caches.
Definition picture.cpp:318
surface get_surface(const image::locator &i_locator, TYPE type, bool skip_cache)
Returns an image surface suitable for software manipulation.
Definition picture.cpp:829
save_result save_image(const locator &i_locator, const std::string &filename)
Definition picture.cpp:1059
@ UNSCALED
Unmodified original-size image.
Definition picture.hpp:173
bool broke_strict()
Definition log.cpp:431
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 clean_saves(const std::string &label)
Delete all autosaves of a certain scenario from the default save directory.
Definition savegame.cpp:63
void set_gamestate(saved_game &gamestate, load_game_metadata &load_data)
Definition savegame.cpp:237
utils::optional< load_game_metadata > load_interactive()
Definition savegame.cpp:163
bool init_sound()
Definition sound.cpp:447
void close_sound()
Definition sound.cpp:510
void flush_cache()
Definition sound.cpp:1072
bool select_campaign(saved_game &state, jump_to_campaign_info jump_to_campaign)
static std::string gettext(const char *str)
Definition gettext.hpp:62
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:691
void set_window_icon(surface &icon)
Sets the icon of the main window.
Definition video.cpp:697
SDL_Window * get_window()
Definition video.cpp:712
void init(fake type)
Initialize the video subsystem.
Definition video.cpp:93
void deinit()
Deinitialize the video subsystem.
Definition video.cpp:122
This file contains the settings handling of the widget library.
std::unique_ptr< MIX_Audio, decltype(&MIX_DestroyAudio)> value
Definition sound.cpp:139
std::string filename
Filename.
An exception object used when an IO error occurs.
Base class for all the errors encountered by the engine.
Error used for any general game error, e.g.
Error used when game loading fails.
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:32
Holds a 2D point.
Definition point.hpp:25
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 i
#define e
#define c