Skip to content

Commit

Permalink
Game Launcher: used an optional for load_data
Browse files Browse the repository at this point in the history
This also fixes an issue where the exception data was actually being copied instead of being moved
since the exception was being passed in as const reference.
  • Loading branch information
Vultraz committed Jan 5, 2021
1 parent 22ef5e8 commit fbe44a4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
42 changes: 22 additions & 20 deletions src/game_launcher.cpp
Expand Up @@ -50,7 +50,6 @@
#include "preferences/display.hpp"
#include "preferences/general.hpp" // for disable_preferences_save, etc
#include "save_index.hpp"
#include "savegame.hpp" // for clean_saves, etc
#include "scripting/application_lua_kernel.hpp"
#include "sdl/surface.hpp" // for surface
#include "serialization/compression.hpp" // for format::NONE
Expand Down Expand Up @@ -183,17 +182,17 @@ game_launcher::game_launcher(const commandline_options& cmdline_opts)
if(cmdline_opts_.editor) {
jump_to_editor_ = true;
if(!cmdline_opts_.editor->empty()) {
load_data_.reset(new savegame::load_game_metadata{
savegame::save_index_class::default_saves_dir(), *cmdline_opts_.editor});
load_data_ = savegame::load_game_metadata{
savegame::save_index_class::default_saves_dir(), *cmdline_opts_.editor};
}
}
if(cmdline_opts_.fps)
preferences::set_show_fps(true);
if(cmdline_opts_.fullscreen)
video_->set_fullscreen(true);
if(cmdline_opts_.load)
load_data_.reset(
new savegame::load_game_metadata{savegame::save_index_class::default_saves_dir(), *cmdline_opts_.load});
load_data_ = savegame::load_game_metadata{
savegame::save_index_class::default_saves_dir(), *cmdline_opts_.load};
if(cmdline_opts_.max_fps) {
int fps = utils::clamp(*cmdline_opts_.max_fps, 1, 1000);
fps = 1000 / fps;
Expand Down Expand Up @@ -483,8 +482,8 @@ bool game_launcher::play_test()
try {
campaign_controller ccontroller(state_);
ccontroller.play_game();
} catch(const savegame::load_game_exception& e) {
load_data_.reset(new savegame::load_game_metadata(std::move(e.data_)));
} catch(savegame::load_game_exception& e) {
load_data_ = std::move(e.data_);
return true;
}

Expand Down Expand Up @@ -578,8 +577,8 @@ game_launcher::unit_test_result game_launcher::single_unit_test()
savegame::replay_savegame save(state_, compression::NONE);
save.save_game_automatic(false, "unit_test_replay");

load_data_.reset(new savegame::load_game_metadata{
savegame::save_index_class::default_saves_dir(), save.filename(), "", true, true, false});
load_data_ = savegame::load_game_metadata{
savegame::save_index_class::default_saves_dir(), save.filename(), "", true, true, false};

if(!load_game()) {
std::cerr << "Failed to load the replay!" << std::endl;
Expand Down Expand Up @@ -659,7 +658,7 @@ bool game_launcher::play_render_image_mode()

bool game_launcher::is_loading() const
{
return !!load_data_;
return utils::has_optional_value(load_data_);
}

bool game_launcher::load_game()
Expand All @@ -671,8 +670,8 @@ bool game_launcher::load_game()
savegame::loadgame load(
savegame::save_index_class::default_saves_dir(), game_config_manager::get()->game_config(), state_);
if(load_data_) {
std::unique_ptr<savegame::load_game_metadata> load_data = std::move(load_data_);
load.data() = std::move(*load_data);
load.data() = std::move(load_data_.value());
clear_loaded_game();
}

try {
Expand Down Expand Up @@ -793,8 +792,11 @@ bool game_launcher::goto_editor()
{
if(jump_to_editor_) {
jump_to_editor_ = false;
std::unique_ptr<savegame::load_game_metadata> load_data = std::move(load_data_);
if(start_editor(filesystem::normalize_path(load_data ? load_data->filename : "")) == editor::EXIT_QUIT_TO_DESKTOP) {

const std::string to_open = load_data_ ? filesystem::normalize_path(load_data_->filename) : "";
clear_loaded_game();

if(start_editor(to_open) == editor::EXIT_QUIT_TO_DESKTOP) {
return false;
}
}
Expand Down Expand Up @@ -928,8 +930,8 @@ bool game_launcher::play_multiplayer(mp_selection res)
}
} catch(const incorrect_map_format_error& e) {
gui2::show_error_message(_("The game map could not be loaded: ") + e.message);
} catch(const savegame::load_game_exception& e) {
load_data_.reset(new savegame::load_game_metadata(std::move(e.data_)));
} catch(savegame::load_game_exception& e) {
load_data_ = std::move(e.data_);
// this will make it so next time through the title screen loop, this game is loaded
} catch(const wml_exception& e) {
e.show();
Expand Down Expand Up @@ -1009,8 +1011,8 @@ void game_launcher::launch_game(RELOAD_GAME_DATA reload)

gui2::dialogs::outro::display(state_.classification());
}
} catch(const savegame::load_game_exception& e) {
load_data_.reset(new savegame::load_game_metadata(std::move(e.data_)));
} catch(savegame::load_game_exception& e) {
load_data_ = std::move(e.data_);
// this will make it so next time through the title screen loop, this game is loaded
} catch(const wml_exception& e) {
e.show();
Expand All @@ -1025,8 +1027,8 @@ void game_launcher::play_replay()
try {
campaign_controller ccontroller(state_);
ccontroller.play_replay();
} catch(const savegame::load_game_exception& e) {
load_data_.reset(new savegame::load_game_metadata(std::move(e.data_)));
} catch(savegame::load_game_exception& e) {
load_data_ = std::move(e.data_);
// this will make it so next time through the title screen loop, this game is loaded
} catch(const wml_exception& e) {
e.show();
Expand Down
5 changes: 3 additions & 2 deletions src/game_launcher.hpp
Expand Up @@ -22,7 +22,9 @@
#include "picture.hpp" // for manager
#include "preferences/game.hpp" // for manager
#include "saved_game.hpp" // for saved_game
#include "savegame.hpp" // for clean_saves, etc
#include "sound.hpp" // for music_thinker
#include "utils/optional_fwd.hpp"

#include <string> // for string
#include <vector> // for vector
Expand All @@ -31,7 +33,6 @@ class commandline_options;
class config;
class CVideo;

namespace savegame { struct load_game_metadata; }
namespace preferences { class advanced_manager; }

struct jump_to_campaign_info
Expand Down Expand Up @@ -156,5 +157,5 @@ class game_launcher
jump_to_campaign_info jump_to_campaign_;

bool jump_to_editor_;
std::unique_ptr<savegame::load_game_metadata> load_data_;
utils::optional<savegame::load_game_metadata> load_data_;
};

0 comments on commit fbe44a4

Please sign in to comment.