Catch exceptions in stockpile (un)serialization

From #964, protobuf exceptions in loadstock/savestock would either fail by only
logging an error to the console (when run from the Lua UI) or by crashing the
game entirely (when run from the console).

Figuring out what actually causes the exceptions in the first place (possibly a
misunderstood structure layout?) would be a better solution than this, but this
will at least stop crashes for now.
develop
lethosor 2016-10-29 02:35:27 -04:00
parent c640006069
commit ec222c0b24
1 changed files with 26 additions and 6 deletions

@ -244,11 +244,20 @@ static command_result savestock ( color_ostream &out, vector <string> & paramete
cereal.enable_debug ( out );
if ( !is_dfstockfile ( file ) ) file += ".dfstock";
try
{
if ( !cereal.serialize_to_file ( file ) )
{
out.printerr ( "could not save to %s\n", file.c_str() );
return CR_FAILURE;
}
}
catch ( std::exception &e )
{
out.printerr ( "serialization failed: protobuf exception: %s\n", e.what() );
return CR_FAILURE;
}
return CR_OK;
}
@ -296,9 +305,17 @@ static command_result loadstock ( color_ostream &out, vector <string> & paramete
StockpileSerializer cereal ( sp );
if ( debug )
cereal.enable_debug ( out );
try
{
if ( !cereal.unserialize_from_file ( file ) )
{
out.printerr ( "unserialization failed\n" );
out.printerr ( "unserialization failed: %s\n", file.c_str() );
return CR_FAILURE;
}
}
catch ( std::exception &e )
{
out.printerr ( "unserialization failed: protobuf exception: %s\n", e.what() );
return CR_FAILURE;
}
return CR_OK;
@ -508,13 +525,16 @@ static int stockpiles_list_settings ( lua_State *L )
return 1;
}
const std::string err_title = "Stockpile Settings Error";
const std::string err_help = "Does the folder exist?\nCheck the console for more information.";
static void stockpiles_load ( color_ostream &out, std::string filename )
{
std::vector<std::string> params;
params.push_back ( filename );
command_result r = loadstock ( out, params );
if ( r != CR_OK )
show_message_box ( "Stockpile Settings Error", "Couldn't load. Does the folder exist?", true );
show_message_box ( err_title, "Couldn't load. " + err_help, true );
}
@ -524,7 +544,7 @@ static void stockpiles_save ( color_ostream &out, std::string filename )
params.push_back ( filename );
command_result r = savestock ( out, params );
if ( r != CR_OK )
show_message_box ( "Stockpile Settings Error", "Couldn't save. Does the folder exist?", true );
show_message_box ( err_title, "Couldn't save. " + err_help, true );
}
DFHACK_PLUGIN_LUA_FUNCTIONS