Skip to content

Commit

Permalink
Fix crash when exiting from options menu after failed loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Qrox committed Jan 29, 2020
1 parent bfee67a commit 04b1494
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
20 changes: 20 additions & 0 deletions src/cata_utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <algorithm>
#include <type_traits>

#include "optional.h"
#include "units.h"

class JsonIn;
Expand Down Expand Up @@ -510,4 +511,23 @@ std::string join( const std::vector<std::string> &strings, const std::string &jo

int modulo( int v, int m );

class on_out_of_scope
{
private:
cata::optional<std::function<void()>> func;
public:
on_out_of_scope( const std::function<void()> &func ) : func( func ) {
}

~on_out_of_scope() {
if( func ) {
( *func )();
}
}

void cancel() {
func.reset();
}
};

#endif // CAT_UTILITY_H
36 changes: 23 additions & 13 deletions src/main_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,11 @@ bool main_menu::opening_screen()
}
if( action == "UP" || action == "CONFIRM" ) {
if( sel2 >= 0 && sel2 < NUM_SPECIAL_GAMES - 1 ) {
on_out_of_scope cleanup( []() {
g->gamemode.reset();
g->u = avatar();
world_generator->set_active_world( nullptr );
} );
g->gamemode = get_special_game( static_cast<special_game_id>( sel2 + 1 ) );
// check world
WORLDPTR world = world_generator->make_new_world( static_cast<special_game_id>( sel2 + 1 ) );
Expand All @@ -601,15 +606,12 @@ bool main_menu::opening_screen()
g->setup();
} catch( const std::exception &err ) {
debugmsg( "Error: %s", err.what() );
g->gamemode.reset();
g->u = avatar();
continue;
}
if( !g->gamemode->init() ) {
g->gamemode.reset();
g->u = avatar();
continue;
}
cleanup.cancel();
start = true;
}
}
Expand Down Expand Up @@ -661,7 +663,7 @@ bool main_menu::opening_screen()

if( action == "UP" || action == "CONFIRM" ) {
if( sel2 == 0 ) {
get_options().show( true );
get_options().show( false );
// The language may have changed- gracefully handle this.
init_strings();
} else if( sel2 == 1 ) {
Expand Down Expand Up @@ -758,6 +760,10 @@ bool main_menu::new_character_tab()
}
if( action == "UP" || action == "CONFIRM" ) {
if( sel2 == 0 || sel2 == 2 || sel2 == 3 || sel2 == 4 ) {
on_out_of_scope cleanup( []() {
g->u = avatar();
world_generator->set_active_world( nullptr );
} );
// First load the mods, this is done by
// loading the world.
// Pick a world, suppressing prompts if it's "play now" mode.
Expand All @@ -770,7 +776,6 @@ bool main_menu::new_character_tab()
g->setup();
} catch( const std::exception &err ) {
debugmsg( "Error: %s", err.what() );
g->u = avatar();
continue;
}
character_type play_type = PLTYPE_CUSTOM;
Expand All @@ -789,7 +794,6 @@ bool main_menu::new_character_tab()
break;
}
if( !g->u.create( play_type ) ) {
g->u = avatar();
load_char_templates();
werase( w_background );
wrefresh( w_background );
Expand All @@ -802,9 +806,9 @@ bool main_menu::new_character_tab()
wrefresh( w_background );

if( !g->start_game() ) {
g->u = avatar();
continue;
}
cleanup.cancel();
start = true;
} else if( sel2 == 1 ) {
layer = 3;
Expand Down Expand Up @@ -865,21 +869,22 @@ bool main_menu::new_character_tab()
}
}
} else if( action == "RIGHT" || action == "CONFIRM" ) {
on_out_of_scope cleanup( []() {
g->u = avatar();
world_generator->set_active_world( nullptr );
} );
WORLDPTR world = world_generator->pick_world();
if( world == nullptr ) {
g->u = avatar();
continue;
}
world_generator->set_active_world( world );
try {
g->setup();
} catch( const std::exception &err ) {
debugmsg( "Error: %s", err.what() );
g->u = avatar();
continue;
}
if( !g->u.create( PLTYPE_TEMPLATE, templates[sel3] ) ) {
g->u = avatar();
load_char_templates();
werase( w_background );
wrefresh( w_background );
Expand All @@ -890,9 +895,9 @@ bool main_menu::new_character_tab()
werase( w_background );
wrefresh( w_background );
if( !g->start_game() ) {
g->u = avatar();
continue;
}
cleanup.cancel();
start = true;
}
}
Expand Down Expand Up @@ -1044,6 +1049,11 @@ bool main_menu::load_character_tab( bool transfer )
}
if( action == "RIGHT" || action == "CONFIRM" ) {
if( sel3 >= 0 && sel3 < static_cast<int>( savegames.size() ) ) {
on_out_of_scope cleanup( []() {
g->u = avatar();
world_generator->set_active_world( nullptr );
} );

werase( w_background );
wrefresh( w_background );

Expand All @@ -1057,11 +1067,11 @@ bool main_menu::load_character_tab( bool transfer )
g->setup();
} catch( const std::exception &err ) {
debugmsg( "Error: %s", err.what() );
g->u = avatar();
continue;
}

g->load( savegames[sel3] );
cleanup.cancel();
start = true;
}
}
Expand Down

0 comments on commit 04b1494

Please sign in to comment.